1. 学习计划

1、Activemq整合spring的应用场景

2、添加商品同步索引库

3、商品详情页面动态展示

4、展示详情页面使用缓存

2. Activemq整合spring

2.1. 使用方法

第一步:引用相关的jar包。

  1.     <dependency>
  2. <groupId>org.springframework</groupId>
  3. <artifactId>spring-jms</artifactId>
  4. </dependency>
  5. <dependency>
  6. <groupId>org.springframework</groupId>
  7. <artifactId>spring-context-support</artifactId>
  8. </dependency>

第二步:配置Activemq整合spring。配置ConnectionFactory

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
  4. xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
  5. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  6. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
  7. http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd
  8. http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd
  9. http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd
  10. http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.2.xsd">
  11.  
  12. <!-- 真正可以产生Connection的ConnectionFactory,由对应的 JMS服务厂商提供 -->
  13. <bean id="targetConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
  14. <property name="brokerURL" value="tcp://192.168.25.168:61616" />
  15. </bean>
  16. <!-- Spring用于管理真正的ConnectionFactory的ConnectionFactory -->
  17. <bean id="connectionFactory"
  18. class="org.springframework.jms.connection.SingleConnectionFactory">
  19. <!-- 目标ConnectionFactory对应真实的可以产生JMS Connection的ConnectionFactory -->
  20. <property name="targetConnectionFactory" ref="targetConnectionFactory" />
  21. </bean>
  22. </beans>

第三步:配置生产者。

使用JMSTemplate对象。发送消息。

第四步:在spring容器中配置Destination。

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
  4. xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
  5. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  6. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
  7. http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd
  8. 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
  9. http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.2.xsd">
  10.  
  11. <!-- 真正可以产生Connection的ConnectionFactory,由对应的 JMS服务厂商提供 -->
  12. <bean id="targetConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
  13. <property name="brokerURL" value="tcp://192.168.25.168:61616" />
  14. </bean>
  15. <!-- Spring用于管理真正的ConnectionFactory的ConnectionFactory -->
  16. <bean id="connectionFactory"
  17. class="org.springframework.jms.connection.SingleConnectionFactory">
  18. <!-- 目标ConnectionFactory对应真实的可以产生JMS Connection的ConnectionFactory -->
  19. <property name="targetConnectionFactory" ref="targetConnectionFactory" />
  20. </bean>
  21. <!-- 配置生产者 -->
  22. <!-- Spring提供的JMS工具类,它可以进行消息发送、接收等 -->
  23. <bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
  24. <!-- 这个connectionFactory对应的是我们定义的Spring提供的那个ConnectionFactory对象 -->
  25. <property name="connectionFactory" ref="connectionFactory" />
  26. </bean>
  27. <!--这个是队列目的地,点对点的 -->
  28. <bean id="queueDestination" class="org.apache.activemq.command.ActiveMQQueue">
  29. <constructor-arg>
  30. <value>spring-queue</value>
  31. </constructor-arg>
  32. </bean>
  33. <!--这个是主题目的地,一对多的 -->
  34. <bean id="topicDestination" class="org.apache.activemq.command.ActiveMQTopic">
  35. <constructor-arg value="topic" />
  36. </bean>
  37. </beans>

第五步:代码测试

  1. @Test
  2. public void testSpringActiveMq() throws Exception {
  3. //初始化spring容器
  4. ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:spring/applicationContext-activemq.xml");
  5. //从spring容器中获得JmsTemplate对象
  6. JmsTemplate jmsTemplate = applicationContext.getBean(JmsTemplate.class);
  7. //从spring容器中取Destination对象
  8. Destination destination = (Destination) applicationContext.getBean("queueDestination");
  9. //使用JmsTemplate对象发送消息。
  10. jmsTemplate.send(destination, new MessageCreator() {
  11.  
  12. @Override
  13. public Message createMessage(Session session) throws JMSException {
  14. //创建一个消息对象并返回
  15. TextMessage textMessage = session.createTextMessage("spring activemq queue message");
  16. return textMessage;
  17. }
  18. });
  19. }

2.2. 代码测试

2.2.1. 发送消息

第一步:初始化一个spring容器

