Spring集成IBM MQ JMS客户端

时间:2017-03-29 04:17:02

标签: spring spring-integration

我编写了使用Spring Integration JMS-message-driven-channel-adapter从IBM MQ发送/读取消息的代码,但是无法解析JMS文本响应消息...下面是我的集成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>

该进程打算通过MQ向外部服务器发送消息,他们可能(或可能不响应)使用确认。

发布商代码:

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());
        }
    }

目前在我的听众中,我尝试将确认打印为:

    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();
                }
            }
        }

问题是,当我收到消息时,正文包含编码字符串:

MessageListener ::::::收到的消息:[在此处打印编码的特殊字符]

如何以正确的格式解析完整的响应文本?我检查了响应对象的实例似乎只是JMS文本消息,但是当在控制台上打印时我只看到编码的字符串消息。

1 个答案:

答案 0 :(得分:1)

行。我认为您的问题确实存在于character encoding,这在发件人和收件人方面有所不同:

试试这个:

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

更多信息在此处:Encoding a JMS TextMessagehere