SpringBoot整合ActiveMQ的p2p模式(一)

1.安装ActiveMQ

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

(1)在Windows上在解压后有个bin下有win64,win64目录下有个activemq.bat,双击activemq.bat启动

 

 

 

(2)访问http://127.0.0.1:8161/

 

 

 (3)输入用户名:admin   密码:admin

(4)看到ActiveMQ这就启动成功了

 

p2p的过程则理解起来更加简单。它好比是两个人打电话,这两个人是独享这一条通信链路的。一方发送消息,另外一方接收,就这么简单。在实际应用中因为有多个用户对使用p2p的链路。

 

2.SpringBoot整合ActiveMQ

(1)这是我的项目结构

 

 

 (2)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.qingfeng</groupId>
	<artifactId>springboot-ActiveMQ</artifactId>
	<version>0.0.1-SNAPSHOT</version>

	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.1.1.RELEASE</version>
		<relativePath /> <!-- lookup parent from repository -->
	</parent>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
		<java.version>1.8</java.version>
	</properties>

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

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

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

			<!-- 热部署 -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-devtools</artifactId>
		</dependency>

	</dependencies>


	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>

</project>

 

(3)application.properties文件配置

server.port=8088
server.servlet.context-path=/springboot-ActiveMQ/
#不能用127.0.0.1,不然会报错
#spring.activemq.broker-url=tcp://127.0.0.1:61616  
spring.activemq.broker-url=tcp://localhost:61616
spring.activemq.in-memory=true  
spring.activemq.pool.enabled=false

  

(4)SpringBoot启动类

package com.qingfeng;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class AppStart {

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

  

(5)生产者Producer类

package com.qingfeng.producer;

import java.util.Map;

import javax.jms.Destination;

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

/**
 *生产者
 */
@Service
public class Producer {

	// 也可以注入JmsTemplate,JmsMessagingTemplate对JmsTemplate进行了封装 
	@Autowired
	private JmsMessagingTemplate jmsMessagingTemplate;


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

	// 发送消息,destination是发送到的队列名称,map类型的发送的消息  
		public void sendMapMessage(Destination destination, Map<String ,String> map){  
			jmsMessagingTemplate.convertAndSend(destination, map);  
		}  
	
}

  

(6)消费者Consumer类

package com.qingfeng.consumer;

import java.util.Map;

import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Component;
/**
 *消费者
 */
@Component
public class Consumer {

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

	// 使用JmsListener配置消费者监听的队列名称qingfeng-map-queue,其中map是接收到的消息 
	@JmsListener(destination = "qingfeng-map-queue")  
	public void receiveMapQueue(Map<String ,String> map) {  
		System.out.println("Consumer收到的生产者map类型的报文为:"+map);  
	}  
	
}

  

(7)测试类TestController

 

package com.qingfeng.controller;

import java.util.HashMap;
import java.util.Map;

import javax.jms.Destination;

import org.apache.activemq.command.ActiveMQQueue;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import com.qingfeng.producer.Producer;

@RestController
public class TestController {

	@Autowired
	private Producer producer;

	//   http://localhost:8088/springboot-ActiveMQ/send?text=qingfeng
	@GetMapping("send")
	public String  send(String text){
		//创建一个队列名称为qingfeng-queue
		Destination destination = new ActiveMQQueue("qingfeng-queue");
		//调用生产者产生消息
		producer.sendMessage(destination, text);
		return "发送成功";
	}
	
//  http://localhost:8088/springboot-ActiveMQ/sendMap
	@GetMapping("sendMap")
	public String  sendMap(){
		//创建一个队列名称为qingfeng-map-queue
		Destination destination = new ActiveMQQueue("qingfeng-map-queue");
		HashMap<String, String> map = new HashMap<>();
		map.put("name", "qingfeng");
		map.put("age", "18");
		map.put("QQ", "37942135");
		//调用生产者产生消息
		producer.sendMapMessage(destination, map);
		return "发送成功";
	}
	
}

 

  

(8)启动项目AppStart类

测试:http://localhost:8088/springboot-ActiveMQ/send?text=qingfeng

 

 

 (9)在控制台上输出

 

 (10)测试:http://localhost:8088/springboot-ActiveMQ/sendMap

 

 (10)在控制台上输出

 

 

(11)可以看到activemq就多了两个qingfeng-queue队列和qingfeng-map-queue队列了

 

 

SpringBoot整合activemq就完成了。

 

posted @ 2020-05-15 20:05  Amy清风  阅读(496)  评论(0编辑  收藏  举报