e3mall商城的归纳总结9之activemq整合spring、redis的缓存

敬给读者

本节主要给大家说一下activemq整合spring,该如何进行配置,上一节我们说了activemq的搭建和测试(单独测试),想看的可以点击时空隧道前去查看。讲完了之后我们还说一说在项目中使用redis缓存的场景。
1、activemq整合spring开发
2、activemq在项目中的使用(添加商品同时索引库也添加)
2、商品详情页使用redis的缓存存数据

一、activemq整合spring开发

项目(e3mall-manager)
在这里插入图片描述
第一步:引用相关的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
applicationContext-activemq.xml

<?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">
	<!-- 真正可以产生Connection的ConnectionFactory,由对应的 JMS服务厂商提供 -->
	<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">
		<!-- 目标ConnectionFactory对应真实的可以产生JMS Connection的ConnectionFactory -->
		<property name="targetConnectionFactory" ref="targetConnectionFactory" />
	</bean>
	
	<!-- 配置生产者 -->
	<!-- Spring提供的JMS工具类,它可以进行消息发送、接收等 -->
	<bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
		<!-- 这个connectionFactory对应的是我们定义的Spring提供的那个ConnectionFactory对象 -->
		<property name="connectionFactory" ref="connectionFactory" />
	</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>
</beans>

整合spring的测试

@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对象发送消息,需要知道Destination
		jmsTemplate.send(queue, new MessageCreator() {
			
			@Override
			public Message createMessage(Session session) throws JMSException {
				TextMessage textMessage = session.createTextMessage("spring activemq test");
				return textMessage;
			}
		});
	}

接收消息Queue
e3-search-Service中接收消息。queque可以异步实行加载,也就是说当manager发送信号后,若search这边没开启,则等待search开启后自动发送到相应的位置。也就是说可以异步加载。
第一步:把Activemq相关的jar包添加到工程中

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

第二步:创建一个MessageListener的实现类。

public class MyMessageListener implements MessageListener {

