1,系统windows7
项目使用maven构建,使用spring+springmvc+mybatis整合rabbitmq
项目能够正常启动,运行,并且连接多数据源(关于多数据源部分,其他的文章里有介绍,不需要可以自行配置)。已经使用postman进行测试,能够发送消息到消息队列并且够取自动取出消息。
下面是关键代码
注意:项目依靠此部分代码无法启动,仅作参考,关键是rabbitmq部分的内容。
=====================================================================================
只附上rabbitmq部分的依赖,版本可以自己选择
1,pom.xml
========================================================================================
2,web.xml
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">
============================================================================
3,spring配置文件 applicationContext.xml
xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:task="http://www.springframework.org/schema/task" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd">
============================================================================
4,mybatis配置文件 spring-mybatis.xml
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
============================================================================
5,rabbitmq配置 application-rabbitmq.xml
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:rabbit="http://www.springframework.org/schema/rabbit"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/rabbit
http://www.springframework.org/schema/rabbit/spring-rabbit-1.4.xsd">
password="${rabbitmq.password}"
host="${rabbitmq.host}"
port="${rabbitmq.port}"
/>
password="${rabbitmq.password}"
host="${rabbitmq.host}"
port="${rabbitmq.port}"
/>
=======================================================================================
6,rabbitmq的properties文件
#rabbitmq
rabbitmq.host=127.0.0.1
rabbitmq.port=5672
rabbitmq.username=guest
rabbitmq.password=guest
rabbitmq.virtual.host=rabbit1
rabbitmq.queue.name1=queueTest
rabbitmq.queue.name2=queueChris
rabbitmq.queue.name3=queueShijj
=========================================================================================
7,Controller层
/**
* 2017年11月22日下午2:00:21
*/
package com.jjmc.server.controller;
import java.io.IOException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import com.jjmc.server.service.MessageProducer;
/**
*
* @author huangtao
* 2017年11月22日下午2:00:21
* business-server
* @parameter
* TODO
*
*/
@Controller
public class MessageController {
private static Logger logger = Logger.getLogger(MessageController.class);
@Autowired
private MessageProducer messageProducer;
@RequestMapping(value = "/SendMessage", method = RequestMethod.POST)
@ResponseBody
public void send(HttpServletRequest request, HttpServletResponse response,@RequestBody Object msg) throws IOException{
logger.info(Thread.currentThread().getName()+"------------send to rabbitmq Start");
messageProducer.sendMessage(msg);
logger.info(Thread.currentThread().getName()+"------------send to rabbitmq End");
}
}
===============================================================
8,service层
/**
* 2017年11月22日上午11:13:27
*/
package com.jjmc.server.service;
import java.io.IOException;
import javax.annotation.Resource;
import org.apache.log4j.Logger;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.stereotype.Service;
/**
*
* @author huangtao
* 2017年11月22日上午11:13:27
* business-server
* @parameter
* TODO
*
*/
@Service
public class MessageProducer {
private static Logger logger = Logger.getLogger(MessageProducer.class);
@Resource(name="amqpTemplate")
private AmqpTemplate amqpTemplate;
@Resource(name="amqpTemplate2")
private AmqpTemplate amqpTemplate2;
public void sendMessage(Object message) throws IOException {
logger.info("to send message:{} "+message);
amqpTemplate.convertAndSend("queueTestKey", message);
amqpTemplate.convertAndSend("queueTestChris", message);
amqpTemplate2.convertAndSend("shijj.xxxx.wsdwd", message);
}
}
==================================================================================
9,filter层
注意:一共有三个消费者,所以有三个类
----------------------------------------------------------------------------
/**
* 2017年11月22日上午11:15:24
*/
package com.jjmc.server.filter;
import org.apache.log4j.Logger;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageListener;
import org.springframework.stereotype.Component;
/**
*
* @author huangtao
* 2017年11月22日上午11:15:24
* business-server
* @parameter
* TODO
*
*/
@Component
public class MessageConsumer implements MessageListener {
private static Logger logger = Logger.getLogger(MessageConsumer.class);
@Override
public void onMessage(Message message) {
logger.info("consumer receive message------->:{} "+message);
}
}
--------------------------------------------------------------------------------
/**
* 2017年11月22日上午11:17:05
*/
package com.jjmc.server.filter;
import org.apache.log4j.Logger;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageListener;
import org.springframework.stereotype.Component;
/**
*
* @author huangtao
* 2017年11月22日上午11:17:05
* business-server
* @parameter
* TODO
*
*/
@Component
public class ChrisConsumer implements MessageListener {
private static Logger logger = Logger.getLogger(ChrisConsumer.class);
@Override
public void onMessage(Message message) {
logger.info("chris receive message------->:{} "+message);
}
}
--------------------------------------------------------------------------------
/**
* 2017年11月22日上午11:18:14
*/
package com.jjmc.server.filter;
import org.apache.log4j.Logger;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageListener;
import org.springframework.stereotype.Component;
/**
*
* @author huangtao
* 2017年11月22日上午11:18:14
* business-server
* @parameter
* TODO
*
*/
@Component
public class ShijjConsumer implements MessageListener {
private static Logger logger = Logger.getLogger(ShijjConsumer.class);
@Override
public void onMessage(Message message) {
logger.info("shijj receive message------->:{} "+message);
}
}
=====================================================================