activeMQ,spring的jmstemplate简单例子
来源:互联网 发布:以色列粮食进口数据 编辑:程序博客网 时间:2024/06/03 02:11
1.使用的是maven的结构,导入activeMQ的包
<dependency> <groupId>org.apache.activemq</groupId> <artifactId>activemq-core</artifactId> <version>5.5.0</version></dependency>
2.创建“生产者”
①xml配置文件
<!-- 创建工厂连接 --><bean id="connectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory"><property name="brokerURL" value="tcp://localhost:61616" /></bean><!-- activeMQ消息目标 队列 --><bean id="rantzDestination" class="org.apache.activemq.command.ActiveMQQueue"><constructor-arg index="0" value="rantz.marketing.queue"></constructor-arg></bean><!-- activeMQ消息目标 主题--> <!--<bean id="rantzDestination" class="org.apache.activemq.command.ActiveMQTopic">--> <!--<constructor-arg index="0" value="rantz.marketing.queue"></constructor-arg>--> <!--</bean>--><bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate"><property name="connectionFactory" ref="connectionFactory" /></bean><bean id="marketingGateway" class="com.jms.service.RantzMarketingGatewayImpl"><property name="jmsTemplate" ref="jmsTemplate" /><property name="destination" ref="rantzDestination" /></bean>
②java代码
主类SpringMain
public static void main(String[] args) {/*开始加载spring配置文件*/ApplicationContext context = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");IRantzMarketingGateway rantzMarketingGateway= (RantzMarketingGatewayImpl) context.getBean("marketingGateway");rantzMarketingGateway.sendMotoristInfo();System.out.println("Start ...");}
消息发送类RantzMarketingGatewayImpl
import javax.jms.Destination;import javax.jms.JMSException;import javax.jms.Message;import javax.jms.Session;import org.springframework.jms.core.JmsTemplate;import org.springframework.jms.core.MessageCreator;public class RantzMarketingGatewayImpl implements IRantzMarketingGateway {private JmsTemplate jmsTemplate;private Destination destination;public JmsTemplate getJmsTemplate() {return jmsTemplate;}public void setJmsTemplate(JmsTemplate jmsTemplate) {this.jmsTemplate = jmsTemplate;}public Destination getDestination() {return destination;}public void setDestination(Destination destination) {this.destination = destination;}public void sendMotoristInfo(){jmsTemplate.send(destination,new MessageCreator(){public Message createMessage(Session session) throws JMSException {return session.createTextMessage("这是一个测试");}});}}
3.创建“消费者”
①xml配置文件
<!-- 创建工厂连接 --><bean id="connectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory"><property name="brokerURL" value="tcp://localhost:61616" /></bean><!-- activeMQ消息目标 队列 --><bean id="rantzDestination" class="org.apache.activemq.command.ActiveMQQueue"><constructor-arg index="0" value="rantz.marketing.queue"></constructor-arg></bean><!-- activeMQ消息目标 主题--><!--<bean id="rantzDestination" class="org.apache.activemq.command.ActiveMQTopic">--><!--<constructor-arg index="0" value="rantz.marketing.queue"></constructor-arg>--><!--</bean>--><bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate"><property name="connectionFactory" ref="connectionFactory" /><property name="defaultDestination" ref="rantzDestination" /></bean><bean id="marketingGateway" class="com.huateng.jms.service.MarketingReceiverGatewayImpl"><property name="jmsTemplate" ref="jmsTemplate" /></bean>
②java代码
主类SpringMain
public static void main(String[] args) {/*开始加载spring配置文件*/ApplicationContext context = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");MarketingReceiverGatewayImpl rantzMarketingGateway= (MarketingReceiverGatewayImpl) context.getBean("marketingGateway");System.out.println("Receive Start ...");try {rantzMarketingGateway.receiveMotorist();} catch (Exception e) {e.printStackTrace();} }
消息发送类MarketingReceiverGatewayImpl
import javax.jms.TextMessage;import org.springframework.jms.core.JmsTemplate;public class MarketingReceiverGatewayImpl {private JmsTemplate jmsTemplate;public JmsTemplate getJmsTemplate() {return jmsTemplate;}public void setJmsTemplate(JmsTemplate jmsTemplate) {this.jmsTemplate = jmsTemplate;}public MarketingReceiverGatewayImpl() {}public void receiveMotorist() throws Exception{TextMessage message = (TextMessage)jmsTemplate.receive();System.out.println("reviced msg is:" + message.getText());}}
4.启动activeQM
在下载的activeQM包中的bin目录中,可以找到一个用于启动activeMQ的脚本。unix用户使用activemq;windows用户使用activemq.bat
5.先运行"生产者",再运行"消费者"
6.结果
Start ...
Receive Start ...
reviced msg is:这是一个测试
- activeMQ,spring的jmstemplate简单例子
- activeMQ,spring的jmstemplate简单例子
- activeMQ,spring的jmstemplate简单例子
- activeMQ,spring的jmstemplate简单例子
- activeMQ,spring的jmstemplate简单例子
- activeMQ,spring的jmstemplate简单例子
- spring的jmsTemplate
- activemq的连接池,通过spring的JmsTemplate发送消息到指定的Destination
- activemq的连接池,通过spring的JmsTemplate发送消息到指定的Destination
- 学习淘淘商城第六十课(Spring与Activemq的整合及用JmsTemplate发送消息)
- 淘淘商城系列——Spring与ActiveMQ的整合及用JmsTemplate发送消息
- spring JmsTemplate的sendAndReceive方法
- ActiveMQ 一个简单的例子
- 结合spring使用jmsTemplate简单消息队列
- ActiveMQ使用spring JmsTemplate发送消息(一)
- ActiveMQ使用spring JmsTemplate生成和订阅消息(二)
- Spring与ActiveMQ整合及用JmsTemplate发送消息
- ActiveMQ使用spring JmsTemplate发送消息(一)
- mysql_init()线程安全问题
- 前端工程师的价值体现在哪里?
- 内核的同步机制(原子锁)
- 第23贴:电阻 & 电容
- QT中 窗口部件的 背景图片 的设置
- activeMQ,spring的jmstemplate简单例子
- javadoc
- 关于团队激励的理念和实践
- .net面试题目
- CentOS常用用户管理命令
- POJ 1151 扫描线+线段树+离散化
- eclipse 开发JNI
- 第24贴:经典接法三极管电路的静态计算
- .net基础问题