ActiveMQ(四)--Spring和ActiveMQ整合的完整实例(个人项目所用)

以前代码,为个人实际项目中所有,均亲测成功

1.配置消费者spring-activemq-producer.xml,这里有两个消息队列





	
	
	


	
	
		
	

	
	
		
		
		
	

	
	
		
		
	
	
	
       
		
    

	
	
	
		
		
	


2.配置消费者spring-activemq-consumer.xml,有两个消息监听器,对应上面两个消息队列





	
	
	

	
	
		
	

	
	
		
		
		
	



	
	
	
	
	

	
	
		
		
		
	

	
	
		
		
		
	


这两个配置文件在spring的配置文件里引入启动

 
 

3.下面我们来看消费者的代码(只举出一个例子)

package com.yundao.store.activemq.service;

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

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;
import org.springframework.stereotype.Service;

import com.yundao.store.entity.Users;
import com.yundao.store.service.IUserService;

/**
 * 生产者服务类
 * 
 * @author  2018-3-26下午4:38:22
 *         将消息生产者做成一个服务,当我们需要发送消息的时候,只需要调用ProducerService实例中的sendMessage
 *         方法就可以向默认目的发送一个消息。
 */
@Service
public class ProducerServiceUser {
	@Autowired
	private JmsTemplate jmsTemplate;

	/**
	 * 向指定队列发送消息
	 */
	public void sendMessage(Destination destination, final String msg) {
		System.out.println(Thread.currentThread().getName() + " 向队列"
				+ destination.toString() + "发送消息---------------------->" + msg);
		jmsTemplate.send(destination, new MessageCreator() {
			public Message createMessage(Session session) throws JMSException {
				return session.createTextMessage(msg);
			}
		});
	}

	/**
	 * 向默认队列发送消息
	 */
	public void sendMessage(final String msg) {
		String destination = jmsTemplate.getDefaultDestinationName();
		System.out.println(Thread.currentThread().getName() + " 向队列"
				+ destination + "发送消息---------------------->" + msg);
		jmsTemplate.send(new MessageCreator() {
			public Message createMessage(Session session) throws JMSException {
				return session.createTextMessage(msg);
			}
		});
	}

}

4.消费者监听器的代码

package com.yundao.store.activemq.listener;

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

import com.yundao.store.service.impl.UserService;

/**
 * 配置消息队列监听者(实际项目中采用的是这种方式)
 * 
 * @author  2018-3-26下午4:46:44
 */
public class userQueueMessageListener implements MessageListener {
	private UserService userService;	
	// 当收到消息后,自动调用该方法
	@Override
	public void onMessage(Message message) {
		TextMessage tm = (TextMessage) message;
		try {
			System.out.println("userQueueMessageListener监听到了文本消息:\t" + tm.getText());
			//处理业务逻辑
		} catch (JMSException e) {
			e.printStackTrace();
		}

	}

}

5.因为不是maven构建的项目,需要自己引入jar包

https://download.csdn.net/download/changhenshui1990/10326333

ActiveMQ(四)--Spring和ActiveMQ整合的完整实例(个人项目所用)_第1张图片

spring-jms-*.jar,这里我们只引用3.2.4的

6.测试方法

/**
 * 用户操作相关Action
 */
@Controller
public class UserAction extends BaseAction {
	private UserService userService;
		
	@Resource
	private ProducerServiceUser producerServiceUser;
	
	@Resource
	private ProducerServiceLog producerServiceLog;
	
	@Resource(name="userQueueDestination")
	private Destination destination;
	
	@Resource(name="logQueueDestination")
	private Destination destination1;

	static Logger logger = LogManager.getLogger(UserAction.class.getName());






	/**
	 * 用户登录
	 */
	public String login() {
		setUtfEncoding();
		String userCode = getRequest().getParameter("userCode");
		String password = getRequest().getParameter("password");

		String param = "登录成功";
		producerServiceUser.sendMessage(destination, param);
		
		String param1 = "日志成功";
		producerServiceLog.sendMessage(destination1, param1);
		return SUCCESS;
	}}
这样我们会在activemq中看到上传的消息spring-user,spring-log,同时控制台会打印出监听器接听到的数据,进行业务逻辑 运算

你可能感兴趣的:(消息队列)