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

环境: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

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

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 6
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值