环境:JDK1.8、MAVEN 3.6.1 、eclipse
1.添加artemis依赖
当前的
pom
文件如下:
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.22.RELEASE</version>
<relativePath />
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- amqp 消息队列的配置 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-artemis</artifactId>
</dependency>
<!-- junit配置 -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
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
以上纯属个人见解,如有问题请联系本人!