<!--这个是订阅模式 用于删除页面-->
<bean id="topicPageDeleteDestination" class="org.apache.activemq.command.ActiveMQTopic">
<constructor-arg value="pinyougou_topic_page_delete"/>
</bean>
@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, "删除失败");
}
}
<!--删除页面 -->
<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>
/**
*删除商品详细页
*@param goodsId
*@return
*/
public boolean deleteItemHtml(Long[] goodsIds);
@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;
}
}
@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();
}
}
}
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) | 黑马程序员IT技术论坛 X3.2 |