SpringBoot之使用ApacheArtemis(ActiveMQ分支)实现发送和处理消息

1.声明

当前内容主要用于本人学习和使用SpringBoot方式操作Artemis(apache-artemis-2.17.0)的基本操作

2.基本pom依赖

<parent>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-parent</artifactId>
	<version>2.1.18.RELEASE</version>
</parent>
<properties>
	<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</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-artemis</artifactId>
	</dependency>
</dependencies>

3.demo

1.application.properties

spring.artemis.mode=native
spring.artemis.host=192.168.1.100
spring.artemis.port=61616
spring.artemis.user=admin
spring.artemis.password=admin

这里的host就是你在创建artemis的broker的时候指定的地址,port为61616和ActiveMQ一致

2.基本的配置类:

import org.apache.activemq.artemis.jms.client.ActiveMQQueue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class ActiveMQConfig {
    
    
	/**
	 * @description 创建发送消息到目的地的队列
	 */
	@Bean
	public ActiveMQQueue getActiveMQQueue() {
    
    
		// 创建发送到目的地的队列myQueue
		ActiveMQQueue activeMQQueue = new ActiveMQQueue("myQueue");
		return activeMQQueue;
	}

}

这里需要注意的是ActiveMQQueue 位于org.apache.activemq.artemis.jms.client这个包下
在这里插入图片描述

3.基本的消息接收者

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

@Component
public class MessageHandler {
    
    
	/**
	 * @description 监听消息的地址,从myQueue中获取数据
	 * @param msg
	 */
	@JmsListener(destination = "myQueue")
	public void massageHandler(String msg) {
    
    
		System.out.println("当前的用户1正在处理消息:" + msg);
	}
}

4.基本的controller

import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Session;
import javax.jms.TextMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class MessageController {
    
    
	@Autowired
	JmsTemplate jmsTemplate;

	
	@RequestMapping("/send")
	public String sendMsg(String msg) {
    
    
		jmsTemplate.send("myQueue", new MessageCreator() {
    
    

			@Override
			public Message createMessage(Session session) throws JMSException {
    
    
				// 通过查看Message的结构树,发现ActiveMQTextMessage可以发送文本消息
				TextMessage textMessage = session.createTextMessage(msg);
				/*
				 * ActiveMQTextMessage textMessage = new ActiveMQTextMessage(session);
				 * textMessage.setText(createTextMessage);
				 */
				return textMessage;
			}
		});
		return "【发送消息:】" + msg + " 成功!";

	}
}

5.基本的入口类:

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
 * @author admin
 * @createTime 2021-03-01 08:18:49
 * @description 当前内容主要用于学习和测试SpringBoot操作另外一个类ActiveMQ的分支Artemis
 * 
 */
@SpringBootApplication
public class SpringBootArtemisApp {
    
    
	public static void main(String[] args) {
    
    
		SpringApplication.run(SpringBootArtemisApp.class, args);
	}
}

4.基本测试

访问ui结果:
在这里插入图片描述
自动创建myQueue,点击该myQueue进入发送消息页面
在这里插入图片描述
点击Send Message
在这里插入图片描述

填写内容并发送
在这里插入图片描述
查看控制台:
在这里插入图片描述

测试controller发送消息:
在这里插入图片描述
在这里插入图片描述

5.总结

1.在springboot的操作下基本上和ActiveMQ差不多,就是包不同了,且发送的Messag创建方式不同了

2.从页面上看artemis的内容更加丰富

猜你喜欢

转载自blog.csdn.net/weixin_45492007/article/details/114250142
今日推荐