I am expecting you might be knowing what it is & why to use it, if not then read this first :
Here is the sample code how to implement it :
import java.io.ByteArrayInputStream;
import java.io.IOException;
import com.ibm.jms.JMSBytesMessage;
import com.ibm.jms.JMSTextMessage;
import com.ibm.websphere.bo.BOXMLSerializer;
import com.ibm.ws.bo.service.BOXMLSerializerImpl;
import commonj.connector.runtime.FunctionSelector;
import commonj.connector.runtime.SelectorException;
import commonj.sdo.DataObject;
public class SampleFunctionSelector implements FunctionSelector {
public String generateEISFunctionName(Object[] argObjects) throws SelectorException
{
String rootElementName = null;
String strMsg =null;
try
{
for (int i = 0; i < argObjects.length; i++) {
if (argObjects[i] instanceof JMSTextMessage){
JMSTextMessage message = (JMSTextMessage) argObjects[i];
String xmlString = message.getText();
System.out.println("MESSAGE is instanceof JMSTextMessage :");
DataObject dataObject =convertXMLToBO(xmlString);
rootElementName = dataObject.getType().getName();
System.out.println("rootElementName::"+rootElementName);
}
else if (argObjects[i] instanceof JMSBytesMessage)
{
System.out.println("MESSAGE is instanceof JMSBytesMessage ");
JMSBytesMessage bytesMsg = (JMSBytesMessage) argObjects[i];
int msgLength = 0;
int BUF_SIZE = 50000;
byte[] data = new byte[BUF_SIZE];
while (true)
{
int len = bytesMsg.readBytes(data);
if (len > 0)
{
msgLength += len;
}
else
{
break;
}
}
byte[] message = new byte[msgLength];
if (msgLength <= BUF_SIZE)
{
System.arraycopy(data, 0, message, 0, msgLength);
}
else
{
bytesMsg.reset(); //reset cursor to beginning
bytesMsg.readBytes(message);
}
strMsg = new String(message);
DataObject dataObject =convertXMLToBO(strMsg);
rootElementName = dataObject.getType().getName();
System.out.println("rootElementName::"+rootElementName);
}
if(rootElementName != null)
{
if(rootElementName.equalsIgnoreCase("Operation1"))
return "processItem";
else if(rootElementName.equalsIgnoreCase("Operation2"))
return "processEmp";
else
System.out.println("no match");
}
}
throw new SelectorException("No Method Identified to process this message");
} catch (Throwable e) {
e.printStackTrace();
throw new SelectorException(e);
}
}
private DataObject convertXMLToBO(String strMsg) throws IOException
{
BOXMLSerializer serializer = new BOXMLSerializerImpl();
ByteArrayInputStream bis = new ByteArrayInputStream(strMsg.getBytes("UTF-8")); //UTF-8 Encoding
DataObject dataObject = serializer.readXMLDocument(bis).getDataObject();
return dataObject;
}
}
Here is the sample code how to implement it :
import java.io.ByteArrayInputStream;
import java.io.IOException;
import com.ibm.jms.JMSBytesMessage;
import com.ibm.jms.JMSTextMessage;
import com.ibm.websphere.bo.BOXMLSerializer;
import com.ibm.ws.bo.service.BOXMLSerializerImpl;
import commonj.connector.runtime.FunctionSelector;
import commonj.connector.runtime.SelectorException;
import commonj.sdo.DataObject;
public class SampleFunctionSelector implements FunctionSelector {
public String generateEISFunctionName(Object[] argObjects) throws SelectorException
{
String rootElementName = null;
String strMsg =null;
try
{
for (int i = 0; i < argObjects.length; i++) {
if (argObjects[i] instanceof JMSTextMessage){
JMSTextMessage message = (JMSTextMessage) argObjects[i];
String xmlString = message.getText();
System.out.println("MESSAGE is instanceof JMSTextMessage :");
DataObject dataObject =convertXMLToBO(xmlString);
rootElementName = dataObject.getType().getName();
System.out.println("rootElementName::"+rootElementName);
}
else if (argObjects[i] instanceof JMSBytesMessage)
{
System.out.println("MESSAGE is instanceof JMSBytesMessage ");
JMSBytesMessage bytesMsg = (JMSBytesMessage) argObjects[i];
int msgLength = 0;
int BUF_SIZE = 50000;
byte[] data = new byte[BUF_SIZE];
while (true)
{
int len = bytesMsg.readBytes(data);
if (len > 0)
{
msgLength += len;
}
else
{
break;
}
}
byte[] message = new byte[msgLength];
if (msgLength <= BUF_SIZE)
{
System.arraycopy(data, 0, message, 0, msgLength);
}
else
{
bytesMsg.reset(); //reset cursor to beginning
bytesMsg.readBytes(message);
}
strMsg = new String(message);
DataObject dataObject =convertXMLToBO(strMsg);
rootElementName = dataObject.getType().getName();
System.out.println("rootElementName::"+rootElementName);
}
if(rootElementName != null)
{
if(rootElementName.equalsIgnoreCase("Operation1"))
return "processItem";
else if(rootElementName.equalsIgnoreCase("Operation2"))
return "processEmp";
else
System.out.println("no match");
}
}
throw new SelectorException("No Method Identified to process this message");
} catch (Throwable e) {
e.printStackTrace();
throw new SelectorException(e);
}
}
private DataObject convertXMLToBO(String strMsg) throws IOException
{
BOXMLSerializer serializer = new BOXMLSerializerImpl();
ByteArrayInputStream bis = new ByteArrayInputStream(strMsg.getBytes("UTF-8")); //UTF-8 Encoding
DataObject dataObject = serializer.readXMLDocument(bis).getDataObject();
return dataObject;
}
}
No comments:
Post a Comment