When you get a message in your MDB, a message from a distributed platform will be of type TextMessage. Messages sent from legacy platforms will most likely be of type BytesMessage. This is the code we use in production (change your codepage to suit):
Code:
if (msg instanceof BytesMessage){
final String theMessage = MDBHelper.codepagePreProcess((BytesMessage)msg,MDBHelper.CP1047_CODEPAGE,MDBHelper.MESSAGE_BUFFER);
manager.process(theMessage);
}else if (msg instanceof TextMessage){
final TextMessage message = (TextMessage) msg;
manager.process(message.getText());
}else {
//send SNMP error
}
}
Code:
public static String codepagePreProcess(BytesMessage bytesMsg, String codePage, int bufferSize) throws Exception{
byte[] buffer = new byte[bufferSize];
byte[] body = new byte[0];
while (true){
int numBytesRead = bytesMsg.readBytes(buffer);
if (numBytesRead == -1){
break;
}
byte[] tmp = new byte[body.length + numBytesRead];
System.arraycopy(body,0,tmp,0,body.length);
System.arraycopy(buffer,0,tmp,body.length,numBytesRead);
body = tmp;
} //end while
return new String(body,codePage);
}
Code:
public final static String CP1047_CODEPAGE = "Cp1047";
public final static int MESSAGE_BUFFER = 10000;
Please be aware that IBM MQ itself has mechanisms to do this transformation, but for other reasons we prefer to do it in code.