商品删除-删除商品详细页 7.1 需求分析执行商品删除后,同时删除每个服务器上的商品详细页 7.2 消息生产者(运营商后台) 7.2.1 配置文件 修改 spring-activemq.xml,添加配置 [AppleScript] 纯文本查看 复制代码 <!--这个是订阅模式 用于删除页面-->
<bean id="topicPageDeleteDestination" class="org.apache.activemq.command.ActiveMQTopic">
<constructor-arg value="pinyougou_topic_page_delete"/>
</bean> 7.1.1 代码实现 修改 GoodsController.java [AppleScript] 纯文本查看 复制代码 @Autowired
private Destination topicPageDeleteDestination;//用于删除静态网页的消息
/**
*批量删除
*@param ids
*@return
*/
@RequestMapping("/delete")
public Result delete(final Long [] ids){
try {
goodsService.delete(ids); jmsTemplate.send(queueSolrDeleteDestination, new MessageCreator() {
@Override
public Message createMessage(Session session) throws JMSException {
return session.createObjectMessage(ids);
}
});
//删除页面
jmsTemplate.send(topicPageDeleteDestination, new MessageCreator() { @Override
public Message createMessage(Session session) throws JMSException {
return session.createObjectMessage(ids);
}
});
return new Result(true, "删除成功");
} catch (Exception e) { e.printStackTrace();
return new Result(false, "删除失败");
}
}
7.1 消息消费者(页面生成服务)7.1.1 配置文件修改 pinyougou-page-service 的 applicationContext-activemq-consumer.xml [AppleScript] 纯文本查看 复制代码 <!--删除页面 -->
<bean id="topicPageDeleteDestination" class="org.apache.activemq.command.ActiveMQTopic">
<constructor-arg value="pinyougou_topic_page_delete"/>
</bean>
<!-- 消息监听容器 -->
<bean class="org.springframework.jms.listener.DefaultMessageListenerContainer">
<property name="connectionFactory" ref="connectionFactory" />
<property name="destination" ref="topicPageDeleteDestination" />
<property name="messageListener" ref="pageDeleteListener" />
</bean> 7.1.1 代码实现 (1)修改 pinyougou-page-interface 的 ItemPageService.java [AppleScript] 纯文本查看 复制代码 /**
*删除商品详细页
*@param goodsId
*@return
*/
public boolean deleteItemHtml(Long[] goodsIds); (2)修改 pinyougou-page-service 的 ItemPageServiceImpl.java [AppleScript] 纯文本查看 复制代码 @Override
public boolean deleteItemHtml(Long[] goodsIds) {
try {
for(Long goodsId:goodsIds){
new File(pagedir+goodsId+".html").delete();
}
return true;
} catch (Exception e) { e.printStackTrace(); return false;
}
} (3)创建监听类 PageDeleteListener [AppleScript] 纯文本查看 复制代码 @Component
public class PageDeleteListener implements MessageListener {
@Autowired
private ItemPageService itemPageService;
@Override
public void onMessage(Message message) {
ObjectMessage objectMessage= (ObjectMessage)message;
try {
Long[] goodsIds = (Long[]) objectMessage.getObject();
System.out.println("ItemDeleteListener 监听接收到消息..."+goodsIds);
boolean b = itemPageService.deleteItemHtml(goodsIds);
System.out.println("网页删除结果:"+b);
} catch (JMSException e) { e.printStackTrace();
}
}
}
|