Springboot简单集成ActiveMQ

##Springboot简单集成ActiveMQ ###消息发送者的实现 ####pom.xml添加依赖 <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-activemq</artifactId> </dependency> <dependency> <groupId>org.messaginghub</groupId> <artifactId>pooled-jms</artifactId> </dependency> ####测试代码 @Autowired JmsMessagingTemplate template; @Test public void contextLoads() { template.convertAndSend("tym_002","hello_02"); } ###消息接受者的实现 ####pom文件依赖和发送者一样 ####创建JmsListener int a=0; @JmsListener(destination = "tym_002",containerFactory = "jmsQueryListenerFactory") public void testreceiveQueue(TextMessage textMessage, Session session) throws JMSException { try { int i=1/0; System.out.println("接受到:"+textMessage.getText()); textMessage.acknowledge(); } catch (Exception e) { a++; System.out.println("重试了 :"+a); session.recover(); } } ####其中containerFactory的赋值为自定义的配置类:

1@Configuration 2public class ActiveMqConfig { 3@Bean 4public RedeliveryPolicy redeliveryPolicy(){ 5 RedeliveryPolicy redeliveryPolicy=new RedeliveryPolicy(); 6 redeliveryPolicy.setUseExponentialBackOff(true); 7 redeliveryPolicy.setMaximumRedeliveries(3); 8 redeliveryPolicy.setInitialRedeliveryDelay(1000L); 9 redeliveryPolicy.setBackOffMultiplier(2); 10 redeliveryPolicy.setMaximumRedeliveryDelay(-1); 11 return redeliveryPolicy; 12} 13@Bean 14public ActiveMQConnectionFactory getActiveMQConnectionFactory(@Autowired RedeliveryPolicy redeliveryPolicy,@Value("${spring.activemq.broker-url}") String url){ 15 ActiveMQConnectionFactory activeMQConnectionFactory=new ActiveMQConnectionFactory(url); 16 activeMQConnectionFactory.setRedeliveryPolicy(redeliveryPolicy); 17 return activeMQConnectionFactory; 18} 19@Bean 20public JmsTemplate getJmsTemplate(@Autowired ActiveMQConnectionFactory factory){ 21 JmsTemplate jmsTemplate=new JmsTemplate(); 22 jmsTemplate.setConnectionFactory(factory); 23 return jmsTemplate; 24} 25@Bean(name="jmsQueryListenerFactory") 26public DefaultJmsListenerContainerFactory jmsListenerContainerFactory(@Autowired ActiveMQConnectionFactory activeMQConnectionFactory){ 27 DefaultJmsListenerContainerFactory factory=new DefaultJmsListenerContainerFactory(); 28 factory.setConnectionFactory(activeMQConnectionFactory); 29 factory.setConcurrency("1-10"); 30 factory.setRecoveryInterval(1000L); 31 factory.setSessionAcknowledgeMode(4); 32 return factory; 33} 34}

####其中factory.setSessionAcknowledgeMode(4)设置4是手动接受消息,防止在接受自动消息时,抛出异常导致消息无效。

点赞
收藏

评论区

加载中...

相关推荐

MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1

文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s

Oracle 分组与拼接字符串同时使用

SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(

MySQL部分从库上面因为大量的临时表tmp_table造成慢查询

背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_

手写Java HashMap源码

HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22

SpringBoot中使用rabbitmq,activemq消息队列和rest服务的调用

1\.activemq  首先引入依赖  pom.xml文件<dependency<groupIdorg.springframework.boot</groupId<artifactIdspringbootstarteractivemq</artifactId</depe

Spring Boot日志集成

!(https://oscimg.oschina.net/oscnet/1bde8e8d00e848be8b84e9d1d44c9e5c.jpg)SpringBoot日志框架SpringBoot支持JavaUtilLogging,Log4j2,Lockback作为日志框架,如果你使用star