/** * 延迟队列交换器, x-delayed-type 和 x-delayed-message 固定 */ @Bean public CustomExchange delayExchange() { Map<String, Object> args = Maps.newHashMap(); args.put("x-delayed-type", "direct"); return new CustomExchange(RabbitConsts.DELAY_MODE_QUEUE, "x-delayed-message", true, false, args); }
@Test public void customExhangeTestBean() { ConfigurableApplicationContext context = new AnnotationConfigApplicationContext(Config.class, CustomExchangeTestBean.class); final Collection<CustomExchange> exchanges = context.getBeansOfType(CustomExchange.class).values(); assertThat(exchanges, hasSize(1)); final CustomExchange exchange = exchanges.iterator().next(); assertEquals("my_custom_exchange", exchange.getName()); assertEquals("custom_type", exchange.getType()); context.close(); }
@Bean CustomExchange customExchange() { Map<String, Object> args = new HashMap<>(); args.put("x-delayed-type", "direct"); //参数二为类型:必须是x-delayed-message return new CustomExchange(DelayedConfig.EXCHANGE_NAME, "x-delayed-message", true, false, args); } // 绑定队列到交换器
public Exchange build() { AbstractExchange exchange; if (ExchangeTypes.DIRECT.equals(this.type)) { exchange = new DirectExchange(this.name, this.durable, this.autoDelete, getArguments()); } else if (ExchangeTypes.TOPIC.equals(this.type)) { exchange = new TopicExchange(this.name, this.durable, this.autoDelete, getArguments()); } else if (ExchangeTypes.FANOUT.equals(this.type)) { exchange = new FanoutExchange(this.name, this.durable, this.autoDelete, getArguments()); } else if (ExchangeTypes.HEADERS.equals(this.type)) { exchange = new HeadersExchange(this.name, this.durable, this.autoDelete, getArguments()); } else { exchange = new CustomExchange(this.name, this.type, this.durable, this.autoDelete, getArguments()); } exchange.setInternal(this.internal); exchange.setDelayed(this.delayed); exchange.setIgnoreDeclarationExceptions(this.ignoreDeclarationExceptions); exchange.setShouldDeclare(this.declare); if (!ObjectUtils.isEmpty(this.admins)) { exchange.setAdminsThatShouldDeclare(this.admins); } return exchange; }
public AbstractExchange buildExchange(ExchangeConfig defaultExchangeConfig) { if (!isDefaultConfigApplied()) { applyDefaultConfig(defaultExchangeConfig); } AbstractExchange exchange = new CustomExchange(getName(), getType().getValue(), getDurable(), getAutoDelete(), getArguments()); exchange.setInternal(getInternal()); exchange.setDelayed(getDelayed()); return exchange; }