ActiveMQ 八 (实战三)


 一 , 消息的发送者(产生者)

package com.xiu.jms.consumer;

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 HelloSender {

public static void main(String[] args) {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext(
new String[] { "classpath:spring-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 {
return session
.createTextMessage("发送消息:hello world activeMQ....");

}
});

System.out.println("成功发送了一条JMS消息");
}

}


二, 消息的接受者(消费者)

package com.xiu.jms.consumer;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;


import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jms.core.JmsTemplate;
public class ProxyJMSConsumer implements MessageListener{
private ProxyJMSConsumer() {
}
private JmsTemplate jmsTemplate;


public JmsTemplate getJmsTemplate() {
return jmsTemplate;
}
public void setJmsTemplate(JmsTemplate jmsTemplate) {
this.jmsTemplate = jmsTemplate;
}
@Override
public void onMessage(Message arg0) {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext(
new String[] { "classpath:spring-jms.xml" });
Destination destination = (Destination) applicationContext
.getBean("destination");
while (true) {
TextMessage txtmsg = (TextMessage) jmsTemplate.receive(destination);

if (null != txtmsg) {


System.out.println("[DB Proxy]" + txtmsg);
try {
System.out.println("[DB Proxy]收到的消息内容为" + txtmsg.getText());
} catch (JMSException e) {
}


}
}

}

}


三   spring-jms.xml的配置文件


<?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:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
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
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd"
default-autowire="byName">
<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://10.188.53.76:61616">
</property>
</bean>
</property>
<property name="maxConnections" value="100"></property>
</bean>
<bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
<property name="connectionFactory" ref="jmsFactory"></property>
<property name="defaultDestinationName" value="subject"></property>
区别采用的模式为false是p2p,true为topic
<property name="pubSubDomain" value="true"></property>
</bean>
发送消息的目的地(一个队列)
<bean id="destination" class="org.apache.activemq.command.ActiveMQTopic">
<constructor-arg index="0" value="subject" />
</bean>
消息监听器
<bean id="listenerContainer"
class="org.springframework.jms.listener.DefaultMessageListenerContainer">
<property name="concurrentConsumers" value="10"></property>
<property name="connectionFactory" ref="jmsFactory"></property>
<property name="destinationName" value="subject"></property>
<property name="messageListener" ref="messageReceiver"></property>
<property name="pubSubNoLocal" value="false"></property>
</bean>
<bean id="messageReceiver" class="com.xiu.jms.consumer.ProxyJMSConsumer">
<property name="jmsTemplate" ref="jmsTemplate"></property>
</bean>

</beans>


四,测试类

package com.xiu.jms.consumer;


import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;


public class JMSTest {

public static void main(String[] args) {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext(
new String[] { "classpath:spring-jms.xml" });

ProxyJMSConsumer proxyJMSConsumer = (ProxyJMSConsumer) applicationContext.getBean("messageReceiver");
System.out.println("初始化消息消费者");
}


}

发布了41 篇原创文章 · 获赞 4 · 访问量 3万+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章