首先下载安装activeMq
地址 http://activemq.apache.org/
创建项目 消息提供服务:
启动类
application.yml配置
spring: activemq: user: admin password: admin broker-url: tcp://127.0.0.1:61616 pool: enabled: true max-connections: 50 packages: trust-all: true #默认情况下activemq提供的是queue模式,若要使用topic模式需要配置下面配置 # jms: # pub-sub-domain: true
如果2种模式都开启可以不做配置,会在配置中配置
pom.xml配置
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-activemq</artifactId>
</dependency>
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-pool</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.messaginghub/pooled-jms -->
<!-- 尝试springboot2.1.5版本必须引入下面jar,否则会找不到 JmsMessagingTemplate -->
<dependency>
<groupId>org.messaginghub</groupId>
<artifactId>pooled-jms</artifactId>
<version>1.0.5</version>
</dependency>
配置类: import org.apache.activemq.command.ActiveMQQueue; import org.apache.activemq.command.ActiveMQTopic; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import javax.jms.Queue; import javax.jms.Topic; @Configuration public class MyActiveMqConfig { @Bean public Queue sendQueue() { return new ActiveMQQueue("queue"); } @Bean public Topic sendTopic(){ return new ActiveMQTopic("topic"); } }
@RestController public class Producter { Logger logger = LoggerFactory.getLogger(getClass()); @Autowired private JmsMessagingTemplate jmsMessagingTemplate; @Autowired private Queue sendQueue; @Autowired private Topic sendTopic; @GetMapping("/sendMess") public void insertVisitLog() { try { logger.info("insertVisitLog :收到请求,--123987"); jmsMessagingTemplate.convertAndSend(sendQueue, "123987"); }catch (Exception e){ e.printStackTrace(); } } @GetMapping("/sendMessTop") public void sendMessTop() { try { logger.info("insertVisitLog :收到请求--zxcvbn"); jmsMessagingTemplate.convertAndSend(sendTopic, "zxcvbn"); }catch (Exception e){ e.printStackTrace(); } } }
消息提供服务完成
下面创建消息消费方:
同上在创建新的springboot项目:修改启动类 、yml、pom
消费端配置类
@Configuration public class MyActiveMqConfig { //Topic模式 @Bean public JmsListenerContainerFactory<?> jmsListenerContainerTopic(ConnectionFactory connectionFactory) { DefaultJmsListenerContainerFactory bean = new DefaultJmsListenerContainerFactory(); bean.setPubSubDomain(true); bean.setConnectionFactory(connectionFactory); return bean; } //Queue模式 @Bean public JmsListenerContainerFactory<?> jmsListenerContainerQueue(ConnectionFactory connectionFactory) { DefaultJmsListenerContainerFactory bean = new DefaultJmsListenerContainerFactory(); bean.setConnectionFactory(connectionFactory); return bean; } }
接受类 @Service public class ConsumerListener { Logger logger = LoggerFactory.getLogger(getClass()); @JmsListener(destination="queue",containerFactory = "jmsListenerContainerQueue") public void queue(String str){ try { logger.info("接受到消息 : "+str); } catch (Exception e) { e.printStackTrace(); } } @JmsListener(destination="topic",containerFactory = "jmsListenerContainerTopic") public void topic(String str){ try { logger.info("接受到消息 : "+str); } catch (Exception e) { e.printStackTrace(); } } }