Using the File Inbound Adapter
This chapter describes how to use the file inbound adapter (EnsLib.File.InboundAdapterOpens in a new tab). It contains the following sections:
Ensemble also provides specialized business service classes that use this adapter, and one of those might be suitable for your needs. If so, no programming would be needed. See the section “Connectivity Options” in Introducing Ensemble.
Overall Behavior
EnsLib.File.InboundAdapterOpens in a new tab finds a file in the configured location, reads the input, and sends the input as a stream to the associated business service. The business service, which you create and configure, uses this stream and communicates with the rest of the production. If the inbound file adapter finds multiple files in the configured location, it processes them in order of the time, earliest first, based on when the file was last modified. But the adapter ignores any fractional seconds in the time value. Consequently, if two or more files have a modified date-time differing only in the fractional second part of the time, the adapter can process them in any order.
The following figure shows the overall flow:
![generated description: inbound](images/efil_inbound.png)
In more detail:
-
Each time the adapter encounters input from its configured data source, it calls the internal ProcessInput() method of the business service class, passing the stream as an input argument.
-
The internal ProcessInput() method of the business service class executes. This method performs basic Ensemble tasks such as maintaining internal information as needed by all business services. You do not customize or override this method, which your business service class inherits.
-
The ProcessInput() method then calls your custom OnProcessInput() method, passing the stream object as input. The requirements for this method are described later in “Implementing the OnProcessInput() Method.”
The response message follows the same path, in reverse.
Creating a Business Service to Use the Inbound Adapter
To use this adapter in your production, create a new business service class as described here. Later, add it to your production and configure it. You must also create appropriate message classes, if none yet exist. See “Defining Ensemble Messages” in Developing Ensemble Productions.
The following list describes the basic requirements of the business service class:
-
Your business service class should extend Ens.BusinessServiceOpens in a new tab.
-
In your class, the ADAPTER parameter should equal EnsLib.File.InboundAdapterOpens in a new tab.
-
Your class should implement the OnProcessInput() method, as described in “Implementing the OnProcessInput Method.”
-
For other options and general information, see “Defining a Business Service Class” in Developing Ensemble Productions.
The following example shows the general structure that you need:
Class EFILE.Service Extends Ens.BusinessService
{
Parameter ADAPTER = "EnsLib.File.InboundAdapter";
Method OnProcessInput(pInput As %FileCharacterStream,pOutput As %RegisteredObject) As %Status
{
set tsc=$$$OK
//your code here
Quit tsc
}
}
The first argument to OnProcessInput() could instead be %FileBinaryStreamOpens in a new tab, depending on the contents of the expected file.
Studio provides a wizard that you can use to create a business service stub similar to the preceding. To access this wizard, click File —> New and then click the Production tab. Then click Business Service and click OK. Note that the wizard provides a generic input argument. If you use the wizard, InterSystems recommends that you edit the method signature to use the specific input argument needed with this adapter; the input argument type should be %FileCharacterStreamOpens in a new tab or %FileBinaryStreamOpens in a new tab.
Implementing the OnProcessInput() Method
Within your business service class, your OnProcessInput() method should have the following signature:
Method OnProcessInput(pInput As %FileCharacterStream,pOutput As %RegisteredObject) As %Status
Or:
Method OnProcessInput(pInput As %FileBinaryStream,pOutput As %RegisteredObject) As %Status
Where:
-
pInput is the message object that the adapter will send to this business service. This can be of type %FileCharacterStreamOpens in a new tab or %FileBinaryStreamOpens in a new tab, depending on the contents of the expected file. You use an adapter setting (Charset) to indicate whether the input file is character or binary; see “Settings for the File Inbound Adapter.”
In either case, pInput.Attributes("Filename") equals the name of the file.
-
pOutput is the generic output argument required in the method signature.
The OnProcessInput() method should do some or all of the following:
-
Examine the input file (pInput) and decide how to use it.
-
Create an instance of the request message, which will be the message that your business service sends.
For information on creating message classes, see “Defining Ensemble Messages” in Developing Ensemble Productions.
-
For the request message, set its properties as appropriate, using values in the input.
-
Call a suitable method of the business service to send the request to some destination within the production. Specifically, call SendRequestSync(), SendRequestAsync(), or (less common) SendDeferredResponse(). For details, see “Sending Request Messages” in Developing Ensemble Productions
Each of these methods returns a status (specifically, an instance of %StatusOpens in a new tab).
-
Make sure that you set the output argument (pOutput). Typically you set this equal to the response message that you have received. This step is required.
-
Return an appropriate status. This step is required.
Invoking Adapter Methods
Within your business service, you might want to invoke the following instance methods of the adapter. Each method corresponds to an adapter setting; these methods provide the opportunity to make adjustments following a change in any setting. For detailed descriptions of each setting, see “Settings for the File Inbound Adapter,” later in this chapter.
Method ArchivePathSet(pInVal As %String) As %Status
pInVal is the directory where the adapter should place a copy of each file after processing.
Method FilePathSet(path As %String) As %Status
path is the directory on the local server in which to look for files.
Method WorkPathSet(path As %String) As %Status
WorkPath
path is the directory on the local server in which to place files while they are being processed.
Example Business Service Classes
Example 1
The following code example shows a business service class that references the EnsLib.File.InboundAdapterOpens in a new tab. This example works as follows:
-
The file has a header. The header information is added to each transaction.
-
The file experiences a number of transactions.
-
The header and transaction XML structures are defined by the classes LBAPP.Header and LBAPP.Transaction (not shown).
-
Some error-handling is shown, but not all.
-
The method RejectBatch() is not shown.
-
The transactions are submitted to the business process asynchronously, so there is no guarantee they are processed in order as they appear in the file.
-
The entire transaction object is passed as the payload of each message to the business process.
-
All of the transactions in one file are submitted as a single Ensemble session.
Class LB.MarketOfferXMLFileSvc Extends Ens.BusinessService
{
Parameter ADAPTER = "EnsLib.File.InboundAdapter";
Method OnProcessInput(pInput As %FileCharacterStream,
pOutput As %RegisteredObject) As %Status
{
// pInput is a %FileCharacterStream containing the file xml
set batch=pInput.Filename // path+name.ext
set batch=##class(%File).GetFilename(batch) // name.ext
// Load the data from the XML stream into the database
set reader = ##class(%XML.Reader).%New()
// first get the header
set sc=reader.OpenStream(pInput)
if 'sc {
do $this.RejectBatch("Invalid XML Structure",sc,pInput,batch)
quit 1
}
do reader.Correlate("Header","LBAPP.Header")
if (reader.Next(.object,.sc)) {set header=object}
else {
if 'sc {do $this.RejectBatch("Invalid Header",sc,pInput,batch)}
else {do $this.RejectBatch("No Header found",sc,pInput,batch)}
quit 1
}
// then get the transactions, and call the BP for each one
do reader.Correlate("Transaction","LBAPP.Transaction")
while (reader.Next(.object,.sc)) {
set object.Header=header
set sc=$this.ValidateTrans(object)
if sc {set sc=object.%Save()}
if 'sc {
do $this.RejectTrans("Invalid transaction",sc,object,batch,tranct)
set sc=1
continue
}
// Call the BP for each Transaction
set request=##class(LB.TransactionReq).%New()
set request.Tran=object
set ..%SessionId="" // make each transaction a new session
set sc=$this.SendRequestAsync("LB.ChurnBPL",request)
}
do reader.Close()
quit sc
}
}
Example 2
The following code example shows another business service class that uses the EnsLib.File.InboundAdapterOpens in a new tab. Code comments explain the activities within OnProcessInput():
Class training.healthcare.service.SrvFilePatient Extends Ens.BusinessService
{
Parameter ADAPTER = "EnsLib.File.InboundAdapter";
Method OnProcessInput(pInput As %RegisteredObject,
pOutput As %RegisteredObject) As %Status
{
//file must be formatted as set of lines, each field comma separated:
//externalcode,
//name, surname, dateBirth, placeBirth, provinceBirth
//nationality, gender,
//address, city, province, country,
//fiscalCode
//note:
//fiscalCode may be optional
//sso is an internal code so must be detected inside ensemble
//operation must be detected as well:
//if the group: name, surname, dateBirth, placeBirth, provinceBirth
//point to a record then it's an UPDATE; if not it's a NEW
//no DELETE via files
Set $ZT="trap"
set counter=1 //records read
while 'pInput.AtEnd {
set line=pInput.ReadLine()
set req=##class(training.healthcare.message.MsgPatient).%New()
set req.source="FILE"
set req.externalCode=$piece(line,",",1)
set req.name=$piece(line,",",2)
set req.surname=$piece(line,",",3)
set req.dateBirth=$piece(line,",",4)
set req.placeBirth=$piece(line,",",5)
set req.provinceBirth=$piece(line,",",6)
set req.nationality=$piece(line,",",7)
set req.gender=$piece(line,",",8)
set req.address=$piece(line,",",9)
set req.city=$piece(line,",",10)
set req.province=$piece(line,",",11)
set req.country=$piece(line,",",12)
set req.fiscalCode=$piece(line,",",13)
//call the process
//res will be Ens.StringResponse type message
set st=..SendRequestAsync(
"training.healthcare.process.PrcPatient", req)
if 'st
$$$LOGERROR("Cannot call PrcMain Process for patient N°" _ counter)
set counter=counter+1
}
$$$LOGINFO("patients loaded : " _ (counter - 1))
Set $ZT=""
Quit $$$OK
trap
$$$LOGERROR("Error loading for record N°" _ counter _ " - " _ $ZERROR)
SET $ECODE = ""
Set $ZT=""
Quit $$$OK
}
}
Example 3
The following code example shows a business service class that references the EnsLib.File.InboundAdapterOpens in a new tab.
Class EnsLib.File.PassthroughService Extends Ens.BusinessService
{
Parameter ADAPTER = "EnsLib.File.InboundAdapter";
/// Configuration item(s) to which to send file stream messages
Property TargetConfigNames As %String(MAXLEN = 1000);
Parameter SETTINGS = "TargetConfigNames";
/// Wrap the input stream object in a StreamContainer message object
/// and send it. If the adapter has a value for ArchivePath, send async;
/// otherwise send synchronously to ensure that we don't return to the
/// Adapter and let it delete the file before the target Config Item is
/// finished processing it.
Method OnProcessInput(pInput As %Stream.Object,
pOutput As %RegisteredObject) As %Status
{
Set tSC=$$$OK, tSource=pInput.Attributes("Filename"),
pInput=##class(Ens.StreamContainer).%New(pInput)
Set tWorkArchive=(""'=..Adapter.ArchivePath)&&(..Adapter.ArchivePath=
..Adapter.WorkPath || (""=..Adapter.WorkPath &&
(..Adapter.ArchivePath=..Adapter.FilePath)))
For iTarget=1:1:$L(..TargetConfigNames, ",")
{
Set tOneTarget=$ZStrip($P(..TargetConfigNames,",",iTarget),"<>W")
Continue:""=tOneTarget
$$$sysTRACE("Sending input Stream ...")
If tWorkArchive {
Set tSC1=..SendRequestAsync(tOneTarget,pInput)
Set:$$$ISERR(tSC1) tSC=$$$ADDSC(tSC,tSC1)
} Else {
#; If not archiving send Sync to avoid Adapter deleting file
#; before Operation gets it
Set tSC1=..SendRequestSync(tOneTarget,pInput)
Set:$$$ISERR(tSC1) tSC=$$$ADDSC(tSC,tSC1)
}
}
Quit tSC
}
}
This example sets the tSource variable to the original file name which is stored in the Filename subscript of the Attributes property of the incoming stream (pInput).
Adding and Configuring the Business Service
To add your business service to an Ensemble production, use the Management Portal to do the following:
-
Add an instance of your business service class to the Ensemble production.
-
Configure the business service. For information on the settings, see “Reference for Settings.”
-
Enable the business service.
-
Run the production.