ActiveMQ-04-SpringBoot整合ActiveMQ

SpringBoot整合ActiveMQ

创建一个SpringBoot项目

可以使用IDEA中的快速创建器创建一个SpringBoot项目,就不啰嗦了

pom.xml
  • 注意:SpringBoot的版本为:2.5.3
<properties>
    <java.version>1.8</java.version>
</properties>
<dependencies>
    <!--SpringBoot整合ActiveMQ的场景启动器-->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-activemq</artifactId>
    </dependency>
    <!--SpringBoot的测试场景启动器-->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>

    <!--SpringBoot的web场景启动器-->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <!--SpringBoot场景启动器-->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter</artifactId>
    </dependency>
</dependencies>
application.yml配置文件
# web占用的端口
server:
  port: 8081

spring:
  activemq:
    # activemq的broker的url
    broker-url: tcp://127.0.0.1:61616
    # 连接activemq的broker所需的账号和密码
    user: admin
    password: admin
  jms:
    #目的地是queue还是topic,false(默认) = queue  true = topic
    pub-sub-domain: false
    
# 自定义队列名称,这只是个常量
myqueue: boot-activemq-queue

# 自定义主题名称
mytopic: boot-activemq-topic

queue生产者和消费者

配置目的地的bean
// 让spring管理的注解,相当于spring中在xml 中写了个bean
@Component
// 开启jms适配
@EnableJms
public class ConfigBean {
        // 注入配置文件中的 myqueue
        @Value("${myqueue}")
        private String myQueue ;

        @Bean   // bean id=""  class="…"
        public Queue queue(){
             return  new ActiveMQQueue(myQueue);
        }
}
队列生产者代码
@Component
public class Queue_Produce {
    // JMS模板
    @Autowired
    private JmsMessagingTemplate  jmsMessagingTemplate ;

    // 这个是我们配置的队列目的地
    @Autowired
    private Queue queue ;

    // 发送消息
    public void produceMessage(){
        // 一参是目的地,二参是消息的内容
        jmsMessagingTemplate.convertAndSend(queue,"****"+ UUID.randomUUID().toString().substring(0,6));
    }

    // 定时任务。每3秒执行一次。非必须代码,仅为演示。
    @Scheduled(fixedDelay = 3000)
    public void produceMessageScheduled(){
        produceMessage();
    }
}
队列消费者代码
@Component
public class Queue_Consumer {
    // 注册一个监听器,destination指定监听的主题。
    @JmsListener(destination = "${myqueue}")
    public void receive(TextMessage textMessage) throws  Exception{
        System.out.println(" ***  消费者收到消息  ***"+textMessage.getText());
    }
}

topic生产者和消费者

配置目的地的bean
@Component
//开启JMS
@EnableJms
public class ConfigBean {
    @Value("${mytopic}")
    private String  topicName ;
    @Bean
    public Topic topic() {
        return new ActiveMQTopic(topicName);
    }
}
主题生产者代码
@Component
public class Topic_Produce {
    @Autowired
    private JmsMessagingTemplate  jmsMessagingTemplate ;
    @Autowired
    private Topic  topic ;
    @Scheduled(fixedDelay = 3000)
    public void produceTopic(){
        jmsMessagingTemplate.convertAndSend(topic,"主题消息"+ UUID.randomUUID().toString().substring(0,6));
    }
}
主题消费者代码
@Component
public class Topic_Consumer {
	 // 注册一个监听器,destination指定监听的主题。
    @JmsListener(destination = "${mytopic}")
    public void receive(TextMessage textMessage) throws  Exception{
        System.out.println("消费者受到订阅的主题:"+textMessage.getText());
    }
}

测试代码

注意:

  • 我们只需要测试生产者即可,因为我们在消费者的代码上指定了监听器,当监听到相应的生产者(队列或者主题)生产消息之后,消费者就会自动被调用。

  • 当我们测试不同形式的目的地的时候记得修改yml配置文件。

    #目的地是queue还是topic,false(默认) = queue  true = topic
    pub-sub-domain: true
    

测试代码:

// 加载主类
@SpringBootTest(classes = SpringbootactivemqApplication.class)
// 加载spring的junit
@RunWith(SpringJUnit4ClassRunner.class)
//加载web
@WebAppConfiguration

class SpringbootactivemqApplicationTests {
    @Autowired
    private Queue_Produce  queue_produce;
    @Autowired
    private Topic_Produce topic_produce;
    //测试队列生产者
    @Test
    public  void testQueueProduce() throws Exception{
        queue_produce.produceMessage();
    }
    //测试主题生产者
    @Test
    public  void testTopicProduce() throws Exception{
        topic_produce.produceTopic();
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

彤彤的小跟班

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值