本人通过学习rabbitmq,自己编写的demo,希望大家提出宝贵的建议
模板项目整体结构:
1、配置文件:application.yml
server:
port: 8089
spring:
rabbitmq:
host: 127.0.0.1
port: 5672
username: guest
password: guest
virtual-host: /
#支持发布确认
publisher-confirms: true
#支持发布返回
publisher-returns: true
listener:
simple:
#采用手动应答
acknowledge-mode: manual
#当前监听容器数量
concurrency: 1
max-concurrency: 1
#是否支持重试
retry:
enabled: true
2、RabbitMQConfig.java 文件 核心
package com.rabbitmqtemplate.config;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
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.Scope;
import com.rabbitmqtemplate.callback.MsgSendConfirmCallBack;
import com.rabbitmqtemplate.callback.MsgSendReturnCallback;
import com.rabbitmqtemplate.constants.RabbitMqExchange;
import com.rabbitmqtemplate.constants.RabbitMqQueue;
import com.rabbitmqtemplate.constants.RabbitMqRoutingKey;
/**
* rabbitmq核心配置
* @author lishilei
*
*/
@Configuration
public class RabbitMQConfig {
@Value("${spring.rabbitmq.host}")
public String host;
@Value("${spring.rabbitmq.port}")
public int port;
@Value("${spring.rabbitmq.username}")
public String username;
@Value("${spring.rabbitmq.password}")
public String password;
@Value("${spring.rabbitmq.virtual-host}")
public String virtualHost;
@Value("${spring.rabbitmq.publisher-confirms}")
public Boolean publisherConfirms;
/**
* 创建工厂