Spring Boot整合ActiveMQ

Spring Boot 做的一个简单的增删改查,前台页面整合Thymeleaf模板,数据源druid,声明式事务,整合redis,并开启redis事务,整合ActiveMQ 项目下载地址:点此下载

如果有不需要使用的功能,只需要删除com.test.springboot.config下对应的配置再启动就行了

安装ActiveMQ


参考博客: ActiveMQ安装和基本使用

引入依赖


 
 
  1. <!-- 整合activemq -->
  2. <dependency>
  3. <groupId>org.springframework.boot </groupId>
  4. <artifactId>spring-boot-starter-activemq </artifactId>
  5. </dependency>

application.properties


 
 
  1. #mq.brokerURL=failover:(tcp://172.20.1.230:61616,tcp://172.20.1.231:61616,tcp://172.20.1.232:61616)?randomize=false&connectionTimeout=10000
  2. spring.activemq.broker-url=tcp:// 172.20 .1 .154: 61616
  3. spring.activemq.user=ryx
  4. spring.activemq.password=ryx
ActiveMQ的其他配置属性:https://docs.spring.io/spring-boot/docs/current/reference/html/common-application-properties.html 然后Ctrl+F搜索activemq和jms

创建ActiveMQ基于java的配置类


 
 
  1. package com.test.springboot.config;
  2. import javax.jms.Queue;
  3. import org.apache.activemq.command.ActiveMQQueue;
  4. import org.springframework.context.annotation.Bean;
  5. import org.springframework.context.annotation.Configuration;
  6. @Configuration
  7. public class AmqConfig {
  8. @Bean(name= "counting")
  9. public Queue counting() {
  10. return new ActiveMQQueue( "ryx.consumption.counting");
  11. }
  12. @Bean(name= "newriskmessage")
  13. public Queue newriskmessage() {
  14. return new ActiveMQQueue( "ryx.consumption.newriskmessage");
  15. }
  16. }
里面只配置要是用的队列,别的配置都是使用默认!

生产者Producer


  
  
  1. package com.test.springboot.service;
  2. import javax.jms.JMSException;
  3. import javax.jms.Message;
  4. import javax.jms.Queue;
  5. import javax.jms.Session;
  6. import javax.jms.TextMessage;
  7. import org.springframework.beans.factory.annotation.Autowired;
  8. import org.springframework.jms.core.JmsMessagingTemplate;
  9. import org.springframework.jms.core.JmsTemplate;
  10. import org.springframework.jms.core.MessageCreator;
  11. import org.springframework.scheduling.annotation.EnableScheduling;
  12. import org.springframework.scheduling.annotation.Scheduled;
  13. import org.springframework.stereotype.Component;
  14. /**
  15. * 消息生产者
  16. * @author xiyan
  17. *
  18. */
  19. @Component
  20. @EnableScheduling
  21. public class Producer {
  22. @Autowired //也可以注入JmsTemplate,JmsMessagingTemplate对JmsTemplate进行了封装
  23. private JmsMessagingTemplate jmsMessagingTemplate;
  24. @Autowired //也可以注入JmsTemplate,JmsMessagingTemplate对JmsTemplate进行了封装
  25. private JmsTemplate jmsTemplate;
  26. @Autowired
  27. private Queue counting;
  28. //发送消息,counting是发送到的队列,message是待发送的消息
  29. //测试的时候,这个方法不要传参数,否则报错,定时任务,是不能有参数的
  30. //Encountered invalid @Scheduled method 'sendMessage': Only no-arg methods may be annotated with @Scheduled
  31. @Scheduled(fixedDelay= 3000)
  32. public void sendMessage(){
  33. jmsTemplate.send(counting, new MessageCreator() {
  34. @Override
  35. public Message createMessage(Session session) throws JMSException {
  36. TextMessage textMessage = session.createTextMessage( "Hello,ActiveMQ!");
  37. //序列化的对象
  38. //session.createObjectMessage();
  39. //流,可以用来传递文件等
  40. //session.createStreamMessage();
  41. //用来传递字节
  42. //session.createBytesMessage();
  43. //这个方法创建出来的就是一个map,可以把它当作map来用,当你看了它的一些方法,你就懂了
  44. //session.createMapMessage();
  45. //这个方法,拿到的是javax.jms.Message,是所有message的接口
  46. //session.createMessage();
  47. return textMessage;
  48. }
  49. });
  50. }
  51. }


