首页 > 编程语言 > 详细

springboot 整合activemq

时间:2020-01-22 01:19:12      阅读:182      评论:0      收藏:0      [点我收藏+]

1、配置连接信息

技术分享图片

 

 引入maven信息

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

        <!-- 如果配置线程池则加入 -->
        <dependency>
            <groupId>org.messaginghub</groupId>
            <artifactId>pooled-jms</artifactId>
        </dependency>


        <!--消息队列连接池-->
        <dependency>
            <groupId>org.apache.activemq</groupId>
            <artifactId>activemq-pool</artifactId>
            <version>5.15.0</version>
        </dependency>

  

 

 2、增加自定义配置

QueueConfig.java
package cn.activemq;

import org.apache.activemq.command.ActiveMQQueue;
import org.apache.activemq.command.ActiveMQTopic;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.jms.Queue;
import javax.jms.Topic;

@Configuration
@EnableJms public class QueueConfig { @Value("${queue}") private String queueName; @Value("${topic}") private String topicName; @Bean public Queue queue(){ return new ActiveMQQueue(queueName); } @Bean public Topic topic(){ return new ActiveMQTopic(topicName); } }

  

生产者 

Producter.java
package cn.activemq;


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

import javax.jms.Queue;
import javax.jms.Topic;
import java.util.List;

/**
 *  生产者
 */
@Component
public class Producter {

    @Autowired
    private JmsMessagingTemplate jmsMessagingTemplate;

    @Autowired
    private Queue queue;

    @Autowired
    private Topic topic;

    public void send1(String  msg){
        jmsMessagingTemplate.convertAndSend(queue, msg);
    }


    public void sendQuene(Weixin object){
        jmsMessagingTemplate.convertAndSend(queue, object);
    }

    public void sendTopic(Weixin object){
        jmsMessagingTemplate.convertAndSend(topic, object);
    }

}

  

消费者

Consumer.java
package cn.activemq;


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

import java.util.List;

/**
 * 消费者
 */
@Component
public class Consumer {



    @JmsListener(destination = "${queue}")
    public void receive12(String str) {
        System.out.println(str);
        System.out.println(11121);
    }

    @JmsListener(destination = "${topic}")
    public void receive2(Object object) {
        System.out.println(object);
        System.out.println(1112);
    }


    @JmsListener(destination = "${topic}")
    public void receive3(List list) {
        System.out.println(list.size());
        System.out.println(11144);
    }
}

  

调用方式:

    /**
     * 注入
     */
    @Autowired
    private Producter producter;    


     /**
     * 调用
     */
    producter.sendQuene("11");

  

springboot 整合activemq

原文:https://www.cnblogs.com/pxblog/p/12227487.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!