第二步:从容器中获得JMSTemplate对象。

第三步:从容器中获得一个Destination对象

第四步:使用JMSTemplate对象发送消息,需要知道Destination

  1. @Test
  2. public void testQueueProducer() throws Exception {
  3. // 第一步:初始化一个spring容器
  4. ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:spring/applicationContext-activemq.xml");
  5. // 第二步:从容器中获得JMSTemplate对象。
  6. JmsTemplate jmsTemplate = applicationContext.getBean(JmsTemplate.class);
  7. // 第三步:从容器中获得一个Destination对象
  8. Queue queue = (Queue) applicationContext.getBean("queueDestination");
  9. // 第四步:使用JMSTemplate对象发送消息,需要知道Destination
  10. jmsTemplate.send(queue, new MessageCreator() {
  11.  
  12. @Override
  13. public Message createMessage(Session session) throws JMSException {
  14. TextMessage textMessage = session.createTextMessage("spring activemq test");
  15. return textMessage;
  16. }
  17. });
  18. }

2.2.2. 接收消息

e3-search-Service中接收消息。

第一步:把Activemq相关的jar包添加到工程中

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

  1. @Test
  2. public void testQueueProducer() throws Exception {
  3. // 第一步:初始化一个spring容器
  4. ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:spring/applicationContext-activemq.xml");
  5. // 第二步:从容器中获得JMSTemplate对象。
  6. JmsTemplate jmsTemplate = applicationContext.getBean(JmsTemplate.class);
  7. // 第三步:从容器中获得一个Destination对象
  8. Queue queue = (Queue) applicationContext.getBean("queueDestination");
  9. // 第四步:使用JMSTemplate对象发送消息,需要知道Destination
  10. jmsTemplate.send(queue, new MessageCreator() {
  11.  
  12. @Override
  13. public Message createMessage(Session session) throws JMSException {
  14. TextMessage textMessage = session.createTextMessage("spring activemq test");
  15. return textMessage;
  16. }
  17. });
  18. }

第三步:配置spring和Activemq整合。

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
  4. xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
  5. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  6. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
  7. http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd
  8. 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
  9. http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.2.xsd">
  10.  
  11. <!-- 真正可以产生Connection的ConnectionFactory,由对应的 JMS服务厂商提供 -->
  12. <bean id="targetConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
  13. <property name="brokerURL" value="tcp://192.168.25.168:61616" />
  14. </bean>
  15. <!-- Spring用于管理真正的ConnectionFactory的ConnectionFactory -->
  16. <bean id="connectionFactory"
  17. class="org.springframework.jms.connection.SingleConnectionFactory">
  18. <!-- 目标ConnectionFactory对应真实的可以产生JMS Connection的ConnectionFactory -->
  19. <property name="targetConnectionFactory" ref="targetConnectionFactory" />
  20. </bean>
  21. <!--这个是队列目的地,点对点的 -->
  22. <bean id="queueDestination" class="org.apache.activemq.command.ActiveMQQueue">
  23. <constructor-arg>
  24. <value>spring-queue</value>
  25. </constructor-arg>
  26. </bean>
  27. <!--这个是主题目的地,一对多的 -->
  28. <bean id="topicDestination" class="org.apache.activemq.command.ActiveMQTopic">
  29. <constructor-arg value="topic" />
  30. </bean>
  31. <!-- 接收消息 -->
  32. <!-- 配置监听器 -->
  33. <bean id="myMessageListener" class="cn.e3mall.search.listener.MyMessageListener" />
  34. <!-- 消息监听容器 -->
  35. <bean class="org.springframework.jms.listener.DefaultMessageListenerContainer">
  36. <property name="connectionFactory" ref="connectionFactory" />
  37. <property name="destination" ref="queueDestination" />
  38. <property name="messageListener" ref="myMessageListener" />
  39. </bean>
  40. </beans>

第四步:测试代码。

  1. @Test
  2. public void testQueueConsumer() throws Exception {
  3. //初始化spring容器
  4. ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:spring/applicationContext-activemq.xml");
  5. //等待
  6. System.in.read();
  7. }

3. 添加商品同步索引库

3.1. Producer

e3-manager-server工程中发送消息。

