1. 同步索引庫分析
方案一:在taotao-manager中,添加商品的業(yè)務(wù)邏輯中,添加一個同步索引庫的業(yè)務(wù)邏輯。
缺點:業(yè)務(wù)邏輯耦合度高,業(yè)務(wù)拆分不明確
方案二:業(yè)務(wù)邏輯在taotao-search中實現(xiàn)州疾,調(diào)用服務(wù)在taotao-manager實現(xiàn)。業(yè)務(wù)邏輯分開皇拣。
缺點:服務(wù)之間的耦合度變高严蓖。服務(wù)的啟動有先后順序。
方案三:使用消息隊列氧急。MQ是一個消息中間件颗胡。
MQ是一個消息中間件,ActiveMQ吩坝、RabbitMQ毒姨、kafka
為了實現(xiàn)系統(tǒng)之間通信,把系統(tǒng)之間調(diào)用的耦合度降低的情況下都可以使用MQ钉寝。
什么是ActiveMQ
ActiveMQ 是Apache出品弧呐,最流行的闸迷,能力強勁的開源消息總線。ActiveMQ 是一個完全支持JMS1.1和J2EE 1.4規(guī)范的 JMS Provider實現(xiàn),盡管JMS規(guī)范出臺已經(jīng)是很久的事情了,但是JMS在當(dāng)今的J2EE應(yīng)用中間仍然扮演著特殊的地位俘枫。
主要特點:
- 多種語言和協(xié)議編寫客戶端腥沽。語言: Java, C, C++, C#, Ruby, Perl, Python, PHP。應(yīng)用協(xié)議: OpenWire,Stomp REST,WS Notification,XMPP,AMQP
- 完全支持JMS1.1和J2EE 1.4規(guī)范 (持久化,XA消息,事務(wù))
- 對Spring的支持,ActiveMQ可以很容易內(nèi)嵌到使用Spring的系統(tǒng)里面去,而且也支持Spring2.0的特性
- 通過了常見J2EE服務(wù)器(如 Geronimo,JBoss 4, GlassFish,WebLogic)的測試,其中通過JCA 1.5 resource adaptors的配置,可以讓ActiveMQ可以自動的部署到任何兼容J2EE 1.4 商業(yè)服務(wù)器上
- 支持多種傳送協(xié)議:in-VM,TCP,SSL,NIO,UDP,JGroups,JXTA
- 支持通過JDBC和journal提供高速的消息持久化
- 從設(shè)計上保證了高性能的集群,客戶端-服務(wù)器,點對點
- 支持Ajax
- 支持與Axis的整合
- 可以很容易得調(diào)用內(nèi)嵌JMS provider,進行測試
ActiveMQ的消息形式
對于消息的傳遞有兩種類型:
- 一種是點對點的崩哩,即一個生產(chǎn)者和一個消費者一一對應(yīng)巡球;
- 另一種是發(fā)布/訂閱模式言沐,即一個生產(chǎn)者產(chǎn)生消息并進行發(fā)送后邓嘹,可以由多個消費者進行接收。
JMS定義了五種不同的消息正文格式险胰,以及調(diào)用的消息類型汹押,允許你發(fā)送并接收以一些不同形式的數(shù)據(jù),提供現(xiàn)有消息格式的一些級別的兼容性起便。
· StreamMessage -- Java原始值的數(shù)據(jù)流
· MapMessage--一套名稱-值對
· TextMessage--一個字符串對象
· ObjectMessage--一個序列化的 Java對象
· BytesMessage--一個字節(jié)的數(shù)據(jù)流
2. ActiveMQ的安裝
進入http://activemq.apache.org/下載ActiveMQ
2.1. 安裝環(huán)境
1棚贾、需要jdk
2、安裝Linux系統(tǒng)榆综。生產(chǎn)環(huán)境都是Linux系統(tǒng)妙痹。
2.2. 安裝步驟
第一步: 把ActiveMQ 的壓縮包上傳到Linux系統(tǒng)。
第二步:解壓縮鼻疮。
第三步:啟動怯伊。
使用bin目錄下的activemq命令啟動:
[root@localhost bin]# ./activemq start
關(guān)閉:
[root@localhost bin]# ./activemq stop
查看狀態(tài):
[root@localhost bin]# ./activemq status
注意:如果ActiveMQ整合spring使用不要使用activemq-all-5.12.0.jar包。建議使用5.11.2
進入管理后臺:
http://192.168.25.168:8161/admin
用戶名:admin
密碼:admin
管理后臺503錯誤的處理
查看hostname
查看hostname是否映射到ip
沒有則添加到文件中
3. ActiveMQ的使用方法
3.1. Queue
3.1.1 Producer
生產(chǎn)者:生產(chǎn)消息判沟,發(fā)送端耿芹。
把jar包添加到工程中。使用5.11.2版本的jar包挪哄。
第一步:創(chuàng)建ConnectionFactory對象吧秕,需要指定服務(wù)端ip及端口號。
第二步:使用ConnectionFactory對象創(chuàng)建一個Connection對象迹炼。
第三步:開啟連接砸彬,調(diào)用Connection對象的start方法。
第四步:使用Connection對象創(chuàng)建一個Session對象斯入。
第五步:使用Session對象創(chuàng)建一個Destination對象(topic砂碉、queue),此處創(chuàng)建一個Queue對象咱扣。
第六步:使用Session對象創(chuàng)建一個Producer對象绽淘。
第七步:創(chuàng)建一個Message對象,創(chuàng)建一個TextMessage對象闹伪。
第八步:使用Producer對象發(fā)送消息沪铭。
第九步:關(guān)閉資源壮池。
@Test
public void testQueueProducer() throws Exception {
// 第一步:創(chuàng)建ConnectionFactory對象,需要指定服務(wù)端ip及端口號杀怠。
//brokerURL服務(wù)器的ip及端口號
ConnectionFactory connectionFactory = new ActiveMQConnectionFactory("tcp://192.168.25.168:61616");
// 第二步:使用ConnectionFactory對象創(chuàng)建一個Connection對象椰憋。
Connection connection = connectionFactory.createConnection();
// 第三步:開啟連接,調(diào)用Connection對象的start方法赔退。
connection.start();
// 第四步:使用Connection對象創(chuàng)建一個Session對象橙依。
//第一個參數(shù):是否開啟事務(wù)。true:開啟事務(wù)硕旗,第二個參數(shù)忽略窗骑。
//第二個參數(shù):當(dāng)?shù)谝粋€參數(shù)為false時,才有意義漆枚。消息的應(yīng)答模式创译。1、自動應(yīng)答2墙基、手動應(yīng)答软族。一般是自動應(yīng)答。
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
// 第五步:使用Session對象創(chuàng)建一個Destination對象(topic残制、queue)立砸,此處創(chuàng)建一個Queue對象。
//參數(shù):隊列的名稱初茶。
Queue queue = session.createQueue("test-queue");
// 第六步:使用Session對象創(chuàng)建一個Producer對象颗祝。
MessageProducer producer = session.createProducer(queue);
// 第七步:創(chuàng)建一個Message對象,創(chuàng)建一個TextMessage對象纺蛆。
/*TextMessage message = new ActiveMQTextMessage();
message.setText("hello activeMq,this is my first test.");*/
TextMessage textMessage = session.createTextMessage("hello activeMq,this is my first test.");
// 第八步:使用Producer對象發(fā)送消息吐葵。
producer.send(textMessage);
// 第九步:關(guān)閉資源。
producer.close();
session.close();
connection.close();
}
3.1.2 Consumer
消費者:接收消息桥氏。
第一步:創(chuàng)建一個ConnectionFactory對象温峭。
第二步:從ConnectionFactory對象中獲得一個Connection對象。
第三步:開啟連接字支。調(diào)用Connection對象的start方法凤藏。
第四步:使用Connection對象創(chuàng)建一個Session對象。
第五步:使用Session對象創(chuàng)建一個Destination對象堕伪。和發(fā)送端保持一致queue揖庄,并且隊列的名稱一致。
第六步:使用Session對象創(chuàng)建一個Consumer對象欠雌。
第七步:接收消息蹄梢。
第八步:打印消息。
第九步:關(guān)閉資源
@Test
public void testQueueConsumer() throws Exception {
// 第一步:創(chuàng)建一個ConnectionFactory對象富俄。
ConnectionFactory connectionFactory = new ActiveMQConnectionFactory("tcp://192.168.25.168:61616");
// 第二步:從ConnectionFactory對象中獲得一個Connection對象禁炒。
Connection connection = connectionFactory.createConnection();
// 第三步:開啟連接而咆。調(diào)用Connection對象的start方法。
connection.start();
// 第四步:使用Connection對象創(chuàng)建一個Session對象幕袱。
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
// 第五步:使用Session對象創(chuàng)建一個Destination對象暴备。和發(fā)送端保持一致queue,并且隊列的名稱一致们豌。
Queue queue = session.createQueue("test-queue");
// 第六步:使用Session對象創(chuàng)建一個Consumer對象涯捻。
MessageConsumer consumer = session.createConsumer(queue);
// 第七步:接收消息。
consumer.setMessageListener(new MessageListener() {
@Override
public void onMessage(Message message) {
try {
TextMessage textMessage = (TextMessage) message;
String text = null;
//取消息的內(nèi)容
text = textMessage.getText();
// 第八步:打印消息望迎。
System.out.println(text);
} catch (JMSException e) {
e.printStackTrace();
}
}
});
//等待鍵盤輸入
System.in.read();
// 第九步:關(guān)閉資源
consumer.close();
session.close();
connection.close();
}
3.2 Topic
3.2.1 Produver
使用步驟:
第一步:創(chuàng)建ConnectionFactory對象障癌,需要指定服務(wù)端ip及端口號。
第二步:使用ConnectionFactory對象創(chuàng)建一個Connection對象擂煞。
第三步:開啟連接混弥,調(diào)用Connection對象的start方法。
第四步:使用Connection對象創(chuàng)建一個Session對象对省。
第五步:使用Session對象創(chuàng)建一個Destination對象(topic、queue)晾捏,此處創(chuàng)建一個Topic對象蒿涎。
第六步:使用Session對象創(chuàng)建一個Producer對象。
第七步:創(chuàng)建一個Message對象惦辛,創(chuàng)建一個TextMessage對象劳秋。
第八步:使用Producer對象發(fā)送消息。
第九步:關(guān)閉資源胖齐。
@Test
public void testTopicProducer() throws Exception {
// 第一步:創(chuàng)建ConnectionFactory對象玻淑,需要指定服務(wù)端ip及端口號。
// brokerURL服務(wù)器的ip及端口號
ConnectionFactory connectionFactory = new ActiveMQConnectionFactory("tcp://192.168.25.168:61616");
// 第二步:使用ConnectionFactory對象創(chuàng)建一個Connection對象呀伙。
Connection connection = connectionFactory.createConnection();
// 第三步:開啟連接补履,調(diào)用Connection對象的start方法。
connection.start();
// 第四步:使用Connection對象創(chuàng)建一個Session對象剿另。
// 第一個參數(shù):是否開啟事務(wù)箫锤。true:開啟事務(wù),第二個參數(shù)忽略雨女。
// 第二個參數(shù):當(dāng)?shù)谝粋€參數(shù)為false時谚攒,才有意義。消息的應(yīng)答模式氛堕。1馏臭、自動應(yīng)答2、手動應(yīng)答讼稚。一般是自動應(yīng)答括儒。
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
// 第五步:使用Session對象創(chuàng)建一個Destination對象(topic浪耘、queue),此處創(chuàng)建一個topic對象塑崖。
// 參數(shù):話題的名稱七冲。
Topic topic = session.createTopic("test-topic");
// 第六步:使用Session對象創(chuàng)建一個Producer對象。
MessageProducer producer = session.createProducer(topic);
// 第七步:創(chuàng)建一個Message對象规婆,創(chuàng)建一個TextMessage對象澜躺。
/*
* TextMessage message = new ActiveMQTextMessage(); message.setText(
* "hello activeMq,this is my first test.");
*/
TextMessage textMessage = session.createTextMessage("hello activeMq,this is my topic test");
// 第八步:使用Producer對象發(fā)送消息。
producer.send(textMessage);
// 第九步:關(guān)閉資源抒蚜。
producer.close();
session.close();
connection.close();
}
3.2.2 Consumer
消費者:接收消息掘鄙。
第一步:創(chuàng)建一個ConnectionFactory對象。
第二步:從ConnectionFactory對象中獲得一個Connection對象嗡髓。
第三步:開啟連接操漠。調(diào)用Connection對象的start方法。
第四步:使用Connection對象創(chuàng)建一個Session對象饿这。
第五步:使用Session對象創(chuàng)建一個Destination對象浊伙。和發(fā)送端保持一致topic,并且話題的名稱一致长捧。
第六步:使用Session對象創(chuàng)建一個Consumer對象嚣鄙。
第七步:接收消息。
第八步:打印消息串结。
第九步:關(guān)閉資源
@Test
public void testTopicConsumer() throws Exception {
// 第一步:創(chuàng)建一個ConnectionFactory對象哑子。
ConnectionFactory connectionFactory = new ActiveMQConnectionFactory("tcp://192.168.25.168:61616");
// 第二步:從ConnectionFactory對象中獲得一個Connection對象。
Connection connection = connectionFactory.createConnection();
// 第三步:開啟連接肌割。調(diào)用Connection對象的start方法卧蜓。
connection.start();
// 第四步:使用Connection對象創(chuàng)建一個Session對象。
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
// 第五步:使用Session對象創(chuàng)建一個Destination對象把敞。和發(fā)送端保持一致topic弥奸,并且話題的名稱一致。
Topic topic = session.createTopic("test-topic");
// 第六步:使用Session對象創(chuàng)建一個Consumer對象先巴。
MessageConsumer consumer = session.createConsumer(topic);
// 第七步:接收消息其爵。
consumer.setMessageListener(new MessageListener() {
@Override
public void onMessage(Message message) {
try {
TextMessage textMessage = (TextMessage) message;
String text = null;
// 取消息的內(nèi)容
text = textMessage.getText();
// 第八步:打印消息。
System.out.println(text);
} catch (JMSException e) {
e.printStackTrace();
}
}
});
System.out.println("topic的消費端03伸蚯。摩渺。。剂邮。摇幻。");
// 等待鍵盤輸入
System.in.read();
// 第九步:關(guān)閉資源
consumer.close();
session.close();
connection.close();
}
4. ActiveMQ整合spring
4.1. 使用方法
第一步:引用相關(guān)的jar包衔瓮。
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jms</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
</dependency>
第二步:配置Activemq的整合spring牵舵。配置ConnectionFactory
第三步:配置生產(chǎn)者斑响。使用JMSTemplate對象官紫。發(fā)送消息。
第四步:在spring容器中配置Destination狂芋。
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.2.xsd">
<!-- 真正可以產(chǎn)生Connection的ConnectionFactory榨馁,由對應(yīng)的 JMS服務(wù)廠商提供 -->
<bean id="targetConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
<property name="brokerURL" value="tcp://192.168.25.128:61616" />
</bean>
<!-- Spring用于管理真正的ConnectionFactory的ConnectionFactory -->
<bean id="connectionFactory" class="org.springframework.jms.connection.SingleConnectionFactory">
<!-- 目標(biāo)ConnectionFactory對應(yīng)真實的可以產(chǎn)生JMS Connection的ConnectionFactory -->
<property name="targetConnectionFactory" ref="targetConnectionFactory" />
</bean>
<!-- 配置JMSTemplete -->
<!-- 配置生產(chǎn)者 -->
<!-- Spring提供的JMS工具類,它可以進行消息發(fā)送帜矾、接收等 -->
<bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
<!-- 這個connectionFactory對應(yīng)的是我們定義的Spring提供的那個ConnectionFactory對象 -->
<property name="connectionFactory" ref="connectionFactory" />
</bean>
<!-- 配置消息的Desination對象 -->
<!--這個是隊列目的地翼虫,點對點的 -->
<bean id="queueDestination" class="org.apache.activemq.command.ActiveMQQueue">
<constructor-arg>
<value>spring-queue</value>
</constructor-arg>
</bean>
<!--這個是主題目的地,一對多的 -->
<bean id="itemAddTopic" class="org.apache.activemq.command.ActiveMQTopic">
<constructor-arg name="name" value="item-add-topic" />
</bean>
</beans>
5. 代碼測試
5.1 發(fā)送消息
第一步:初始化一個spring容器
第二步:從容器中獲得JMSTemplate對象屡萤。
第三步:從容器中獲得一個Destination對象
第四步:使用JMSTemplate對象發(fā)送消息珍剑,需要知道Destination
@Test
public void testQueueProducer() throws Exception {
// 第一步:初始化一個spring容器
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:spring/applicationContext-activemq.xml");
// 第二步:從容器中獲得JMSTemplate對象。
JmsTemplate jmsTemplate = applicationContext.getBean(JmsTemplate.class);
// 第三步:從容器中獲得一個Destination對象
Queue queue = (Queue) applicationContext.getBean("queueDestination");
// 第四步:使用JMSTemplate對象發(fā)送消息死陆,需要知道Destination
jmsTemplate.send(queue, new MessageCreator() {
@Override
public Message createMessage(Session session) throws JMSException {
TextMessage textMessage = session.createTextMessage("spring activemq test");
return textMessage;
}
});
}
5.2 接收消息
Taotao-search-Service中接收消息招拙。
第一步:把Activemq相關(guān)的jar包添加到工程中
第二步:創(chuàng)建一個MessageListener的實現(xiàn)類。
public class MyMessageListener implements MessageListener {
@Override
public void onMessage(Message message) {
try {
TextMessage textMessage = (TextMessage) message;
//取消息內(nèi)容
String text = textMessage.getText();
System.out.println(text);
} catch (JMSException e) {
e.printStackTrace();
}
}
}
第三步:配置spring和Activemq整合措译。
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.2.xsd">
<!-- 真正可以產(chǎn)生Connection的ConnectionFactory别凤,由對應(yīng)的 JMS服務(wù)廠商提供 -->
<bean id="targetConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
<property name="brokerURL" value="tcp://192.168.25.168:61616" />
</bean>
<!-- Spring用于管理真正的ConnectionFactory的ConnectionFactory -->
<bean id="connectionFactory"
class="org.springframework.jms.connection.SingleConnectionFactory">
<!-- 目標(biāo)ConnectionFactory對應(yīng)真實的可以產(chǎn)生JMS Connection的ConnectionFactory -->
<property name="targetConnectionFactory" ref="targetConnectionFactory" />
</bean>
<!--這個是隊列目的地,點對點的 -->
<bean id="queueDestination" class="org.apache.activemq.command.ActiveMQQueue">
<constructor-arg>
<value>spring-queue</value>
</constructor-arg>
</bean>
<!--這個是主題目的地瞳遍,一對多的 -->
<bean id="topicDestination" class="org.apache.activemq.command.ActiveMQTopic">
<constructor-arg value="topic" />
</bean>
<!-- 接收消息 -->
<!-- 配置監(jiān)聽器 -->
<bean id="myMessageListener" class="com.taotao.search.listener.MyMessageListener" />
<!-- 消息監(jiān)聽容器 -->
<bean class="org.springframework.jms.listener.DefaultMessageListenerContainer">
<property name="connectionFactory" ref="connectionFactory" />
<property name="destination" ref="queueDestination" />
<property name="messageListener" ref="myMessageListener" />
</bean>
</beans>
第四步:測試代碼闻妓。
@Test
public void testQueueConsumer() throws Exception {
//初始化spring容器
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:spring/applicationContext-activemq.xml");
//等待
System.in.read();
}
6. 添加商品同步索引庫
6.1 Producer
Taotao-manager-server工程中發(fā)送消息。
當(dāng)商品添加完成后發(fā)送一個TextMessage掠械,包含一個商品id。
@Override
public TaotaoResult addItem(TbItem item, String desc) {
// 1注祖、生成商品id
final long itemId = IDUtils.genItemId();
....
// 6猾蒂、向商品描述表插入數(shù)據(jù)
itemDescMapper.insert(itemDesc);
`在需要使用MQ的地方` 使用jmsTemplate
//發(fā)送一個商品添加消息
jmsTemplate.send(topicDestination, new MessageCreator() {
@Override
public Message createMessage(Session session) throws JMSException {
TextMessage textMessage = session.createTextMessage(itemId + "");
return textMessage;
}
});
// 7、TaotaoResult.ok()
return TaotaoResult.ok();
}
6.2. Counsumer
6.2.1. 功能分析
1是晨、接收消息肚菠。需要創(chuàng)建MessageListener接口的實現(xiàn)類。
2罩缴、取消息蚊逢,取商品id。
3箫章、根據(jù)商品id查詢數(shù)據(jù)庫烙荷。
4、創(chuàng)建一個SolrInputDocument對象檬寂。
5终抽、使用SolrServer對象寫入索引庫。
6、返回成功昼伴,返回TaotaoResult
DAO層
根據(jù)商品id查詢商品信息匾旭。
映射文件:
<select id="getItemById" parameterType="long" resultType="com.taotao.common.pojo.SearchItem">
SELECT
a.id,
a.title,
a.sell_point,
a.price,
a.image,
b. NAME category_name,
c.item_desc
FROM
tb_item a
JOIN tb_item_cat b ON a.cid = b.id
JOIN tb_item_desc c ON a.id = c.item_id
WHERE a.status = 1
AND a.id=#{itemId}
</select>
Service層
參數(shù):商品ID
業(yè)務(wù)邏輯:
1、根據(jù)商品id查詢商品信息圃郊。
2价涝、創(chuàng)建一SolrInputDocument對象。
3持舆、使用SolrServer對象寫入索引庫色瘩。
4、返回成功吏廉,返回TaotaoResult泞遗。
返回值:TaotaoResult
public TaotaoResult addDocument(long itemId) throws Exception {
// 1、根據(jù)商品id查詢商品信息席覆。
SearchItem searchItem = searchItemMapper.getItemById(itemId);
// 2史辙、創(chuàng)建一SolrInputDocument對象。
SolrInputDocument document = new SolrInputDocument();
// 3佩伤、使用SolrServer對象寫入索引庫聊倔。
document.addField("id", searchItem.getId());
document.addField("item_title", searchItem.getTitle());
document.addField("item_sell_point", searchItem.getSell_point());
document.addField("item_price", searchItem.getPrice());
document.addField("item_image", searchItem.getImage());
document.addField("item_category_name", searchItem.getCategory_name());
document.addField("item_desc", searchItem.getItem_desc());
// 5、向索引庫中添加文檔生巡。
solrServer.add(document);
solrServer.commit();
// 4耙蔑、返回成功,返回TaotaoResult孤荣。
return TaotaoResult.ok();
}
Listener
public class ItemChangeListener implements MessageListener {
@Autowired
private SearchItemServiceImpl searchItemServiceImpl;
@Override
public void onMessage(Message message) {
try {
TextMessage textMessage = null;
Long itemId = null;
//取商品id
if (message instanceof TextMessage) {
textMessage = (TextMessage) message;
itemId = Long.parseLong(textMessage.getText());
}
//向索引庫添加文檔
searchItemServiceImpl.addDocument(itemId);
} catch (Exception e) {
e.printStackTrace();
}
}
}
Spring配置監(jiān)聽