PYG电商项目开发 -- day09 消息中间件解决方案

一、Activemq安装


详情请见:https://blog.csdn.net/wingzhezhe/article/details/73462380


二、使用activemq完成功能


1、商品上架后将对应商品发送到activemq队列中


(1)、在sellergoods-service中加入activemq的jar包坐标


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


(2)、sellergoods-service中加入activemq的配置


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:p="http://www.springframework.org/schema/p"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:jms="http://www.springframework.org/schema/jms"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
					http://www.springframework.org/schema/beans/spring-beans.xsd
					http://www.springframework.org/schema/jms 
					http://www.springframework.org/schema/jms/spring-jms.xsd
				        http://www.springframework.org/schema/context 
				        http://www.springframework.org/schema/context/spring-context.xsd">
	<!-- connectionFactory -->
	<bean id="targetConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
		<constructor-arg name="brokerURL" value="tcp://192.168.25.128:61616" />
	</bean>
	<bean id="connectionFactory" class="org.springframework.jms.connection.SingleConnectionFactory">
		<!-- 目标连接工厂 -->
		<property name="targetConnectionFactory" ref="targetConnectionFactory"></property>
	</bean>
	
	<!-- JMSTemplate, spring提供的JMS工具类,可用于发送、接收消息 -->
	<bean class="org.springframework.jms.core.JmsTemplate">
		<!-- connectionFactory对应的是我们自己定义的spring提供的ide为connectionFactory的对象 -->
		<property name="connectionFactory" ref="connectionFactory"></property>
	</bean>
	
	<!-- 定义Destination 
		class="org.apache.activemq.command.ActiveMQTopic" 表示发布订阅模式
	-->
	<bean id="textDestination" class="org.apache.activemq.command.ActiveMQTopic">
		<constructor-arg value="item_page_html"></constructor-arg>
	</bean>
</beans>


(3)、在GoodsServiceImpl.java的商品上架方法中加入发送该消息的方法




2、创建web工程itempage-consumer用来进行消息的接收


(1)、创建itempage-consumer工程






(2)、pom.xml


<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<parent>
		<groupId>com.pinyougou</groupId>
		<artifactId>pinyougou-parent</artifactId>
		<version>0.0.1-SNAPSHOT</version>
	</parent>
	<artifactId>pinyougou-itempage-consumer</artifactId>
	<packaging>war</packaging>

	<dependencies>
		<dependency>
			<groupId>com.pinyougou</groupId>
			<artifactId>pinyougou-common</artifactId>
			<version>0.0.1-SNAPSHOT</version>
		</dependency>
		<dependency>
			<groupId>com.pinyougou</groupId>
			<artifactId>pinyougou-dao</artifactId>
			<version>0.0.1-SNAPSHOT</version>
		</dependency>
		<dependency>
			<groupId>com.pinyougou</groupId>
			<artifactId>pinyougou-search-interface</artifactId>
			<version>0.0.1-SNAPSHOT</version>
		</dependency>

		<!-- 引入相关jar包 -->
		<!-- Spring -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-context</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-beans</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-webmvc</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-jdbc</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-aspects</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-jms</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-context-support</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-test</artifactId>
		</dependency>
		<!-- dubbo相关 -->
		<dependency>
			<groupId>com.alibaba</groupId>
			<artifactId>dubbo</artifactId>
		</dependency>
		<dependency>
			<groupId>org.apache.zookeeper</groupId>
			<artifactId>zookeeper</artifactId>
		</dependency>
		<dependency>
			<groupId>com.github.sgroschupf</groupId>
			<artifactId>zkclient</artifactId>
		</dependency>
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
		</dependency>
		<dependency>
			<groupId>com.alibaba</groupId>
			<artifactId>fastjson</artifactId>
		</dependency>
		<dependency>
			<groupId>javassist</groupId>
			<artifactId>javassist</artifactId>
		</dependency>
		<dependency>
			<groupId>commons-codec</groupId>
			<artifactId>commons-codec</artifactId>
		</dependency>
		<dependency>
			<groupId>javax.servlet</groupId>
			<artifactId>servlet-api</artifactId>
			<scope>provided</scope>
		</dependency>
		<!-- 加入activemq相关包 -->
		<dependency>
			<groupId>org.apache.activemq</groupId>
			<artifactId>activemq-all</artifactId>
		</dependency>
		<dependency>
			<groupId>org.freemarker</groupId>
			<artifactId>freemarker</artifactId>
		</dependency>
	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.apache.tomcat.maven</groupId>
				<artifactId>tomcat7-maven-plugin</artifactId>
				<version>2.2</version>
				<configuration>
					<!-- 指定端口 -->
					<port>9000</port>
					<!-- 请求路径 -->
					<path>/</path>
				</configuration>
			</plugin>
		</plugins>
	</build>
