ActiveMQ使用spring JmsTemplate发送消息(一)
下载地址:http://activemq.apache.org/download-archives.html
解压 启动bin\activemq.bat
生产者接口:
public interface ProducerService {/** * 向指定队列发送消息 */ void sendMessage(Destination destination, final String msg); /** * 向默认队列发送消息 */ void sendMessage(final String msg); }
消费者接口:
public interface ConsumerService {/** * 接受消息 */ void receive(Destination destination); }
生产者实现:
@Service("producerService") public class ProducerServiceImpl implements ProducerService {@Resource private JmsTemplate jmsTemplate; /** * 向指定队列发送消息 */ public void sendMessage(Destination destination, final String msg) {System.out.println("向队列" + destination.toString() + "发送了消息------------" + msg); jmsTemplate.send(destination, new MessageCreator() {public Message createMessage(Session session) throws JMSException {return session.createTextMessage(msg); }}); }/** * 向默认队列发送消息 */ public void sendMessage(final String msg) {String destination = jmsTemplate.getDefaultDestination().toString(); System.out.println("向队列" +destination+ "发送了消息------------" + msg); jmsTemplate.send(new MessageCreator() {public Message createMessage(Session session) throws JMSException {return session.createTextMessage(msg); }}); } }
消费者实现:
@Service("consumerService") public class ConsumerServiceImpl implements ConsumerService {@Resource private JmsTemplate jmsTemplate; /** * 接受消息 */ public void receive(Destination destination) {TextMessage tm = (TextMessage) jmsTemplate.receive(destination); try {System.out.println("从队列" + destination.toString() + "收到了消息:\t" + tm.getText()); } catch (JMSException e) {e.printStackTrace(); }} }
maven:
<dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> </dependency> <dependency> <groupId>com.hankcs</groupId> <artifactId>hanlp</artifactId> <version>portable-1.3.2</version> </dependency> <dependency> <groupId>org.apache.activemq</groupId> <artifactId>activemq-all</artifactId> <version>5.11.0</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jms</artifactId> <version>4.1.4.RELEASE</version> </dependency> </dependencies>
spring:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd"> <!-- spring 注解支持 --> <context:annotation-config /> <context:component-scan base-package="com.activemq.service" /> <!-- 配置JMS连接工厂 --> <bean id="connectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory"> <property name="brokerURL" value="failover:(tcp://localhost:61616)" /> </bean> <!-- 定义消息队列(Queue) --> <bean id="queueDestination" class="org.apache.activemq.command.ActiveMQQueue"> <!-- 设置消息队列的名字 --> <constructor-arg> <value>queue1</value> </constructor-arg> </bean> <!-- 配置JMS模板(Queue),Spring提供的JMS工具类,它发送、接收消息。 --> <bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate"> <property name="connectionFactory" ref="connectionFactory" /> <property name="defaultDestination" ref="queueDestination" /> <property name="receiveTimeout" value="10000" /> </bean> </beans>
测试:
@Test public void producerTest(){// 加载spring ClassPathXmlApplicationContext springContext = SpringContainer.getSpringContext(); ProducerService producerService = (ProducerService)springContext.getBean("producerService"); ConsumerService consumerService = (ConsumerService)springContext.getBean("consumerService"); Destination destination = (Destination)springContext.getBean("queueDestination"); producerService.sendMessage("测试消息队列"); consumerService.receive(destination); }
public class SpringContainer {public static final String DEFAULT_SPRING_CONFIG = "classpath:META-INF/spring/*.xml"; public static ClassPathXmlApplicationContext getSpringContext() {return MySpringContainer.context; }private static class MySpringContainer{private static ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(DEFAULT_SPRING_CONFIG.split("[,\\s]+")); } }
ActiveMQ使用spring JmsTemplate发送消息(一)相关推荐
- JMSTemplate发送消息
操作步骤 第一步:初始化一个spring容器 第二步:从容器中获得JMSTemplate对象. 第三步:从容器中获得一个Destination对象 第四步:使用JMSTemplate对象发送消息,需要 ...
- activeMQ,spring的jmstemplate简单例子
1.使用的是maven的结构,导入activeMQ的包 [html] view plain copy <dependency> <groupId>org.apache.acti ...
- activemq后台管理 看topic消息_「Java」 - SpringBoot amp; ActiveMQ
一.消息队列 消息队列中间件是分布式系统中重要的组件,主要解决应用耦合.异步消息.流量削锋等问题,实现高性能.高可用.可伸缩和最终一致性架构,是大型分布式系统不可缺少的中间件. 目前在生产环境中使用较 ...
- ActiveMQ学习总结(10)——ActiveMQ采用Spring注解方式发送和监听
对于ActiveMQ消息的发送,原声的api操作繁琐,而且如果不进行二次封装,打开关闭会话以及各种创建操作也是够够的了.那么,Spring提供了一个很方便的去收发消息的框架,spring jms.整合 ...
- ActiveMQ结合Spring收发消息
直接使用 ActiveMQ 的方式需要重复写很多代码,且不利于管理,Spring 提供了一种更加简便的方式----Spring JMS ,通过它可以更加方便地使用 ActiveMQ.Maven 依赖结 ...
- 如何在优雅地Spring 中实现消息的发送和消费
本文将对rocktmq-spring-boot的设计实现做一个简单的介绍,读者可以通过本文了解将RocketMQ Client端集成为spring-boot-starter框架的开发细节,然后通过一个 ...
- activeMQ高并发发送消息异常解决方法
高并发发送消息异常解决方法: 现象:使用10个线程每100ms发送一条消息,大约3000多条后,出现异常,所有线程停 止: javax.jms.JMSException:Could not conne ...
- 如何在优雅地Spring 中实现消息的发送和消费 1
本文将对rocktmq-spring-boot的设计实现做一个简单的介绍,读者可以通过本文了解将RocketMQ Client端集成为spring-boot-starter框架的开发细节,然后通过一个 ...
- ibm linux mq 发送消息_RabbitMq、ActiveMq、Kafka和Redis做Mq对比
一.RabbitMq RabbitMQ是一个Advanced Message Queuing Protocol(AMQP)的开源实现,由以高性能.可伸缩性出名的Erlang写成.RabbitMQ Se ...
最新文章
- 力扣第三题java_LeetCode 题解 | 力扣杯 LCP 06. 拿硬币
- Struts09---验证框架
- 关于printf()与自增自减运算符结和问题
- Effective Java之利用有限制通配符提升API的灵活性(二十八)
- linux c 静态连接,Linux cmake 静态链接boost
- 剑三哪些插件是必备的_盘点那些年用过的神级CAD插件,每一款都舍不得卸载
- 手机上做c语言作业的软件下载,手机c语言编程软件
- Java关键字及其作用详解
- FPGA--串口通信基础知识
- Qunee学习开发体会
- 利用路由器实现VLAN
- 交换机和路由器技术-24-OSPF单区域配置
- 恶意代码检测理论(静态与动态分析基础)
- C++学习(四一七)AndroidStudio中的libs和jniLibs
- Android集成高德地图实现自定义Marker
- Fiona简介及Shapefile数据读取
- Building designing
- Win7开启wifi热点
- 我想当计算机工程师英语翻译,工程师英语怎么说
- 【JDK】win 10 / win 11:jdk 8 升级为 jdk 17
热门文章
- 学JAVA要学redis_新手学习Java对Redis简单操作
- 有关使用sklearn LogisticRegression出现的 DeprecationWarning:
- (王道408考研数据结构)第六章图-第二节1:图的存储结构(邻接矩阵、邻接表、十字链表和邻接多重表)
- 发现个Asp.net英文Blog,嘿嘿,刚好对俺学e文有用:)
- CLOUD信用管理设置
- [转]让iframe自适应高度-真正解决
- 【洛谷P4315】月下“毛景树”(树链剖分)
- Java语言程序设计(基础篇) 第二章
- Codeforces632E Thief in a Shop(NTT + 快速幂)
- Hibernate与MyBatis区别