Creating a user-defined JMS data binding

A user-defined JMS data binding is necessary when the JMS client is expecting a JavaBean and the SCA application is expecting a business object.

You should have a module with your SCA application and a JMS client.
You will create a user-defined JMS data binding that will map between a JavaBean and its business object representation. In this task, the JMS client is expecting a JavaBean BookObject.java as the payload of a JMS object message. Because the provided default JMS data bindings would not serialize this properly, a user-defined JMS data binding is needed.

The JavaBean BookObject.java code is shown below:

public class BookObject implements java.io.Serializable {

	private String ISDN;

	private String author;

	private String title;

	private float price;

	public String getAuthor() {
		return author;
	}

	public void setAuthor(String author) {
		this.author = author;
	}

	public String getISDN() {
		return ISDN;
	}

	public void setISDN(String isdn) {
		ISDN = isdn;
	}

	public float getPrice() {
		return price;
	}

	public void setPrice(int price) {
		this.price = price;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}
}
  1. You will need to represent the JavaBean BookObject as a business object. If you do not already have a business object to represent BookObject, the easiest way to accomplish this would be to create a business object with four attributes to represent the four attributes of the JavaBean BookObject. Right click on Data Types in your module, and select New > Business Object from the contextual menu. Specify an appropriate name such as BookObjectBO for your business object, and select Finish. Add the appropriate attributes to your business object to fully describe it, and save.
    BookObject business object
  2. You will now need to create a JMS data binding that would map the JavaBean BookObject and the business object BookObjectBO created in step 1. The data binding implementation must implement com.ibm.websphere.sca.jms.data.JMSDataBinding. The skeleton of such a class is shown as follows:
    import javax.jms.JMSException;
    import javax.jms.Message;
    
    import com.ibm.websphere.sca.jms.data.JMSDataBinding;
    import commonj.connector.runtime.DataBindingException;
    import commonj.sdo.DataObject;
    
    public class BookObjectDataBinding implements JMSDataBinding {
    
    public int getMessageType() {return 0;}
    public void read(Message arg0) throws JMSException {}
    public void write(Message arg0) throws JMSException {}
    public boolean isBusinessException() {return false;}
    public void setBusinessException(boolean arg0) {}
    public DataObject getDataObject() throws DataBindingException {return null;}
    public void setDataObject(DataObject arg0) throws DataBindingException {}
    }
  3. You will now need to provide the implementation. For a detailed description of the methods see JMS and MQ JMS data bindings. The following implementation of the getMessageType(), read(Message arg0) and write(Message arg0) could be used to map between the BookObjectBO business object and the BookObject JavaBean.
    	public void read(Message message) throws JMSException {
    		//		 Get the payload of the JMS Object message
    		BookObject bookObj = (BookObject) ((ObjectMessage) message).getObject();
    
    		//		 Create the SDO
    		com.ibm.websphere.sca.ServiceManager serviceManager = new com.ibm.websphere.sca.ServiceManager();
    		com.ibm.websphere.bo.BOFactory factory = (com.ibm.websphere.bo.BOFactory) serviceManager
    				.locateService("com/ibm/websphere/bo/BOFactory");
    		DataObject bookBO = factory.create("http://BB_Books_SCA",
    				"BookObjectBO");
    
    		bookBO.setString("ISDN", bookObj.getISDN());
    		bookBO.setString("Author", bookObj.getAuthor());
    		bookBO.setString("Title", bookObj.getTitle());
    		bookBO.setFloat("Price", bookObj.getPrice());
    
    		try {
    			//		 Set the data object
    			setDataObject(bookBO);
    		} catch (DataBindingException e) {
    			throw new JMSException(e.getLocalizedMessage());
    		}
    	}
    
    	public void write(Message message) throws JMSException {
    		try {
    			//		 Get the data object
    			DataObject bookBO = getDataObject();
    
    			//		 Create the Book Object
    			BookObject bookObj = new BookObject();
    			bookObj.setISDN(bookBO.getString("ISDN"));
    			bookObj.setAuthor(bookBO.getString("Author"));
    			bookObj.setTitle(bookBO.getString("Title"));
    			bookObj.setPrice(bookBO.getFloat("Price"));
    
    			//		 Set the payload of the JMS Object Message
    			((ObjectMessage) message).setObject(bookObj);
    
    		} catch (DataBindingException e) {
    			throw new JMSException(e.getLocalizedMessage());
    		}
    	}
You can customize JMS bindings also in the runtime environment. This customization is similar to this sample in that you can translate your own custom object streams to data objects. See JMS data binding samples for WebSphere Process Server V 6.0.2 for more information on this approach.
Related tasks
Creating a JMS import to communicate with a JMS client
Creating a JMS export to communicate with a JMS client
Creating a JMS client to receive messages from a JMS import
Creating an import from an export using a JMS binding

Feedback
(C) Copyright IBM Corporation 2005, 2007. All Rights Reserved.