SpringBoot中简单使用artemis发送和处理消息

环境:JDK1.8、MAVEN 3.6.1 、eclipse

1.添加artemis依赖

当前的pom文件如下:

    
		UTF-8
	

	
		org.springframework.boot
		spring-boot-starter-parent
		1.5.22.RELEASE
		
	


	
		
			org.springframework.boot
			spring-boot-starter-web
		
		
		
			org.springframework.boot
			spring-boot-starter-artemis
		
		
		
			junit
			junit
			test
		
	

	
		
			
				org.springframework.boot
				spring-boot-maven-plugin
				
					1.8
					1.8
				
			
		
	

2.创建配置类

@Component
public class AetemisConfig {

	@Bean
	public ActiveMQQueue getActiveMQQueue() {
		ActiveMQQueue activeMQQueue = new ActiveMQQueue("myQueue");
		return activeMQQueue;
	}
}

3.创建消息处理者

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

4.创建入口类

/**
 * @description 使用SpringBoot操作artemis
 * @author hy
 * @date 2019-08-13
 */
@RestController
@SpringBootApplication
public class Application {

	@Autowired
	JmsTemplate jmsTemplate;

	@RequestMapping("/send")
	public String send(String msg) {
		jmsTemplate.convertAndSend("myQueue", msg);
		return "【发送消息】:" + msg;
	}

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

5.测试

发现需要启动artemis获取连接才可以,使用当前的项目一直报错误,就是没有获取连接的原因!

6.下载artemis

https://activemq.apache.org/components/artemis/download/

7.安装并启动

借鉴下面这篇博客:
https://www.cnblogs.com/ye-hcj/p/9782970.html

8.编写artemis的相关配置文件

spring.artemis.mode=native
spring.artemis.host=localhost
spring.artemis.port=61616
spring.artemis.user=root
spring.artemis.password=root

发现启动的时候需要.Net XXX版本,当前未测试,但是理论上是可以访问成功的

9.总结

1.感觉当前的用法与ActiveMQ基本上完全一致,但是当前的artemis需要下载启动,并设置密码,当前项目需要配置相关的信息才能使用!

2.基本上都是使用JmsTemplate 模板,ActiveMQQueue、和@JmsListener

以上纯属个人见解,如有问题请联系本人!

你可能感兴趣的:(消息队列,SpringBoot)