Spring集成JMS
来源:互联网 发布:路由器主人网络关了 编辑:程序博客网 时间:2024/05/29 05:54
一.:要在spring下集成ActiveMQ,那么就需要将jar包导入项目,所需jar包如下:
这些jar包来自“apache-activemq-5.6.0\lib”中。
二:相关的实例
1.实例--1
1):.配置Spring与ActiveMQ的配置文件:
- <?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:context="http://www.springframework.org/schema/context"
- xsi:schemaLocation="http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
- http://www.springframework.org/schema/context
- http://www.springframework.org/schema/context/spring-context-2.5.xsd"
- default-autowire="byName">
- <!-- 配置connectionFactory -->
- <bean id="jmsFactory" class="org.apache.activemq.pool.PooledConnectionFactory"
- destroy-method="stop">
- <property name="connectionFactory">
- <bean class="org.apache.activemq.ActiveMQConnectionFactory">
- <property name="brokerURL">
- <value>tcp://127.0.0.1:61616</value>
- </property>
- </bean>
- </property>
- <property name="maxConnections" value="100"></property>
- </bean>
- <!-- Spring JMS Template -->
- <bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
- <property name="connectionFactory">
- <ref local="jmsFactory" />
- </property>
- <property name="defaultDestinationName" value="subject" />
- <!-- 区别它采用的模式为false是p2p为true是订阅 -->
- <property name="pubSubDomain" value="true" />
- </bean>
- <!-- 发送消息的目的地(一个Topic) -->
- <bean id="destination" class="org.apache.activemq.command.ActiveMQTopic">
- <!-- 设置消息队列的名字 -->
- <constructor-arg index="0" value="subject" />
- </bean>
- <bean id="messageReceiver" class="com.pwc.spring.jms.example.JMSReceiver">
- <property name="jmsTemplate" ref="jmsTemplate"></property>
- </bean>
- </beans>
- package com.pwc.spring.jms.example;
- import javax.jms.Destination;
- import javax.jms.JMSException;
- import javax.jms.Message;
- import javax.jms.Session;
- import org.springframework.context.ApplicationContext;
- import org.springframework.context.support.ClassPathXmlApplicationContext;
- import org.springframework.jms.core.JmsTemplate;
- import org.springframework.jms.core.MessageCreator;
- public class JMSSender {
- /**
- * @param args
- */
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- ApplicationContext applicationContext=new ClassPathXmlApplicationContext("applicationContext-jms.xml");
- JmsTemplate template=(JmsTemplate)applicationContext.getBean("jmsTemplate");
- Destination destination=(Destination)applicationContext.getBean("destination");
- template.send(destination, new MessageCreator(){
- @Override
- public Message createMessage(Session session) throws JMSException {
- // TODO Auto-generated method stub
- return session.createTextMessage("Send Message:Hello ActiveMQ Text Message!");
- }
- });
- System.out.println("Send the JMS Message Successfully!");
- }
- }
A:消息接收的入口:
- package com.pwc.spring.jms.example;
- import org.springframework.context.ApplicationContext;
- import org.springframework.context.support.ClassPathXmlApplicationContext;
- public class JMSReceiverMain {
- /**
- * @param args
- */
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- ApplicationContext applicationContext=new ClassPathXmlApplicationContext("applicationContext-jms.xml");
- JMSReceiver proxyJMSConsumer=(JMSReceiver)applicationContext.getBean("messageReceiver");
- proxyJMSConsumer.receive();
- System.out.println("Initial Consumer End! ");
- }
- }
- package com.pwc.spring.jms.example;
- import javax.jms.Destination;
- import javax.jms.JMSException;
- import javax.jms.TextMessage;
- import org.springframework.context.ApplicationContext;
- import org.springframework.context.support.ClassPathXmlApplicationContext;
- import org.springframework.jms.JmsException;
- import org.springframework.jms.core.JmsTemplate;
- public class JMSReceiver {
- public JMSReceiver(){
- }
- private JmsTemplate jmsTemplate;
- public JmsTemplate getJmsTemplate() {
- return jmsTemplate;
- }
- public void setJmsTemplate(JmsTemplate jmsTemplate) {
- this.jmsTemplate = jmsTemplate;
- }
- public void receive(){
- ApplicationContext applicationContext=new ClassPathXmlApplicationContext("applicationContext-jms.xml");
- Destination destination=(Destination)applicationContext.getBean("destination");
- while(true){
- try {
- TextMessage txtmsg=(TextMessage)jmsTemplate.receive(destination);
- if(null!=txtmsg){
- System.out.println("DB Proxy: "+txtmsg);
- System.out.println("DB Proxy Message: "+txtmsg.getText());
- }else{
- break;
- }
- } catch (JmsException e) {
- e.printStackTrace();
- } catch (JMSException e) {
- e.printStackTrace();
- }
- }
- }
- }
运行结果:
2. 实例--2
1)Spring与ActiveMQ配置文件信息:
- <?xml version="1.0" encoding="UTF-8"?>
- <beans
- xmlns="http://www.springframework.org/schema/beans"
- 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-2.0.xsd">
- <!--创建连接工厂-->
- <bean id="connectionFactory"
- class="org.apache.activemq.ActiveMQConnectionFactory">
- <property name="brokerURL" value="tcp://localhost:61616"></property>
- </bean>
- <!-- 声明ActiveMQ消息目标,目标可以是一个队列,也可以是一个主题ActiveMQTopic-->
- <bean id="destination" class="org.apache.activemq.command.ActiveMQQueue">
- <constructor-arg index="0" value="springactivemq"></constructor-arg>
- </bean>
- <!---->
- <bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
- <property name="connectionFactory" ref="connectionFactory"></property>
- <property name="defaultDestination" ref="destination"></property>
- <property name="receiveTimeout" value="600"></property>
- </bean>
- <bean id="sender" class="com.pwc.spring.jms.further.Sender">
- <property name="jmsTemplate" ref="jmsTemplate"></property>
- </bean>
- <bean id="receiver" class="com.pwc.spring.jms.further.Receiver">
- <property name="jmsTemplate" ref="jmsTemplate"></property>
- </bean>
- </beans>
A:发送消息的入口
- package com.pwc.spring.jms.further;
- import org.springframework.context.ApplicationContext;
- import org.springframework.context.support.ClassPathXmlApplicationContext;
- public class SenderMain {
- /**
- * @param args
- */
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
- Sender sender=(Sender)context.getBean("sender");
- sender.sendInfo();
- }
- }
- package com.pwc.spring.jms.further;
- import javax.jms.JMSException;
- import javax.jms.MapMessage;
- import javax.jms.Message;
- import javax.jms.Session;
- import org.springframework.jms.core.JmsTemplate;
- import org.springframework.jms.core.MessageCreator;
- public class Sender {
- private JmsTemplate jmsTemplate;
- public JmsTemplate getJmsTemplate() {
- return jmsTemplate;
- }
- public void setJmsTemplate(JmsTemplate jmsTemplate) {
- this.jmsTemplate = jmsTemplate;
- }
- public void sendInfo(){
- jmsTemplate.send(new MessageCreator(){
- @Override
- public Message createMessage(Session session) throws JMSException {
- MapMessage message=session.createMapMessage();
- message.setString("lastName", "Leio");
- return message;
- }
- });
- }
- }
A:接收消息入口:
- package com.pwc.spring.jms.further;
- import org.springframework.context.ApplicationContext;
- import org.springframework.context.support.ClassPathXmlApplicationContext;
- public class ReceiverMain {
- /**
- * @param args
- */
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
- Receiver receiver=(Receiver) context.getBean("receiver");
- System.out.println(receiver.receiveMessage());
- }
- }
- package com.pwc.spring.jms.further;
- import javax.jms.JMSException;
- import javax.jms.MapMessage;
- import org.springframework.jms.core.JmsTemplate;
- public class Receiver {
- private JmsTemplate jmsTemplate;
- public JmsTemplate getJmsTemplate() {
- return jmsTemplate;
- }
- public void setJmsTemplate(JmsTemplate jmsTemplate) {
- this.jmsTemplate = jmsTemplate;
- }
- public Receiver(){
- }
- public String receiveMessage(){
- String receiveMessage="";
- MapMessage message=(MapMessage)jmsTemplate.receive();
- try {
- receiveMessage=message.getString("lastName");
- } catch (JMSException e) {
- e.printStackTrace();
- }
- return receiveMessage;
- }
- }
0 0
- Spring集成JMS
- Spring集成JMS .
- Spring集成JMS
- Spring集成JMS
- Spring集成JMS
- spring集成jms用法
- Spring集成JMS(二).
- JMS 集成Spring 实现ActiveMQ
- Spring JMS和ActiveMQ集成
- jms activeMQ与spring集成进阶篇
- jms activeMQ与spring的集成
- jms activeMQ与spring的集成
- spring学习:与jms的集成
- jms activeMQ与spring集成进阶篇
- spring集成redis和jms时注意
- spring学习:与jms的集成
- ActiveMQ(二)-Spring集成JMS
- Spring 里JMS和DB的事务集成
- hdu 2079 选课时间
- WAS集群:记一次Node Agent不活动问题解决过程
- java-jvm电子书合集
- portal 的基础设施
- R语言常用包分类
- Spring集成JMS
- Maven权威指南-电子书合集
- Quartz 2D 详细解说
- ubuntu root默认密码(初始密码)
- Android与与服务器交换数据、上传、下载、读取文件
- MySQL and MariaDB 电子书合集
- 巧用PyUnit中unittest特性解决接口测试产生脏数据问题
- 渣渣ACM日记——1131-买水果(NYOJ)
- MFC 子窗口全屏显示扩展控件