Example Receiver Implementation Outline

The following code and pseudo code outlines an example implementation for a JMS Receiver.

public class CustomJMSReceiver implements ReceiverInterface {
         private Context m_context = null;
         private ReceiverConfig m_rcvConfig = null;
         public void init(Context context, ReceiverConfig receiverConfig) {
           this.m_context = context;
           this.m_rcvConfig = receiverConfig; 
return;
}
public void refreshConfig(ReceiverConfig rcvconfig) 
                 throws BCGReceiverException {
          this.m_rcvConfig = rcvconfig;
 
// Check which receiver targets are updated, added newly or deleted
// If new target is added, create a new thread and start polling the target
// If current target is updated, stop the thread which is polling the
// target, and using the updated configuration information, start polling 
// If current target is deleted, stop the thread which is polling the
// target and delete the thread which is responsible for polling the
//target.
...
return;
}
 
 public void startReceiving() throws BCGReceiverException {
// Read the list of targets in the ReceiverConfig object
// For each target create a UserJMSThread and start the thread 
return;
}
public void processResponse(ResponseCorrelation respCorr, 
                             ReceiverDocumentInterface response)
                    throws BCGReceiverException {
// get the correlation information like reply-to-queue, correlation id 
// and send the response to that queue 
return;
}
public void stopReceiving() throws BCGReceiverException {
// get the list of UserJMSReceiverThreads associated with each target
// call stop method.
... 
return;
}
private class UserJMSReceiverThread extends Thread {
 public UserJMSReceiverThread(Config targetConfig) {
// create the queue session, connection, queue receiver
...
}
public void run() {
 while (true) {
       try{
          // call receive method on the queue
          // if message is available read the message and process the
          // document
 
             processDocument(data);
 
          // else continue to poll the queue.
 
        ...
 
       } catch(Exception e) {
 
          ...
      }
  }
}
//Upon receiving the document from the queue, start processing the
//documenting using Receiver FrameWork APIs 
 
public void processDocument(byte[] data) throws BCGReceiverException{
             //Get the temporary location where data can be written
         File tempDir = BCGReceiverUtil.getTempDir();
            // Now create the temp file and write the data into it
         File fileLocation = new File(tempDir, fileStr);
         FileOutputStream fos = new FileOutStream(fileLocation);
         fow.write(data);
         fos.close(); 
// Create the ReceiverDocument object
        ReceiverDocumentInterface request = 
                             BCGReceiverUtil.createReceiverDocument();
       // set document, transport headers and BCG headers in the
       // request
        request.setDocument(fileLocation);
     ...
      //Now start processing the document using ReceiverFrameWork APIs
          ReceiverFrameWorkInterface rcvFramework = 
                                BCGReceiverUtil.getReceiverFramework();
 
          ReceiverDocumentInterface requestDocs[] = 
                rcvFramework.preprocess(transportType,target,request);
      //Check if the requestDocs length is 1, if yes document is not
      //split into multiple documents 
         boolean sync =
                      rcvFramework.syncCheck(transportType,target,request);
     ...
        if(!sync) {
            //request is not synchronous message
              rcvFramework.process(transportType,target,request);
      }
 }



Copyright IBM Corp. 2003, 2004