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萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章