10.spring整合方式2

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_16855077/article/details/81330316

手动模式

1.rabbitMQ配置

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	xmlns:task="http://www.springframework.org/schema/task"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	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/context http://www.springframework.org/schema/context/spring-context.xsd
		http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.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/rabbit http://www.springframework.org/schema/rabbit/spring-rabbit.xsd
		http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd">

	<!-- 定义Rabbit,指定连接工厂
	publisher-confirms="true" 确定失败的回调才会执行
	 -->
	<rabbit:connection-factory id="connectionFactory"  
	    host="${rabbit.ip}"  virtual-host="${rabbit.virtual-host}"  
	    username="${rabbit.username}"  password="${rabbit.pwd}"  
	    port="${rabbit.port}"  publisher-confirms="true" />      
	
	<!-- MQ的管理,包括队列、交换器等 -->
	<rabbit:admin connection-factory="connectionFactory"/>
	
	<!-- 定义Rabbit模板,指定连接工厂以及定义exchange 
	queue="" 如果发送到队列则写队列
	exchange="" 如果发送到交换机则写交换机
	routing-key="" 定义路由的关键字 
	confirm-callback  成功时,执行那个
	return-callback  失败时,执行那个
	-->
	<rabbit:template id="amqpTemplate" routing-key="" 
	  exchange="fanoutExchange" connection-factory="connectionFactory" 
	  confirm-callback="confirmCallBackListener"  return-callback="returnCallBackListener"
	  mandatory="true"/>	
	
	<!-- 定义队列 -->
	<!-- queue 队列声明 --> 
    <!--    durable 是否持久化 ,exclusive 仅创建者可以使用的私有队列,断开后自动删除 ,auto-delete 当所有消费端连接断开后,是否自动删除队列   -->
    <rabbit:queue name="confirm_test" durable="true" auto-delete="false" exclusive="false"/>
	
	
	
	<rabbit:direct-exchange name="dircect_ex" id="dircect_ex" durable="true" auto-delete="false">
			<!-- 将队列绑定到交换机 -->
		    <rabbit:bindings>
		        <rabbit:binding  queue="confirm_test"/>
		    </rabbit:bindings>
	</rabbit:direct-exchange> 
	
	<!-- 配置监听 消费者   acknowledeg = manual,auto,none   -->
    <rabbit:listener-container connection-factory="connectionFactory" acknowledge="manual" >
        <!-- queues 监听队列,多个用逗号分隔; ref 监听器 -->
        <rabbit:listener queue-names="confirm_test"  ref="receiveConfirmListence" />
    </rabbit:listener-container>
</beans>

2.确定监听

package com.cloudtech.web.mq.spring;

import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.rabbit.support.CorrelationData;
import org.springframework.stereotype.Component;

/**
 * 
* @ClassName: ConfirmCallBackListener  
* @Description:   确定时监听
* @author wude  
* @date 2018年8月1日  
*
 */
@Component
public class ConfirmCallBackListener implements RabbitTemplate.ConfirmCallback{

	@Override
	public void confirm(CorrelationData correlationData, boolean ack, String cause) {
		System.err.println("确认回调 ack:"+ack+",cause:"+cause);
	}

	
}

3.失败监听

package com.cloudtech.web.mq.spring;

import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.stereotype.Component;

/**
 * 
* @ClassName: ConfirmCallBackListener  
* @Description:   失败时监听
* @author wude  
* @date 2018年8月1日  
*
 */
@Component
public class ReturnCallBackListener implements RabbitTemplate.ReturnCallback{

	@Override
	public void returnedMessage(Message message, int replyCode, String replyText, String exchage, String routingKey) {
		System.err.println("失败message:"+new String(message.getBody())+",replyCode:"+replyCode
				+",replyText:"+replyText+",exchage:"+exchage+",routingKey:"+routingKey);
	}
	

	
}

4.接受监听

package com.cloudtech.web.mq.spring;

import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.core.ChannelAwareMessageListener;
import org.springframework.stereotype.Component;

import com.rabbitmq.client.Channel;

@Component
public class ReceiveConfirmListence implements ChannelAwareMessageListener{

	/**
	 * 收到消息时执行的监听
	 */
	@Override
	public void onMessage(Message message, Channel channel) throws Exception {
		try {
			String s ="消费者收到了消息:"+message;
			System.err.println(s);
			channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
		} catch (Exception e) {
			e.printStackTrace();
			channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
		}
	}

}

5.配置启动

package com.cloudtech.web.mq.spring;

import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class PublishUtil {
	@Autowired
	private AmqpTemplate amqpTemplate;
	
	public void send(String exchange,String routingkey,Object object){
		amqpTemplate.convertAndSend(exchange,routingkey,object);
	}
	
}	

6.测试

package com.cloudtech.web.mq.spring;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class TestMain {
	@Autowired
	private PublishUtil publishUtil;
	private static String exChange="dircect_ex";   //交换机
	private static String queue="confirm_test";   //队列
	
	/**
	 * exchange queue都对, confirm会执行,ack=true
	 * @throws InterruptedException
	 */
	@Test
	public void test() throws InterruptedException{
		String msg = "当前时间是:"+System.currentTimeMillis();
		publishUtil.send(exChange, queue, msg);
		Thread.sleep(2000);
	}
	
	/**
	 * exchange 错误 queue对, confirm会执行,ack=false
	 * @throws InterruptedException
	 */
	@Test
	public void test1() throws InterruptedException{
		String msg = "当前时间是:"+System.currentTimeMillis();
		publishUtil.send(exChange+"i", queue, msg);
		Thread.sleep(2000);
	}
	
	/**
	 * exchange 对 queue错误, confirm会执行,ack=true 失败会执行
	 * @throws InterruptedException
	 */
	@Test
	public void test2() throws InterruptedException{
		String msg = "当前时间是:"+System.currentTimeMillis();
		publishUtil.send(exChange, queue+"i", msg);
		Thread.sleep(2000);
	}
	
	/**
	 * exchange 错误 queue错误, confirm会执行,ack=false
	 * @throws InterruptedException
	 */
	@Test
	public void test3() throws InterruptedException{
		String msg = "当前时间是:"+System.currentTimeMillis();
		publishUtil.send(exChange+"i", queue+"i", msg);
		Thread.sleep(2000);
	}
}

猜你喜欢

转载自blog.csdn.net/qq_16855077/article/details/81330316
今日推荐