ActiveMQ 入门使用实例

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 中配置

1
2
3
4
5
<dependency>
  <groupId>org.apache.activemq</groupId>
  <artifactId>activemq-all</artifactId>
  <version>5.9.0</version>
</dependency>

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

4.创建一个消息队列

1
Consumer.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
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();
        }
    }
     
     
}

  

1
Producer.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
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 产生的消息

posted @   panie2015  阅读(197)  评论(0编辑  收藏  举报
编辑推荐:
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?
点击右上角即可分享
微信分享提示