当商品添加完成后发送一个TextMessage,包含一个商品id。

  1. @Override
  2. public e3Result addItem(TbItem item, String desc) {
  3. // 1、生成商品id
  4. final long itemId = IDUtils.genItemId();
  5. // 2、补全TbItem对象的属性
  6. item.setId(itemId);
  7. //商品状态,1-正常,2-下架,3-删除
  8. item.setStatus((byte) 1);
  9. Date date = new Date();
  10. item.setCreated(date);
  11. item.setUpdated(date);
  12. // 3、向商品表插入数据
  13. itemMapper.insert(item);
  14. // 4、创建一个TbItemDesc对象
  15. TbItemDesc itemDesc = new TbItemDesc();
  16. // 5、补全TbItemDesc的属性
  17. itemDesc.setItemId(itemId);
  18. itemDesc.setItemDesc(desc);
  19. itemDesc.setCreated(date);
  20. itemDesc.setUpdated(date);
  21. // 6、向商品描述表插入数据
  22. itemDescMapper.insert(itemDesc);
  23. //发送一个商品添加消息
  24. jmsTemplate.send(topicDestination, new MessageCreator() {
  25.  
  26. @Override
  27. public Message createMessage(Session session) throws JMSException {
  28. TextMessage textMessage = session.createTextMessage(itemId + "");
  29. return textMessage;
  30. }
  31. });
  32. // 7、e3Result.ok()
  33. return e3Result.ok();
  34. }

3.2. Consumer

3.2.1. 功能分析

1、接收消息。需要创建MessageListener接口的实现类。

2、取消息,取商品id。

3、根据商品id查询数据库。

4、创建一SolrInputDocument对象。

5、使用SolrServer对象写入索引库。

6、返回成功,返回e3Result。

3.2.2. Dao层

根据商品id查询商品信息。

映射文件:

  1. <select id="getItemById" parameterType="long" resultType="cn.e3mall.common.pojo.SearchItem">
  2. SELECT
  3. a.id,
  4. a.title,
  5. a.sell_point,
  6. a.price,
  7. a.image,
  8. b. NAME category_name,
  9. c.item_desc
  10. FROM
  11. tb_item a
  12. JOIN tb_item_cat b ON a.cid = b.id
  13. JOIN tb_item_desc c ON a.id = c.item_id
  14. WHERE a.status = 1
  15. AND a.id=#{itemId}
  16. </select>

3.2.3. Service层

参数:商品ID

业务逻辑:

1、根据商品id查询商品信息。

2、创建一SolrInputDocument对象。

3、使用SolrServer对象写入索引库。

4、返回成功,返回e3Result。

返回值:e3Result

  1. public e3Result addDocument(long itemId) throws Exception {
  2. // 1、根据商品id查询商品信息。
  3. SearchItem searchItem = searchItemMapper.getItemById(itemId);
  4. // 2、创建一SolrInputDocument对象。
  5. SolrInputDocument document = new SolrInputDocument();
  6. // 3、使用SolrServer对象写入索引库。
  7. document.addField("id", searchItem.getId());
  8. document.addField("item_title", searchItem.getTitle());
  9. document.addField("item_sell_point", searchItem.getSell_point());
  10. document.addField("item_price", searchItem.getPrice());
  11. document.addField("item_image", searchItem.getImage());
  12. document.addField("item_category_name", searchItem.getCategory_name());
  13. document.addField("item_desc", searchItem.getItem_desc());
  14. // 5、向索引库中添加文档。
  15. solrServer.add(document);
  16. solrServer.commit();
  17. // 4、返回成功,返回e3Result。
  18. return e3Result.ok();
  19. }

3.2.4. Listener

  1. public class ItemChangeListener implements MessageListener {
  2.  
  3. @Autowired
  4. private SearchItemServiceImpl searchItemServiceImpl;
  5.  
  6. @Override
  7. public void onMessage(Message message) {
  8. try {
  9. TextMessage textMessage = null;
  10. Long itemId = null;
  11. //取商品id
  12. if (message instanceof TextMessage) {
  13. textMessage = (TextMessage) message;
  14. itemId = Long.parseLong(textMessage.getText());
  15. }
  16. //向索引库添加文档
  17. searchItemServiceImpl.addDocument(itemId);
  18.  
  19. } catch (Exception e) {
  20. e.printStackTrace();
  21. }
  22. }
  23.  
  24. }

