ActiveMQ整合Spring,并且在项目中使用案例

ActiveMQ整合spirng其实就是把那些繁琐的步骤都配置化了,交给spring去管理,然后使用jsmTemplate来发送接收消息. 

导入依赖

		
			org.springframework
			spring-jms
            4.2.4.RELEASE
		
		
			org.springframework
			spring-context-support
            4.2.4.RELEASE
		
		
			org.apache.activemq
			activemq-all
            5.11.2
		

applicationContext-ActiveMQ.xml配置文件 ,把里面的activeMQip和队列名改成自己想要的就可以了



	
	
	
		
	
	
	
		
		
	
	
	
	
	
	
	
		
		
	
	
	
		
			spring-queue    
		
	
	
	
		
	
	

 测试发送消息

package cn.e3mall.activemq;

import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Session;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;

public class ActiveMqSpring {

	@Test
	public void sendMessage() throws Exception {
		//初始化spring容器
		ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:spring/applicationContext-activemq.xml");
		//从容器中获得JmsTemplate对象。
		JmsTemplate jmsTemplate = applicationContext.getBean(JmsTemplate.class);
		//从容器中获得一个Destination对象。
		Destination destination = (Destination) applicationContext.getBean("queueDestination");
		//发送消息
		jmsTemplate.send(destination, new MessageCreator() {
			@Override
			public Message createMessage(Session session) throws JMSException {
				return session.createTextMessage("send activemq message");
			}
		});
	}
}

ActiveMQ整合Spring,并且在项目中使用案例_第1张图片

接收消息,在最开始实现接收消息的时候,使用的是MessageListener来接收的,现在让spring管理了,所以得自己建一个MessageListener的实现类

ActiveMQ整合Spring,并且在项目中使用案例_第2张图片

package cn.e3mall.search.message;

import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;

public class MyMessageListener implements MessageListener {

	@Override
	public void onMessage(Message message) {
		//取消息内容
		TextMessage textMessage = (TextMessage) message;
		try {
			String text = textMessage.getText();
			System.out.println(text);
		} catch (JMSException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
	}

}

创建好后把它注入到spring工厂中,重新创建一个spring配置文件



	
	
	
		
	
	
	
		
		
	
	
	
	
	
		
			spring-queue
		
	
	
	
		
	
	
	
	
	
	
		
		
		
	
	
	
	
		
		
		
	

配置好后,测试的时候直接启动spring容器就能接收到消息了,就执行了自己写的那个MessageListener类了.

项目中使用案例,新添商品的时候,把商品资料同步到索引库中去,添加商品是一个服务,添加索引库又是一个服务,所以这时候就用到是activeMQ,实现过程呢,就是在添加商品的时候发送一个消息,只把商品id传过去就可以了因为,要是传一个实体对象的话,用不到那么多数据,传过去后,在索引库服务那边,通过id从数据库中查出来然后添加到索引库就实现了.

这个就是插入商品的方法,插入完后发送一个消息,注入进来两个配置文件的对象,配置文件上面的已经配置好了看看应该能看明白.

ActiveMQ整合Spring,并且在项目中使用案例_第3张图片

ActiveMQ整合Spring,并且在项目中使用案例_第4张图片

 

这个是消息接收方的处理逻辑,接收到消息后保存到索引库,MessageListener可以配置多个,需要哪个用哪个就可以了,这儿使用的是topics的方式发送下消息,因为有可能别的服务也要接收这个id干一些别的时间,根据个人情况而定.Thread.sleep(1000)写这个的原因是,在添加商品那边如果电脑慢的话很可能还没添加到数据库中事务还没提交呢,消息已经发送过来了,然后这把从数据库中查不到,等它一秒的话就可以了,还有一种解决办法是把发送消息的逻辑写到controller层,那样的话数据肯定已经添加到数据库当中了.    activeMQ在项目中的使用已经完了,就解决了启动项目的时候先开启哪个服务后开启哪个服务都可以了没有先后顺序了.

package cn.e3mall.search.message;

import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;

import org.apache.solr.client.solrj.SolrServer;
import org.apache.solr.common.SolrInputDocument;
import org.springframework.beans.factory.annotation.Autowired;

import cn.e3mall.common.pojo.SearchItem;
import cn.e3mall.search.mapper.ItemMapper;

/**
 * 监听商品添加消息,接收消息后,将对应的商品信息同步到索引库
 * 

Title: ItemAddMessageListener

*

Description:

*

Company: www.itcast.cn

* @version 1.0 */ public class ItemAddMessageListener implements MessageListener { @Autowired private ItemMapper itemMapper; @Autowired private SolrServer solrServer; @Override public void onMessage(Message message) { try { //从消息中取商品id TextMessage textMessage = (TextMessage) message; String text = textMessage.getText(); Long itemId = new Long(text); //等待事务提交 Thread.sleep(1000); //根据商品id查询商品信息 SearchItem searchItem = itemMapper.getItemById(itemId); //创建一个文档对象 SolrInputDocument document = new SolrInputDocument(); //向文档对象中添加域 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()); //把文档写入索引库 solrServer.add(document); //提交 solrServer.commit(); } catch (Exception e) { e.printStackTrace(); } } }

 

你可能感兴趣的:(activeMQ)