Web项目容器集成ActiveMQ & SpringBoot整合ActiveMQ

  集成tomcat就是随项目启动而启动tomcat,最简单的方法就是监听器监听容器创建之后以Broker的方式启动ActiveMQ。

1.web项目中Broker启动的方式进行集成

  在这里采用Listener监听ServletContext创建和销毁进行Broker的启动和销毁。

0.需要的jar包:

1.listener实现ServletContextListener接口

package cn.qlq.listener;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import org.apache.activemq.broker.BrokerService;

public class ActiveMQListener implements ServletContextListener {

    private static final BrokerService brokerService = new BrokerService();

    @Override
    public void contextDestroyed(ServletContextEvent arg0) {
        try {
            brokerService.stop();
            System.out.println("broker 停止");
        } catch (Exception e) {

        }
    }

    @Override
    public void contextInitialized(ServletContextEvent arg0) {
        try {
            brokerService.setUseJmx(true);
            brokerService.addConnector("tcp://localhost:61616");
            brokerService.start();
            System.out.println("broker 启动");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}

web.xml进行监听器的配置:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://java.sun.com/xml/ns/javaee"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    id="WebApp_ID" version="2.5">
    <display-name>MyWeb</display-name>
    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>

    <listener>
        <listener-class>cn.qlq.listener.ActiveMQListener</listener-class>
    </listener>

</web-app>

测试方法:向容器中发送和接收消息,成功证明整合成功。

2.SpringBoot中以Broker方式启动ActiveMQ

1.maven中增加如下配置:

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

2.编写Listener

package cn.qlq.listener;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import org.apache.activemq.broker.BrokerService;

public class ActiveMQListener implements ServletContextListener {
    private static final BrokerService brokerService = new BrokerService();

    @Override
    public void contextDestroyed(ServletContextEvent arg0) {
        try {
            brokerService.stop();
            System.out.println("broker 停止");
        } catch (Exception e) {

        }
    }

    @Override
    public void contextInitialized(ServletContextEvent arg0) {
        try {
            brokerService.setUseJmx(true);
            brokerService.addConnector("tcp://localhost:61616");
            brokerService.start();
            System.out.println("broker 启动");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

3.注册Listener到容器中:

package cn.qlq.config;

import org.springframework.boot.web.servlet.ServletListenerRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import cn.qlq.listener.ActiveMQListener;/**
 *
 * @author Administrator
 *
 */
@Configuration
public class ListenerConfig {

    @Bean
    public ServletListenerRegistrationBean<ActiveMQListener> listenerRegist3() {
        ServletListenerRegistrationBean<ActiveMQListener> srb = new ServletListenerRegistrationBean<ActiveMQListener>();
        srb.setListener(new ActiveMQListener());
        return srb;
    }

}

测试方法:向容器中发送和接收消息,成功证明整合成功。

3.SpringBoot整合ActiveMQ进行开发

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.7.0</version> -->
        </dependency>

application.properties增加activeMQ配置

spring.activemq.brokerUrl=tcp://127.0.0.1:61616

#spring.activemq.user=admin
#spring.activemq.password=123456
#spring.activemq.in-memory=true
#spring.activemq.pool.enabled=false

编写生产者代码:

package cn.qlq.activemq;

import javax.jms.Destination;

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

@Component("producer")
public class Producer {
    // 也可以注入JmsTemplate,JmsMessagingTemplate对JmsTemplate进行了封装
    @Autowired
    private JmsMessagingTemplate jmsTemplate;

    // 发送消息,destination是发送到的队列,message是待发送的消息
    public void sendMessage(Destination destination, final String message) {
        jmsTemplate.convertAndSend(destination, message);
    }
}

消费者代码:

package cn.qlq.activemq;

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

@Component
public class Consumer {

    // 使用JmsListener配置消费者监听的队列,其中text是接收到的消息
    @JmsListener(destination = "myQueue")
    public void receiveQueue(String text) {
        System.out.println("Consumer收到的报文为:" + text);
    }

}

测试代码:

import javax.jms.Destination;

import org.apache.activemq.command.ActiveMQQueue;
import org.junit.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.SpringRunner;

import cn.qlq.MySpringBootApplication;
import cn.qlq.activemq.Producer;

@RunWith(SpringRunner.class)
@SpringBootTest(classes = MySpringBootApplication.class)
public class PlainTest {
    @Autowired
    private Producer producer;

    @Test
    public void contextLoads() throws InterruptedException {
        Destination destination = new ActiveMQQueue("myQueue");

        for (int i = 0; i < 5; i++) {
            producer.sendMessage(destination, "message" + i);
        }
    }
}

  当然了这种方式也可以使用外部的ActiveMQ,也就是不用Broker方式启动ActiveMQ,以bat文件启动ActiveMQ之后整合方式同上。

  git地址:https://github.com/qiao-zhi/springboot-ssm.git

原文地址:https://www.cnblogs.com/qlqwjy/p/10698218.html

时间: 2024-08-29 09:59:53

Web项目容器集成ActiveMQ & SpringBoot整合ActiveMQ的相关文章

解决Springboot整合ActiveMQ发送和接收topic消息的问题

环境搭建 1.创建maven项目(jar) 2.pom.xml添加依赖 <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.4.0.RELEASE</version> </parent> <dependencies> &l

06_在web项目中集成Spring

在web项目中集成Spring 一.使用Servlet进行集成测试 1.直接在Servlet 加载Spring 配置文件 ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml"); HelloService helloService = (HelloService) applicationContext.getBean("helloS

在java web项目中集成webservice

公司要求在项目中加入webservice服务,因为项目中使用了spring框架,所以在这里使用与spring兼容性较好的cxf来实现 cxf所需jar包 spring的jar包就不贴了 一:创建webservice服务器 1)创建一个服务接口 package com.service; import javax.jws.WebParam; import javax.jws.WebService; @WebService public interface IHelloWorld { public S

Java Web学习系列——Maven Web项目中集成使用Spring、MyBatis实现对MySQL的数据访问

本篇内容还是建立在上一篇Java Web学习系列——Maven Web项目中集成使用Spring基础之上,对之前的Maven Web项目进行升级改造,实现对MySQL的数据访问. 添加依赖Jar包 这部分内容需要以下Jar包支持 mysql-connector:MySQL数据库连接驱动,架起服务端与数据库沟通的桥梁: MyBatis:一个支持普通SQL查询,存储过程和高级映射的优秀持久层框架: log4j:Apache的开源项目,一个功能强大的日志组件,提供方便的日志记录: 修改后的pom.xm

SpringBoot整合ActiveMQ发送邮件

虽然ActiveMQ以被其他MQ所替代,但仍有学习的意义,本文采用邮件发送的例子展示ActiveMQ 1. 生产者1.1 引入maven依赖1.2 application.yml配置1.3 创建配置类ConfigQueue1.4 创建生产者类Producer1.5 启动类AppProducer2. 消费者2.1 引入maven依赖2.2 application.yml配置2.3 创建消费者类Consumer2.4 启动类AppConsumer3. 启动截图3.1 生产者截图3.2 消费者截图3.

SpringBoot整合ActiveMQ

先建工程 .. .. .. .. ..先看一下最终目录结构(实际上核心就是两个类,但是其他的多写写还是没有坏处的) 消息实体类 package com.example.demo.domain; import java.io.Serializable; import java.util.Date; public class Message implements Serializable { private int id; private String from; private String to

SpringBoot 整合 ActiveMq

消息队列,用来处理开发中的高并发问题,通过线程池.多线程高效的处理并发任务. 首先,需要下载一个ActiveMQ的管理端:我本地的版本是 activemq5.15.8,打开activemq5.15.8\bin\win64\wrapper.exe客户端,可以根据localhost:端口号,访问ActiveMQ的管理界面.默认的用户名.密码都是admin. (一)pom 文件中添加 ActiveMq 依赖 <dependency> <groupId>org.apache.activem

SpringBoot整合ActiveMQ实现持久化

点对点(P2P) 结构 创建生产者和消费者两个springboot工程 导入依赖 <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-activemq</artifactId> </dependency> 生产者 步骤一:application.properties文件 spring.activemq.brok

springboot 整合ActiveMq

pom.xml <!-- 配置ActiveMQ启动器 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-activemq</artifactId> </dependency> 创建消息队列 //创建队列 @Bean public Queue queue(){ return new Acti