集成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

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

  1. SpringBoot系列八:SpringBoot整合消息服务(SpringBoot 整合 ActiveMQ、SpringBoot 整合 RabbitMQ、SpringBoot 整合 Kafka)

    声明:本文来源于MLDN培训视频的课堂笔记,写在这里只是为了方便查阅. 1.概念:SpringBoot 整合消息服务 2.具体内容 对于异步消息组件在实际的应用之中会有两类: · JMS:代表作就是 ...

  2. SpringBoot整合ActiveMQ快速入门

    Spring Boot 具有如下特性: 为基于 Spring 的开发提供更快的入门体验 开箱即用,没有代码生成,也无需 XML 配置.同时也可以修改默认值来满足特定的需求. 提供了一些大型项目中常见的 ...

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

    环境搭建 1.创建maven项目(jar) 2.pom.xml添加依赖 <parent> <groupId>org.springframework.boot</group ...

  4. SpringBoot整合ActiveMQ和开启持久化

    一.点对点 1.提供者目录展示 2.导入依赖 <dependency> <groupId>org.springframework.boot</groupId> &l ...

  5. ActiveMQ 笔记(四)Spring\SpringBoot 整合 Activemq

    个人博客网:https://wushaopei.github.io/    (你想要这里多有) 一.Spring 整合Activemq 1.所需jar包 <dependencies> &l ...

  6. 06_在web项目中集成Spring

    在web项目中集成Spring 一.使用Servlet进行集成测试 1.直接在Servlet 加载Spring 配置文件 ApplicationContext applicationContext = ...

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

    本篇内容还是建立在上一篇Java Web学习系列——Maven Web项目中集成使用Spring基础之上,对之前的Maven Web项目进行升级改造,实现对MySQL的数据访问. 添加依赖Jar包 这 ...

  8. Springboot整合activemq

    今天呢心血来潮,也有很多以前的学弟问到我关于消息队列的一些问题,有个刚入门,有的有问题都来问我,那么今天来说说如何快速入门mq. 一.首先说下什么是消息队列? 1.消息队列是在消息的传输过程中保存消息 ...

  9. SpringBoot整合ActiveMQ发送邮件

    虽然ActiveMQ以被其他MQ所替代,但仍有学习的意义,本文采用邮件发送的例子展示ActiveMQ 1. 生产者1.1 引入maven依赖1.2 application.yml配置1.3 创建配置类 ...

随机推荐

  1. java基础问题巩固(1)

    你对java垃圾回收了解吗?什么时候需要使用? 答: 垃圾回收器的作用是查找和回收(清理)无用的对象,从而让jvm更 有效的使用内存.但是运行因为垃圾回收本身会有开销,过于频繁的使用会导致性能下降.比 ...

  2. Sass和less的区别是什么?用哪个好

    什么是Sass和Less?       Sass和Less都属于CSS预处理器,那什么是 CSS 预处理器呢?        CSS 预处理器定义了一种新的语言,其基本思想是,用一种专门的编程语言,为 ...

  3. python中的__dict__,__getattr__,__setattr__

    python class 通过内置成员dict 存储成员信息(字典) 首先用一个简单的例子看一下dict 的用法 class A(): def __init__(self,a,b): self.a = ...

  4. 常规 nginx 代理apache

    nginx: server { server_name www.xx.com xx.com; access_log /data/wwwlogs/www.xx.com_nginx.log combine ...

  5. 【MyBatis】Mapper XML 文件

    Mapper XML文件 MyBatis 的真正强大在于它的映射语句,也是它的魔力所在.由于它的异常强大,映射器的 XML 文件就显得相对简单.如果拿它跟具有相同功能的 JDBC 代码进行对比,你会立 ...

  6. 在中断服务函数中使用FreeRTOS系统延时函数vTaskDelay导致看门狗复位的情况

    @2019-04-09 [问题] 控制程序工作一段时间异常重启 [分析] 经定位分析重启原因为看门狗复位导致 [解决] 经排查发现在中断服务函数中使用了FreeRTOS的系统时延函数vTaskDela ...

  7. SPOJ DIVCNT2

    SPOJ DIVCNT2 题目大意: 求\(S2(n)=\sum_{i=1}^{n}\sigma_0{(i^2)}\) . 题解 我们可以先考虑括号里只有一个\(i\)的情况,这样,我们把\(i\)分 ...

  8. django rest framework pagination

    REST framework 包含对可定制分页样式的支持.这使你可以将较大的结果集分成单独的数据页面. 分页 API 支持: 以分页链接的形式作为响应内容的一部分. 以分页链接的形式包含在响应的 he ...

  9. zabbix3.2监控mysql主从(复制)状态

    问题背景: MySQL主从架构最常见不过了,在生产环境下,主从关系的状态的重要性不言而喻,这里小记一下通过zabbix监控主从状态,并用微信告警测试. 测试环境: 软件 MySQL-master My ...

  10. 洛谷P2179 骑行川藏

    什么毒瘤... 解:n = 1的,发现就是一个二次函数,解出来一个v的取值范围,选最大的即可. n = 2的,猜测可以三分.于是先二分给第一段路多少能量,然后用上面的方法求第二段路的最短时间.注意剩余 ...