一叁熔、簡介
關(guān)于jms,請參閱https://docs.oracle.com/javaee/6/tutorial/doc/bncdr.html床牧,看內(nèi)容是做rpc服務(wù)的東西荣回,公司用的是weblogic自帶的jms,其實(shí)原理差不多戈咳,都是創(chuàng)建一個消息工廠心软,添加隊列,從隊列中發(fā)消息著蛙;現(xiàn)在應(yīng)該不少公司再用dubbo和zookeeper做吧删铃,這個以后再研究。
二踏堡、使用
1.添加依賴
<!-- activemq -->
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-core</artifactId>
<version>5.7.0</version>
</dependency>
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-pool</artifactId>
<version>5.12.1</version>
</dependency>
<!-- xbean 如<amq:connectionFactory /> -->
<dependency>
<groupId>org.apache.xbean</groupId>
<artifactId>xbean-spring</artifactId>
<version>3.16</version>
</dependency>
2.生產(chǎn)消費(fèi)者模式猎唁,其實(shí)就是發(fā)送者和接收者
package com.web.jms.service;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import javax.jms.*;
@Service
public class ProducerService {
@Resource(name = "jmsQueueTemplate")
private JmsTemplate jmsTemplate;
@Resource(name = "queueDestination")
private Destination responseDestination;
public void sendMessage(final String msg){
System.out.println(">>>>\n"+Thread.currentThread().getName()+" 向隊列"+responseDestination+"發(fā)送消息---------------------->"+msg);
jmsTemplate.send(new MessageCreator() {
public Message createMessage(Session session) throws JMSException {
TextMessage textMessage = session.createTextMessage(msg);
textMessage.setJMSReplyTo(responseDestination);
return textMessage;
}
});
}
}
消費(fèi)者
package com.web.jms.service;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.TextMessage;
@Service
public class ConsumerService {
@Resource(name = "jmsQueueTemplate")
private JmsTemplate jmsTemplate;
public TextMessage receive(Destination destination){
TextMessage textMessage = (TextMessage) jmsTemplate.receive(destination);
try{
System.out.println("從隊列" + destination.toString() + "收到了消息:\t"
+ textMessage.getText());
} catch (JMSException e) {
e.printStackTrace();
}
return textMessage;
}
}
3.消費(fèi)者監(jiān)聽
package com.web.jms.listener;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;
public class JmsListener implements MessageListener {
public void onMessage(Message message) {
TextMessage textMsg = (TextMessage) message;
try {
System.out.println("接收到了消息,消息內(nèi)容是:" + textMsg.getText());
} catch (JMSException e) {
e.printStackTrace();
}
}
}
4.errorhandler
package com.web.jms.handler;
import org.springframework.stereotype.Service;
import org.springframework.util.ErrorHandler;
@Service
public class JmsErrorHandler implements ErrorHandler{
public void handleError(Throwable throwable) {
System.out.println("出錯了顷蟆!");
throwable.printStackTrace();
}
}
5.配置applicationContext-ActiveMQ.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:p="http://www.springframework.org/schema/p"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:amq="http://activemq.apache.org/schema/core"
xmlns:jms="http://www.springframework.org/schema/jms"
xmlns:mvc="http://www.alibaba.com/schema/stat"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.1.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.1.xsd
http://activemq.apache.org/schema/core
http://activemq.apache.org/schema/core/activemq-core-5.7.0.xsd
http://www.alibaba.com/schema/stat http://www.alibaba.com/schema/stat.xsd
http://www.springframework.org/schema/jms
http://www.springframework.org/schema/jms/spring-jms-4.1.xsd ">
<context:annotation-config />
<!-- ActiveMQ 連接工廠 -->
<!-- 真正可以產(chǎn)生Connection的ConnectionFactory胖秒,由對應(yīng)的 JMS服務(wù)廠商提供-->
<!-- 如果連接網(wǎng)絡(luò):tcp://ip:61616缎患;未連接網(wǎng)絡(luò):tcp://localhost:61616 以及用戶名,密碼-->
<amq:connectionFactory id="amqConnectionFactory"
brokerURL="tcp://localhost:61616"
userName="admin" password="admin" />
<!-- Spring Caching連接工廠 -->
<!-- Spring用于管理真正的ConnectionFactory的ConnectionFactory -->
<bean id="connectionFactory" class="org.springframework.jms.connection.CachingConnectionFactory">
<!-- 目標(biāo)ConnectionFactory對應(yīng)真實(shí)的可以產(chǎn)生JMS Connection的ConnectionFactory -->
<property name="targetConnectionFactory" ref="amqConnectionFactory"></property>
<!-- 同上阎肝,同理 -->
<!-- <constructor-arg ref="amqConnectionFactory" /> -->
<!-- Session緩存數(shù)量 -->
<property name="sessionCacheSize" value="100" />
</bean>
<!-- Spring JmsTemplate 的消息生產(chǎn)者 start-->
<!-- 定義JmsTemplate的Queue類型 -->
<bean id="jmsQueueTemplate" class="org.springframework.jms.core.JmsTemplate">
<!-- 這個connectionFactory對應(yīng)的是我們定義的Spring提供的那個ConnectionFactory對象 -->
<constructor-arg ref="connectionFactory" />
<!-- 非pub/sub模型(發(fā)布/訂閱),即隊列模式 -->
<property name="pubSubDomain" value="false" />
<property name="defaultDestination" ref="queueDestination" />
</bean>
<!--Spring JmsTemplate 的消息生產(chǎn)者 end-->
<!-- 消息消費(fèi)者 start-->
<!-- 配置消息隊列監(jiān)聽者(Queue) -->
<bean id="queueMessageListener" class="com.web.jms.listener.JmsListener" />
<!-- 顯示注入消息監(jiān)聽容器(Queue)肮街,配置連接工廠风题,監(jiān)聽的目標(biāo)是Destination,監(jiān)聽器是上面定義的監(jiān)聽器 -->
<bean id="queueListenerContainer"
class="org.springframework.jms.listener.DefaultMessageListenerContainer">
<property name="errorHandler" ref="JmsErrorHandler"/>
<property name="connectionFactory" ref="connectionFactory" />
<property name="destination" ref="queueDestination" />
<property name="messageListener" ref="queueMessageListener" />
</bean>
<bean id="JmsErrorHandler" class="com.web.jms.handler.JmsErrorHandler"></bean>
<!-- 消息消費(fèi)者 end -->
<!-- 定義隊列目的地 嫉父,點(diǎn)對點(diǎn) -->
<bean id="queueDestination" class="org.apache.activemq.command.ActiveMQQueue">
<constructor-arg>
<value>queue</value>
</constructor-arg>
</bean>
</beans>
6.web.xml中添加啟動文件
<!-- 1.針對Spring配置:讀取配置文件 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:config/applicationContext.xml,
classpath:config/applicationContext-ActiveMQ.xml
</param-value>
</context-param>
7.controller
package com.web.jms.controller;
import com.web.jms.service.ConsumerService;
import com.web.jms.service.ProducerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.annotation.Resource;
import javax.jms.Destination;
import javax.jms.TextMessage;
import java.text.SimpleDateFormat;
import java.util.Date;
@Controller
@RequestMapping("/jms")
public class JmsController {
@Resource(name = "queueDestination")
private Destination destination;
//隊列消息生產(chǎn)者
@Autowired
private ProducerService producer;
//隊列消息消費(fèi)者
@Autowired
private ConsumerService consumer;
@RequestMapping(value = "/SendMessage", method = RequestMethod.GET)
@ResponseBody
public void send() {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:ss");
String msg = sdf.format(new Date());
System.out.println(Thread.currentThread().getName()+"------------send to jms Start");
producer.sendMessage(msg);
System.out.println(Thread.currentThread().getName()+"------------send to jms End");
}
@RequestMapping(value= "/ReceiveMessage",method = RequestMethod.GET)
@ResponseBody
public void receive(){
System.out.println(Thread.currentThread().getName()+"------------receive from jms Start");
TextMessage tm = consumer.receive(destination);
System.out.println(Thread.currentThread().getName()+"------------receive from jms End");
}
}
-end-