(WID/WPS) JMSCustomDataBinding

This post is related to a specific requirement where client sends message (Business Object format) to a Queue based system & receive response again from a queue based system (String format)


Now the requirement is :
In Request flow : To convert message (BO Format) to message (String Format) before sending it to queue.
In Response flow : To convert message (String Format) to message (BO Format) before sending it to caller (may be a business process) for further processing.
We have to make use of custom data binding which would intercept the communication between service provider & consumer.


We have to set the binding property of MQ import with 'Default data format' value which is "JMSCustomDataBindingStrImpl" in this case (Shown in diagram below)




This binding class JMSCustomDataBindingStrImpl extends JMSDataBindingImplXML class & override write() (Used when writing to the queue : Request part) & read() (Used when reading from the queue : Response part) methods.


Here is the code :
public class JMSCustomDataBindingStrImpl extends JMSDataBindingImplXML

{
/*
* write method is overwritten here to post the string data into Queue as text message.
* message variable get the value from the string process variable form the BPEL.
* @see com.ibm.websphere.sca.jms.data.impl.JMSDataBindingImplXML#write(javax.jms.Message)
*/
public void write(Message message) throws JMSException
{
String strMessagea = null;
commonj.sdo.DataObject dataObject = this.convertStrToDataObject(); 
strMessagea =  dataObject.getString(0);
System.out.println(" Message To Post in the Queue :" + strMessagea );
((TextMessage)message).setText(strMessagea);
System.out.println(" Message posted successfully.....");
}
private DataObject convertStrToDataObject() 
{
// TODO Auto-generated method stub
String bo = getAsString();
commonj.sdo.DataObject dataObject = null;
try {
// create BO from XML string
byte[] C_START = new byte[] {'<', '!', '[', 'C', 'D', 'A', 'T', 'A', '['};
byte[] C_END = new byte[] {']', ']', '>'};
String encoding = "UTF-8";
String Cstart = new String(C_START, encoding);
String Cend = new String(C_END, encoding);
if (bo.startsWith(Cstart)) {
bo = bo.substring(Cstart.length(), bo.length()-Cend.length());
}
com.ibm.websphere.bo.BOXMLSerializer boXMLSerializer = (com.ibm.websphere.bo.BOXMLSerializer) com.ibm.websphere.sca.ServiceManager.INSTANCE.locateService("com/ibm/websphere/bo/BOXMLSerializer");
com.ibm.websphere.bo.BOXMLDocument boDoc = boXMLSerializer.readXMLDocument(new java.io.ByteArrayInputStream(bo.getBytes(encoding)));
dataObject = boDoc.getDataObject();
}
catch(java.io.IOException ex){
}
return dataObject;
}
public void read(Message responseMessage) throws JMSException {
JMSTextMessage txt= (JMSTextMessage)responseMessage;
String responseXMLString = txt.getText();
System.out.println("The response string is : " + responseXMLString);
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 responseBO = factory.create("http://Test","InputBO");
responseBO.setString("field1", responseXMLString);
setDataObject(responseBO);

}
}

No comments:

Post a Comment