02 教案-AB-3 001 教案-3 008 宜立方商城-day09-v3 0_第1页
02 教案-AB-3 001 教案-3 008 宜立方商城-day09-v3 0_第2页
02 教案-AB-3 001 教案-3 008 宜立方商城-day09-v3 0_第3页
02 教案-AB-3 001 教案-3 008 宜立方商城-day09-v3 0_第4页
02 教案-AB-3 001 教案-3 008 宜立方商城-day09-v3 0_第5页
已阅读5页,还剩15页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

宜立方商城第九天1. 课程计划第八天:1、 Activemq整合springMQ的应用场景2、 添加商品同步索引库3、 商品详情页面动态展示4、 展示详情页面使用缓存2. Activemq整合spring2.1. 使用方法第一步:引用相关的jar包。org.springframeworkspring-jmsorg.springframeworkspring-context-support第二步:配置Activemq整合spring。配置ConnectionFactory第三步:配置生产者。使用JMSTemplate对象。发送消息。第四步:在spring容器中配置Destination。spring-queue第五步:代码测试Testpublic void testSpringActiveMq() throws Exception /初始化spring容器ApplicationContext applicationContext = new ClassPathXmlApplicationContext(classpath:spring/applicationContext-activemq.xml);/从spring容器中获得JmsTemplate对象JmsTemplate jmsTemplate = applicationContext.getBean(JmsTemplate.class);/从spring容器中取Destination对象Destination destination = (Destination) applicationContext.getBean(queueDestination);/使用JmsTemplate对象发送消息。jmsTemplate.send(destination, new MessageCreator() Overridepublic Message createMessage(Session session) throws JMSException /创建一个消息对象并返回TextMessage textMessage = session.createTextMessage(spring activemq queue message);return textMessage;);2.2. 代码测试2.2.1. 发送消息第一步:初始化一个spring容器第二步:从容器中获得JMSTemplate对象。第三步:从容器中获得一个Destination对象第四步:使用JMSTemplate对象发送消息,需要知道DestinationTestpublic void testQueueProducer() throws Exception / 第一步:初始化一个spring容器ApplicationContext applicationContext = new ClassPathXmlApplicationContext(classpath:spring/applicationContext-activemq.xml);/ 第二步:从容器中获得JMSTemplate对象。JmsTemplate jmsTemplate = applicationContext.getBean(JmsTemplate.class);/ 第三步:从容器中获得一个Destination对象Queue queue = (Queue) applicationContext.getBean(queueDestination);/ 第四步:使用JMSTemplate对象发送消息,需要知道DestinationjmsTemplate.send(queue, new MessageCreator() Overridepublic Message createMessage(Session session) throws JMSException TextMessage textMessage = session.createTextMessage(spring activemq test);return textMessage;);2.2.2. 接收消息e3-search-Service中接收消息。第一步:把Activemq相关的jar包添加到工程中第二步:创建一个MessageListener的实现类。public class MyMessageListener implements MessageListener Overridepublic void onMessage(Message message) try TextMessage textMessage = (TextMessage) message;/取消息内容String text = textMessage.getText();System.out.println(text); catch (JMSException e) e.printStackTrace();第三步:配置spring和Activemq整合。spring-queue第四步:测试代码。Testpublic void testQueueConsumer() throws Exception /初始化spring容器ApplicationContext applicationContext = new ClassPathXmlApplicationContext(classpath:spring/applicationContext-activemq.xml);/等待System.in.read();3. 添加商品同步索引库3.1. Producere3-manager-server工程中发送消息。当商品添加完成后发送一个TextMessage,包含一个商品id。Overridepublic e3Result addItem(TbItem item, String desc) / 1、生成商品idfinal long itemId = IDUtils.genItemId();/ 2、补全TbItem对象的属性item.setId(itemId);/商品状态,1-正常,2-下架,3-删除item.setStatus(byte) 1);Date date = new Date();item.setCreated(date);item.setUpdated(date);/ 3、向商品表插入数据itemMapper.insert(item);/ 4、创建一个TbItemDesc对象TbItemDesc itemDesc = new TbItemDesc();/ 5、补全TbItemDesc的属性itemDesc.setItemId(itemId);itemDesc.setItemDesc(desc);itemDesc.setCreated(date);itemDesc.setUpdated(date);/ 6、向商品描述表插入数据itemDescMapper.insert(itemDesc);/发送一个商品添加消息jmsTemplate.send(topicDestination, new MessageCreator() Overridepublic Message createMessage(Session session) throws JMSException TextMessage textMessage = session.createTextMessage(itemId + );return textMessage;);/ 7、e3Result.ok()return e3Result.ok();3.2. Consumer3.2.1. 功能分析1、 接收消息。需要创建MessageListener接口的实现类。2、 取消息,取商品id。3、 根据商品id查询数据库。4、 创建一SolrInputDocument对象。5、 使用SolrServer对象写入索引库。6、 返回成功,返回e3Result。3.2.2. Dao层根据商品id查询商品信息。映射文件:SELECTa.id,a.title,a.sell_point,a.price,a.image,b. NAME category_name,c.item_descFROMtb_item aJOIN tb_item_cat b ON a.cid = b.idJOIN tb_item_desc c ON a.id = c.item_idWHERE a.status = 1 AND a.id=#itemId3.2.3. Service层参数:商品ID业务逻辑:1、 根据商品id查询商品信息。2、 创建一SolrInputDocument对象。3、 使用SolrServer对象写入索引库。4、 返回成功,返回e3Result。返回值:e3Resultpublic e3Result addDocument(long itemId) throws Exception / 1、根据商品id查询商品信息。SearchItem searchItem = searchItemMapper.getItemById(itemId);/ 2、创建一SolrInputDocument对象。SolrInputDocument document = new SolrInputDocument();/ 3、使用SolrServer对象写入索引库。document.addField(id, searchItem.getId();document.addField(item_title, searchItem.getTitle();document.addField(item_sell_point, searchItem.getSell_point();document.addField(item_price, searchItem.getPrice();document.addField(item_image, searchItem.getImage();document.addField(item_category_name, searchItem.getCategory_name();document.addField(item_desc, searchItem.getItem_desc();/ 5、向索引库中添加文档。solrServer.add(document);solrSmit();/ 4、返回成功,返回e3Result。return e3Result.ok();3.2.4. Listenerpublic class ItemChangeListener implements MessageListener Autowiredprivate SearchItemServiceImpl searchItemServiceImpl;Overridepublic void onMessage(Message message) try TextMessage textMessage = null;Long itemId = null; /取商品idif (message instanceof TextMessage) textMessage = (TextMessage) message;itemId = Long.parseLong(textMessage.getText();/向索引库添加文档searchItemServiceImpl.addDocument(itemId); catch (Exception e) e.printStackTrace();3.2.5. Spring配置监听e3-managere3-searchActiveMQ|-item-add-topic添加商品发送消息ItemAddMessageListener将商品添加到索引库3.2.6. 实现流程4. 商品详情页面展示创建一个商品详情页面展示的工程。是一个表现层工程。4.1. 工程搭建e3-item-web。打包方式war。可以参考e3-portal-web4.1.1. Pom文件4.0.0cn.e3malle3-parent0.0.1-SNAPSHOTcn.e3malle3-item-web0.0.1-SNAPSHOTwarcn.e3malle3-manager-interface0.0.1-SNAPSHOTtynettyorg.apache.zookeeperzookeepercom.github.sgroschupfzkclientjunitjunitorg.apache.tomcat.maventomcat7-maven-plugin8086/4.2. 功能分析在搜索结果页面点击商品图片或者商品标题,展示商品详情页面。请求的url:/item/itemId参数:商品id返回值:String 逻辑视图业务逻辑:1、 从url中取参数,商品id2、 根据商品id查询商品信息(tb_item)得到一个TbItem对象,缺少images属性,可以创建一个pojo继承TbItem,添加一个getImages方法。在e3-item-web工程中。public class Item extends TbItem public String getImages() String image2 = this.getImage();if (image2 != null & !.equals(image2) String strings = image2.split(,);return strings;return null;public Item() public Item(TbItem tbItem) this.setBarcode(tbItem.getBarcode();this.setCid(tbItem.getCid();this.setCreated(tbItem.getCreated();this.setId(tbItem.getId();this.setImage(tbItem.getImage();this.setNum(tbItem.getNum();this.setPrice(tbItem.getPrice();this.setSellPoint(tbItem.getSellPoint();this.setStatus(tbItem.getStatus();this.setTitle(tbItem.getTitle();this.setUpdated(tbItem.getUpdated();3、 根据商品id查询商品描述。4、 展示到页面。4.3. Dao层查询tb_item, tb_item_desc两个表,都是单表查询。可以使用逆向工程。4.4. Service层1、 根据商品id查询商品信息参数:商品id返回值:TbItem2、 根据商品id查询商品描述参数:商品id返回值:TbItemDescOverridepublic TbItemDesc getItemDescById(long itemId) TbItemDesc itemDesc = itemDescMapper.selectByPrimaryKey(itemId);return itemDesc;4.5. 表现层4.5.1. Controller请求的url:/item/itemId参数:商品id返回值:String 逻辑视图Controllerpublic class ItemController Autowiredprivate ItemService itemService;RequestMapping(/item/itemId)public String showItemInfo(PathVariable Long itemId, Model model) /跟据商品id查询商品信息TbItem tbItem = itemService.getItemById(itemId);/把TbItem转换成Item对象Item item = new Item(tbItem);/根据商品id查询商品描述TbItemDesc tbItemDesc = itemService.getItemDescById(itemId);/把数据传递给页面model.addAttribute(item, item);model.addAttribute(itemDesc, tbItemDesc);return item;4.6. 向业务逻辑中添加缓存4.6.1. 缓存添加分析使用redis做缓存。业务逻辑:1、 根据商品id到缓存中命中2、 查到缓存,直接返回。3、 差不到,查询数据库4、 把数据放到缓存中5、 返回数据缓存中缓存热点数据,提供缓存的使用率。需要设置缓存的有效期。一般是一天的时间,可以根据实际情况跳转。需要使用String类型来保存商品数据。可以加前缀方法对象redis中的key进行归类。ITEM_INFO:123456:BASEITEM_INFO:123456:DESC如果把二维表保存到redis中:1、 表名就是第一层2、 主键是第二层3、 字段名第三次三层使用“:”分隔作为key,value就是字段中的内容。4.6.2. 把redis相关的jar包添加到工程4.6.3. 添加缓存Overridepublic TbItem getItemById(long itemId) try /查询缓存String json = jedisClient.get(ITEM_INFO_PRE + : + itemId + :BASE);if (StringUtils.isNotBlank(json) /把json转换为java对象TbItem item = JsonUtils.jsonToPojo(json, TbItem.class);return item; catch (Exception e) e.printStackTrace();/根据商品id查询商品信息/TbItem tbItem = itemMapper.selectByPrimaryKey(itemId);TbItemExample example = new TbItemExample();/设置查询条件Criteria criteria = example.createCriteria();criteria.andIdEqualTo(itemId);List list = itemMapper.selectByExample(example);if (list != null & list.size(

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论