码迷,mamicode.com
首页 > 其他好文 > 详细

ActiveMQ 入门使用实例

时间:2016-06-08 12:25:14      阅读:158      评论:0      收藏:0      [点我收藏+]

标签:

1.下载ActiveMQ

去官方网站下载:http://activemq.apache.org/download-archives.html

2.运行ActiveMQ

解压缩apache-activemq-5.9.0-bin.zip,然后双击apache-activemq-5.9.0\bin\activemq.bat运行ActiveMQ程序。

3.在Eclipse 创建项目并运行

如果是使用 Maven 项目,则在pom.xml 中配置

<dependency>
  <groupId>org.apache.activemq</groupId>
  <artifactId>activemq-all</artifactId>
  <version>5.9.0</version>
</dependency>

或者 直接创建项目,导入 activemq-all-5.9.0.jar ,以及其日志依赖包

4.创建一个消息队列

Consumer.java
package com.panie.mq.queue;

import java.util.Date;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.MapMessage;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.MessageListener;
import javax.jms.MessageProducer;
import javax.jms.Session;

import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;

public class Consumer
{
    public static void main(String[] args)
    {
        String user = ActiveMQConnection.DEFAULT_USER;
        String password = ActiveMQConnection.DEFAULT_PASSWORD;
        String url = ActiveMQConnection.DEFAULT_BROKER_URL;
        String subject = "TOOL.DEFAULT";
        
        ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(user, password, url);
        
        try
        {
            Connection connection = connectionFactory.createConnection();
            connection.start();
            final Session session = connection.createSession(Boolean.TRUE, Session.AUTO_ACKNOWLEDGE);
            Destination destination = session.createQueue(subject);
            MessageConsumer consumer = session.createConsumer(destination);
            consumer.setMessageListener(new MessageListener()
            {
                
                @Override
                public void onMessage(Message msg)
                {
                    MapMessage message = (MapMessage)msg;
                    Date date = new Date();
                    try
                    {
                        System.out.println("--收到消息:"+new Date(message.getLong("count")));

                        session.commit();
                    }
                    catch (JMSException e)
                    {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
            });
            Thread.sleep(10000);
            session.close();
            connection.close();
        }
        catch (Exception e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    
    
}

  

Producer.java
package com.panie.mq.queue;

import java.util.Date;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.MapMessage;
import javax.jms.MessageProducer;
import javax.jms.Session;

import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;

public class Producer
{
    public static void main(String[] args)
    {
        String user = ActiveMQConnection.DEFAULT_USER;
        String password = ActiveMQConnection.DEFAULT_PASSWORD;
        String url = ActiveMQConnection.DEFAULT_BROKER_URL;
        String subject = "TOOL.DEFAULT";
        
        ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(user, password, url);
        
        try
        {
            Connection connection = connectionFactory.createConnection();
            connection.start();
            Session session = connection.createSession(Boolean.TRUE, Session.AUTO_ACKNOWLEDGE);
            Destination destination = session.createQueue(subject);
            MessageProducer producer = session.createProducer(destination);
            for(int i=0;i<=20;i++)
            {
                MapMessage message = session.createMapMessage();
                Date date = new Date();
                message.setLong("count", date.getTime());
                Thread.sleep(1000);
                producer.send(message);
                System.out.println("--发送消息:"+date);
            }
            session.commit();
            session.close();
            connection.close();
        }
        catch (Exception e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    
    
}

  

运行这两个方法,则可以在控制台看到  consumer 输出了 producter 产生的消息

ActiveMQ 入门使用实例

标签:

原文地址:http://www.cnblogs.com/panie2015/p/5569356.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!