如何创建SpringBoot项目

访问https://start.spring.io/。
【20200129】SpringBoot   ActiveMQ - 图1
依次选择构建工具Maven Project、语言java、Spring Boot版本2.0.5,点击Generate Project下载项目压缩包,解压后倒入到ide中即可。(idea集成了SpringBoot,可直接创建项目)

如何整合ActiveMQ

1、添加spring-boot-starter-activemq依赖

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

2、添加配置

  1. spring.activemq.in-memory=true
  2. spring.activemq.pool.enabled=false

3、添加SampleActiveMQApplication类

  1. @SpringBootApplication
  2. @EnableJms
  3. public class SampleActiveMQApplication {
  4. @Bean
  5. public Queue queue() {
  6. return new ActiveMQQueue("sample.queue");
  7. }
  8. public static void main(String[] args) {
  9. SpringApplication.run(SampleActiveMQApplication.class, args);
  10. }
  11. }

4、添加Consumer类

  1. @Component
  2. public class Consumer {
  3. @JmsListener(destination = "sample.queue")
  4. public void receiveQueue(String text) {
  5. System.out.println(text);
  6. }
  7. }

5、添加Producer类

  1. @Component
  2. public class Producer implements CommandLineRunner {
  3. @Autowired
  4. private JmsMessagingTemplate jmsMessagingTemplate;
  5. @Autowired
  6. private Queue queue;
  7. @Override
  8. public void run(String... args) throws Exception {
  9. send("Sample message");
  10. System.out.println("Message was sent to the Queue");
  11. }
  12. public void send(String msg) {
  13. this.jmsMessagingTemplate.convertAndSend(this.queue, msg);
  14. }
  15. }

6、启动服务
控制台输出:

  1. Message was sent to the Queue
  2. Sample message

以上例子使用的是SpringBoot内部的ActiveMQ,实际使用时肯定会用外部的ActiveMQ。

如何连接外部的ActiveMQ

1、添加依赖

  1. <dependency>
  2. <groupId>org.apache.activemq</groupId>
  3. <artifactId>activemq-pool</artifactId>
  4. </dependency>

2、修改配置

  1. spring.activemq.broker-url=tcp://localhost:61616
  2. spring.activemq.close-timeout=5000
  3. spring.activemq.in-memory=false
  4. spring.activemq.pool.enabled=true
  5. spring.activemq.pool.max-connections=100
  6. spring.activemq.send-timeout=3000

3、启动服务
首先,确保ActiveMQ已经启动。控制台输出:

  1. Message was sent to the Queue
  2. Sample message

如何使用topic

1、在SampleActiveMQApplication中添加:

  1. @Bean
  2. public Topic topic() {
  3. return new ActiveMQTopic("sample.topic");
  4. }
  5. @Bean
  6. public JmsListenerContainerFactory<?> jmsListenerContainerTopic(ConnectionFactory activeMQConnectionFactory) {
  7. DefaultJmsListenerContainerFactory bean = new DefaultJmsListenerContainerFactory();
  8. bean.setPubSubDomain(true);
  9. bean.setConnectionFactory(activeMQConnectionFactory);
  10. return bean;
  11. }

2、创建TopicConsumer类

  1. @Component
  2. public class TopicConsumer {
  3. @JmsListener(destination = "sample.topic", containerFactory = "jmsListenerContainerTopic")
  4. public void receiveTopic1(String text) {
  5. System.out.println(text);
  6. }
  7. @JmsListener(destination = "sample.topic", containerFactory = "jmsListenerContainerTopic")
  8. public void receiveTopic2(String text) {
  9. System.out.println(text);
  10. }
  11. }

3、创建TopicProducer类

  1. @Component
  2. public class TopicProducer implements CommandLineRunner {
  3. @Autowired
  4. private JmsMessagingTemplate jmsMessagingTemplate;
  5. @Autowired
  6. private Topic topic;
  7. @Override
  8. public void run(String... args) throws Exception {
  9. send("Topic message");
  10. System.out.println("Message was sent to the Topic");
  11. }
  12. public void send(String msg) {
  13. this.jmsMessagingTemplate.convertAndSend(this.topic, msg);
  14. }
  15. }

4、启动服务
控制台输出:

  1. Message was sent to the Topic
  2. Topic message
  3. Topic message

项目地址:SpringBoot2.0整合ActiveMQ