Announcement Announcement Module
Collapse
No announcement yet.
Need to convert incoming JMS messages from EBCDIC encoding to ASCII Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Need to convert incoming JMS messages from EBCDIC encoding to ASCII

    Hello,

    I am using Spring JMS to receive messages from a WebSphere MQ queue. The problem with these messages is that they are all being sent by an IBM mainframe which has encoded them all in EBCDIC format. How can I convert the incoming messages back into ASCII or some other encoding that I can read and use ? Can I leverage some kind of MessageConverter or is it more complicated?

    Thanks,

    Brad

  • #2
    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.

    Comment


    • #3
      The code works really well...but I'm wondering what IBM MQ has to do the transformation. Does anyone know?

      Comment

      Working...
      X