消息中间件-ACTIVEMQ-6:ActiveMQ和SpringBoot整合

目录

1:导包

2:队列

2.1:写配置

2.2:测试生产者

1:设置队列等信息的config类

2:发送队列消息的程序方法类

 3:测试:

2.3:测试消费者

3:主题

3.1:配置

3.2:测试生产者

3.3:测试消费者

3.4:先开启订阅后再发布消息


 

1:导包

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

        <!--springboot和activemq整合的启动器-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-activemq</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

2:队列

2.1:写配置

server.port=8081

spring.activemq.broker-url=tcp://192.168.43.122:61616
spring.activemq.user=admin
spring.activemq.password=damin

#是否为主题;现在为队列,所以为false,默认也是false
spring.jms.pub-sub-domain=false

myqueue=boot-activemq-queue

2.2:测试生产者

1:设置队列等信息的config类

package com.wkl.springboot_activemq.activemq;

import org.apache.activemq.command.ActiveMQQueue;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

import javax.jms.Queue;

/**
 * Description:写springboot下的mq的配置
 * Date:       2020/9/7 - 下午 8:42
 * author:     wangkanglu
 * version:    V1.0
 */
@Component
public class config {

    @Value("${myqueue}")
    private String myqueue;

    @Bean
    public Queue queue(){
        return new ActiveMQQueue(myqueue);
    }
}

2:发送队列消息的程序方法类

package com.wkl.springboot_activemq.activemq;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.stereotype.Component;

import javax.jms.Queue;

/**
 * Description:队列的生产者
 * Date:       2020/9/7 - 下午 8:45
 * author:     wangkanglu
 * version:    V1.0
 */
@Component
public class Produce {

    @Autowired
    private JmsMessagingTemplate jmsMessagingTemplate;

    @Autowired
    private Queue queue;

    public void send(){
        jmsMessagingTemplate.convertAndSend(queue,"boot发送消息---");
    }
}

 3:测试:

package com.wkl.springboot_activemq;

import com.wkl.springboot_activemq.activemq.Produce;
import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;

@SpringBootTest
@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
class SpringbootActivemqApplicationTests {

    @Autowired
    private Produce produce;

    @Test
    void contextLoads() {
        produce.send();
        System.out.println("----end----");
    }

}

2.3:测试消费者

package com.wkl.springbootactivemqqueueconsumer.consumer;

import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Component;
import sun.plugin2.message.TextEventMessage;

import javax.jms.JMSException;
import javax.jms.TextMessage;

/**
 * Description:队列的消费者,直接配置监听器
 * Date:       2020/9/8 - 上午 9:39
 * author:     wangkanglu
 * version:    V1.0
 */
@Component
public class MQconsumer {

    @JmsListener(destination = "${myqueue}")
    public void receive(TextMessage message){
        try {
            System.out.println("消费者收到消息:"+message.getText());
        } catch (JMSException e) {
            e.printStackTrace();
        }
    }
}

结果:

3:主题

3.1:配置

server.port=8083

spring.activemq.broker-url=tcp://192.168.43.122:61616
spring.activemq.user=admin
spring.activemq.password=damin

#是否为主题;现在为主题,所以为true,默认也是false
spring.jms.pub-sub-domain=true

mytopic=boot-activemq-topic

3.2:测试生产者

配置:

package com.wkl.springboot_activemq_topicproduce.activemq;

import org.apache.activemq.command.ActiveMQTopic;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

import javax.jms.Topic;

/**
 * Description:主题的配置类,将主题放进容器中
 * Date:       2020/9/8 - 上午 10:53
 * author:     wangkanglu
 * version:    V1.0
 */
@Component
public class config {

    @Value("${mytopic}")
    private String topicName;

    @Bean
    public Topic topic(){
        return new ActiveMQTopic(topicName);
    }
}

发布消息:

package com.wkl.springboot_activemq_topicproduce.activemq;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.jms.Topic;
import java.util.UUID;

/**
 * Description:主题的发布者
 * Date:       2020/9/8 - 上午 10:55
 * author:     wangkanglu
 * version:    V1.0
 */
@Component
public class Produce {

    @Autowired
    private JmsMessagingTemplate jmsMessagingTemplate;
    @Autowired
    private Topic topic;

    /*让这个主题发布者,每3秒中发布一条消息*/
    @Scheduled(fixedDelay = 3000)
    public void send(){
        jmsMessagingTemplate.convertAndSend(topic, UUID.randomUUID().toString().substring(0,7));
    }
}

3.3:测试消费者

package com.wkl.springboot_activemq_topicconsumer.activemq;

import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Component;

import javax.jms.JMSException;
import javax.jms.TextMessage;

/**
 * Description:主题的消费者
 * Date:       2020/9/8 - 上午 11:00
 * author:     wangkanglu
 * version:    V1.0
 */
@Component
public class Consumer {

    @JmsListener(destination = "${mytopic}")
    public void recive(TextMessage message) throws JMSException {
        System.out.println("收到消息:"+message.getText());
    }

}

3.4:先开启订阅后再发布消息

 

 

 

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

苍煜

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

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

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

打赏作者

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

抵扣说明:

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

余额充值