Java环境和SpringBoot环境集成ActiveMQ简单应用

废话不多说直接来搞(前提你先把自己的Linux activeMQ服务跑起来)

JAVA环境

先引入依赖:

<dependency>
      <groupId>org.apache.activemq</groupId>
      <artifactId>activemq-core</artifactId>
      <version>5.7.0</version>
    </dependency>

然后开发后台测试代码

P2P方式

//生产者
    @Test
    public void queueProduct() throws JMSException {
        //创建工厂
        ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory("tcp://192.168.247.139:61616");
        //创建连接
        QueueConnection connection = connectionFactory.createQueueConnection();
        //创建session
        Session session = connection.createSession(true, Session.AUTO_ACKNOWLEDGE);
        //创建生产者
        Destination destination =new ActiveMQQueue("java-queue");
        MessageProducer producer = session.createProducer(destination);
        //创建消息
        TextMessage textMessage = session.createTextMessage("nihao ya ");
        producer.send(textMessage);
        //提交
        session.commit();
        session.close();
        connection.close();
    }
   //消费者
    @Test
    public void queueCousumer() throws JMSException {
        //创建工厂
        ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory("tcp://192.168.247.139:61616");
        //创建连接
        QueueConnection connection = connectionFactory.createQueueConnection();
        connection.start();
        //创建session
        Session session = connection.createSession(true, Session.AUTO_ACKNOWLEDGE);
        //创建消费着
        Destination destination =new ActiveMQQueue("java-queue");
        MessageConsumer consumer = session.createConsumer(destination);
        ActiveMQTextMessage message = (ActiveMQTextMessage) consumer.receive();
        System.out.println(message.getText());
        session.commit();

    }

订阅模式

  @Test
    public void topicProduct() throws JMSException {
        ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory("tcp://192.168.247.139:61616");
        TopicConnection connection = connectionFactory.createTopicConnection();
        Session session = connection.createSession(true, Session.AUTO_ACKNOWLEDGE);
        TextMessage textMessage = session.createTextMessage("zhe shi topic message");
        Destination destination = new ActiveMQTopic("java-topic");
        MessageProducer producer = session.createProducer(destination);
        producer.send(textMessage);
        session.commit();
        session.close();
        connection.close();
    }
    @Test
    public void topicConsumer() throws JMSException {
        ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory("tcp://192.168.247.139:61616");
        TopicConnection connection = connectionFactory.createTopicConnection();
        connection.start();
        Session session = connection.createSession(true, Session.AUTO_ACKNOWLEDGE);
        while (true){
            Destination destination = new ActiveMQTopic("java-topic");
            MessageConsumer consumer = session.createConsumer(destination);
            TextMessage message = (TextMessage) consumer.receive();
            if (message!=null){
                System.out.println(message.getText());
            }else {
                break;
            }
        }

    }

springboot集成

先引入jar (跟随springboot版本即可)

 <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-activemq -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-activemq</artifactId>
        </dependency>

配置文件

#服务端口,8080被另一服务占用
server.port=9090
spring.activemq.broker-url=tcp://127.0.0.1:61616
# 在考虑结束之前等待的时间
#spring.activemq.close-timeout=15s
# 默认代理URL是否应该在内存中。如果指定了显式代理,则忽略此值。
spring.activemq.in-memory=true 
# 是否在回滚回滚消息之前停止消息传递。这意味着当启用此命令时,消息顺序不会被保留。
spring.activemq.non-blocking-redelivery=false
# 等待消息发送响应的时间。设置为0等待永远。
spring.activemq.send-timeout=0
#默认情况下activemq提供的是queue模式,若要使用topic模式需要配置下面配置
spring.jms.pub-sub-domain=true
#账号
spring.activemq.user=admin
# 密码
spring.activemq.password=admin
# 是否信任所有包
#spring.activemq.packages.trust-all=
# 要信任的特定包的逗号分隔列表(当不信任所有包时)
#spring.activemq.packages.trusted=
# 当连接请求和池满时是否阻塞。设置false会抛“JMSException异常”。
#spring.activemq.pool.block-if-full=true
# 如果池仍然满,则在抛出异常前阻塞时间。
#spring.activemq.pool.block-if-full-timeout=-1ms
# 是否在启动时创建连接。可以在启动时用于加热池。
#spring.activemq.pool.create-connection-on-startup=true
# 是否用Pooledconnectionfactory代替普通的ConnectionFactory。
#spring.activemq.pool.enabled=false
# 连接过期超时。
#spring.activemq.pool.expiry-timeout=0ms
# 连接空闲超时
#spring.activemq.pool.idle-timeout=30s
# 连接池最大连接数
#spring.activemq.pool.max-connections=1
# 每个连接的有效会话的最大数目。
#spring.activemq.pool.maximum-active-session-per-connection=500
# 当有"JMSException"时尝试重新连接
#spring.activemq.pool.reconnect-on-exception=true
# 在空闲连接清除线程之间运行的时间。当为负数时,没有空闲连接驱逐线程运行。
#spring.activemq.pool.time-between-expiration-check=-1ms
# 是否只使用一个MessageProducer
#spring.activemq.pool.use-anonymous-producers=true

P2P模式太简单了就不说了

订阅模式

编写一个测试controller


@RestController
@RequestMapping("topic")
public class TopicController {
    @Autowired
    private JmsTemplate jmsTemplate;

    @RequestMapping("product")
    public String topicProduct(String message) {
        Destination destination = new ActiveMQTopic("spring-topic");
        jmsTemplate.convertAndSend(destination,message);
        return "ok";
    }
}

然后编写两个监听类

 @JmsListener(destination = "spring-topic")
    public void topic1(String message){

        System.out.println("监听器111111:           "+message);
    }
    @JmsListener(destination = "spring-topic")
    public void topic2(String message){
        
        System.out.println("监听器222222:           "+message);
    }

最最重要的一点:

默认情况下activemq提供的是queue模式,若要使用topic模式需要配置下面配置
spring.jms.pub-sub-domain=true

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值