欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

WebSphere MQ简单实例(发送消息)

程序员文章站 2022-07-13 12:17:52
...
MQ消息发送:
package com.main;

import javax.jms.JMSException;
import javax.jms.MapMessage;
import javax.jms.Message;
import javax.jms.Session;
import javax.jms.TextMessage;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;

/**
* MQ发送消息
*
* @author
*/
public class TestSend
{

    /**
     * jms模板,封装链接工厂、队列、消息生产者
     */
    private JmsTemplate jmsTemplate;

    public TestSend()
    {
        ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
        jmsTemplate = (JmsTemplate)ctx.getBean("jmsTemplate");
    }

    /**
     * 发送消息
     *
     * @param msg消息
     */
    public void showResult(String msg)
    {
        // Text消息
        MessageCreator messageCreator = createTxtMsg(msg);
        jmsTemplate.send(messageCreator);
        // Map消息
        messageCreator = createMapMsg(msg);
        jmsTemplate.send(messageCreator);
    }

    /**
     * 创建text消息
     *
     * @param msg 消息
     * @return message
     */
    private MessageCreator createTxtMsg(final String msg)
    {
        return new MessageCreator()
        {

            public Message createMessage(Session session) throws JMSException
            {
                TextMessage textMessage = session.createTextMessage();
                textMessage.setText(msg);
                return textMessage;
            }
        };
    }

    /**
     * 创建map消息
     *
     * @param msg消息
     * @return message
     */
    private MessageCreator createMapMsg(final String msg)
    {
        return new MessageCreator()
        {

            public Message createMessage(Session session) throws JMSException
            {
                MapMessage mapMessage = session.createMapMessage();
                mapMessage.setString("map", "javax.jms.MapMessage");
                mapMessage.setString("map1", msg);
                return mapMessage;
            }
        };
    }

    /**
     * @param args
     */
    public static void main(String[] args)
    {
        TestSend send = new TestSend();
        send.showResult("start, send!");
    }

}

配置文件:
<bean id="jmsConnectionFactory" class="com.ibm.mq.jms.MQQueueConnectionFactory">
<property name="hostName" value="10.21.139.43" />
<property name="port" value="1414" />
<property name="CCSID" value="1381" />
<property name="queueManager" value="QM_SN_CNHQ_9379C" />
</bean>

<bean id="queue" class="com.ibm.mq.jms.MQQueue">
<property name="baseQueueName" value="default" />
</bean>

<bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
<property name="connectionFactory" ref="jmsConnectionFactory" />
<property name="defaultDestination" ref="queue" />
<property name="pubSubDomain" value="false" />
</bean>
  • src.zip (3.3 KB)
  • 下载次数: 158
相关标签: MQ