activeMQ入门demo

矫情吗;* 2022-05-27 07:00 296阅读 0赞

首先从官网下载http://activemq.apache.org/download-archives.html
解压后执行bin下面的activemq.bat即可启动
新建maven项目,导入jar包

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

activemq主要有以下几个成员:

  1. ConnectionFactory: 连接工厂,封装好的连接池
  2. Connection: 连接
  3. Session:
  4. Destination:消息目的地
  5. MessageProduce:消息产生者
  6. MessageConsumer:消费者

上代码,以下是生产者:

  1. public class Sender {
  2. private final static int num = 5;
  3. public static void main(String[] args) {
  4. ConnectionFactory connectionFactory;// 连接工厂
  5. Connection connection = null;// 连接
  6. Session session;// 消息进程
  7. Destination destination;// 消息目的地
  8. MessageProducer produce;// 消息发送者
  9. connectionFactory = new ActiveMQConnectionFactory(ActiveMQConnectionFactory.DEFAULT_USER,
  10. ActiveMQConnectionFactory.DEFAULT_PASSWORD, "tcp://localhost:61616");// 建立连接工厂
  11. try {
  12. connection = connectionFactory.createConnection();// 从连接工厂获取连接
  13. connection.start();//
  14. /*AUTO_ACKNOWLEDGE:当客户端从 receive 或 onMessage成功返回时,Session 自动签收客户端的这条消息的收条 *CLIENT_ACKNOWLEDGE:需要调用acknowledge方法来接收 */
  15. session = connection.createSession(Boolean.TRUE, Session.AUTO_ACKNOWLEDGE);
  16. destination = session.createQueue("hello");// 设置消息队列,发送者跟消费者要相同才能相互传递消息
  17. produce = session.createProducer(destination);
  18. produce.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
  19. // 发送业务消息
  20. sendMessage(session, produce, "test");
  21. session.commit();
  22. } catch (JMSException e) {
  23. e.printStackTrace();
  24. } finally {
  25. try {
  26. if (null != connection) {
  27. connection.close();
  28. }
  29. } catch (JMSException e) {
  30. e.printStackTrace();
  31. }
  32. }
  33. }
  34. private static void sendMessage(Session session, MessageProducer produce, String name) throws JMSException {
  35. TextMessage message = session.createTextMessage("i am message number" + name);
  36. produce.send(message);
  37. }
  38. }

消费者:

  1. public class Receiver {
  2. public static void main(String[] args) {
  3. ConnectionFactory connectionFactory;
  4. Connection connection = null;
  5. Session session;
  6. Destination destination;
  7. MessageConsumer consumer;//消费者
  8. connectionFactory = new ActiveMQConnectionFactory(ActiveMQConnectionFactory.DEFAULT_USER,
  9. ActiveMQConnectionFactory.DEFAULT_PASSWORD, "tcp://localhost:61616");
  10. try {
  11. connection = connectionFactory.createConnection();
  12. connection.start();
  13. session = connection.createSession(Boolean.FALSE, Session.AUTO_ACKNOWLEDGE);//设置为true即消费消息后不清除
  14. destination = session.createQueue("hello");
  15. consumer = session.createConsumer(destination);
  16. while (true) {
  17. TextMessage message = (TextMessage) consumer.receive(30*1000);//设置消费者消费时间
  18. if (null != message) {
  19. // message.acknowledge();
  20. System.out.println("shoudao "+message.getText());
  21. } else {
  22. break;
  23. }
  24. }
  25. } catch (JMSException e) {
  26. e.printStackTrace();
  27. } finally {
  28. try {
  29. if (null != connection) {
  30. connection.close();
  31. }
  32. } catch (JMSException e) {
  33. e.printStackTrace();
  34. }
  35. }
  36. }
  37. }

发表评论

表情:
评论列表 (有 0 条评论,296人围观)

还没有评论,来说两句吧...

相关阅读

    相关 ActiveMQ入门

    前言:最近在公司用到了activeMq接收消息,从网上查了些资料,形成了一个大概的理解,所以总结一下,因为知识都是网上学习的难免有疏漏之处,望理解。 参考: [https:

    相关 activeMQ入门

    目录 一、下载activeMQ包 二、准备Linux环境 三、所遇问题 -------------------- 一、下载activeMQ包         可

    相关 activeMQ Jms Demo

    概述 ActiveMQ 是Apache出品,最流行的,能力强劲的开源消息总线。ActiveMQ 是一个完全支持JMS1.1和J2EE 1.4规范的 JMS Provide

    相关 ActiveMQ入门

    ActiveMQ介绍      MQ是消息中间件,是一种在分布式系统中应用程序借以传递消息的媒介,常用的有ActiveMQ,RabbitMQ,kafka。ActiveMQ