首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > Web前端 >

WebSphere MQ容易实例(发送消息)

2012-07-25 
WebSphere MQ简单实例(发送消息)MQ消息发送:package com.mainimport javax.jms.JMSExceptionimport java

WebSphere MQ简单实例(发送消息)
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" 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" value="default" />
</bean>

<bean id="jmsTemplate" ref="jmsConnectionFactory" />
<property name="defaultDestination" ref="queue" />
<property name="pubSubDomain" value="false" />
</bean> 1 楼 formatall 2012-05-10   Caused by: java.lang.NoClassDefFoundError: com/ibm/disthub2/impl/client/SessionConfig
at com.ibm.msg.client.wmq.factories.WMQFactoryFactory.setupDistHubTracing(WMQFactoryFactory.java:357)
at com.ibm.msg.client.wmq.factories.WMQFactoryFactory.<init>(WMQFactoryFactory.java:327)
at com.ibm.msg.client.wmq.factories.WMQFactoryFactory.getInstance(WMQFactoryFactory.java:180)
at com.ibm.msg.client.wmq.factories.WMQComponent.getFactoryInstance(WMQComponent.java:131)
at com.ibm.msg.client.jms.internal.JmsFactoryFactoryImpl.getInstance(JmsFactoryFactoryImpl.java:152)
at com.ibm.msg.client.jms.admin.JmsConnectionFactoryImpl.setProviderFactory(JmsConnectionFactoryImpl.java:163)
at com.ibm.mq.jms.MQConnectionFactory.<init>(MQConnectionFactory.java:276)
at com.ibm.mq.jms.MQQueueConnectionFactory.<init>(MQQueueConnectionFactory.java:76)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:147)
... 16 more
2 楼 SpaceCity 2012-05-14   缺少配置和jar;建议将MQ的lib下的jar全部加载。

热点排行