##Springboot简单集成ActiveMQ ###消息发送者的实现 ####pom.xml添加依赖
@Configuration
public class ActiveMqConfig {
@Bean
public RedeliveryPolicy redeliveryPolicy(){
RedeliveryPolicy redeliveryPolicy=new RedeliveryPolicy();
redeliveryPolicy.setUseExponentialBackOff(true);
redeliveryPolicy.setMaximumRedeliveries(3);
redeliveryPolicy.setInitialRedeliveryDelay(1000L);
redeliveryPolicy.setBackOffMultiplier(2);
redeliveryPolicy.setMaximumRedeliveryDelay(-1);
return redeliveryPolicy;
}
@Bean
public ActiveMQConnectionFactory getActiveMQConnectionFactory(@Autowired RedeliveryPolicy redeliveryPolicy,@Value("${spring.activemq.broker-url}") String url){
ActiveMQConnectionFactory activeMQConnectionFactory=new ActiveMQConnectionFactory(url);
activeMQConnectionFactory.setRedeliveryPolicy(redeliveryPolicy);
return activeMQConnectionFactory;
}
@Bean
public JmsTemplate getJmsTemplate(@Autowired ActiveMQConnectionFactory factory){
JmsTemplate jmsTemplate=new JmsTemplate();
jmsTemplate.setConnectionFactory(factory);
return jmsTemplate;
}
@Bean(name="jmsQueryListenerFactory")
public DefaultJmsListenerContainerFactory jmsListenerContainerFactory(@Autowired ActiveMQConnectionFactory activeMQConnectionFactory){
DefaultJmsListenerContainerFactory factory=new DefaultJmsListenerContainerFactory();
factory.setConnectionFactory(activeMQConnectionFactory);
factory.setConcurrency("1-10");
factory.setRecoveryInterval(1000L);
factory.setSessionAcknowledgeMode(4);
return factory;
}
}
####其中factory.setSessionAcknowledgeMode(4)设置4是手动接受消息,防止在接受自动消息时,抛出异常导致消息无效。