一、SpringMVC框架搭建请参照,SpringMVC框架搭建
二、SpringMVC整合RabbitMQ
-
添加RabbitMQ的jar包,在pom.xml中添加:
<dependency> <groupId>org.springframework.amqp</groupId> <artifactId>spring-rabbit</artifactId> <version>2.1.2.RELEASE</version> </dependency>
-
在
src/main/resource
新建rabbit.properties,配置内容如下:# rabbit ip rabbit.host=127.0.0.1 # rabbit 端口 rabbit.port=5672 # rabbit 用户名 rabbit.username=admin # rabbit 密码 rabbit.password=admin
-
在
src/main/resource
下新建spring-rabbit-send.xml
,内容如下:<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:rabbit="http://www.springframework.org/schema/rabbit" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/rabbit http://www.springframework.org/schema/rabbit/spring-rabbit.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd"> <context:property-placeholder location="classpath:rabbit.properties" ignore-unresolvable="true" /> <!-- 配置连接工厂 --> <rabbit:connection-factory id="connectionFactory" host="${rabbit.host}" port="${rabbit.port}" username="${rabbit.username}" password="${rabbit.password}" /> <!-- 定义mq管理 --> <rabbit:admin connection-factory="connectionFactory" /> <!-- 声明队列 --> <rabbit:queue name="queue" auto-declare="true" durable="true" /> <!-- 定义交换机绑定队列(路由模式) --> <rabbit:direct-exchange name="IExchange" id="IExchange"> <rabbit:bindings> <rabbit:binding queue="queue" key="queuekey" /> </rabbit:bindings> </rabbit:direct-exchange> <!-- 消息对象json转换类 --> <bean id="jsonMessageConverter" class="org.springframework.amqp.support.converter.Jackson2JsonMessageConverter" /> <!-- 定义模版 --> <rabbit:template id="rabbitTemplate" connection-factory="connectionFactory" exchange="IExchange" message-converter="jsonMessageConverter" /> </beans>
-
在
src/main/resource
下新建spring-rabbit-recv.xml
,内容如下:<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:rabbit="http://www.springframework.org/schema/rabbit" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/rabbit http://www.springframework.org/schema/rabbit/spring-rabbit.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd"> <context:property-placeholder location="classpath:rabbit.properties" ignore-unresolvable="true" /> <!-- 配置连接工厂 --> <rabbit:connection-factory id="connectionFactory" host="${rabbit.host}" port="${rabbit.port}" username="${rabbit.username}" password="${rabbit.password}" /> <!-- 定义mq管理 --> <rabbit:admin connection-factory="connectionFactory" /> <!-- 声明队列 --> <rabbit:queue name="queue" auto-declare="true" durable="true" /> <!-- 定义消费者 --> <bean name="queuehandler" class="com.climber.rabbitmq.RecvHandler" /> <!-- 定义消费者监听队列 --> <rabbit:listener-container connection-factory="connectionFactory"> <rabbit:listener ref="queuehandler" queues="queue" /> </rabbit:listener-container> </beans>
-
定义RecvHandler类,具体代码如下:
public class RecvHandler implements MessageListener { private static final ObjectMapper MAPPER = new ObjectMapper(); public void onMessage(Message msg) { try { // msg就是rabbitmq传来的消息,需要的同学自己打印看一眼 // 使用jackson解析 JsonNode jsonData = MAPPER.readTree(msg.getBody()); System.out.println("我是可爱的小猪,我的id是" + jsonData.get("id").asText() + ",我的名字是" + jsonData.get("name").asText()); } catch (IOException e) { e.printStackTrace(); } } }
-
定义Controller层:
@Controller public class RabbitController { @Autowired private RabbitTemplate rabbitTemplate; @RequestMapping(value="rabbit",method=RequestMethod.GET) public void rabbit(){ HashMap<String, String> map = new HashMap<String, String>(); map.put("id", "1"); map.put("name", "pig"); //根据key发送到对应的队列 rabbitTemplate.convertAndSend("queuekey", map); map.put("id", "2"); map.put("name", "cat"); //根据key发送到对应的队列 rabbitTemplate.convertAndSend("queuekey", map); } }
-
进入浏览器,输入:
http://localhost:8080/rabbitmq/rabbit.do
,可看到控制台打印从RabbitMQ获取的信息