</project>


(3)、web.xml


<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns="http://java.sun.com/xml/ns/javaee"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
	version="2.5">
	<display-name>pinyougou-itempage-consumer</display-name>
	<welcome-file-list>
		<welcome-file>index.html</welcome-file>
		<welcome-file>index.htm</welcome-file>
		<welcome-file>index.jsp</welcome-file>
		<welcome-file>default.html</welcome-file>
		<welcome-file>default.htm</welcome-file>
		<welcome-file>default.jsp</welcome-file>
	</welcome-file-list>

	<!-- 加载spring容器 -->
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath*:spring/applicationContext*.xml</param-value>
	</context-param>
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>
</web-app>


(4)、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:p="http://www.springframework.org/schema/p"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:jms="http://www.springframework.org/schema/jms"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
						http://www.springframework.org/schema/beans/spring-beans.xsd
						http://www.springframework.org/schema/jms 
						http://www.springframework.org/schema/jms/spring-jms.xsd
				        http://www.springframework.org/schema/context 
				        http://www.springframework.org/schema/context/spring-context.xsd">
				        
	<!-- 配置扫描包 -->
	<context:component-scan base-package="com.pinyougou.itempage" />
	
	<!-- 读取properties -->
	<context:property-placeholder location="classpath*:*/*.properties" />
	
	<!-- connectionFactory -->
	<bean id="targetConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
		<constructor-arg name="brokerURL" value="tcp://192.168.25.128:61616" />
	</bean>
	
	<!-- spring用于管理工厂的真正的ConnectionFactory -->
	<bean id="connectionFactory" class="org.springframework.jms.connection.SingleConnectionFactory">
		<!-- 目标连接工厂 -->
		<property name="targetConnectionFactory" ref="targetConnectionFactory"></property>
	</bean>
	
	<!-- 定义Destination 
		class="org.apache.activemq.command.ActiveMQTopic" 表示发布订阅模式
	-->
	<bean id="textDestination" class="org.apache.activemq.command.ActiveMQTopic">
		<constructor-arg value="item_page_html"></constructor-arg>
	</bean>
	
	<!-- 自定义消息监听类,用来处理消息 -->
	<bean id="MyListener" class="com.pinyougou.itempage.consumer.ItemPageConsumer">
	</bean>

	<!-- Message容器 MessageListenerContainer -->
	<bean id="myMessageContainer"
		class="org.springframework.jms.listener.DefaultMessageListenerContainer">
		<property name="connectionFactory" ref="connectionFactory"></property>
		<property name="destination" ref="textDestination"></property>
		<property name="messageListener" ref="MyListener"></property>
	</bean>
	
	<!-- 加入freemarker配置 -->
	<bean id="freemarkerConfig" class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
		<!-- 配置模板路径 -->
		<property name="templateLoaderPath" value="/WEB-INF/ftl/" />
		<!-- 编码格式 -->
		<property name="defaultEncoding" value="UTF-8" />
	</bean>
</beans>


(5)、resource.properties


# 生成静态文件的目录
HTML_URL=D:\\JavaEE\\Nginx\\pinyougou\\


(6)、创建消息监听类ItemPageConsumer.java


package com.pinyougou.itempage.consumer;

