业务需求:
监听商品添加消息,获取商品ID,使用freemarker生成静态页面
配置freemarker
springmvc.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:dubbo="http://code.alibabatech.com/schema/dubbo" xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd
http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd">
<!-- 加载配置文件 -->
<context:property-placeholder location="classpath:conf/resource.properties" />
<context:component-scan base-package="lx.test.item.controller" />
<mvc:annotation-driven />
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</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>
<!-- 引用dubbo服务 -->
<dubbo:application name="e3-item-web" />
<dubbo:registry protocol="zookeeper" address="192.168.25.200:2181" />
<dubbo:reference interface="lx.test.service.ItemService"
id="itemService" />
</beans>
配置activemq接收端
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.200: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="topicDestination" class="org.apache.activemq.command.ActiveMQTopic">
<constructor-arg value="itemAddTopic" />
</bean>
<!-- 消息监听容器 -->
<bean id="htmlGenListener" class="lx.test.item.listener.HtmlGenListener"/>
<bean class="org.springframework.jms.listener.DefaultMessageListenerContainer">
<property name="connectionFactory" ref="connectionFactory" />
<property name="destination" ref="topicDestination" />
<property name="messageListener" ref="htmlGenListener" />
</bean>
</beans>
监听器
HtmlGenListener.java
package lx.test.item.listener;
import java.io.File;
import java.io.FileWriter;
import java.io.Writer;
import java.util.HashMap;
import java.util.Map;
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.web.servlet.view.freemarker.FreeMarkerConfigurer;
import freemarker.template.Configuration;
import freemarker.template.Template;
import lx.test.item.pojo.Item;
import lx.test.pojo.TbItem;
import lx.test.pojo.TbItemDesc;
import lx.test.service.ItemService;
/**
* 监听商品添加消息,生成对应的静态页面
* @author lx
*
*/
public class HtmlGenListener implements MessageListener{
@Autowired
private ItemService itemService;
@Autowired
private FreeMarkerConfigurer freeMarkerConfigurer;
@Value("${HTML_GEN_PATH}")
private String HTML_GEN_PATH;
@Override
public void onMessage(Message message) {
//创建一个模板,参考jsp
//从消息中取商品id
try {
TextMessage textMessage = (TextMessage) message;
String text = textMessage.getText();
Long itemid = new Long(text);
//等待事务提交
Thread.sleep(1000);
//根据商品id查询商品信息,商品基本信息和商品描述。
TbItem tbItem = itemService.getItemById(itemid);
Item item = new Item(tbItem);
//取商品描述
TbItemDesc tbItemDesc = itemService.getItemDescById(itemid);
//创建一个数据集,把商品数据封装
Map data = new HashMap<>();
data.put("item", item);
data.put("itemDesc", tbItemDesc);
//加载模板对象
Configuration configuration = freeMarkerConfigurer.getConfiguration();
configuration.setDefaultEncoding("utf-8");
Template template = configuration.getTemplate("item.ftl");
//创建一个输出流,指定输出的目录及文件名。
Writer out = new FileWriter(new File(HTML_GEN_PATH+itemid+".html"));
//生成静态页面。
template.process(data, out);
//关闭流
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}