springboot给我们的开发带了很大简便,帮住我们管理复杂的项目依赖,以springboot与activemq集成为例,在集成activemq的时候我们只需要加上一个依赖就可以了
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-activemq</artifactId>
</dependency>
加上依赖以后我们只需要配置对应brokenUrl,user,paswword即可
下面给大家展示下我对activemq的一个简单集成处理
@Configuration @EnableJms public class DefaultConf { @Autowired PropretyConf propretyConf; @Bean public RedeliveryPolicy redeliveryPolicy(){ RedeliveryPolicy redeliveryPolicy = new RedeliveryPolicy(); redeliveryPolicy.setMaximumRedeliveries(3); redeliveryPolicy.setUseExponentialBackOff(true); return redeliveryPolicy; } @Bean public ActiveMQConnectionFactory activeMQConnectionFactory(RedeliveryPolicy redeliveryPolicy) throws IllegalArgumentException { ActiveMQConnectionFactory activeMQConnectionFactory = new ActiveMQConnectionFactory(StringUtils.isEmpty(propretyConf.getBrokenUrl())?ActiveMQConnectionFactory.DEFAULT_BROKER_URL:propretyConf.getBrokenUrl()); activeMQConnectionFactory.setUserName(StringUtils.isEmpty(propretyConf.getUser())?ActiveMQConnectionFactory.DEFAULT_USER:propretyConf.getUser()); activeMQConnectionFactory.setPassword(StringUtils.isEmpty(propretyConf.getPassword())?ActiveMQConnectionFactory.DEFAULT_PASSWORD:propretyConf.getPassword()); activeMQConnectionFactory.setRedeliveryPolicy(redeliveryPolicy); return activeMQConnectionFactory; } @Bean public JmsTemplate jmsQueueTemplate(ActiveMQConnectionFactory activeMQConnectionFactory){ JmsTemplate jmsQueueTemplate = new JmsTemplate(); if(StringUtils.isEmpty(propretyConf.getDestination())) throw new IllegalArgumentException("缺少destination参数。。。"); Queue jmsQueue = new ActiveMQQueue(propretyConf.getDestination()); // 连接工厂 jmsQueueTemplate.setConnectionFactory(activeMQConnectionFactory); // 消息持久化 jmsQueueTemplate.setDeliveryPersistent(propretyConf.isDeliveryPersistent()); // 目的地 jmsQueueTemplate.setDefaultDestination(jmsQueue); return jmsQueueTemplate; } @Bean public JmsTemplate jmsTopicTemplate(ActiveMQConnectionFactory activeMQConnectionFactory){ JmsTemplate jmsTopicTemplate = new JmsTemplate(); if(StringUtils.isEmpty(propretyConf.getDestination())) throw new IllegalArgumentException("缺少destination参数。。。"); Topic jmsTopic = new ActiveMQTopic(propretyConf.getDestination()); // 连接工厂 jmsTopicTemplate.setConnectionFactory(activeMQConnectionFactory); // 消息持久化 jmsTopicTemplate.setDeliveryPersistent(propretyConf.isDeliveryPersistent()); // 目的地 jmsTopicTemplate.setDefaultDestination(jmsTopic); // 设置订阅模式 jmsTopicTemplate.setPubSubDomain(true); return jmsTopicTemplate; } @Bean public DefaultJmsListenerContainerFactory chenTopicJmsListenerContainerFactory(ActiveMQConnectionFactory activeMQConnectionFactory){ DefaultJmsListenerContainerFactory chenJmsListenerContainerFactory = new DefaultJmsListenerContainerFactory(); chenJmsListenerContainerFactory.setPubSubDomain(true); chenJmsListenerContainerFactory.setConnectionFactory(activeMQConnectionFactory); return chenJmsListenerContainerFactory; } @Bean public DefaultJmsListenerContainerFactory chenQueueJmsListenerContainerFactory(ActiveMQConnectionFactory activeMQConnectionFactory){ DefaultJmsListenerContainerFactory chenJmsListenerContainerFactory = new DefaultJmsListenerContainerFactory(); chenJmsListenerContainerFactory.setPubSubDomain(false); chenJmsListenerContainerFactory.setConnectionFactory(activeMQConnectionFactory); return chenJmsListenerContainerFactory; } }
@Component @PropertySource("classpath:application.yml") @ConfigurationProperties(prefix="jms") public class PropretyConf { public String destination; public String brokenUrl; public String user; public String password; public boolean deliveryPersistent; public String getDestination() { return destination; } public void setDestination(String destination) { this.destination = destination; } public String getBrokenUrl() { return brokenUrl; } public void setBrokenUrl(String brokenUrl) { this.brokenUrl = brokenUrl; } public String getUser() { return user; } public void setUser(String user) { this.user = user; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public boolean isDeliveryPersistent() { return deliveryPersistent; } public void setDeliveryPersistent(boolean deliveryPersistent) { this.deliveryPersistent = deliveryPersistent; } }
@Component public class JmsQueueSender { @Autowired JmsTemplate jmsQueueTemplate; public void send(ChenMessage message){ jmsQueueTemplate.convertAndSend(JsonUtils.Object2Json(message)); } public void sendText(String message){ jmsQueueTemplate.convertAndSend(message); } public JmsQueueSender() { } }
消息生产者
@Service public class SendMsg { @Autowired JmsSenderFactory jmsSenderFactory; public void send(){ try { Thread.sleep(5000); } catch (InterruptedException e) { e.printStackTrace(); } jmsSenderFactory.getJmsTopicSender().sendText("我是测数据1"); } public void sendQueue(){ jmsSenderFactory.getJmsQueueSender().sendText("我是测数据1"); } }
消息消费者
@Service public class RecevierMsg { @JmsListener(destination = "chen.queue",containerFactory = "chenQueueJmsListenerContainerFactory") public void recevierMsg(String string){ System.out.println("queue接受到数据》》》》》"+string); } @JmsListener(destination = "chen.queue",containerFactory = "chenTopicJmsListenerContainerFactory") public void recevierMsg1(String string){ System.out.println("topic接受到数据》》》》》"+string); } }具体代码集成https://gitee.com/pgleo/spring-boot-jms.git