一個JMS點對點的小例子,發送方將消息發送至隊列,接收者在隊列中取出消息並且返回一個確認
maven導入:
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jms</artifactId>
<version>4.1.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.apache.xbean</groupId>
<artifactId>xbean-spring</artifactId>
<version>4.4</version>
</dependency>
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-all</artifactId>
<version>5.10.0</version>
</dependency>
發送方的jms配置:
<?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"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:jee="http://www.springframework.org/schema/jee"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/jee
http://www.springframework.org/schema/jee/spring-jee.xsd">
<!-- 配置JMS連接工廠 -->
<bean id="connectionFactory"
class="org.apache.activemq.ActiveMQConnectionFactory">
<property name="brokerURL" value="tcp://localhost:61616" />
</bean>
<!--配置activemq的聯接池 對聯接進行池化操作-->
<bean id="PooledConnectionFactory" class="org.apache.activemq.pool.PooledConnectionFactory">
<property name="connectionFactory" ref="connectionFactory"/>
<!-- 最大聯接數 -->
<property name="maxConnections" value="100"/>
<!-- 最大激活數 -->
<!-- <property name="maximumActive" value="50"/> -->
</bean>
<!--配置spring的聯接工廠,將pooledConnectionFactory,這個聯接工廠將緩存session,生產者和消費者 -->
<bean id="CachingConnectionFactory" class="org.springframework.jms.connection.CachingConnectionFactory">
<constructor-arg ref="PooledConnectionFactory"></constructor-arg>
</bean>
<!--配置spring提供的幫助類jsmTemplate -->
<bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
<constructor-arg ref="CachingConnectionFactory"></constructor-arg>
</bean>
<!--配置發送消息的隊列 -->
<bean id="sendDestination" class="org.apache.activemq.command.ActiveMQQueue">
<constructor-arg value="sendDestinationQueue"></constructor-arg>
</bean>
<!--配置響應消息的隊列 -->
<bean id="replyDestination" class="org.apache.activemq.command.ActiveMQQueue">
<constructor-arg value="replyDestinationQueue"></constructor-arg>
</bean>
<!--配置監聽消費者回送信息的監聽器 -->
<bean id="replayListener" class="com.yc.jms.listener.ReplayListener">
</bean>
<!--配置這個監聽器運行的容器 -->
<bean id="" class="org.springframework.jms.listener.DefaultMessageListenerContainer">
<property name="connectionFactory" ref="CachingConnectionFactory" />
<property name="destination" ref="replyDestination" />
<property name="messageListener" ref="replayListener" />
</bean>
</beans>
發送方法的接口:
package com.yc.biz;
import java.util.Map;
public interface SendMessage {
public void SendMail(Map<String,String> message);
}
實現接口:
package com.yc.biz.impl;
import java.util.Map;
import javax.annotation.Resource;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Queue;
import javax.jms.Session;
import javax.jms.TextMessage;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;
import org.springframework.jms.core.MessagePostProcessor;
import org.springframework.stereotype.Service;
import com.yc.biz.SendMessage;
@Service
public class SendMessageImpl implements SendMessage {
private JmsTemplate jmsTemplate;
private Queue sendDestination; // 發送隊列
private Queue replyDestination; // 接收隊列
@Resource(name = "jmsTemplate")
public void setJmsTemplate(JmsTemplate jmsTemplate) {
this.jmsTemplate = jmsTemplate;
}
@Resource(name = "sendDestination")
public void setSendDestination(Queue sendDestination) {
this.sendDestination = sendDestination;
}
@Resource(name = "replyDestination")
public void setReplyDestination(Queue replyDestination) {
this.replyDestination = replyDestination;
}
public void SendMail(Map<String, String> message) {
jmsTemplate.convertAndSend(sendDestination, message,
new MessagePostProcessor() {
public Message postProcessMessage(Message message)
throws JMSException {
return message;
}
});
}
}
監聽器,用來監聽接收隊列中的接收者返回的確認消息
package com.yc.jms.listener;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;
public class ReplayListener implements MessageListener {
//當jms中間件中的隊列有消息時,這個消息中間會發送一個消息到本應用,激活本應用中的這個監聽器,回調onMessage
public void onMessage(Message message) {
TextMessage tm=(TextMessage) message;
try {
//TODO:通過這一代碼,表示消費成功運行
//真實項目中,這裏考慮修改數據狀態(數據有可能存在數據庫,或者日誌)
System.out.println(tm.getText().toString());
} catch (JMSException e) {
e.printStackTrace();
}
}
}
測試方法:
public void testApp08() {
ApplicationContext ac = new ClassPathXmlApplicationContext("beans_jms.xml");
SendMessage lub=(SendMessage) ac.getBean("sendMessageImpl");
Map<String, String> map = new HashMap<String, String>();
map.put("name", "張三");
lub.SendMail( map );
}
這只是發送方 ,接收方可以寫在一起,也可以不寫在一起。另外使用了中間件的具體實現ActiveMQ,具體怎麼使用就不在這一一講述了。