spring boot 设置延时消息发送交换机启动报错 Unexpected exchange type: x-delayed-message

/*
 * Copyright 2002-2016 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.springframework.amqp.core;

/**
 * Constants for the standard Exchange type names.
 *
 * @author Mark Fisher
 * @author Gary Russell
 */
public abstract class ExchangeTypes {

	public static final String DIRECT = "direct";

	public static final String TOPIC = "topic";

	public static final String FANOUT = "fanout";

	public static final String HEADERS = "headers";

	public static final String SYSTEM = "system";

	/**
	 * The constant to represent {@code x-delayed-message} exchange mode.
	 * @deprecated since 1.6.4, it's not a user-available exchange type,
	 * the delayed {@code boolean} is used for that.
	 */
	@Deprecated
	public static final String DELAYED = "x-delayed-message";
}

使用 public static final String DELAYED = “x-delayed-message”;会报错
错误代码示例

    @RabbitListener(
            containerFactory = "rabbitListenerContainerFactory",
            bindings = @QueueBinding(
                    exchange = @Exchange(value = "ex.inter.bank",durable = "true" ,type = ExchangeTypes.DELAYED ,arguments = @Argument(name = "x-delayed-type",value="direct")),
                    value = @Queue(value = "q.inter.bank",durable = "true"),
                    key = "q.inter.bank"
            )
    )
    public void process(@Payload String body, @Headers Map<String,Object> heads, Channel channel, Message message) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        System.out.println("接受:"+sdf.format(new Date()));
        System.out.println(body);
    }

因为程序启动时扫描注解 不能判断到这一类型的交换机
在这里插入图片描述
解决办法

    @RabbitListener(
            containerFactory = "rabbitListenerContainerFactory",
            bindings = @QueueBinding(
                    exchange = @Exchange(value = "ex.inter.bank",durable = "true",delayed = "true" ,type = ExchangeTypes.DIRECT,arguments = @Argument(name = "x-delayed-type",value="direct")),
                    value = @Queue(value = "q.inter.bank",durable = "true"),
                    key = "q.inter.bank"
            )
    )

参考

Version 1.6 introduces support for the Delayed Message Exchange Plugin

[Note]
The plugin is currently marked as experimental but has been available for over a year (at the time of writing). If changes to the plugin make it necessary, we will add support for such changes as soon as practical. For that reason, this support in Spring AMQP should be considered experimental, too. This functionality was tested with RabbitMQ 3.6.0 and version 0.0.1 of the plugin.

To use a RabbitAdmin to declare an exchange as delayed, simply set the delayed property on the exchange bean to true. The RabbitAdmin will use the exchange type (Direct, Fanout etc) to set the x-delayed-type argument and declare the exchange with type x-delayed-message.

The delayed property (default false) is also available when configuring exchange beans using XML.

<rabbit:topic-exchange name=“topic” delayed=“true” />
To send a delayed message, it’s simply a matter of setting the x-delay header, via the MessageProperties:

MessageProperties properties = new MessageProperties();
properties.setDelay(15000);
template.send(exchange, routingKey,
MessageBuilder.withBody(“foo”.getBytes()).andProperties(properties).build());
or

rabbitTemplate.convertAndSend(exchange, routingKey, “foo”, new MessagePostProcessor() {

@Override
public Message postProcessMessage(Message message) throws AmqpException {
    message.getMessageProperties().setDelay(15000);
    return message;
}

});
To check if a message was delayed, use the getReceivedDelay() method on the MessageProperties. It is a separate property to avoid unintended propagation to an output message generated from an input message.

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值