ActiveMQ消息隊列和SSM整合實際使用流程

生活就像海洋
只有意志堅強的人
才能到達彼岸。

ActiveMQ整合Spring

關於ActiveMQ消息隊列配置可查看ActiveMQ消息隊列的配置和使用操作

涉及知識點:

Solr索引庫,Redis緩存數據庫,ActiveMQ消息隊列,Dubbo+Zookeeper實現負載均衡和分佈式架構,Maven項目管理工具,SSM三大框架,Nginx服務器實現圖片存儲。

1,準備工作

這邊項目使用消息隊列的場景是:
原始方法:添加商品後,需要導入索引庫solr進行搜索索引,而導入索引庫非常麻煩,使用一鍵導入會重新導入所有商品,商品較多的話,會影響工程速度和用戶體驗。

採用Active消息隊列:在每次添加商品後獲取商品id,通過消息隊列把商品id發送給負責索引庫導入的業務層,業務層接受id,查找數據庫,把當前添加的商品導入索引庫,提升商品導入效率。

->>>>這種情況在MVC架構會很好解決,只是這邊使用的是SOA架構,項目模塊分離,用過消息隊列交流數據會好一點。

1.1,導包

<dependency>
		<groupId>org.springframework</groupId>
		<artifactId>spring-jms</artifactId>
</dependency>
<dependency>
		<groupId>org.springframework</groupId>
		<artifactId>spring-context-support</artifactId>
</dependency>
<dependency>
		<groupId>org.apache.activemq</groupId>
		<artifactId>activemq-all</artifactId>
</dependency>

1.2,配置配置文件

1.2.1,消費者配置

分爲三部分。
全局ActiveMQ配置
Queue配置+自己的消息監聽容器
Topic配置+自己的消息監聽容器

<!-- 真正可以產生Connection的ConnectionFactory,由對應的 JMS服務廠商提供 -->
<bean id="targetConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
	<property name="brokerURL" value="tcp://1.1.1.1:61616" />
</bean>
<!-- Spring用於管理真正的ConnectionFactory的ConnectionFactory -->
<bean id="connectionFactory"
	class="org.springframework.jms.connection.SingleConnectionFactory">
	<!-- 目標ConnectionFactory對應真實的可以產生JMS Connection的ConnectionFactory -->
	<property name="targetConnectionFactory" ref="targetConnectionFactory" />
</bean>
<!-- queue消息監聽容器 -->
<bean id="myMessageListener" class="cn.e3mall.search.message.MyMessageListener"/>
<bean class="org.springframework.jms.listener.DefaultMessageListenerContainer">
	<property name="connectionFactory" ref="connectionFactory" />
	<property name="destination" ref="topicDestination" />
	<property name="messageListener" ref="myMessageListener" />
</bean>
	<!--這個是隊列目的地,點對點的 -->
<bean id="queueDestination" class="org.apache.activemq.command.ActiveMQQueue">
	<constructor-arg>
		<value>spring-queue</value>
	</constructor-arg>
</bean>
<!-- topic消息監聽容器 -->
<bean id="itemAddMessageListener" class="cn.e3mall.search.message.ItemAddMessageListener" />
<bean class="org.springframework.jms.listener.DefaultMessageListenerContainer">
	<property name="connectionFactory" ref="connectionFactory" />
	<property name="destination" ref="topicDestination" />
	<property name="messageListener" ref="itemAddMessageListener" />
</bean>
	<!--這個是主題目的地,一對多的 -->
<bean id="topicDestination" class="org.apache.activemq.command.ActiveMQTopic">
	<constructor-arg value="spring-topic" />
</bean>

1.2.2,生產者配置

分爲三部分。
全局ActiveMQ配置
Queue配置
Topic配置

<!-- 真正可以產生Connection的ConnectionFactory,由對應的 JMS服務廠商提供 -->
<bean id="targetConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
	<property name="brokerURL" value="tcp://1.1.1.1:61616" />
</bean>
<!-- Spring用於管理真正的ConnectionFactory的ConnectionFactory -->
<bean id="connectionFactory"
	class="org.springframework.jms.connection.SingleConnectionFactory">
	<!-- 目標ConnectionFactory對應真實的可以產生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="spring-topic" />
</bean>

2,實際開發

2.1,Dao層

//定義Mapper映射文件接口
SearchItem getItemById(Long itemId);
//多表查詢,xml映射文件
<select id="getItemById" parameterType="Long" resultType="cn.e3mall.common.pojo.SearchItem">
	SELECT
		a.id,
		a.title,
		a.sell_point,
		a.price,
		a.image,
		b. NAME category_name
	FROM
		tb_item a
	LEFT JOIN tb_item_cat b ON a.cid = b.id
	WHERE
		a.status = 1
	AND a.id=#{itemId}
</select>

2.2,Service層(接受消息)

@Override
public void onMessage(Message message) {
	
	try {
		//提取商品id
		TextMessage textMessage=(TextMessage) message;
		String text = textMessage.getText();
		//轉換爲Long
		Long itemId=new Long(text);
		System.out.println("------"+itemId);
		//通過id查詢商品信息,返回SearchItem
		//讓線程休眠0.5秒,須確保商品添加到數據庫之後再查詢
		Thread.sleep(1000);
		SearchItem searchItem = itemMapper.getItemById(itemId);
		System.out.println("-------"+searchItem);
		//把商品信息添加到solr服務器
		//創建文檔對象
		SolrInputDocument document = new SolrInputDocument();
		//向文檔對象中添加域
		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());
		//把文檔對象寫入索引庫
		solrServer.add(document);
		//提交SolrServer
		solrServer.commit();
	} catch (Exception e) {
		e.printStackTrace();
	}

}

2.3,Service層(發送消息)

在這裏插入圖片描述

	//添加消息隊列,生產者發送消息
	jmsTemplate.send(topicDestination,new MessageCreator() {
		@Override
		public Message createMessage(Session session) throws JMSException {
			TextMessage message = session.createTextMessage(itemId+"");
			return message;
		}
	});

3,測試

這是之前索引庫的數據
在這裏插入圖片描述
添加商品
在這裏插入圖片描述
查看索引庫數據
在這裏插入圖片描述

文章持續更新,可以微信搜索「 紳堂Style 」第一時間閱讀,回覆【資料】有我準備的面試題筆記。
GitHub https://github.com/dtt11111/Nodes 有總結面試完整考點、資料以及我的系列文章。歡迎Star。
在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章