Deepak Sharma
Deepak Sharma

Reputation: 11

Spring Integration IBM MQ JMS client

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.

Upvotes: 1

Views: 1355

Answers (1)

Artem Bilan
Artem Bilan

Reputation: 121560

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.

Upvotes: 1

Related Questions