當前位置:
首頁 >
ActiveMQ添加商品发送消息
發布時間:2025/3/20
54
豆豆
生活随笔
收集整理的這篇文章主要介紹了
ActiveMQ添加商品发送消息
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
添加商品
需要同步索引庫,同步緩存,生成靜態頁面等等
很多地方,都需要監聽添加商品的事件,所以,這里使用Topic模式
方案選擇
方案一:直接寫業務邏輯
在商品服務模塊,添加商品的業務邏輯中,添加同步索引庫的業務邏輯
缺點:業務邏輯耦合度高,業務拆分不明確
方案二:模塊之間服務調用
在商品服務模塊,添加商品業務邏輯服務中,調用索引服務模塊,添加索引的服務,業務邏輯分開,通過服務調用
缺點:服務之間耦合度高,服務的啟動有先后順序
方案三:使用消息隊列
在商品服務模塊,添加商品業務邏輯中,通過消息中間件發布消息,然后索引模塊中,通過監聽接收消息,并添加索引
消息中間件可以使用ActiveMQ、RabbitMQ、kafka
JMSTemplate
配置信息
<!-- 配置JMSTemplate --> <bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate"><property name="connectionFactory" ref="connectionFactory"/> </bean> <!-- 配置消息的Destination對象 --> <bean id="test-queue" class="org.apache.activemq.command.ActiveMQQueue"><constructor-arg name="name" value="test-queue"></constructor-arg> </bean> <bean id="itemAddtopic" class="org.apache.activemq.command.ActiveMQTopic"><constructor-arg name="name" value="item-add-topic"></constructor-arg> </bean>添加商品
向Activemq發送,商品添加消息
@Service public class ItemServiceImpl implements ItemService {@Autowiredprivate TbItemMapper itemMapper;@Autowiredprivate TbItemDescMapper itemDescMapper;@Autowiredprivate JmsTemplate jmsTemplate;@Resource(name="itemAddtopic")private Destination destination;@Overridepublic TaotaoResult addItem(TbItem item, String desc) {//生成商品idfinal long itemId = IDUtils.genItemId();//補全item的屬性item.setId(itemId);//商品狀態,1-正常,2-下架,3-刪除item.setStatus((byte) 1);item.setCreated(new Date());item.setUpdated(new Date());//向商品表插入數據itemMapper.insert(item);//創建一個商品描述表對應的pojoTbItemDesc itemDesc = new TbItemDesc();//補全pojo的屬性itemDesc.setItemId(itemId);itemDesc.setItemDesc(desc);itemDesc.setUpdated(new Date());itemDesc.setCreated(new Date());//向商品描述表插入數據itemDescMapper.insert(itemDesc);//向Activemq發送商品添加消息jmsTemplate.send(destination, new MessageCreator() {@Overridepublic Message createMessage(Session session) throws JMSException {//發送商品idTextMessage textMessage = session.createTextMessage(itemId + "");return textMessage;}});//返回結果return TaotaoResult.ok();}}總結
以上是生活随笔為你收集整理的ActiveMQ添加商品发送消息的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Spring整合ActiveMQ接收消息
- 下一篇: ActiveMQ添加商品接收消息