消费者Consumer


 
 
  1. package com.test.springboot.service;
  2. import javax.jms.JMSException;
  3. import javax.jms.Message;
  4. import javax.jms.TextMessage;
  5. import org.springframework.jms.annotation.JmsListener;
  6. import org.springframework.stereotype.Component;
  7. /**
  8. * 消息消费者
  9. * @author xiyan
  10. *
  11. */
  12. @Component
  13. public class Consumer {
  14. //使用JmsListener配置消费者监听的队列
  15. //日志打印最好使用INFO ,因为使用监听,后台DEBUG会有别的日志干扰
  16. @JmsListener(destination = "ryx.consumption.counting")
  17. public void receiveQueue(Message message) throws JMSException {
  18. TextMessage textMessage =(TextMessage)message;
  19. System.out.println( "Consumer收到的报文为:"+textMessage.getText());
  20. }
  21. }
启动SpringBoot项目,建议把日志改为INFO 状态,DEBUG还在打印监听的日志,查看控制台

关于消费异常,重新消费问题

默认已经开启消费异常重新消费的功能,默认是消费6次,不算第一次消费,就是说第一次消费异常之后,再次消费6次,总共加起来7次!自己可以测试一下。测试的时候,把上面的 Producer类上的两个注解去掉,定时发送消息会有影响查看日志,可以使用下面的方法发送消息

 
 
  1. public static void send(Integer i) throws Exception {
  2. //根据用户名,密码,url创建一个连接工厂
  3. ConnectionFactory connectionFactory = new ActiveMQConnectionFactory( "ryx", "ryx", "tcp://172.20.1.154:61616");
  4. //从工厂中获取一个连接
  5. Connection connection = connectionFactory.createConnection();
  6. connection.start();
  7. //第一个参数:是否支持事务,如果为true,则会忽略第二个参数,被jms服务器设置为SESSION_TRANSACTED
  8. //第二个参数为false时,paramB的值可为Session.AUTO_ACKNOWLEDGE,Session.CLIENT_ACKNOWLEDGE,DUPS_OK_ACKNOWLEDGE其中一个。
  9. //Session.AUTO_ACKNOWLEDGE为自动确认,客户端发送和接收消息不需要做额外的工作。哪怕是接收端发生异常,也会被当作正常发送成功。
  10. //Session.CLIENT_ACKNOWLEDGE为客户端确认。客户端接收到消息后,必须调用javax.jms.Message的acknowledge方法。jms服务器才会当作发送成功,并删除消息。
  11. //DUPS_OK_ACKNOWLEDGE允许副本的确认模式。一旦接收方应用程序的方法调用从处理消息处返回,会话对象就会确认消息的接收;而且允许重复确认。
  12. Session session = connection.createSession(Boolean.TRUE, Session.AUTO_ACKNOWLEDGE);
  13. //=======================================================================
  14. //创建一个到达的目的地(队列),如果这个队列不存在,将会被创建
  15. //点对点模式 session.createQueue
  16. Destination destination = session.createQueue( "ryx.consumption.counting");
  17. //发布订阅模式
  18. //Destination destination = session.createTopic("ryx.consumption.counting");
  19. //=======================================================================
  20. //从session中,获取一个消息生产者
  21. MessageProducer producer = session.createProducer(destination);
  22. //设置生产者的模式,有两种可选
  23. //DeliveryMode.PERSISTENT 当activemq关闭的时候,队列数据将会被保存
  24. //DeliveryMode.NON_PERSISTENT 当activemq关闭的时候,队列里面的数据将会被清空
  25. producer.setDeliveryMode(DeliveryMode.PERSISTENT);
  26. //创建一条消息,当然,消息的类型有很多,如文字,字节,对象等,可以通过session.create..方法来创建出来
  27. //通过消息生产者发出消息
  28. TextMessage message = session.createTextMessage( "这是数据第 "+i+ " 条!");
  29. /*message.setLongProperty(ScheduledMessage.AMQ_SCHEDULED_DELAY, 5*1000);
  30. message.setLongProperty(ScheduledMessage.AMQ_SCHEDULED_PERIOD, 5*1000);
  31. message.setIntProperty(ScheduledMessage.AMQ_SCHEDULED_REPEAT, 9);*/
  32. producer.send(message);
  33. session.commit();
  34. session.close();
  35. connection.close();
  36. }


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值