3.2.5. Spring配置监听

3.2.6. 实现流程

4. 商品详情页面展示

创建一个商品详情页面展示的工程。是一个表现层工程。

4.1. 工程搭建

e3-item-web。打包方式war。可以参考e3-portal-web

4.1.1. Pom文件

  1. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  2. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  3. <modelVersion>4.0.0</modelVersion>
  4. <parent>
  5. <groupId>cn.e3mall</groupId>
  6. <artifactId>e3-parent</artifactId>
  7. <version>0.0.1-SNAPSHOT</version>
  8. </parent>
  9. <groupId>cn.e3mall</groupId>
  10. <artifactId>e3-item-web</artifactId>
  11. <version>0.0.1-SNAPSHOT</version>
  12. <packaging>war</packaging>
  13. <dependencies>
  14. <dependency>
  15. <groupId>cn.e3mall</groupId>
  16. <artifactId>e3-manager-interface</artifactId>
  17. <version>0.0.1-SNAPSHOT</version>
  18. </dependency>
  19. <!-- Spring -->
  20. <dependency>
  21. <groupId>org.springframework</groupId>
  22. <artifactId>spring-context</artifactId>
  23. </dependency>
  24. <dependency>
  25. <groupId>org.springframework</groupId>
  26. <artifactId>spring-beans</artifactId>
  27. </dependency>
  28. <dependency>
  29. <groupId>org.springframework</groupId>
  30. <artifactId>spring-webmvc</artifactId>
  31. </dependency>
  32. <dependency>
  33. <groupId>org.springframework</groupId>
  34. <artifactId>spring-jdbc</artifactId>
  35. </dependency>
  36. <dependency>
  37. <groupId>org.springframework</groupId>
  38. <artifactId>spring-aspects</artifactId>
  39. </dependency>
  40. <dependency>
  41. <groupId>org.springframework</groupId>
  42. <artifactId>spring-jms</artifactId>
  43. </dependency>
  44. <dependency>
  45. <groupId>org.springframework</groupId>
  46. <artifactId>spring-context-support</artifactId>
  47. </dependency>
  48. <!-- JSP相关 -->
  49. <dependency>
  50. <groupId>jstl</groupId>
  51. <artifactId>jstl</artifactId>
  52. </dependency>
  53. <dependency>
  54. <groupId>javax.servlet</groupId>
  55. <artifactId>servlet-api</artifactId>
  56. <scope>provided</scope>
  57. </dependency>
  58. <dependency>
  59. <groupId>javax.servlet</groupId>
  60. <artifactId>jsp-api</artifactId>
  61. <scope>provided</scope>
  62. </dependency>
  63. <!-- dubbo相关 -->
  64. <dependency>
  65. <groupId>com.alibaba</groupId>
  66. <artifactId>dubbo</artifactId>
  67. <!-- 排除依赖 -->
  68. <exclusions>
  69. <exclusion>
  70. <groupId>org.springframework</groupId>
  71. <artifactId>spring</artifactId>
  72. </exclusion>
  73. <exclusion>
  74. <groupId>org.jboss.netty</groupId>
  75. <artifactId>netty</artifactId>
  76. </exclusion>
  77. </exclusions>
  78. </dependency>
  79. <dependency>
  80. <groupId>org.apache.zookeeper</groupId>
  81. <artifactId>zookeeper</artifactId>
  82. </dependency>
  83. <dependency>
  84. <groupId>com.github.sgroschupf</groupId>
  85. <artifactId>zkclient</artifactId>
  86. </dependency>
  87. <dependency>
  88. <groupId>junit</groupId>
  89. <artifactId>junit</artifactId>
  90. </dependency>
  91. </dependencies>
  92. <!-- 配置tomcat插件 -->
  93. <build>
  94. <plugins>
  95. <plugin>
  96. <groupId>org.apache.tomcat.maven</groupId>
  97. <artifactId>tomcat7-maven-plugin</artifactId>
  98. <configuration>
  99. <port>8086</port>
  100. <path>/</path>
  101. </configuration>
  102. </plugin>
  103. </plugins>
  104. </build>
  105. </project>

