ActiveMQ菜鸟入门教程

编程知识 行业动态 更新时间:2024-06-13 00:22:55

本篇文章介绍一个简单的ActiveMQdemo搭建,本人试过了代码可以运行成功。

原文链接:https://www.songliguo/activemq-getting-started.html

ActiveMQ官网下载地址: http://activemq.apache/download.html  

ActiveMQ安装可参考: https://blog.csdn/mr_haixin/article/details/80418204

ActiveMQ是Apache的一个开源项目,它是一个能力强劲的开源消息总线,也是一个中间件产品。它是JMS的一个实现。
在介绍ActiveMQ之前,先来复习一下J2EE中的JMS规范。
JMS是Java Message Service的简称,用来发送异步消息,在不同系统和不同的模块之间我们可以利用它实现集成。
JMS有两个好处,第一个就是让模块之间或者系统之间的耦合度降低,第二个是异步通信。
JMS的消息机制有2种模型,一种是Point to Point,表现为队列的形式。发送的消息,只能被一个接收者取走;另一种是Topic,可以被多个订阅者订阅,类似于群发。

在JMS中有这样几个重要的核心接口和类:

  • ConnectionFactory,用于jms client获取与jms provider的连接。不同的jms产品,对这个接口有不同的实现,比如说ActiveMQ,这个接口的实现类是ActiveMQConnectionFactory
  • Connection,是由ConnectionFactory产生的,表示jms client与jms provider的连接
  •  Session,是由Connection产生的,表示一个会话。Session是关键组件,Message、Producer/Consumer、Destination都是在Session上创建的
  •  Message,这个组件很好理解,就是传输的消息,里面包括head、properties、body,其中head是必选的
  •  Destination,是消息源,对发送者来说,就是消息发到哪里;对接收者来说,就是从哪里取消息。Destination有2个子接口,Queue和Topic,分别对应上面提到的2种模型
  • MessageProducer,是消息发送者,创建这个组件的代码类似:

 

 

1

2

3

4

5

6

 

//创建一个Queue,名称为SongLiGuo_FirstQueue

destination = session.createQueue("SongLiGuo_FirstQueue");

//得到消息生产者【发送者】

messageProducer = session.createProducer(destination);

 

可以注意到,这里需要把Destination作为参数,传入createProducer()方法,这说明消息发送者是绑定到Destination上的,这个发送者发送的消息,会发送到这个绑定的Destination上

  •  MessageConsumer,是消息接收者,和Message Producer是相反的一种组件

 

对JMS有所了解之后,我们来看ActiveMQ。

1.下载ActiveMQ

去官方网站下载:http://activemq.apache/

2.运行ActiveMQ

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

启动ActiveMQ以后,登陆:http://localhost:8161/admin/,创建一个Queue,命名为SongLiGuo_FirstQueue(如果这里创建了程序就不用创建)

3.创建Eclipse项目并运行

创建project:ActiveMQ-5.5,并导入apache-activemq-5.5.1lib目录下需要用到的jar文件,项目结构如下图所示:

3.1.Sender.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

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

 

package com.songliguo.activemq;

 

import javax.jms.Connection;

import javax.jms.ConnectionFactory;

import javax.jms.DeliveryMode;

import javax.jms.Destination;

import javax.jms.MessageProducer;

import javax.jms.Session;

import javax.jms.TextMessage;

 

import org.apache.activemq.ActiveMQConnection;

import org.apache.activemq.ActiveMQConnectionFactory;

 

/**

*

*    

* 项目名称:ActiveMQ-5.5  

* 类名称:Sender  

* 类描述:   ActiveMQ发送者

* 创建人:Songliguo  

* 创建时间:2017年3月14日 上午10:01:02  

* 修改人:  

* 修改时间:

* 修改备注:  

* @version    

*

*/