	@Override
	public void onMessage(Message message) {
		
		try {
			TextMessage textMessage = (TextMessage) message;
			//取消息内容
			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">

	<!-- 真正可以产生Connection的ConnectionFactory,由对应的 JMS服务厂商提供 -->
	<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">
		<!-- 目标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="topic" />
	</bean>
	<!-- 接收消息 -->
	<!-- 配置监听器 -->
	<bean id="myMessageListener" class="cn.e3mall.search.listener.MyMessageListener" />
	<!-- 消息监听容器 -->
	<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();
	}

二、activemq在项目中的使用(添加商品同时索引库也添加)

a、在e3ll-manager-service工程中发送消息。

当商品添加完成后发送一个TextMessage,包含一个商品id。
在这里插入图片描述
在这里插入图片描述
上图中由于我们再配置文件中配置了两个Destination,一个是queue,一个是Topic,所以我们采用@resource注解,首先根据Byname 方式 topicDestination查询配置文件中的id进行自动装配。


	//添加商品
	@Override
	public E3Result InsertItem(TbItem item, String desc) {
		final long id = IDUtils.genItemId();
		//补齐数据
		item.setId(id);
		//1-正常,2-下架,3-删除
		item.setStatus((byte) 1);
		item.setCreated(new Date());
		item.setUpdated(new Date());
		TbItemMapper.insert(item);
		TbItemDesc itemDesc = new TbItemDesc();
		itemDesc.setItemId(id);
		itemDesc.setItemDesc(desc);
		itemDesc.setCreated(new Date());
		itemDesc.setUpdated(new Date());
		itemDescMapper.insert(itemDesc);
		//向activemq中发送topic消息,广播一下
		try {
			jmsTemplate.send(topicDestination,new MessageCreator() {
				@Override
				public Message createMessage(Session session) throws JMSException {
					// 发送数据
				TextMessage textMessgage = session.createTextMessage(id+"");
				return textMessgage;
				}
			});
		} catch (JmsException e) {
			e.printStackTrace();
		}
		return E3Result.ok();
	}

代码逻辑:
向数据库中插入商品和商品详情,插入完成后通过jmsTemplate.send()方法发送id。我们要保证发送数据不能影响代码的正常运行,因此需要try{}catch()一下。
Consumer
3.2.1.功能分析
1、接收消息。需要创建MessageListener接口的实现类。
2、取消息,取商品id。
3、根据商品id查询数据库。
4、创建一SolrInputDocument对象。
5、使用SolrServer对象写入索引库。
6、返回成功,返回e3Result。

b、在solr-service中接受消息

1、接收消息。需要创建MessageListener接口的实现类。
2、取消息,取商品id。
3、根据商品id查询数据库。
4、创建一SolrInputDocument对象。
5、使用SolrServer对象写入索引库。
返回成功,返回e3Result。
我们先看看Dao层,因为solr索引库中的业务域是我们自己定义的,因此我们需要查询的数据要包含两个表(商品表和商品类型表),所以用逆向工程的代码是不行的,需要我们自己写sql语句。
Dao层:
在这里插入图片描述
SearchMapper.xml文件内容

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="cn.tsu.search.e3mall.mapper.SearchMapper" >
  <select id="getItemList" resultType="cn.tsu.e3mall.pojo.SearchResult">
	  	SELECT
			a.id,
			a.title,
			a.sell_point,
			a.price,
			a.image,
			b.NAME category_id
		FROM
			tb_item a
		LEFT JOIN tb_item_cat b ON a.cid = b.id WHERE a.`status`=1
	</select>  
	<select id="getItemById" parameterType="Long" resultType="cn.tsu.e3mall.pojo.SearchResult">
	  	SELECT
			a.id,
			a.title,
			a.sell_point,
			a.price,
			a.image,
			b.NAME category_id
		FROM
			tb_item a
		LEFT JOIN tb_item_cat b ON a.cid = b.id WHERE a.`status`=1 AND a.id=#{itemId}
	</select>		
</mapper>

使用的是LEFT JOIN让两个表相关联(以左表为主).

Service层代码,service层代码主要包含监听topic发送来的信息,还有向索引库中添加商品的代码逻辑
参数:商品ID
业务逻辑:
1、根据商品id查询商品信息。
2、创建一SolrInputDocument对象。
3、使用SolrServer对象写入索引库。
4、返回成功,返回e3Result。
返回值:e3Result

添加商品的代码逻辑:(因为manager向数据库中已插入该条信息,因此我们可以向数据库中通过商品id查询该条商品信息,然后再把商品信息存放到solr索引库中。)

public e3Result addDocument(long itemId) throws Exception {
		// 1、根据商品id查询商品信息。
		SearchItem searchItem = searchItemMapper.getItemById(itemId);
		// 2、创建一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());
		// 5、向索引库中添加文档。
		solrServer.add(document);
		solrServer.commit();
		// 4、返回成功,返回e3Result。
		return e3Result.ok();
	}

在这里插入图片描述

package cn.tsu.search.e3mall.listener;

import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;

import org.springframework.beans.factory.annotation.Autowired;

import cn.tsu.search.e3mall.service.Impl.SearchServiceImpl;

public class MyMessageListenter implements MessageListener{

	@Autowired
	private SearchServiceImpl searchServiceImpl;
	
	@Override
	public void onMessage(Message message) {
		// TODO Auto-generated method stub
		TextMessage textMessage = (TextMessage) message;
		try {
			Thread.sleep(1000);
			String text = textMessage.getText();
			System.out.println("收到.....searchservice"+text); 
			Long id = new Long(text);
			searchServiceImpl.mqAddDoc(id);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}

MyMessageListenter 写完之后需要在配置文件中配置一下
在这里插入图片描述
代码:

<?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:dubbo="http://code.alibabatech.com/schema/dubbo" 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://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd
	http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.2.xsd">
	
	
	<!-- 真正可以产生Connection的ConnectionFactory,由对应的 JMS服务厂商提供 -->
	<bean id="targetConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
		<property name="brokerURL" value="tcp://192.168.25.110: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="itemaddtopic" />
	</bean>
	<!-- 接收消息 -->
	<!-- 配置监听器 -->
	<bean id="myMessageListener" class="cn.tsu.search.e3mall.listener.MyMessageListenter" />
	<!-- 消息监听容器 -->
	<bean class="org.springframework.jms.listener.DefaultMessageListenerContainer">
		<property name="connectionFactory" ref="connectionFactory" />
		<property name="destination" ref="topicDestination" />
		<property name="messageListener" ref="myMessageListener" />
	</bean>
	
</beans>

业务逻辑:
在这里插入图片描述

三、redis缓存在项目中的使用

业务逻辑:
由于本商城的商品有很多,把那么多的商品存放在redis中,可能会导致redis的内存不足,所以我们采用TTL过期时间的逻辑对商品详情进行冷热处理,那什么是冷热处理呢?
也就是当用户第一次访问我们的商品详情时,redis缓存中没有该商品,则前往数据库中搜索该商品,搜索到该商品后,我们把该商品存放到redis缓存中,设置TTL时间,比如5田之后过期。这样的话只有热卖商品存放到redis缓存中,而数据库的压力也减轻了很多。同时也兼顾了redis的内存问题。保存的格式,string-value方式,因为涉及到TTl过期时间,所以不能使用hashMap存储方式

添加jar包
在这里插入图片描述

Dao层:
逆向工程
service层:
也就是简单的通过id进行查询数据库,查询,查询到返回数据
当用户第一次访问我们的商品详情时,redis缓存中没有该商品,则前往数据库中搜索该商品,搜索到该商品后,我们把该商品存放到redis缓存中,设置TTL时间,比如5田之后过期。这样的话只有热卖商品存放到redis缓存中,
代码:
取商品缓存:

@Override
	public TbItem getItemById(long itemId) {
		try {
			//查询缓存
			String json = jedisClient.get(ITEM_INFO_PRE + ":" + itemId + ":BASE");
			if (StringUtils.isNotBlank(json)) {
				//把json转换为java对象
				TbItem item = JsonUtils.jsonToPojo(json, TbItem.class);
				return item;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		//根据商品id查询商品信息
		//TbItem tbItem = itemMapper.selectByPrimaryKey(itemId);
		TbItemExample example = new TbItemExample();
		//设置查询条件
		Criteria criteria = example.createCriteria();
		criteria.andIdEqualTo(itemId);
		List<TbItem> list = itemMapper.selectByExample(example);
		if (list != null && list.size() > 0) {
			TbItem item = list.get(0);
			try {
				//把数据保存到缓存
				jedisClient.set(ITEM_INFO_PRE + ":" + itemId + ":BASE", JsonUtils.objectToJson(item));
				//设置缓存的有效期
				jedisClient.expire(ITEM_INFO_PRE + ":" + itemId + ":BASE", ITEM_INFO_EXPIRE);
			} catch (Exception e) {
				e.printStackTrace();
			}
			return item;
		}
		return null;
	}

取商品详情缓存:

@Override
	public TbItemDesc getItemDescById(long itemId) {
		try {
			String json = jedisClient.get(ITEM_INFO_PRE + ":" + itemId + ":DESC");
			//判断缓存是否命中
			if (StringUtils.isNotBlank(json) ) {
				//转换为java对象
				TbItemDesc itemDesc = JsonUtils.jsonToPojo(json, TbItemDesc.class);
				return itemDesc;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		TbItemDesc itemDesc = itemDescMapper.selectByPrimaryKey(itemId);
		try {
			jedisClient.set(ITEM_INFO_PRE + ":" + itemId + ":DESC", JsonUtils.objectToJson(itemDesc));
			//设置过期时间
			jedisClient.expire(ITEM_INFO_PRE + ":" + itemId + ":DESC", ITEM_INFO_EXPIRE);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return itemDesc;
	}

controller层:

package cn.tsu.item.e3mall.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import cn.tsu.e3mall.pojo.TbItem;
import cn.tsu.e3mall.pojo.TbItemDesc;
import cn.tsu.e3mall.service.ItemService;
import cn.tsu.item.e3mall.pojo.Item;

/**
 * 页面详情页
 * @author xiaofeng
 *
 */
@Controller
public class ItemController { 
	
	@Autowired
	private ItemService itemService;

	@RequestMapping("/item/{itemId}")
	public String ShowItemInfo(@PathVariable Long itemId,Model model) {
		//获取商品信息
		TbItem tbItem = itemService.getItemByid(itemId);
		//获取商品详情信息
		TbItemDesc descDesc = itemService.descEdit(itemId);
		//因为tbItem中图片是一个数据,创建一个对象,继承tbItem,然后getimages方法进行分开提取
		Item item = new Item(tbItem);
		model.addAttribute("item", item);
		model.addAttribute("itemDesc", descDesc);
		return "item";
		
	}
}

继承的item 代码:

package cn.tsu.item.e3mall.pojo;

import com.alibaba.dubbo.common.utils.StringUtils;

import cn.tsu.e3mall.pojo.TbItem;

public class Item extends TbItem {
	
	public String[] getImages() {
		String image = this.getImage();
		if(!StringUtils.isBlank(image)) {
			String[] split = image.split(",");
			return split;
		}
		return null;
	}

	public Item(TbItem tbItem) {
		 this.setId(tbItem.getId());
	     this.setTitle(tbItem.getTitle());
	     this.setSellPoint(tbItem.getSellPoint());
	     this.setPrice(tbItem.getPrice());
	     this.setNum(tbItem.getNum());
	     this.setBarcode(tbItem.getBarcode());
	     this.setImage(tbItem.getImage());
	     this.setCid(tbItem.getCid());
	     this.setStatus(tbItem.getStatus());
	     this.setCreated(tbItem.getCreated());
	     this.setUpdated(tbItem.getUpdated());
	}
}

本文讲解的很详细,希望可以给您带来不同之处,如果您有问题,欢迎下方评论,博主看到后会第一时间回复大家.

posted @ 2019-01-22 18:20  拉风的小锋  阅读(196)  评论(0编辑  收藏  举报