4.2. 功能分析

在搜索结果页面点击商品图片或者商品标题,展示商品详情页面。

请求的url:/item/{itemId}

参数:商品id

返回值:String 逻辑视图

业务逻辑:

1、从url中取参数,商品id

2、根据商品id查询商品信息(tb_item)得到一个TbItem对象,缺少images属性,可以创建一个pojo继承TbItem,添加一个getImages方法。在e3-item-web工程中。

  1. public class Item extends TbItem {
  2.  
  3. public String[] getImages() {
  4. String image2 = this.getImage();
  5. if (image2 != null && !"".equals(image2)) {
  6. String[] strings = image2.split(",");
  7. return strings;
  8. }
  9. return null;
  10. }
  11.  
  12. public Item() {
  13.  
  14. }
  15.  
  16. public Item(TbItem tbItem) {
  17. this.setBarcode(tbItem.getBarcode());
  18. this.setCid(tbItem.getCid());
  19. this.setCreated(tbItem.getCreated());
  20. this.setId(tbItem.getId());
  21. this.setImage(tbItem.getImage());
  22. this.setNum(tbItem.getNum());
  23. this.setPrice(tbItem.getPrice());
  24. this.setSellPoint(tbItem.getSellPoint());
  25. this.setStatus(tbItem.getStatus());
  26. this.setTitle(tbItem.getTitle());
  27. this.setUpdated(tbItem.getUpdated());
  28. }
  29.  
  30. }

4、展示到页面。3、根据商品id查询商品描述。

4.3. Dao层

查询tb_item, tb_item_desc两个表,都是单表查询。可以使用逆向工程。

4.4. Service层

1、根据商品id查询商品信息

参数:商品id

返回值:TbItem

2、根据商品id查询商品描述

参数:商品id

返回值:TbItemDesc

  1. @Override
  2. public TbItemDesc getItemDescById(long itemId) {
  3. TbItemDesc itemDesc = itemDescMapper.selectByPrimaryKey(itemId);
  4. return itemDesc;
  5. }

4.5. 表现层

4.5.1. Controller

请求的url:/item/{itemId}

参数:商品id

返回值:String 逻辑视图

  1. @Controller
  2. public class ItemController {
  3.  
  4. @Autowired
  5. private ItemService itemService;
  6.  
  7. @RequestMapping("/item/{itemId}")
  8. public String showItemInfo(@PathVariable Long itemId, Model model) {
  9. //跟据商品id查询商品信息
  10. TbItem tbItem = itemService.getItemById(itemId);
  11. //把TbItem转换成Item对象
  12. Item item = new Item(tbItem);
  13. //根据商品id查询商品描述
  14. TbItemDesc tbItemDesc = itemService.getItemDescById(itemId);
  15. //把数据传递给页面
  16. model.addAttribute("item", item);
  17. model.addAttribute("itemDesc", tbItemDesc);
  18. return "item";
  19. }
  20. }

4.6. 向业务逻辑中添加缓存

4.6.1. 缓存添加分析

使用redis做缓存。

业务逻辑:

1、根据商品id到缓存中查找

2、查到缓存,直接返回。

3、查不到,查询数据库

4、把数据放到缓存中

5、返回数据

缓存中缓存热点数据,提供缓存的使用率。需要设置缓存的有效期。一般是一天的时间,可以根据实际情况跳转。

需要使用String类型来保存商品数据。

可以加前缀方法对象redis中的key进行归类。

ITEM_INFO:123456:BASE

ITEM_INFO:123456:DESC

如果把二维表保存到redis中:

1、表名就是第一层

2、主键是第二层

3、字段名第三次

三层使用“:”分隔作为key,value就是字段中的内容。

4.6.2. 把redis相关的jar包添加到工程