public class Sender {

 

private static final int SEND_NUMBER = 10;

public static void main(String[] args) {

//ConnectionFactory是连接工厂,JMS用它创建连接

ConnectionFactory connectionFactory;

//Connection JMS客户端到JMS provider的连接

Connection connection = null;

//Session 一个发送或者接收消息的线程

Session session;

//Destination 消息发送目的地,消息发送给谁接收

Destination destination;

//MessageProducer 消息发送者

MessageProducer messageProducer;

//构造ConnectionFactory 实例对象,此处采用ActiveMQ的实现jar

connectionFactory = new ActiveMQConnectionFactory(

ActiveMQConnection.DEFAULT_USER,

ActiveMQConnection.DEFAULT_PASSWORD,

"tcp://localhost:61616");

try {

//构造工厂得到连接对象

connection = connectionFactory.createConnection();

//启动

connection.start();

//获取操作连接

session = connection.createSession(Boolean.TRUE, Session.AUTO_ACKNOWLEDGE);

//创建一个Queue,名称为SongLiGuo_FirstQueue

                        destination = session.createQueue("SongLiGuo_FirstQueue");

//得到消息生产者【发送者】

messageProducer = session.createProducer(destination);

//设置不持久化,根据实际情况而定

messageProducer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);

//构造消息,此处写死,项目就是参数或者方法获取

sendMessage(session, messageProducer);

sessionmit();

} catch (Exception e) {

e.printStackTrace();

}finally {

try {

if(null != connection){

connection.close();

}

} catch (Throwable ignore) {

}

}

}

public static void sendMessage(Session session, MessageProducer producer)throws Exception {

        for (int i = 1; i <= SEND_NUMBER; i++) {

            TextMessage message = session.createTextMessage("ActiveMq 发送的消息" + i);

            // 发送消息到目的地方

            System.out.println("发送消息:" + "ActiveMq 发送的消息" + i);

            producer.send(message);

        }

    }

}

 

 

 

3.2.Receiver.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

71

72

73

74

75

76

77

78

79

80

 

package com.songliguo.activemq;

 

import javax.jms.Connection;

import javax.jms.ConnectionFactory;

import javax.jms.Destination;

import javax.jms.MessageConsumer;

import javax.jms.Session;

import javax.jms.TextMessage;

 

import org.apache.activemq.ActiveMQConnection;

import org.apache.activemq.ActiveMQConnectionFactory;

 

/**

*

*    

* 项目名称:ActiveMQ-5.5  

* 类名称:Receiver  

* 类描述:  activeMQ接收类

* 创建人:Songliguo  

* 创建时间:2017年3月14日 上午10:31:35  

* 修改人:  

* 修改时间:

* 修改备注:  

* @version    

*

*/

public class Receiver {

 

public static void main(String[] args) {

//connectionFactory 连接工厂,JMS用它创建连接

ConnectionFactory connectionFactory;

//connection JMS客户端到JMS provider 的连接

Connection connection = null;

//session一个发送或者接收的线程

Session session;

//destination 消息目的地,发送给谁接收

Destination destination;

//消费者消息接收者

MessageConsumer consumer;

connectionFactory = new ActiveMQConnectionFactory(

ActiveMQConnection.DEFAULT_USER,

ActiveMQConnection.DEFAULT_PASSWORD,

"tcp://localhost:61616");

try {

//构造工厂得到连接对象

connection = connectionFactory.createConnection();

//启动

connection.start();

//获取操作连接

session = connection.createSession(Boolean.FALSE, Session.AUTO_ACKNOWLEDGE);

destination = session.createQueue("SongLiGuo_FirstQueue");

            consumer = session.createConsumer(destination);

            while(true){

             //设置接收者收消息的时间,为了方便测试,这里暂定设置为100s

             TextMessage message = (TextMessage)consumer.receive(100);

             if(null != message){

             System.out.println("收到消息==="+message.getText());

             }else{

             break;

             }

            }

} catch (Exception e) {

e.printStackTrace();

}finally{

try {

if(null != connection){

connection.close();

}

} catch (Throwable ignore) {

}

}

}

}

 

 

 

4.注意事项

项目所引用的jar最后在ActiveMQ下的lib中找

5.测试结果

运行sender,在运行完sender以后,我们可以看到如下console

我们再切换到receiver运行后的console,如下图所示:

 

6.我们可以在http://localhost:8161/admin/queues.jsp看到消息发送和接收情况

转载请注明原文链接:首页 -> 技术交流 -> JAVA开发 -> ActiveMQ菜鸟入门教程

更多推荐

ActiveMQ菜鸟入门教程

本文发布于:2023-04-03 13:31:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/cf1901263bd5989bc46794012563f32f.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:菜鸟   入门教程   ActiveMQ

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!