springboot整合activemq小demo

直接上干货。。。

1、首先配置pom.xml文件如下:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.mq.demo</groupId>
<artifactId>mq-demo</artifactId>
<version>0.0.1-SNAPSHOT</version>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot</artifactId>
<version>1.4.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-test</artifactId>
<version>1.4.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<version>1.4.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>1.4.1.RELEASE</version>
</dependency>
<!-- mq -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-activemq</artifactId>
<version>1.4.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.javassist</groupId>
<artifactId>javassist</artifactId>
<version>3.18.1-GA</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.6.1</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.6.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.16</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.5</version>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>18.0</version>
</dependency>

</dependencies>

<!-- 打包成一个可执行的jar包 -->
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>1.4.1.RELEASE</version>
</plugin>
</plugins>
</build>
</project>

2、创建包config,该包下创建类ActiveMQConfig.java文件,实现如下:

@Configuration
public class ActiveMQConfig {

@Bean
public ActiveMQConnectionFactory getActiveMQConnectionFactory() {
ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory();
connectionFactory.setBrokerURL("tcp://localhost:61616");
connectionFactory.setCopyMessageOnSend(false);
connectionFactory.setAlwaysSyncSend(true);
connectionFactory.setUseAsyncSend(true);
connectionFactory.setOptimizeAcknowledge(true);
connectionFactory.setTrustAllPackages(true);
return connectionFactory;
}

}

创建包listener,包下创建MQListener.java文件,内容如下:

public class MQListener implements MessageListener{
private Session session;

public MQListener(){
}

public MQListener(Session session) {
this.session = session;
}

/*
* @see javax.jms.MessageListener#onMessage(javax.jms.Message)
*/
public void onMessage(Message message) {
ObjectMessage objMsg = (ObjectMessage) message;
try {
System.out.println("收到消息 : " + objMsg.getObject());
session.commit();
} catch (JMSException e) {
e.printStackTrace();
}
}
}

创建包consumer,包下创建MyConsumer.java文件,内容如下:

@Component
@EnableScheduling
public class MyConsumer {
@Autowired
private ActiveMQConnectionFactory activeMQConnectionFactory;

@Scheduled(fixedDelay = 3000)
public void receive() throws ConnectException {
try {
System.out.println("======================开始消费========================");
Connection connection = activeMQConnectionFactory.createConnection();
connection.start();
Session session = connection.createSession(Boolean.TRUE, Session.AUTO_ACKNOWLEDGE);
Destination destination = session.createQueue("test");
MessageConsumer consumer = session.createConsumer(destination);
consumer.setMessageListener(new MQListener(session));
// ObjectMessage message = (ObjectMessage) consumer.receive();
// System.out.println("收到消息 : " + message.getObject());
// session.close();
}
catch (JMSException e) {
e.printStackTrace();
}
}
}

创建包producer,包下创建MyProducer.java文件,内容如下:

@Component
@EnableScheduling
public class MyProducer {

@Autowired
private ActiveMQConnectionFactory activeMQConnectionFactory;

@Scheduled(fixedDelay = 3000)
public void send() throws JMSException {
System.out.println("开始发送-------------------------------");
Connection connection = activeMQConnectionFactory.createConnection();
connection.start();
Session session = connection.createSession(Boolean.TRUE, Session.AUTO_ACKNOWLEDGE);
Destination destination = session.createQueue("test");
MessageProducer producer = session.createProducer(destination);
ObjectMessage objMsg = session.createObjectMessage();
objMsg.setObject("hello mq");
producer.send(objMsg);
session.commit();
session.close();
}
}

最后创建包start,包下创建Start.java文件,内容如下:

@EnableAutoConfiguration
@ComponentScan(basePackages = "com.mq.demo")
@Configuration
public class Start {

public static void main(String[] args) {
SpringApplication.run(Start.class, args);
}
}

时间: 2024-10-01 03:14:33

springboot整合activemq小demo的相关文章

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

集成tomcat就是随项目启动而启动tomcat,最简单的方法就是监听器监听容器创建之后以Broker的方式启动ActiveMQ. 1.web项目中Broker启动的方式进行集成 在这里采用Listener监听ServletContext创建和销毁进行Broker的启动和销毁. 0.需要的jar包: 1.listener实现ServletContextListener接口 package cn.qlq.listener; import javax.servlet.ServletContextEv

解决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

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整合微信小程序登录

1. 开发前准备 1.1 前置知识 java基础 SpringBoot简单基础知识 1.2 环境参数 开发工具:IDEA 基础环境:Maven+JDK8 所用技术:SpringBoot.lombok.mybatisplus.微信小程序 SpringBoot版本:2.1.4 1.3 涉及知识点 微信小程序登录流程 2. 微信小程序登录流程 微信小程序登录流程涉及到三个角色:小程序.开发者服务器.微信服务器 三者交互步骤如下: 第一步:小程序通过wx.login()获取code. 第二步:小程序通过

SpringBoot整合ActiveMQ实现持久化

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

struts2和hibernate整合的小Demo

jar包下载地址 创建一个web项目. 导入jar包 配置web.xml <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="

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

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