springboot集成activeMQ

什么是activeMQ

Apache ActiveMQ是Apache软件基金会所研发的开放源代码消息中间件;由于ActiveMQ是一个纯Java程序,因此只需要操作系统支持Java虚拟机,ActiveMQ便可执行。
学了这篇文章,你就可以从零开始,会springboot集成activeMQ

1.下载activeMQ

1.点击此处下载activeMQ
2.我们选择的是下载apache-activemq-5.15.9-bin.zip
3.下载完毕之后进入bin目录,根据系统选择32还是64位,然后启动activemq.bat
4.在浏览器上访问localhost:8161 点击Manage ActiveMQ broker输入账号密码admin

2.springboot集成activeMQ基本配置

项目结构

在这里插入图片描述
1.pom文件

<!--   其他可能 版本会出现启动不了的问题 -->
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.0.1.RELEASE</version>
</parent>

<!--<properties>

    <rocketmq.version>4.1.0-incubating</rocketmq.version>

</properties>-->
<dependencies>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>


    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-devtools</artifactId>
        <optional>true</optional>
    </dependency>


    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
    <!-- 整合消息队列ActiveMQ -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-activemq</artifactId>
    </dependency>

    <!-- 如果配置线程池则加入 -->
    <dependency>
        <groupId>org.apache.activemq</groupId>
        <artifactId>activemq-pool</artifactId>
    </dependency>

    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-databind</artifactId>
        <version>2.9.9</version>
        <scope>compile</scope>
    </dependency>

</dependencies>
<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

2.application.properties
注意:端口为默认61616,账号密码默认为admin

 #整合jms测试   --  如果安装在别的机器,防火墙和端口记得开放
spring.activemq.broker-url=tcp://127.0.0.1:61616
#账号密码
spring.activemq.user=admin
spring.activemq.password=admin

#连接池
spring.activemq.pool.enabled=true
spring.activemq.pool.max-connections=100

3.启动类
注意事项:一定要配置第三个方法连接池,兼容点对点模型和订阅发布模型

@SpringBootApplication
@EnableJms //支持jms
public class ApplicationRunner {

    @Bean
    public Queue queue(){
        return new ActiveMQQueue("common.queue");
    }
    /***
     *      订阅和发布的主题
     */
    @Bean
    public Topic topic(){
        return new ActiveMQTopic("video.topic");
    }
    /***
     *      解决  同时支持点对点模型  和  订阅和发布模型的问题
     */
    @Bean
    public JmsListenerContainerFactory<?> jmsListenerContainerTopic(ConnectionFactory activeMQConnectionFactory){
        DefaultJmsListenerContainerFactory bean = new DefaultJmsListenerContainerFactory();
        bean.setPubSubDomain(true);
        bean.setConnectionFactory(activeMQConnectionFactory);
        return bean;
    }

    public static void main(String[] args) {
        SpringApplication.run(ApplicationRunner.class);
    }
}

3.点对点模型

1.ProducerService

	/**
     *      使用指定消息队列发送消息
     */
    void sendMessage(Destination destination, final String message);

    /***
     *      使用默认消息队列发送消息
     */
    void sendMessage(final String message);

2.ProducerServiceImpl

    @Autowired
    private Queue queue;		//这个queue是启动类里面配置的queue,地址为common.queue

    @Autowired
    private JmsMessagingTemplate jmsTemplate;

    @Override
    public void sendMessage(Destination destination, final String message) {
        jmsTemplate.convertAndSend(destination,message);
    }

    @Override
    public void sendMessage(String message) {
        jmsTemplate.convertAndSend(this.queue,message);
    }

3.MQcontroller

	@Autowired
    private ProducerService producerService;

    @GetMapping("point")
    public Object order(String msg){
         Destination destination = new ActiveMQQueue("point.queue");
         producerService.sendMessage(destination,msg);
         return "success";
    }

    @GetMapping("common")
    public Object common(String msg){
        producerService.sendMessage(msg);
        return "success";
    }

4.测试点对点
启动activeMQ
在浏览器上访问 http://localhost:8080/point?msg=卢本伟牛皮
然后查看 http://localhost:8161/ 点击Manage ActiveMQ broker输入账号密码默认为admin
点击queues查看点对点消息队列
在这里插入图片描述
此时就能看到自己的消息队列
在这里插入图片描述
5.监听点对点模型
当点对点模型的消息队列之后的消息被处理(dequeue)之后,就会调用监听到的方法,如下

@Component
public class PointConsumer {

    /***
     *      监听指定的订单消息队列
     */
    @JmsListener(destination = "point.queue")
    public void receiveQueue(String text){
        System.out.println("point收到的报文为:"+text);
    }
}

重新访问localhost:8080/point?msg=111,就会执行这个方法,就会执行打印语句
这个方法是在消息队列消息被处理之后才执行,在项目实战中,可以在里面处理业务

4.订阅和发布模型

注意:不要忘了启动类ApplicationRunner里面的配置,兼容点对点和订阅发布模型(默认为点对点)

 	@Bean
    public JmsListenerContainerFactory<?> jmsListenerContainerTopic(ConnectionFactory activeMQConnectionFactory){
        DefaultJmsListenerContainerFactory bean = new DefaultJmsListenerContainerFactory();
        bean.setPubSubDomain(true);
        bean.setConnectionFactory(activeMQConnectionFactory);
        return bean;
    }

1.ProducerService

/***
 *      订阅和发布模型的发布者
 */
void publish(String msg);

2.ProducerServiceImpl

	/***
     *      订阅和发布模型的发布者
     */
    @Autowired
    private Topic topic;		//启动类里面配置的topic
    @Override
    public void publish(String msg) {
        this.jmsTemplate.convertAndSend(topic,msg);
    }

3.MqController

/***
 *      发布者
 */
@GetMapping("topic")
public Object topic(String msg){
    producerService.publish(msg);
    return "success";
}

4.监听

@Component
public class TopicSub {
    @JmsListener(destination = "video.topic",containerFactory = "jmsListenerContainerTopic")
    public void receiveTopic1(String text){
        System.out.println("videoTopic订阅者1:"+text);
    }
    @JmsListener(destination = "video.topic",containerFactory = "jmsListenerContainerTopic")
    public void receiveTopic2(String text){
        System.out.println("videoTopic订阅者2:"+text);
    }
    @JmsListener(destination = "video.topic")
    public void receiveTopic3(String text){
        System.out.println("videoTopic订阅者3:"+text);
    }
}

5.测试订阅和发布模型
访问localhost:8080/topic?msg=hahah
控制台打印2次(因为只配置了2个containerFactory = “jmsListenerContainerTopic”)
在这里插入图片描述
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值