ssm整合rabbitmq消息队列的简单使用案例

项目:ssm(spring+springmvc+mybatis)

编译工具:eclipse

消息队列:rabbitmq

 

1.在pom.xml添加rabbitmq依赖:

  
          
            org.springframework.amqp  
            spring-rabbit  
            1.3.5.RELEASE  
        

2.编写rabbitmq的配置文件:rabbitMQ.xml:



    
    
    

    
    

    
    

    
    
        
            
        
    

    
    

    
    

    
    
        
    

    
    

    
    
        
            
        
    

    
    

    
    
        
    

    
    
    

    
    

    
    

    
    
        
            
        
    

    
    

    
    

    
    
        
    

3.在ssm项目的上下文配置文件spring-content.xml中引入rabbitmq的配置文件

spring-content.xml:


 
	
	
	
	   
	
     
		
    
    
    
  

 4.编写测试代码:

1.消息生产者:

package com.comit.appointment.modules.test.cs;

import java.io.IOException;

import javax.annotation.Resource;

import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.amqp.core.Message;
import org.springframework.stereotype.Service;

import com.comit.appointment.modules.sys.entity.Users;

@Service("MessageProducer2")
public class MessageProducer2 {
	
	@Resource(name="amqpTemplate")
	private AmqpTemplate  amqpTemplate;
	
	public void sendMessage(Object message)throws IOException{
		amqpTemplate.convertAndSend("queueTestKey",message);
	}
	
	

}

注:queueTestKey 是在rabbitMQ.xml绑定到队列的,根据这个key来发送消息到相应的消息队列中。

 

 

2.消息消费者:获取消息并把消息插入日志记录表中

package com.comit.appointment.modules.test;


import java.io.UnsupportedEncodingException;
import java.text.ParseException;
import java.text.SimpleDateFormat;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageListener;
import org.springframework.beans.factory.annotation.Autowired;

import com.comit.appointment.modules.sys.dao.LogsMapper;
import com.comit.appointment.modules.sys.entity.Logs;


public class MessageConsumer implements MessageListener {
     private Logger logger = LoggerFactory.getLogger(MessageConsumer.class); 
     
     @Autowired
     private LogsMapper logsMapper;
     
    @Override
    public void onMessage(Message message) {
    	  try {
			String messages = new String(message.getBody(), "UTF-8");
			String[] str=messages.split(",");
			Logs log=new Logs();
			log.setNAME(str[0]);
			log.setTIME(str[1]);
			log.setACTION(str[2]);
			System.out.println("log-->"+log.toString());
			logsMapper.insertSelective(log);
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
         logger.info("consumer receive message------->:{}", message);  
    }

}

在登录时发送已登录的消息:

package com.comit.appointment.modules.sys.controller.impl;

import java.io.IOException;
import java.util.Date;

import javax.servlet.http.HttpSession;

import org.apache.shiro.session.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import com.comit.appointment.modules.sys.controller.IBaseController;
import com.comit.appointment.modules.sys.entity.Users;
import com.comit.appointment.modules.sys.service.IOperaterService;
import com.comit.appointment.modules.sys.service.IUsersService;
import com.comit.appointment.modules.test.cs.MessageProducer2;

@Controller
@RequestMapping("/base")
public class BaseController implements IBaseController {
	
	@Autowired
	private IUsersService usersService;
	
	@Qualifier(value="MessageProducer2")
	@Autowired
	private MessageProducer2 messageProducer;

	@RequestMapping("/")
	public String login() {
		return "modules/sys/login";
	}
	
	@Override
	@RequestMapping("/login")
	public String login(HttpSession session,Users user) {
		Users user1=usersService.findUser(user);
		if(user1!=null) {
			try {
				messageProducer.sendMessage(user1.getACCOUNT()+","+new Date()+",登录了");
			} catch (IOException e) {
				e.printStackTrace();
			}
			session.setAttribute("user", user1);
			return "modules/sys/index";
		}
		return "modules/sys/login";
	}
	
	

}

在登录时便发送了一个消息,控制台打印:

 在消息消费者那里把该消息插入了日志记录表:

ssm整合rabbitmq消息队列的简单使用案例_第1张图片

注:运行项目时记得打开rabbitmq服务,要不然连接不上会报错。

没有安装rabbitmq的可以看看 Win10安装RabbitMQ

 

我的座右铭:不会,我可以学;落后,我可以追赶;跌倒,我可以站起来;我一定行。

你可能感兴趣的:(rabbitmq)