1
votes

I have written code to send/read message from IBM MQ using Spring Integration JMS-message-driven-channel-adapter but not able to parse the JMS text response message...below is my integration xml

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:int="http://www.springframework.org/schema/integration"
        xmlns:int-jms="http://www.springframework.org/schema/integration/jms"
        xmlns:context="http://www.springframework.org/schema/context"
        xsi:schemaLocation="
            http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans.xsd
            http://www.springframework.org/schema/context
            http://www.springframework.org/schema/context/spring-context.xsd
            http://www.springframework.org/schema/integration
            http://www.springframework.org/schema/integration/spring-integration.xsd
            http://www.springframework.org/schema/integration/jms
            http://www.springframework.org/schema/integration/jms/spring-integration-jms.xsd">

        <context:annotation-config />
        <context:component-scan base-package="com.*****" />

        <!-- Factory Defintions -->
        <bean id="connectionFactory" class="com.ibm.mq.jms.MQQueueConnectionFactory">
            <property name="transportType" value="1" />
            <property name="queueManager" value="****" />
            <property name="hostName" value="******" />
            <property name="port" value="****" />
            <property name="channel" value="******" />
        </bean>

        <!-- Queue Definition -->
        <bean id="inQueue" class="com.ibm.mq.jms.MQQueue" depends-on="connectionFactory">
            <constructor-arg index="0" value="****" />
            <constructor-arg index="1" value="*****" />
        </bean>

        <bean id="outQueue" class="com.ibm.mq.jms.MQQueue" depends-on="connectionFactory">
            <constructor-arg index="0" value="*****" />
            <constructor-arg index="1" value="******" />
        </bean>

        <bean id="messageListener" class="com.***.MessageListener"/>
        <bean id="messagePublisher" class="com.*****.MessagePublisher"/>

        <!-- OUTBOUND settings -->

        <int:channel id="senderChannel" />

        <int-jms:outbound-channel-adapter id="jmsOut" destination="outQueue" channel="senderChannel"/>

       <!--  <int:service-activator output-channel="senderChannel" ref="messagePublisher" method="processMessage" /> -->

        <!-- INBOUND settings -->

        <int:channel id="recieverChannel" />    

        <int-jms:message-driven-channel-adapter id="jmsIn" destination="inQueue" channel="recieverChannel" extract-payload="false" />

        <int:service-activator input-channel="recieverChannel" ref="messageListener" method="processMessage" />

    </beans>

The process intends to send a message over MQ to external server and they may(or may not respond) with an ack.

Publisher code:

import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.integration.support.MessageBuilder;
    import org.springframework.messaging.MessageChannel;

    public class MessagePublisher {

        @Autowired
        private MessageChannel senderChannel;

        public void processMessage(String message) {
            System.out.println("MessagePublisher::::::Sent message: " + message);

            senderChannel.send(MessageBuilder.withPayload(message).build());
        }
    }

Currently in my listener I try to print the ack as :

    import javax.jms.JMSException;
    import javax.jms.Message;
    import javax.jms.TextMessage;

    public class MessageListener {

        public void processMessage(Message message) {

            if (message instanceof TextMessage) {
                TextMessage txtmsg = (TextMessage) message;

                try {

                    System.out.println("MessageListener::::::Received message: "
                            + txtmsg.getText());
                } catch (JMSException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }

The problem is when I get the message back the body contains encoded string as:

MessageListener::::::Received message: [prints encoded special chars here]

How can I parse the complete response text in a proper format? I checked the instance of response object seems to be JMS text message only, but when printed on console I only see encoded string message.

1
We don't know the encoding algorithm, therefore can't help you parse from void. Please, provide more info about content of that message.Artem Bilan
The message is simply "Test Message " sent to external JMS server. The ack received back has JMS headers along with body text.I am able to read the headers fine but seems the body text is encrypted and since it is all special characters , I can't paste them here(it more looks like a byte message) but I cant confirm if its special IBm encoding just by looking at the response.Deepak Sharma
So, just ask that "external server" what they send to you! Not sure what help are you seeking here, since the problem isn't related to any tool usage. This is just a business task and data format in process.Artem Bilan
We have another application that runs using a basic MQ implementation(not spring based) .So we know they send us "Success" or "ERROR" in that message response. I feel somewhere I am missing to specify the encoding while reading the message, but not able to figure out.Anyways thanks for your help, let me know if you see anything wrong I am doing while reading the message from channel.Deepak Sharma

1 Answers

1
votes

OK. I think your problem is really with character encoding, which is different on sender and receiver:

Try this:

byte[] by = ((TextMessage) msg).getText().getBytes("ISO-8859-1");
String text = new String(by,"UTF-8");

More info is here: Encoding a JMS TextMessage and here.