SpringBoot集成ActiveMq,并将管理界面集成到后台系统

1:下载安装

下载地址: http://activemq.apache.org/download.html

开发环境使用Windows版本,运行在自己的主机,防止相互干扰。

解压缩,点击下图所示批处理文件,即可运行。

image.png

默认的管理后台地址为:ip:8161,可以访问此网址验证是否安装成功。

image.png

管理平台默认用户名:admin,默认密码:admin

2:基本介绍

MQ是消息中间件,是一种在分布式系统中应用程序借以传递消息的媒介,常用的有ActiveMQ,RabbitMQ,Kafka。ActiveMQ是Apache下的开源项目,完全支持JMS1.1和J2EE1.4规范的JMS Provider实现。 

特点: 

1、支持多种语言编写客户端 

2、对spring的支持,很容易和spring整合 

3、支持多种传输协议:TCP,SSL,NIO,UDP等 

4、支持AJAX 

消息形式: 

1、点对点(queue) 

2、一对多(topic) 

3:项目集成

配置pom.xml:

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

        <dependency>
            <groupId>org.apache.activemq</groupId>
            <artifactId>activemq-pool</artifactId>
            <version>5.14.5</version>
        </dependency>
 

配置全局配置文件application-*.yml

#消息队列配置
activemq:
    user: admin
    password: admin
    broker-url: tcp://127.0.0.1:61616
    pool:
      enabled: true
      max-connections: 10
 
#可以根据业务,配置多个队列名
#消息队列的 queue 名称
queueName: publish.queue
#消息队列的 topic 名称
topicName: publish.topic

添加启动配置ActiveMqConfig.java:

package cc.ahxb.config;

/**
 * Describe
 *
 * @author Gao
 * @date 2018/12/1
 */

import org.apache.activemq.ActiveMQConnectionFactory;
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 org.springframework.jms.config.DefaultJmsListenerContainerFactory;
import org.springframework.jms.config.JmsListenerContainerFactory;

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


/**
 * @author: Gao
 */
@Configuration
public class ActiveMqConfig {
    @Value("${queueName}")
    private String queueName;

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

    @Value("${spring.activemq.user}")
    private String usrName;

    @Value("${spring.activemq.password}")
    private  String password;

    @Value("${spring.activemq.broker-url}")
    private  String brokerUrl;

    /**
     * 实际项目中,可以通过不同的queueName,分别实例化多个Queue,用在不同类型的消息的区分
     * @return
     */
    @Bean
    public Queue queue(){
        return new ActiveMQQueue(queueName);
    }

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

    @Bean
    public ActiveMQConnectionFactory connectionFactory() {
        return new ActiveMQConnectionFactory(usrName, password, brokerUrl);
    }

    @Bean
    public JmsListenerContainerFactory jmsListenerContainerQueue(ActiveMQConnectionFactory connectionFactory){
        DefaultJmsListenerContainerFactory bean = new DefaultJmsListenerContainerFactory();
        bean.setConnectionFactory(connectionFactory);

        return bean;
    }

    @Bean
    public JmsListenerContainerFactory jmsListenerContainerTopic(ActiveMQConnectionFactory connectionFactory){
        DefaultJmsListenerContainerFactory bean = new DefaultJmsListenerContainerFactory();
        //设置为发布订阅方式, 默认情况下使用的生产消费者方式
        bean.setPubSubDomain(true);
        bean.setConnectionFactory(connectionFactory);
        return bean;
    }
}

4:定义消息队列消费者:

抽象接口

image.png

image.png

QueueConsumer.java

package cc.ahxb.mq.consumer;

import cc.ahxb.mqinterface.IMqConsumer;
import org.apache.activemq.ActiveMQConnectionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.jms.config.DefaultJmsListenerContainerFactory;
import org.springframework.stereotype.Component;

import javax.jms.*;


/**
 * Describe
 *
 * @author Jxx
 * @date 2018/12/1
 */
//加上Component注解,启动即监听了消息队列。
@Component
public class QueueConsumer implements IMqConsumer {

    @Autowired
    private ActiveMQConnectionFactory connectionFactory;

    /**
     * 接收到的Queue类型的消息,
     * destination(消息的目的地)为"publish.queue",在配置文件中配置
     * containerFactory(容器工厂),jmsListenerContainerQueue,在ActiveMqConfig 中初始化
     * @param message  接收到的消息
     * @throws Exception
     */
    @Override
    @JmsListener(destination = "publish.queue", containerFactory = "jmsListenerContainerQueue")
    public void onReceive(String message) throws Exception {
        System.out.println("接收到publish.queue中消息:"+message+"");

    }