4.6.3. 添加缓存

  1. @Override
  2. public TbItem getItemById(long itemId) {
  3. try {
  4. //查询缓存
  5. String json = jedisClient.get(ITEM_INFO_PRE + ":" + itemId + ":BASE");
  6. if (StringUtils.isNotBlank(json)) {
  7. //把json转换为java对象
  8. TbItem item = JsonUtils.jsonToPojo(json, TbItem.class);
  9. return item;
  10. }
  11. } catch (Exception e) {
  12. e.printStackTrace();
  13. }
  14. //根据商品id查询商品信息
  15. //TbItem tbItem = itemMapper.selectByPrimaryKey(itemId);
  16. TbItemExample example = new TbItemExample();
  17. //设置查询条件
  18. Criteria criteria = example.createCriteria();
  19. criteria.andIdEqualTo(itemId);
  20. List<TbItem> list = itemMapper.selectByExample(example);
  21. if (list != null && list.size() > 0) {
  22. TbItem item = list.get(0);
  23. try {
  24. //把数据保存到缓存
  25. jedisClient.set(ITEM_INFO_PRE + ":" + itemId + ":BASE", JsonUtils.objectToJson(item));
  26. //设置缓存的有效期
  27. jedisClient.expire(ITEM_INFO_PRE + ":" + itemId + ":BASE", ITEM_INFO_EXPIRE);
  28. } catch (Exception e) {
  29. e.printStackTrace();
  30. }
  31. return item;
  32. }
  33. return null;
  34. }

取商品描述添加缓存:

  1. @Override
  2. public TbItemDesc getItemDescById(long itemId) {
  3. try {
  4. String json = jedisClient.get(ITEM_INFO_PRE + ":" + itemId + ":DESC");
  5. //判断缓存是否命中
  6. if (StringUtils.isNotBlank(json) ) {
  7. //转换为java对象
  8. TbItemDesc itemDesc = JsonUtils.jsonToPojo(json, TbItemDesc.class);
  9. return itemDesc;
  10. }
  11. } catch (Exception e) {
  12. e.printStackTrace();
  13. }
  14. TbItemDesc itemDesc = itemDescMapper.selectByPrimaryKey(itemId);
  15. try {
  16. jedisClient.set(ITEM_INFO_PRE + ":" + itemId + ":DESC", JsonUtils.objectToJson(itemDesc));
  17. //设置过期时间
  18. jedisClient.expire(ITEM_INFO_PRE + ":" + itemId + ":DESC", ITEM_INFO_EXPIRE);
  19. } catch (Exception e) {
  20. e.printStackTrace();
  21. }
  22. return itemDesc;
  23. }

JAVAEE——宜立方商城09:Activemq整合spring的应用场景、添加商品同步索引库、商品详情页面动态展示与使用缓存的更多相关文章

  1. JAVAEE——宜立方商城07:Linux上搭建Solr服务、数据库导入索引库、搜索功能的实现

    1. 学习计划 1.Solr服务搭建 2.Solrj使用测试 3.把数据库中的数据导入索引库 4.搜索功能的实现 2. Solr服务搭建 2.1. Solr的环境 Solr是java开发. 需要安装j ...

  2. JAVAEE——宜立方商城08:Zookeeper+SolrCloud集群搭建、搜索功能切换到集群版、Activemq消息队列搭建与使用

    1. 学习计划 1.solr集群搭建 2.使用solrj管理solr集群 3.把搜索功能切换到集群版 4.添加商品同步索引库. a) Activemq b) 发送消息 c) 接收消息 2. 什么是So ...

  3. JAVAEE——宜立方商城01:电商行业的背景、商城系统架构、后台工程搭建、SSM框架整合

    1. 学习计划 第一天: 1.电商行业的背景. 2.宜立方商城的系统架构 a) 功能介绍 b) 架构讲解 3.工程搭建-后台工程 a) 使用maven搭建工程 b) 使用maven的tomcat插件启 ...

  4. JAVAEE——宜立方商城02:服务中间件dubbo、工程改造为基于soa架构、商品列表实现

    1. 学习计划 第二天:商品列表功能实现 1.服务中间件dubbo 2.工程改造为基于soa架构 3.商品列表查询功能实现. 2. 将工程改造为SOA架构 2.1. 分析 由于宜立方商城是基于soa的 ...

  5. JAVAEE——宜立方商城10:使用freemarker实现网页静态化、ActiveMq同步生成静态网页、Sso单点登录系统分析

    1. 学习计划 1.使用freemarker实现网页静态化 2.ActiveMq同步生成静态网页 2. 网页静态化 可以使用Freemarker实现网页静态化. 2.1. 什么是freemarker ...

  6. JAVAEE——宜立方商城05:前台系统搭建、首页展示、Cms系统的实现

    1. 学习计划 1.前台系统搭建 2.商城首页展示 3.Cms系统的实现 a) 内容分类管理 b) 内容管理 4.前台内容动态展示 2. 商城首页展示 系统架构: 页面位置: 2.1. 工程搭建 可以 ...

  7. JAVAEE——宜立方商城12:购物车实现、订单确认页面展示

    1. 学习计划 第十二天: 1.购物车实现 2.订单确认页面展示 2. 购物车的实现 2.1. 功能分析 1.购物车是一个独立的表现层工程. 2.添加购物车不要求登录.可以指定购买商品的数量. 3.展 ...

  8. JAVAEE——宜立方商城06:Redis安装、数据类型和持久化方案、Redis集群分析与搭建、实现缓存和同步

    1. 学习计划 1.首页轮播图展示 2.Redis服务器搭建 3.向业务逻辑中添加缓存. 4.使用redis做缓存 5.缓存同步. 2. 首页轮播图动态展示 2.1. 功能分析 根据分类id查询内容列 ...

  9. JAVAEE——宜立方商城11:sso登录注册功能实现、通过token获得用户信息、Ajax跨域请求(jsonp)

    1. 学习计划 第十一天: 1.sso注册功能实现 2.sso登录功能实现 3.通过token获得用户信息 4.Ajax跨域请求(jsonp) 2. Sso系统工程搭建 需要创建一个sso服务工程,可 ...

