spring注解编写rabbitmq轮询代码各种坑

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_16855077/article/details/81986977
package com.cloudtech.web.mq.config;

import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.annotation.Scope;

/**
 *  日志管理的Rabbit配置类的具体实现类
* @ClassName: LogRabbitConfiguration  
* @Description:   
* @author wude  
* @date 2018年8月13日  
*
 */
@Configuration
@PropertySource(value={"classpath:rabbit.properties"})
public class LogRabbitConfiguration extends AbstractRabbitConfiguration {
	
	/** 质控模块分钟队列名称  */
    private String qaQueueName;
	 
   
    @Bean(name="qaMinuteQueue")
    public Queue qaMinuteQueue() {
    	//声明队列  说明队列存在则什么都不做  如果不存在则自动创建
		//参数1.队列名称
		//参数2 是否持久化队列,我们中的队列模式是指内存中的,如果rabbit重启会丢失,如果我们设置为true,则会保存到el-lang自带的数据库中重启后,会重新读取
		//参数3 是否排外,有两个作用,当我们连接关闭后,是否自动关闭队列,作用二 是否私用当天的队列,如果私有了,则其他队列不允许访问,如果为true,一般只适用于一个消费者的时候
		//参数4 是否自动删除
        return new Queue(qaQueueName,true);
    }
    
    @Bean(name="qaMinuteTemplate")
    @Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
    public RabbitTemplate qaMinuteTemplate() {
        RabbitTemplate template = new RabbitTemplate(rabbit_connectionFactory());
        //对象格式
        template.setMessageConverter(jsonMessageConverter());
        //template.setExchange("");
        //template.setQueue(qaQueueName);
        return template;
    }
}
package com.cloudtech.web.service.impl;

import javax.annotation.Resource;

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

import com.cloudtech.web.entity.MessageDetails;
import com.cloudtech.web.service.RabbitSend;
import com.cloudtech.web.vo.SecondDetailTempVo;

/**
 * 用于发送日志消息的通用实现类
 * 
 * @author wude
 *
 */
@Component
public class LogRabbitSend implements RabbitSend<String> {
	 @Resource(name = "qaMinuteQueue")
	 private Queue qaMinuteQueue;


	@Override
	public void sendQaMinute(MessageDetails detail) {
		qaMinuteTemplate.convertAndSend(qaMinuteQueue.getName(),detail);		
	}
}

注意:qaMinuteTemplate.convertAndSend(qaMinuteQueue.getName(),detail);    

重点就是这句代码,第一个参数一定要设置队列,在config中配置无效,我研究了很久才发现需要设置这个参数

猜你喜欢

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