    /**
     * 设置消费者接收器(接收自定义队列的消息,会阻塞线程)
     * @param messageListener  消息监听器
     * @param destination    自定义队列的名称,必须与发送端保持一致
     *
     */
    @Override
    public void setMessageListener(MessageListener messageListener,String destination) throws Exception {
        //1、创建工厂连接对象,需要制定ip和端口号

        //2、使用连接工厂创建一个连接对象
        Connection connection = connectionFactory.createConnection();
        //3、开启连接
        connection.start();
        //4、使用连接对象创建会话(session)对象
        Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        //5、使用会话对象创建目标对象,包含queue和topic(一对一和一对多)
        Queue queue = session.createQueue(destination);
        //6、使用会话对象创建生产者对象
        MessageConsumer consumer = session.createConsumer(queue);
        //7、向consumer对象中设置一个messageListener对象,用来接收消息
        consumer.setMessageListener(messageListener);
        //8、程序等待接收用户消息
        System.in.read();
        //9、关闭资源
        consumer.close();
        session.close();
        connection.close();



    }

}

TopicConsumer.java

package cc.ahxb.mq.consumer;

import cc.ahxb.mqinterface.IMqConsumer;
import org.apache.activemq.ActiveMQConnectionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Component;

import javax.jms.*;

/**
 * Describe
 *
 * @author Jxx
 * @date 2018/12/1
 */
//加上Component注解,启动即监听了消息队列。
@Component
public class TopicConsumer implements IMqConsumer {

    @Autowired
    private ActiveMQConnectionFactory connectionFactory;

    /**
     * 接收到的Topic类型的消息,
     * destination(消息的目的地)为"publish.topic",在配置文件中配置
     * containerFactory(容器工厂),jmsListenerContainerTopic,在ActiveMqConfig 中初始化
     * @param message  接收到的消息
     * @throws Exception
     */
    @Override
    @JmsListener(destination = "publish.topic", containerFactory = "jmsListenerContainerTopic")
    public void onReceive(String message) throws Exception {

        System.out.println("接收到publish.topic中消息:"+message+"");

    }
    /**
     * 设置消费者接收器(接收自定义队列的消息,会阻塞线程)
     * @param messageListener  消息监听器
     * @param destination    自定义队列的名称,必须与发送端保持一致
     *  @throws Exception
     */

    @Override
    public void setMessageListener(MessageListener messageListener, String destination) throws Exception {
        //2、使用连接工厂创建一个连接对象
        Connection connection = connectionFactory.createConnection();
        //3、开启连接
        connection.start();
        //4、使用连接对象创建会话(session)对象
        Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        //5、使用会话对象创建目标对象,包含queue和topic(一对一和一对多)
        Topic topic = session.createTopic(destination);
        //6、使用会话对象创建生产者对象
        MessageConsumer consumer = session.createConsumer(topic);
        //7、向consumer对象中设置一个messageListener对象,用来接收消息
        consumer.setMessageListener(messageListener);
        //8、程序等待接收用户消息
        System.in.read();
        //9、关闭资源
        consumer.close();
        session.close();
        connection.close();
    }
}

5:定义消息队列的生产者:

image.png

ActiveMqProducer.java

package cc.ahxb.mq.producer;

import cc.ahxb.mqinterface.IMqProducer;
import org.apache.activemq.command.ActiveMQQueue;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.stereotype.Component;

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

/**
 * Describe
 *
 * @author Jxx
 * @date 2018/12/1
 */

@Component
public class ActiveMqProducer implements IMqProducer {

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

    /**
     * 发送Queue类型的消息
     * @param message
     * @throws Exception
     */
    @Override
    public void sendQueueMessage(String message) throws Exception {
        jmsMessagingTemplate.convertAndSend(this.queue,message);
    }

    /**
     * 发送Topic类型的消息
     * @param message
     * @throws Exception
     */
    @Override
    public void sendTopicMessage(String message) throws Exception {

        jmsMessagingTemplate.convertAndSend(this.topic,message);
    }


    /**
     *  向自定义队列发送消息
     *  @param queueName  自定义的 destination队列名
     * @param message    消息内容
     * @throws Exception
     */
    @Override
    public void sendQueueMessage(String queueName,String message) throws Exception {

        jmsMessagingTemplate.convertAndSend(new ActiveMQQueue(queueName),message);

    }




}

6:调用示例:

package cc.ahxb.controller;

import cc.ahxb.model.Log;
import cc.ahxb.mq.consumer.QueueConsumer;
import cc.ahxb.mq.producer.ActiveMqProducer;
import cc.ahxb.util.JsonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.listener.adapter.AbstractAdaptableMessageListener;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.jms.*;

/**
 * Describe
 *
 * @author Jxx
 * @date 2018/12/1
 */
@Controller
@RequestMapping("/mq")
public class MqController {

    @Autowired
    private ActiveMqProducer activeMqProducer;

    @GetMapping(value = "/sendMessage")
    @ResponseBody
    public JsonUtil sendMessage(){
        JsonUtil result = new JsonUtil();
        try {
            activeMqProducer.sendQueueMessage("测试消息队列");
            result.setMessage("发送成功");
        }catch (Exception e){
            e.printStackTrace();
            result.setMessage("发送失败,请重新发送");
        }
        return result;
    }
}

补充:如果需要做到在Iframe中显示管理界面,需要更改activeMQ 自带服务器的X-FRAME-OPTIONS策略

                

猜你喜欢

转载自blog.csdn.net/yaojun19791206/article/details/84954596