`
SpaceCity
  • 浏览: 97820 次
  • 性别: Icon_minigender_1
  • 来自: 南京
社区版块
存档分类
最新评论

WebSphere MQ简单实例(接收消息)

阅读更多
MQ接收消息:
package com.main;

import java.io.UnsupportedEncodingException;
import java.util.Enumeration;
import javax.jms.BytesMessage;
import javax.jms.JMSException;
import javax.jms.MapMessage;
import javax.jms.Message;
import javax.jms.ObjectMessage;
import javax.jms.StreamMessage;
import javax.jms.TextMessage;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jms.core.JmsTemplate;

/**
* MQ接收消息
*
* @author 88183239
*/
public class TestReceive
{
    /**
     * jms模板,封装链接工厂、队列、消息生产者
     */
    private JmsTemplate jmsTemplate;

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

    /**
     * 接收消息
     *
     * @param msg消息
     */
    public void showResult()
    {
        Message msg = jmsTemplate.receive();
        onMessage(msg);
        msg = jmsTemplate.receive();
        onMessage(msg);
    }

    @SuppressWarnings("unchecked")
    private void onMessage(Message msg)
    {
        // text消息
        if (msg instanceof TextMessage)
        {
            TextMessage message = (TextMessage)msg;
            try
            {
                String data = message.getText();
                System.out.println(data);
            }
            catch (JMSException e)
            {
                throw new RuntimeException("JMSException", e);
            }
        }
        // 对象消息
        else if (msg instanceof ObjectMessage)
        {
            ObjectMessage message = (ObjectMessage)msg;
            try
            {
                int id = message.getIntProperty("id");
                System.out.println(id);
                boolean flag = message.getBooleanProperty("flag");
                System.out.println(flag);
            }
            catch (JMSException e)
            {
                e.printStackTrace();
            }
        }
        // map消息
        else if (msg instanceof MapMessage)
        {
            MapMessage message = (MapMessage)msg;
            try
            {
                Enumeration mapNames = message.getMapNames();
                while (mapNames.hasMoreElements())
                {
                    String data = (String)mapNames.nextElement();
                    System.out.println(message.getString(data));
                }
            }
            catch (JMSException e)
            {
                throw new RuntimeException("JMSException", e);
            }
        }
        // bytes消息
        else if (msg instanceof BytesMessage)
        {
            BytesMessage message = (BytesMessage)msg;
            byte[] buff = null;
            String data = null;
            try
            {
                long length = message.getBodyLength();
                buff = new byte[(int)length];
                message.readBytes(buff);
                data = new String(buff, "UTF-8");
                System.out.println(data);
            }
            catch (JMSException e)
            {
                e.printStackTrace();
            }
            catch (UnsupportedEncodingException e)
            {
                e.printStackTrace();
            }
        }
        // stream消息
        else if (msg instanceof StreamMessage)
        {
            StreamMessage message = (StreamMessage)msg;
            try
            {
                String data = message.readString();
                System.out.println(data);
            }
            catch (JMSException e)
            {
                e.printStackTrace();
            }
        }

    }

    /**
     * @param args
     */
    public static void main(String[] args)
    {
        TestReceive send = new TestReceive();
        send.showResult();
    }

}

MQ监听消息:
package com.mq;

import java.io.UnsupportedEncodingException;
import java.util.Enumeration;
import javax.jms.BytesMessage;
import javax.jms.JMSException;
import javax.jms.MapMessage;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.ObjectMessage;
import javax.jms.StreamMessage;
import javax.jms.TextMessage;

/**
* 消息监听
*
* @author
*/
public class ProductView implements MessageListener
{

    @SuppressWarnings("unchecked")
    public void onMessage(Message msg)
    {
        // text消息
        if (msg instanceof TextMessage)
        {
            TextMessage message = (TextMessage)msg;
            try
            {
                String data = message.getText();
                System.out.println(data);
            }
            catch (JMSException e)
            {
                throw new RuntimeException("JMSException", e);
            }
        }
        // 对象消息
        else if (msg instanceof ObjectMessage)
        {
            ObjectMessage message = (ObjectMessage)msg;
            try
            {
                int id = message.getIntProperty("id");
                System.out.println(id);
                boolean flag = message.getBooleanProperty("flag");
                System.out.println(flag);
            }
            catch (JMSException e)
            {
                e.printStackTrace();
            }
        }
        // map消息
        else if (msg instanceof MapMessage)
        {
            MapMessage message = (MapMessage)msg;
            try
            {
                Enumeration mapNames = message.getMapNames();
                while (mapNames.hasMoreElements())
                {
                    String data = (String)mapNames.nextElement();
                    System.out.println(message.getString(data));
                }
            }
            catch (JMSException e)
            {
                throw new RuntimeException("JMSException", e);
            }
        }
        // bytes消息
        else if (msg instanceof BytesMessage)
        {
            BytesMessage message = (BytesMessage)msg;
            byte[] buff = null;
            String data = null;
            try
            {
                long length = message.getBodyLength();
                buff = new byte[(int)length];
                message.readBytes(buff);
                data = new String(buff, "UTF-8");
                System.out.println(data);
            }
            catch (JMSException e)
            {
                e.printStackTrace();
            }
            catch (UnsupportedEncodingException e)
            {
                e.printStackTrace();
            }
        }
        // stream消息
        else if (msg instanceof StreamMessage)
        {
            StreamMessage message = (StreamMessage)msg;
            try
            {
                String data = message.readString();
                System.out.println(data);
            }
            catch (JMSException e)
            {
                e.printStackTrace();
            }

        }
    }

}

配置信息:
<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>

<!-- 此为接收MQ数据用的配置 -->
<bean id="productViewJmsContainer"
class="org.springframework.jms.listener.DefaultMessageListenerContainer">
<property name="connectionFactory" ref="jmsConnectionFactory" />
<property name="destination" ref="queue" />
<property name="messageListener">
<bean class="com.mq.ProductView" />
</property>
<property name="concurrentConsumers" value="10" />
</bean>
  • src.zip (3.3 KB)
  • 下载次数: 150
分享到:
评论
2 楼 weiqiulai 2012-06-21  
请问<bean id="queue" class="com.ibm.mq.jms.MQQueue">
<property name="baseQueueName" value="default" />
</bean>
您的队列名就是“default”吗?
1 楼 weiqiulai 2012-06-20  
不明白ProductView监听启动到了什么作用?我觉得逻辑是MQ接到数据然后,被ProductView监听到于是启动TestReceive接收消息??楼主的意见呢????

相关推荐

    WebSphere MQ 多实例部署方案

    多实例队列管理器特征是MQV7.0.1版本之后引进的新特征,它是MQ产品的内置功能,丰富了 MQ 高可用性的解决方案。 用户可以在不同机器上定义并启动此队列管理器的多个实例,包括一个活动实例和一个备用实例。 激活的...

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

    NULL 博文链接:https://spacecity.iteye.com/blog/1503900

    Websphere MQ入门教程

    6.2.4 WebSphere MQ对象配置实例 81 6.3通道的维护 83 6.3.1通道的状态 83 6.3.2通道维护命令 84 6.3.3设置MaxChannels和MaxActiveChannels属性 88 6.4配置侦听程序 88 6.4.1 Windows 平台 88 6.4.2 unix 平台 88 ...

    websphere mq 多实例的二份资料

    websphere mq 多实例的二份资料,对搞MQ7。0的还是可以看看的,一份是 check list 的PDF,一份是较详细的介绍,是官方的PPT转成的

    WebSphere MQ自学笔记

    本人自学IBM的WebSphereMQ自学笔记,内有MQ安装文档,...3、WebSphere MQ Java编程实例 11 3.1 开发前步骤 11 3.1.1将消息发送至本地队列 11 3.1.2 将消息发送至远程队列 14 3.1.3 在客户机 - 服务器配置上发送消息 19

    IBM WebSphere MQ 安装包

    IBM WebSphere MQ是IBM业界领先面向消息的中间件产品,也是MQ系列产品的基础和核心,它使不同的应用程序能够以企业级的性能,在广泛的平台上安全而可靠地通讯。

    C# 实现消息的收发IBM WebSphere MQ 消息队列

    一个C#实现IBM WebSphere MQ 消息收发的实例,包含 发送接收等. 使用的时候只需要修改 appconfig 文件的内容即可. 如有问题.请留言

    WebSphereMQ_V7.5.0.2_for_Windows(4-1)

    通过为重要的消息和事务提供可靠的、一次且仅一次的传递,Websphere MQ 可以处理复杂的通信协议,并动态地将消息传递工作负载分配给可用的资源。 IBM 消息中间件MQ以其独特的安全机制、简便快速的编程风格、卓越不凡...

    IBM WEBSPHERE MQ实现本地队列消息传收

    IBM WEBSPHERE MQ实现本地队列消息传送接收。websphere资源管理器中队列,通道等的创建过程,以及java代码实现数据的传输

    Websphere MQ入门教程7

    全书共分为3部分共14章,第一部分 WebSphere MQ原理和体系结构,分为两章;第二部分 WebSphere MQ系统管理,分为六章,分别介绍安装、配置、管理、控制命令和问题确定;第三部分 WebSphere MQ应用开发,由五章组成,...

    非常全面的WebsphereMQ的PDF学习资料

    Websphere MQ Programming Guide,Websphere MQ Using C++,WebSphere MQ Using Java,WEBSPHERE MQ6.0 JAVA编程,WebSphere MQ基础教程,IBM WEBSPHERE MQ教程,精通WebSphere MQ,WebSphere MQ开发快速入门,IBM ...

    WebSphereMQ_V7.5.0.2_for_Windows.part2.rar

    通过为重要的消息和事务提供可靠的、一次且仅一次的传递,Websphere MQ 可以处理复杂的通信协议,并动态地将消息传递工作负载分配给可用的资源。 IBM 消息中间件MQ以其独特的安全机制、简便快速的编程风格、卓越不凡...

    IBM Websphere MQ 教程之备份与恢复

    IBM Websphere MQ 教程之备份与恢复

    WebSphere MQ7.0配置与测试(java 源码,含图)

    WebSphere MQ7.0配置与测试 WebSphere MQ发送接收消息的实现 附java 源码

    WebSphere MQ工作原理

    消息中间件及WebSphere MQ入门,介绍WebSphere MQ工作原理,消息队列技术,MQ的基本概念,MQ的通讯模式

    IBM Websphere MQ PCF 编程实例

    IBM Websphere MQ PCF 编程 实例

    WebSphere MQ 开发培训

    WebSphere MQ 开发培训 IBM软件部 软件部 WebSphere MQ 议程 WebSphere MQ 概览 WebSphere MQ 安装配置 WebSphere MQ 应用开发 WebSphere MQ 参考

    IBM Websphere mq安装

    IBM Websphere mq安装IBM Websphere mq安装

Global site tag (gtag.js) - Google Analytics