springboot 整合activemq

1、配置连接信息

springboot 整合activemq

 引入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");

相关推荐