Using File Adapters with Ensemble
Using the File Inbound Adapter
[Home] [Back] [Next]
InterSystems: The power behind what matters   

This chapter describes how to use the file inbound adapter (EnsLib.File.InboundAdapter). 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.InboundAdapter 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:
In more detail:
  1. 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.
  2. 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.
  3. 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:
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 %FileBinaryStream, 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 %FileCharacterStream or %FileBinaryStream.
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
Method OnProcessInput(pInput As %FileBinaryStream,pOutput As %RegisteredObject) As %Status
The OnProcessInput() method should do some or all of the following:
  1. Examine the input file (pInput) and decide how to use it.
  2. 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.
  3. For the request message, set its properties as appropriate, using values in the input.
  4. 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 %Status).
  5. 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.
  6. 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
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.InboundAdapter. This example works as follows:
  1. The file has a header. The header information is added to each transaction.
  2. The file experiences a number of transactions.
  3. The header and transaction XML structures are defined by the classes LBAPP.Header and LBAPP.Transaction (not shown).
  4. Some error-handling is shown, but not all.
  5. The method RejectBatch() is not shown.
  6. 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.
  7. The entire transaction object is passed as the payload of each message to the business process.
  8. 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

 // 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.InboundAdapter. Code comments explain the activities within OnProcessInput():
Class 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:
  //name, surname, dateBirth, placeBirth, provinceBirth
  //nationality, gender,
  //address, city, province, country,
  //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(
    set req.source="FILE"

    set req.externalCode=$piece(line,",",1)
    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.province=$piece(line,",",11)
    set req.fiscalCode=$piece(line,",",13)

    //call the process
    //res will be Ens.StringResponse type message
    set st=..SendRequestAsync(
           "", 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

  $$$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.InboundAdapter.
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"),
  Set tWorkArchive=(""'=..Adapter.ArchivePath)&&(..Adapter.ArchivePath=
    ..Adapter.WorkPath || (""=..Adapter.WorkPath && 
  For iTarget=1:1:$L(..TargetConfigNames, ",")
    Set tOneTarget=$ZStrip($P(..TargetConfigNames,",",iTarget),"<>W")
    $$$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:
  1. Add an instance of your business service class to the Ensemble production.
  2. Configure the business service. For information on the settings, see Reference for Settings.”
  3. Enable the business service.
  4. Run the production.

Send us comments on this page
Copyright © 1997-2019 InterSystems Corporation, Cambridge, MA