杭州做网站排名软件,怎么建立微信群,免费的个人简历模板word,网站建设温州创建2个工程#xff0c;生产者和消费者
测试点对点和广播发送常见类型的消息例如#xff1a;文本(String)、对象(Object) 文本转Long String text “123”; Long.parseLong(text) 数组转集合 Arrays.toArray(list)
引入依赖
?xml version1.0 enco…创建2个工程生产者和消费者
测试点对点和广播发送常见类型的消息例如文本(String)、对象(Object) 文本转Long String text “123”; Long.parseLong(text) 数组转集合 Arrays.toArray(list)
引入依赖
?xml version1.0 encodingUTF-8?project xmlnshttp://maven.apache.org/POM/4.0.0 xmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsdmodelVersion4.0.0/modelVersiongroupIdcom.gblfy.activemq/groupIdartifactIdproducer/artifactIdversion1.0-SNAPSHOT/versionpackagingwar/packagingnameproducer Maven Webapp/nameurlhttp://www.gblfy.com/urlproperties!--全局编码设置--project.build.sourceEncodingUTF-8/project.build.sourceEncoding!--全局maven编译版本--maven.compiler.source1.8/maven.compiler.sourcemaven.compiler.target1.8/maven.compiler.target!--全局版本控制--spring.version4.2.4.RELEASE/spring.version/propertiesdependencies!-- Spring Start --dependencygroupIdorg.springframework/groupIdartifactIdspring-context/artifactIdversion${spring.version}/version/dependencydependencygroupIdorg.springframework/groupIdartifactIdspring-beans/artifactIdversion${spring.version}/version/dependencydependencygroupIdorg.springframework/groupIdartifactIdspring-webmvc/artifactIdversion${spring.version}/version/dependencydependencygroupIdorg.springframework/groupIdartifactIdspring-aspects/artifactIdversion${spring.version}/version/dependencydependencygroupIdorg.springframework/groupIdartifactIdspring-jms/artifactIdversion${spring.version}/version/dependencydependencygroupIdorg.springframework/groupIdartifactIdspring-context-support/artifactIdversion${spring.version}/version/dependencydependencygroupIdorg.springframework/groupIdartifactIdspring-test/artifactIdversion${spring.version}/version/dependency!-- Spring End --dependencygroupIdjunit/groupIdartifactIdjunit/artifactId/dependency!--解析/反解析数据--dependencygroupIdcom.alibaba/groupIdartifactIdfastjson/artifactId/dependencydependencygroupIdjavassist/groupIdartifactIdjavassist/artifactId/dependencydependencygroupIdcommons-codec/groupIdartifactIdcommons-codec/artifactId/dependencydependencygroupIdjavax.servlet/groupIdartifactIdservlet-api/artifactIdscopeprovided/scope/dependency!--ActiveMQ Start--dependencygroupIdorg.apache.activemq/groupIdartifactIdactivemq-client/artifactIdversion5.13.4/version/dependency!--ActiveMQ End--/dependenciesbuildplugins!--Tomcat7插件--plugingroupIdorg.apache.tomcat.maven/groupIdartifactIdtomcat7-maven-plugin/artifactIdversion2.2/versionconfiguration!-- 指定端口 --port9004/port!-- 请求路径 --path//path/configuration/plugin/plugins/build
/project消费者
引入配置文件 ?xml version1.0 encodingUTF-8?
beans xmlnshttp://www.springframework.org/schema/beans xmlns:contexthttp://www.springframework.org/schema/context xmlns:xsihttp://www.w3.org/2001/XMLSchema-instance xmlns:amqhttp://activemq.apache.org/schema/corexmlns:jmshttp://www.springframework.org/schema/jmsxsi:schemaLocationhttp://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd!-- 真正可以产生Connection的ConnectionFactory由对应的 JMS服务厂商提供-- bean idtargetConnectionFactory classorg.apache.activemq.ActiveMQConnectionFactory property namebrokerURL valuetcp://192.168.25.135:61616/ /bean!-- Spring用于管理真正的ConnectionFactory的ConnectionFactory -- bean idconnectionFactory classorg.springframework.jms.connection.SingleConnectionFactory !-- 目标ConnectionFactory对应真实的可以产生JMS Connection的ConnectionFactory -- property nametargetConnectionFactory reftargetConnectionFactory/ /bean !-- Spring提供的JMS工具类它可以进行消息发送、接收等 -- bean idjmsTemplate classorg.springframework.jms.core.JmsTemplate !-- 这个connectionFactory对应的是我们定义的Spring提供的那个ConnectionFactory对象 -- property nameconnectionFactory refconnectionFactory/ /bean !--这个是队列目的地导入索引库-- bean idqueueSolrDestination classorg.apache.activemq.command.ActiveMQQueue constructor-arg valuepinyougou_queue_solr/ /bean !--这个是队列目的地导入索引库-- bean idqueueSolrDeleteDestination classorg.apache.activemq.command.ActiveMQQueue constructor-arg valuepinyougou_queue_solr_delete/ /bean !--这个是订阅模式 生成商品详细页-- bean idtopicPageDestination classorg.apache.activemq.command.ActiveMQTopic constructor-arg valuepinyougou_topic_page/ /bean !--这个是订阅模式 删除商品详细页-- bean idtopicPageDeleteDestination classorg.apache.activemq.command.ActiveMQTopic constructor-arg valuepinyougou_topic_page_delete/ /bean /beans
注扫描包如果spring的配置文件中有此配置可以省略 在需要发送消息的类中注入2个对象
Autowired
private Destination queueSolrDestination;//用于发送solr导入的消息
Autowired
private JmsTemplate jmsTemplate;假设发送的数据是一个listlist没有实现序列化而发送的消息类型如果是对象必须实现序列化
ListTbItem itemList goodsService.findItemListByGoodsIdandStatus(ids, status);建议发送消息类型采用文本类型 利用fastjson工具类将list转换为json字符串接收的时候在转回来是很方便的 final String jsonString JSON.toJSONString(itemList); jmsTemplate.send(queueSolrDestination, new MessageCreator() { Overridepublic Message createMessage(Session session) throws JMSException { return session.createTextMessage(jsonString);}}); 消费者客户端 引入依赖和生产者一样 引入配置文件applicationContext-jms-consumer.xml
?xml version1.0 encodingUTF-8?
beans xmlnshttp://www.springframework.org/schema/beans xmlns:contexthttp://www.springframework.org/schema/context xmlns:xsihttp://www.w3.org/2001/XMLSchema-instance xmlns:amqhttp://activemq.apache.org/schema/corexmlns:jmshttp://www.springframework.org/schema/jmsxsi:schemaLocationhttp://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd!-- 真正可以产生Connection的ConnectionFactory由对应的 JMS服务厂商提供-- bean idtargetConnectionFactory classorg.apache.activemq.ActiveMQConnectionFactory property namebrokerURL valuetcp://192.168.25.135:61616/ /bean!-- Spring用于管理真正的ConnectionFactory的ConnectionFactory -- bean idconnectionFactory classorg.springframework.jms.connection.SingleConnectionFactory !-- 目标ConnectionFactory对应真实的可以产生JMS Connection的ConnectionFactory -- property nametargetConnectionFactory reftargetConnectionFactory/ /bean !--这个是队列目的地导入索引库-- bean idqueueSolrDestination classorg.apache.activemq.command.ActiveMQQueue constructor-arg valuepinyougou_queue_solr/ /bean !-- 消息监听容器 导入索引库--bean classorg.springframework.jms.listener.DefaultMessageListenerContainerproperty nameconnectionFactory refconnectionFactory /property namedestination refqueueSolrDestination /property namemessageListener refitemSearchListener //bean!--这个是队列目的地导入索引库-- bean idqueueSolrDeleteDestination classorg.apache.activemq.command.ActiveMQQueue constructor-arg valuepinyougou_queue_solr_delete/ /bean !-- 消息监听容器 导入索引库--bean classorg.springframework.jms.listener.DefaultMessageListenerContainerproperty nameconnectionFactory refconnectionFactory /property namedestination refqueueSolrDeleteDestination /property namemessageListener refitemDeleteListener //bean/beans
新建一个监听类itemSearchListener接收文本类型消息 注这个监听类上加上Component注解并且能被spring的包扫描扫描到 下面监听类的包是package com.pinyougou.search.service.impl; 在此项目spring的配置文件中有扫描包的配置因此可以被扫描到
package com.pinyougou.search.service.impl;import java.util.List;import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;import com.alibaba.fastjson.JSON;
import com.pinyougou.pojo.TbItem;
import com.pinyougou.search.service.ItemSearchService;
Component
public class ItemSearchListener implements MessageListener {Autowiredprivate ItemSearchService itemSearchService;Overridepublic void onMessage(Message message) {TextMessage textMessage(TextMessage)message;try {String text textMessage.getText();//json字符串System.out.println(监听到消息:text);ListTbItem itemList JSON.parseArray(text, TbItem.class);itemSearchService.importList(itemList);System.out.println(导入到solr索引库);} catch (JMSException e) {// TODO Auto-generated catch blocke.printStackTrace();}}
}发送对象类型消息 Autowiredprivate JmsTemplate jmsTemplate;Autowiredprivate Destination queueTextDestination;/*** 发送文本消息* param text*/public void sendTextMessage(final String text){jmsTemplate.send(queueTextDestination, new MessageCreator() {public Message createMessage(Session session) throws JMSException {
// return session.createTextMessage(text);return session.createObjectMessage(对象);}});}新建一个监听接收对象类型消息
在这里插入代码片package com.pinyougou.search.service.impl;import java.util.Arrays;import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.ObjectMessage;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;import com.pinyougou.search.service.ItemSearchService;
Component
public class ItemDeleteListener implements MessageListener {Autowiredprivate ItemSearchService itemSearchService;Overridepublic void onMessage(Message message) {ObjectMessage objectMessage (ObjectMessage)message;try {Long[] goodsIds (Long[]) objectMessage.getObject();System.out.println(监听获取到消息goodsIds);itemSearchService.deleteByGoodsIds(Arrays.asList(goodsIds));System.out.println(执行索引库删除);} catch (JMSException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}