SpringBoot中简单的使用ActiveMQ实现发送和处理消息

环境:JDK1.8、MAVEN 3.6.1、eclipse

1.导入ActiveMQ的依赖

当前的pom文件内容如下:

   <parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>1.5.22.RELEASE</version><!-- 由于低版本的不支持ActiveMQ,所以跟换版本 -->
		<relativePath />
	</parent>

	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<!-- ActiveMQ 消息队列的配置 -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-activemq</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.创建ActiveMQ的配置类,并添加消息目的地

当前的ActiveMQConfig内容如下:

/**
 * @description 创建ActiveMQ的配置类
 * @author hy
 * @date 2019-08-13
 */
@Configuration
public class ActiveMQConfig {
	/**
	 * @description 创建发送消息到目的地的队列
	 */
	@Bean
	public ActiveMQQueue getActiveMQQueue() {
	// 通过查看https://activemq.apache.org/spring-support
	// 得知:Spring支持一个方便的抽象,JmsTemplate,它允许您在发送消息时隐藏一些较低级别的JMS细节。

   /**
	 * 通过查看 JmsTemplate的源码得知当前的其中有个send(final Destination destination, final
	 * MessageCreator messageCreator) destination通过中文翻译为:目的地;messageCreator为消息创建者
	 * 通过Destination(这个只是一个接口,什么都没有)获得其子类的实现类为ActiveMQQueue
	 */

		// 创建发送到目的地的队列myQueue
		ActiveMQQueue activeMQQueue = new ActiveMQQueue("myQueue");
		return activeMQQueue;
	}
}

3.创建消息处理类

创建两个GetMessageUser1和GetMessageUser2

/**
 * @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.创建入口类

其中Application.properties的内容如下:

/**
 * @description 使用SpringBoot简单的操作消息队列ActiveMQ
 * @author hy
 * @date 2019-08-13
 */
@SpringBootApplication
@RestController
public class Application {
	@Autowired
	JmsTemplate jmsTemplate;
	// 通过查看https://activemq.apache.org/spring-support
	// 得知:Spring支持一个方便的抽象,JmsTemplate,它允许您在发送消息时隐藏一些较低级别的JMS细节。

	/**
	 * 通过查看 JmsTemplate的源码得知当前的其中有个send(final Destination destination, final
	 * MessageCreator messageCreator) destination通过中文翻译为:目的地;messageCreator为消息创建者
	 * 通过Destination(这个只是一个接口,什么都没有)获得其子类的实现类为ActiveMQQueue
	 */
	//@Autowired
	//Destination destination;// 实际上注入的是ActiveMQQueue这个类型

	@RequestMapping("/send")
	public String send(final String msg) {
		/*
		 * JmsMessagingTemplate jmsMessagingTemplate=new
		 * JmsMessagingTemplate(jmsTemplate);
		 */
		System.out.println(jmsTemplate);
		jmsTemplate.send("myQueue", new MessageCreator() {

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

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

5.测试消息队列

结果显示【发送消息:】你好!张三 成功!
对应的控制台也显示了结果!

6.另外一种发送消息的方式

修改后的send方法:

	@RequestMapping("/send")
	public String send(final String msg) {
		System.out.println(jmsTemplate);
		jmsTemplate.convertAndSend("myQueue", msg); 
		return "【发送消息:】" + msg + " 成功!";
	}

7.测试消息列队

结果跟上面一致!

8.总结

1.关于消息队列中主要使用Spring提供的一个简化的JmsTemplate类来操作

2.需要创建消息的目的地Destination,一般都会用实现类ActiveMQQueue来作为存放消息的目的地

3.需要创建消息队列的消费者:创建类,并在类上添加注解@Component,并创建处理消息的方法,需要与发送的消息类型一致,该方法需要添加@JmsListener(destination = "myQueue"),而且需要指定消息队列的名称

4.发送消息的时候需要使用JmsTemplate来发送消息,既可以使用send并创建ActiveMQTextMessage对象来发送消息,也可以直接使用convertAndSend方法来发送消息

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

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值