Sample MDB for receiving callout requests from IMS

Because an MDB implements the InboundListener interface, you must implement the onMessage() and onNotification() methods that are used for synchronous and asynchronous processing, respectively.

The following sample shows a bean implementation class for an MDB (IMSCalloutIVPMDBBean) that is configured to listen for both synchronous and asynchronous callout messages from IMS.

package ejbs;

import java.io.IOException;
import javax.resource.ResourceException;
import javax.resource.cci.Record;
import com.ibm.connector2.ims.ico.IMSInputStreamRecord;
import commonj.connector.runtime.InboundInteractionSpec;

/**
 * Bean implementation class for Enterprise Bean: IMSInboundMessage
 */
public class IMSCalloutIVPMDBBean
	implements
		javax.ejb.MessageDrivenBean,
		com.ibm.j2ca.base.ExtendedInboundListener {

	private javax.ejb.MessageDrivenContext fMessageDrivenCtx;

	/**
	 * getMessageDrivenContext
	 */
	public javax.ejb.MessageDrivenContext getMessageDrivenContext() {
		return fMessageDrivenCtx;
	}

	/**
	 * setMessageDrivenContext
	 */
	public void setMessageDrivenContext(javax.ejb.MessageDrivenContext ctx) {
		fMessageDrivenCtx = ctx;
	}

	/**
	 * ejbCreate
	 */
	public void ejbCreate() {
	}

	/**
	 * onMessage
	 */
	public void onMessage(javax.jms.Message msg) {
	}

	/**
	 * ejbRemove
	 */
	public void ejbRemove() {
	}

	/**
	  * onMessage
	  */
	public javax.resource.cci.Record onMessage(
		javax.resource.cci.Record arg0,
		javax.resource.cci.InteractionSpec arg1)
		throws javax.resource.ResourceException {
		
		return processSyncMessageReceived(arg1);
	}

	/**
	 * onNotification
	 */
	public void onNotification(
		javax.resource.cci.Record arg0,
		javax.resource.cci.InteractionSpec arg1)
		throws javax.resource.ResourceException {
		
		processAsyncMessageReceived(arg0);	
	}

	public void onNotification(Record arg0) throws ResourceException {
		processAsyncMessageReceived(arg0);	
	}

	public Record onMessage(Record arg0) throws ResourceException {
		return processSyncMessageReceived(arg0);
	}

	public Record onMessage(InboundInteractionSpec arg0, Record arg1) 
throws ResourceException {
		return processSyncMessageReceived(arg1);
	}

	public void onNotification(InboundInteractionSpec arg0, Record arg1) 
throws ResourceException {
		processAsyncMessageReceived(arg1);	
	}
	
	/*
	 * Process synchronous callout requests from IMS and return a response
	 */
	public Record processSyncMessageReceived(Object event) {
		
        SYNCCALLOUTREQUEST request = new SYNCCALLOUTREQUEST();
		SYNCCALLOUTRESPONSE response = new SYNCCALLOUTRESPONSE();
    	
    	try {
    		// Request
    		request.setBytes(((IMSInputStreamRecord)event).getBytes());
			
			System.out.println("Synchronous callout request from IMS: " +
					request.getSync__callout__request__str());

			// Response
			response.setSync__callout__response__str("HELLO FROM WEBSPHERE MDB");

			System.out.println("Synchronous callout response from WAS MDB: " + 
					response.getSync__callout__response__str());

    	} catch (IOException e) {
        	System.err.println(e);
       }
        		
		return response;
	}
	
	/*
	 * Receive asynchonrous callout requests from IMS
	 */
	public void processAsyncMessageReceived(Object event) {
        ASYNCCALLOUTREQUEST request = new ASYNCCALLOUTREQUEST();
    	
    	try {
    		request.setBytes(((IMSInputStreamRecord)event).getBytes());
			
			System.out.println("Asynchronous callout request from IMS: " + 
				request.getAsync__callout__request__str());
			
        } catch (IOException e) {
        	System.err.println(e);
        }
	}
}