activeMQ学习笔记 -- day03 整合spirng
来源:互联网 发布:java定位内存泄露 编辑:程序博客网 时间:2024/06/05 02:07
一、配置生产者
1、生产者整合spring时的配置文件
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"xmlns:context="http://www.springframework.org/schema/context"xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"xmlns:task="http://www.springframework.org/schema/task"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsdhttp://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.0.xsd"><!-- connectionFactory --><bean id="targetConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory"><constructor-arg name="brokerURL" value="tcp://192.168.200.134:61616" /></bean><bean id="connectionFactory" class="org.springframework.jms.connection.SingleConnectionFactory"><!-- 目标连接工厂 --><property name="targetConnectionFactory" ref="targetConnectionFactory"></property></bean><!-- JMSTemplate --><bean class="org.springframework.jms.core.JmsTemplate"><!-- connectionFactory对应的是我们自己定义的spring提供的ide为connectionFactory的对象 --><property name="connectionFactory" ref="connectionFactory"></property></bean><!-- Destination --><bean id="testQueue" class="org.apache.activemq.command.ActiveMQQueue"><constructor-arg value="test-spring-queue"></constructor-arg></bean></beans>
2、生产者测试代码
@Testpublic void testSpring4QueueProducer(){ApplicationContext app = new ClassPathXmlApplicationContext("classpath:spring/applicationContext-activemq.xml");JmsTemplate jmsTemplate = app.getBean(JmsTemplate.class);Destination destination = app.getBean(Destination.class);jmsTemplate.send(destination,new MessageCreator() {@Overridepublic Message createMessage(Session session) throws JMSException {return session.createTextMessage("测试spring与activemq整合");}});}
二、配置消费者
1、消费者整合spirng的配置文件
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"xmlns:context="http://www.springframework.org/schema/context"xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"xmlns:task="http://www.springframework.org/schema/task"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsdhttp://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.0.xsd"><!-- connectionFactory --><bean id="targetConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory"><constructor-arg name="brokerURL" value="tcp://192.168.200.134:61616" /></bean><bean id="connectionFactory" class="org.springframework.jms.connection.SingleConnectionFactory"><!-- 目标连接工厂 --><property name="targetConnectionFactory" ref="targetConnectionFactory"></property></bean><!-- Destination --><bean id="testQueue" class="org.apache.activemq.command.ActiveMQQueue"><constructor-arg value="test-spring-queue"></constructor-arg></bean><!-- 自定义的MessageListener --><bean id="myMessageListener" class="cn.e3mall.search.listener.MyMessageListener"></bean><!-- Message容器 MessageListenerContainer --><bean id="myMessageContainer" class="org.springframework.jms.listener.DefaultMessageListenerContainer"><property name="connectionFactory" ref="connectionFactory"></property><property name="destination" ref="testQueue"></property><property name="messageListener" ref="myMessageListener"></property></bean></beans>
2、消费者中消息监听器
package cn.e3mall.search.listener;import javax.jms.Message;import javax.jms.MessageListener;import javax.jms.TextMessage;public class MyMessageListener implements MessageListener {/** * 消息处理方法 */public void onMessage(Message message) {try {//接收消息if(message instanceof TextMessage){TextMessage textMessage = (TextMessage) message;System.out.println("接收到的消息是:" + textMessage.getText());}} catch (Exception e) {e.printStackTrace();}}}
3、消费者测试代码
@Testpublic void testSpring4QueueConsumer() throws Exception{ApplicationContext app = new ClassPathXmlApplicationContext("classpath:spring/applicationContext-activemq.xml");System.in.read();}
阅读全文
0 0
- activeMQ学习笔记 -- day03 整合spirng
- Maven学习笔记 -- day03 Maven整合SSH
- struts2学习笔记 -- day03 struts2整合hibernate环境搭建
- Spring学习笔记 - Spring 整合 JMS - ActiveMQ
- java 学习笔记 day03
- JQuery学习笔记--Day03
- C++学习笔记 day03
- mysql 学习笔记 day03
- Java学习笔记 Day03
- 基础学习day03笔记
- Oracle学习笔记--day03
- Java学习笔记Day03
- struts2 day03学习笔记
- spirng cloud学习笔记-Zuul
- Mybatis学习笔记---day03缓存
- python学习笔记day03 Number
- ActiveMQ、Spring整合学习
- ActiveMQ、Spring整合学习
- QPushButton使用样例
- $JAVA_HOME
- 时间是选择
- Qt各个版本和插件的下载方法
- 宏函数为什么使用do{...}while{0}包起来
- activeMQ学习笔记 -- day03 整合spirng
- 分布式事务操作之Spring +JTA
- [Unity&重装系统]GHOST WIN7 64位系统 安装 之后,安装Microsoft .NET Framework 4安装失败
- 策略模式
- Win10开机无法输入密码
- centos7下安装freeswitch1.6版本,亲测可行
- 解决Setting property 'source' to 'org.eclipse.jst.jee.server的问题
- 专业制作微信小程序
- Tomcat源码分析-catalina.home和catalina.base