MQ消息队列

最近开发工作中平台之间的交互用到了MQ消息队列,之前一直没有过多的接触,通过几天的接触,略懂。。。写一个小的demo作为参考。

首先从搭配环境说起:从Apache官网中下载apache-activemq-5.4.0。解压点击bin/activemq.bat文件,启动MQ。默认端口号为http://localhost:8161/admin

直接看代码(spring+MQ):

<?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:context="http://www.springframework.org/schema/context" 
             xsi:schemaLocation="http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans-2.5.xsd 
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context-2.5.xsd"> 

        <!-- 配置JMS连接工厂 --> 
        <bean id="connectionFactory" class="org.apache.activemq.spring.ActiveMQConnectionFactory"> 
                <property name="brokerURL" value="tcp://localhost:61616"/> 
        </bean> 

        <!-- 配置JMS模版 --> 
        <bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate"> 
                <property name="connectionFactory" ref="connectionFactory"/> 
        </bean> 

        <!-- 发送消息的目的地(一个队列) --> 
        <bean id="destination" class="org.apache.activemq.command.ActiveMQQueue"> 
                <!-- 设置消息队列的名字 --> 
                <constructor-arg index="0" value="HelloWorldQueue"/> 
        </bean> 
</beans>

看创建一个消息的发送者:

    public class MySender { 
            public static void main(String[] args) { 
                    ApplicationContext ctx = new ClassPathXmlApplicationContext("/applicationContext.xml"); 
                    JmsTemplate template = (JmsTemplate) ctx.getBean("jmsTemplate"); 
                    Destination destination = (Destination) ctx.getBean("destination"); 
                    template.send(destination, new MessageCreator() { 
                            public Message createMessage(Session session) throws JMSException {
                                    return session.createTextMessage(Calendar.getInstance().getTime() + "发送消息:Hello ActiveMQ Text Message!"); 
                            } 
                    }); 
                    System.out.println("成功发送了一条JMS消息"); 
            }
            
    }

消息的接收者:

public class MyReceiver implements MessageListener{ 
    private static Session session;
    private static Destination destination;
    private static MessageProducer replyProducer;
        public static void main(String[] args) throws JMSException { 
                ApplicationContext ctx = new ClassPathXmlApplicationContext("/applicationContext.xml"); 
                JmsTemplate template = (JmsTemplate) ctx.getBean("jmsTemplate"); 
                Destination destination = (Destination) ctx.getBean("destination"); 
                while (true) {
                        TextMessage txtmsg = (TextMessage) template.receive(destination); 
                        System.out.println("收到消息内容为: " + txtmsg.getText());
                }
        }
}

另外:消息的接收有两种方式,一种直接调用receive方法

MessageConsumer comsumer = session.createConsumer(destination);
        comsumer.setMessageListener(new MessageListener(){  
            @Override  
            public void onMessage(Message m) {  
                TextMessage textMsg = (TextMessage) m;  
                try {  
                    System.out.println(textMsg.getText());  
                } catch (JMSException e) {  
                    e.printStackTrace();  
                }  
            }  
             
        });

 

posted @ 2014-08-23 15:43  942391815  阅读(246)  评论(0)    收藏  举报