Freemarker
实现商品页面静态化
ActiveMQ
使用topic模式,添加商品时
监听到消息,根据商品id
从数据库中查询商品信息,生成静态页面
注意
不从redis中获取商品信息
添加商品时,redis没有该商品信息
根据商品id,从数据库中查询商品信息
不直接传递商品信息,而是根据id,从数据库中查询
因为,不在一个项目中,静态化页面是一个独立的项目
传输数据的效率不高,修改商品信息时,也需要更新静态页面
引入ActiveMQ
<!-- activemq的jar包 -->
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-all</artifactId>
</dependency>
监听器
ItemAddMesssageListener
public class ItemAddMesssageListener implements MessageListener {
@Autowired
private ItemService itemService;
@Autowired
private FreeMarkerConfigurer freeMarkerConfigurer;
@Value("${HTML_OUT_PATH}")
private String HTML_OUT_PATH;
@Override
public void onMessage(Message message) {
try {
//从消息中取商品id
TextMessage textMessage = (TextMessage) message;
String strId = textMessage.getText();
Long itemId = Long.parseLong(strId);
//等待事务提交
Thread.sleep(1000);
//根据商品id查询商品信息及商品描述
TbItem tbItem = itemService.getItemById(itemId);
Item item = new Item(tbItem);
TbItemDesc itemDesc = itemService.getItemDescById(itemId);
//使用freemarker生成静态页面
Configuration configuration = freeMarkerConfigurer.getConfiguration();
//1.创建模板
//2.加载模板对象
Template template = configuration.getTemplate("item.ftl");
//3.准备模板需要的数据
Map data = new HashMap<>();
data.put("item", item);
data.put("itemDesc", itemDesc);
//4.指定输出的目录及文件名
Writer out = new FileWriter(new File(HTML_OUT_PATH + strId + ".html"));
//5.生成静态页面
template.process(data, out);
//关闭流
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
配置监听器
springmvc-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">
<!-- JMS服务厂商提供的ConnectionFactory -->
<bean id="targetConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
<constructor-arg name="brokerURL" value="tcp://192.168.25.168:61616"/>
</bean>
<!-- spring对象ConnectionFactory的封装 -->
<bean id="connectionFactory" class="org.springframework.jms.connection.SingleConnectionFactory">
<property name="targetConnectionFactory" ref="targetConnectionFactory"></property>
</bean>
<bean id="itemAddTopic" class="org.apache.activemq.command.ActiveMQTopic">
<constructor-arg name="name" value="item-add-topic"></constructor-arg>
</bean>
<bean id="itemAddMessageListener" class="com.taotao.item.listener.ItemAddMesssageListener"/>
<bean class="org.springframework.jms.listener.DefaultMessageListenerContainer">
<property name="connectionFactory" ref="connectionFactory" />
<property name="destination" ref="itemAddTopic" />
<property name="messageListener" ref="itemAddMessageListener" />
</bean>
</beans>
加载配置文件
Web.xml中配置Servlet
<!-- 前端控制器 -->
<servlet>
<servlet-name>taotao-item-web</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- contextConfigLocation不是必须的, 如果不配置contextConfigLocation, springmvc的配置文件默认在:WEB-INF/servlet的name+"-servlet.xml" -->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/springmvc*.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>