随机推荐

  1. asp.net后台操作javascript:confirm返回值

    在asp.net中使用confirm可以分为两种: 1.没有使用ajax,confirm会引起也面刷新 2.使用了ajax,不会刷新 A.没有使用ajax,可以用StringBuilder来完成. ( ...

  2. 基本控件文档-UITextField属性

    CHENYILONG Blog 基本控件文档-UITextField属性 Fullscreen   UITextField属性技术博客http://www.cnblogs.com/ChenYilong ...

  3. 【译】第九篇 SQL Server代理了解作业和安全

    本篇文章是SQL Server代理系列的第九篇,详细内容请参考原文 在这一系列的上一篇,学习了如何在SQL Server代理作业步骤启动外部程序.你可以使用过时的ActiveX系统,运行批处理命令脚本 ...

  4. classList属性

    1.传统方法: 在操作类名的时候,需要通过className属性添加.删除和替换类名.如下面例子: ? 1 <div class="bd user disabled"> ...

  5. [转]closed-form solution (闭合解/解析解)和数值解的理解

    参考整理自:http://hi.baidu.com/cjb366/item/7290773b2d2eb9f2a9842873 closed-form solution :一般翻译为闭合解/解析解.这一 ...

  6. 20165230 《Java程序设计》实验三 敏捷开发与XP实践 实验报告

    20165230 <Java程序设计>实验三 敏捷开发与XP实践 实验报告 一.实验报告封面 课程:Java程序设计 班级:1652班 姓名:田坤烨 学号:20165230 成绩: 指导教 ...

  7. 【navicat112_premium】navicat112_premium数据库连接工具安装过程

    此工具及其方便,可以连接mysql.oracle.sqlserver登数据库... 1.下载安装包Navicat Premium_11.2.7简体中文版.rar 下载地址:http://qiaoliq ...

  8. linux nginx大量TIME_WAIT的解决办法--转

    netstat -n | awk '/^tcp/ {++S[$NF]} END {for(a in S) print a, S[a]}' TIME_WAIT 8535 CLOSE_WAIT 5 FIN ...

  9. fc26 url

    aarch64 http://linux.yz.yamagata-u.ac.jp/pub/linux/fedora-projects/fedora-secondary/releases/26/Ever ...

  10. aarch64_g4

    golang-github-inconshreveable-muxado-devel-0-0.7.gitf693c7e.fc26.noarch.rpm 2017-02-11 16:47 30K fed ...