import java.io.File;
import java.io.FileWriter;
import java.io.Writer;
import java.util.List;

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

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;

import com.groupentity.Goods;
import com.groupentity.HtmlItem;
import com.pinyougou.itempage.service.GoodsService;
import com.pinyougou.pojo.TbItem;

import freemarker.template.Configuration;
import freemarker.template.Template;

/**
 * 自定义消息监听类
 * 
 * @author wingz
 *
 */
@Component
public class ItemPageConsumer implements MessageListener {

	@Autowired
	private GoodsService goodsService;

	@Autowired
	private FreeMarkerConfigurer freemarkerConfig;

	@Value("${HTML_URL}")
	private String HTML_URL;

	public void onMessage(Message message) {
		TextMessage textMessage = (TextMessage) message;
		try {
			// 获取商品id
			Long goodsId = Long.parseLong(textMessage.getText());
			// 使用Freemarker生成商品的静态页面
			Goods goods = goodsService.findOne(goodsId);
			Configuration configuration = freemarkerConfig.getConfiguration();
			Template template = configuration.getTemplate("item.ftl");
			// 遍历获取SKU
			List<TbItem> itemList = goods.getItemList();
			for (TbItem item : itemList) {
				// 创建组合类
				HtmlItem htmlItem = new HtmlItem();
				htmlItem.setGoods(goods);
				htmlItem.setItem(item);
				Writer out = new FileWriter(new File(HTML_URL + item.getId() + ".html"));
				template.process(htmlItem, out);
				out.close();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}


(7)、GoodsService.java


package com.pinyougou.itempage.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

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

import com.groupentity.Goods;
import com.pinyougou.mapper.TbGoodsDescMapper;
import com.pinyougou.mapper.TbGoodsMapper;
import com.pinyougou.mapper.TbItemCatMapper;
import com.pinyougou.mapper.TbItemMapper;
import com.pinyougou.pojo.TbGoods;
import com.pinyougou.pojo.TbGoodsDesc;
import com.pinyougou.pojo.TbItem;
import com.pinyougou.pojo.TbItemExample;

/**
 * 用于查询商品的类
 * 
 * @author wingz
 *
 */
@Service
public class GoodsService {

	@Autowired
	private TbGoodsMapper goodsMapper;

	@Autowired
	private TbItemCatMapper itemCatMapper;

	@Autowired
	private TbGoodsDescMapper goodsDescMapper;

	@Autowired
	private TbItemMapper itemMapper;

	public Goods findOne(Long goodsId) {
		Goods goods = new Goods();
		// 查询商品信息
		TbGoods tbGoods = goodsMapper.selectByPrimaryKey(goodsId);
		goods.setTbGoods(tbGoods);
		// 获取商品对应的三级分类的名称
		String category1Name = itemCatMapper.selectByPrimaryKey(tbGoods.getCategory1Id()).getName();
		String category2Name = itemCatMapper.selectByPrimaryKey(tbGoods.getCategory2Id()).getName();
		String category3Name = itemCatMapper.selectByPrimaryKey(tbGoods.getCategory3Id()).getName();
		Map<String, String> categoryMap = new HashMap<String, String>();
		categoryMap.put("category1Name", category1Name);
		categoryMap.put("category2Name", category2Name);
		categoryMap.put("category3Name", category3Name);
		goods.setCategoryMap(categoryMap);
		// 查询商品描述
		TbGoodsDesc tbGoodsDesc = goodsDescMapper.selectByPrimaryKey(goodsId);
		goods.setTbGoodsDesc(tbGoodsDesc);
		// 查询SKU表数据
		TbItemExample example = new TbItemExample();
		example.createCriteria().andGoodsIdEqualTo(goodsId);
		List<TbItem> itemList = itemMapper.selectByExample(example);
		goods.setItemList(itemList);
		return goods;
	}
}


(8)、将模板文件拷入WEB-INF目录下




三、

猜你喜欢

转载自blog.csdn.net/wingzhezhe/article/details/80716780