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.

相关推荐
©️2020 CSDN 皮肤主题: 编程工作室 设计师:CSDN官方博客 返回首页