Freemarker静态化ActiveMQ实现
生活随笔
收集整理的這篇文章主要介紹了
Freemarker静态化ActiveMQ实现
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
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 {@Autowiredprivate ItemService itemService;@Autowiredprivate FreeMarkerConfigurer freeMarkerConfigurer;@Value("${HTML_OUT_PATH}")private String HTML_OUT_PATH;@Overridepublic void onMessage(Message message) {try {//從消息中取商品idTextMessage 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.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsdhttp://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.xsdhttp://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>總結
以上是生活随笔為你收集整理的Freemarker静态化ActiveMQ实现的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Freemarker商品页面静态化
- 下一篇: Spring整合Hibernate