当前位置:   article > 正文

RabbitMQ--集成Springboot--02--RabbitListener注解_rabbitlistener注解配置

rabbitlistener注解配置

RabbitMQ–集成Springboot–02–RabbitListener注解


代码位置

https://gitee.com/DanShenGuiZu/learnDemo/tree/master/rabbitMq-learn/rabbitMq-01
  • 1

1、 RabbitListener注解内容

 
package org.springframework.amqp.rabbit.annotation;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Repeatable;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

import org.springframework.messaging.handler.annotation.MessageMapping;
 
@Target({ ElementType.TYPE, ElementType.METHOD, ElementType.ANNOTATION_TYPE })
@Retention(RetentionPolicy.RUNTIME)
@MessageMapping
@Documented
@Repeatable(RabbitListeners.class)
public @interface RabbitListener {

	/**
	 * The unique identifier of the container managing for this endpoint.
	 * <p>If none is specified an auto-generated one is provided.
	 * @return the {@code id} for the container managing for this endpoint.
	 * @see org.springframework.amqp.rabbit.listener.RabbitListenerEndpointRegistry#getListenerContainer(String)
	 */
	String id() default "";

	/**
	 * The bean name of the {@link org.springframework.amqp.rabbit.listener.RabbitListenerContainerFactory}
	 * to use to create the message listener container responsible to serve this endpoint.
	 * <p>If not specified, the default container factory is used, if any.
	 * @return the {@link org.springframework.amqp.rabbit.listener.RabbitListenerContainerFactory}
	 * bean name.
	 */
	String containerFactory() default "";

	/**
	 * The queues for this listener.
	 * The entries can be 'queue name', 'property-placeholder keys' or 'expressions'.
	 * Expression must be resolved to the queue name or {@code Queue} object.
	 * The queue(s) must exist, or be otherwise defined elsewhere as a bean(s) with
	 * a {@link org.springframework.amqp.rabbit.core.RabbitAdmin} in the application
	 * context.
	 * Mutually exclusive with {@link #bindings()} and {@link #queuesToDeclare()}.
	 * @return the queue names or expressions (SpEL) to listen to from target
	 * @see org.springframework.amqp.rabbit.listener.MessageListenerContainer
	 */
	String[] queues() default {};

	/**
	 * The queues for this listener.
	 * If there is a {@link org.springframework.amqp.rabbit.core.RabbitAdmin} in the
	 * application context, the queue will be declared on the broker with default
	 * binding (default exchange with the queue name as the routing key).
	 * Mutually exclusive with {@link #bindings()} and {@link #queues()}.
	 * @return the queue(s) to declare.
	 * @see org.springframework.amqp.rabbit.listener.MessageListenerContainer
	 * @since 2.0
	 */
	Queue[] queuesToDeclare() default {};

	/**
	 * When {@code true}, a single consumer in the container will have exclusive use of the
	 * {@link #queues()}, preventing other consumers from receiving messages from the
	 * queues. When {@code true}, requires a concurrency of 1. Default {@code false}.
	 * @return the {@code exclusive} boolean flag.
	 */
	boolean exclusive() default false;

	/**
	 * The priority of this endpoint. Requires RabbitMQ 3.2 or higher. Does not change
	 * the container priority by default. Larger numbers indicate higher priority, and
	 * both positive and negative numbers can be used.
	 * @return the priority for the endpoint.
	 */
	String priority() default "";

	/**
	 * Reference to a {@link org.springframework.amqp.rabbit.core.RabbitAdmin
	 * RabbitAdmin}. Required if the listener is using auto-delete
	 * queues and those queues are configured for conditional declaration. This
	 * is the admin that will (re)declare those queues when the container is
	 * (re)started. See the reference documentation for more information.
	 * @return the {@link org.springframework.amqp.rabbit.core.RabbitAdmin} bean name.
	 */
	String admin() default "";

	/**
	 * Array of {@link QueueBinding}s providing the listener's queue names, together
	 * with the exchange and optional binding information.
	 * Mutually exclusive with {@link #queues()} and {@link #queuesToDeclare()}.
	 * @return the bindings.
	 * @see org.springframework.amqp.rabbit.listener.MessageListenerContainer
	 * @since 1.5
	 */
	QueueBinding[] bindings() default {};

	/**
	 * If provided, the listener container for this listener will be added to a bean
	 * with this value as its name, of type {@code Collection<MessageListenerContainer>}.
	 * This allows, for example, iteration over the collection to start/stop a subset
	 * of containers.
	 * @return the bean name for the group.
	 * @since 1.5
	 */
	String group() default "";

	/**
	 * Set to "true" to cause exceptions thrown by the listener to be sent to the sender
	 * using normal {@code replyTo/@SendTo} semantics. When false, the exception is thrown
	 * to the listener container and normal retry/DLQ processing is performed.
	 * @return true to return exceptions. If the client side uses a
	 * {@code RemoteInvocationAwareMessageConverterAdapter} the exception will be re-thrown.
	 * Otherwise, the sender will receive a {@code RemoteInvocationResult} wrapping the
	 * exception.
	 * @since 2.0
	 */
	String returnExceptions() default "";

	/**
	 * Set an
	 * {@link org.springframework.amqp.rabbit.listener.api.RabbitListenerErrorHandler} to
	 * invoke if the listener method throws an exception. A simple String representing the
	 * bean name. If a Spel expression (#{...}) is provided, the expression must
	 * evaluate to a bean name or a
	 * {@link org.springframework.amqp.rabbit.listener.api.RabbitListenerErrorHandler}
	 * instance.
	 * @return the error handler.
	 * @since 2.0
	 */
	String errorHandler() default "";

	/**
	 * Set the concurrency of the listener container for this listener. Overrides the
	 * default set by the listener container factory. Maps to the concurrency setting of
	 * the container type.
	 * <p>For a
	 * {@link org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer
	 * SimpleMessageListenerContainer} if this value is a simple integer, it sets a fixed
	 * number of consumers in the {@code concurrentConsumers} property. If it is a string
	 * with the form {@code "m-n"}, the {@code concurrentConsumers} is set to {@code m}
	 * and the {@code maxConcurrentConsumers} is set to {@code n}.
	 * <p>For a
	 * {@link org.springframework.amqp.rabbit.listener.DirectMessageListenerContainer
	 * DirectMessageListenerContainer} it sets the {@code consumersPerQueue} property.
	 * @return the concurrency.
	 * @since 2.0
	 */
	String concurrency() default "";

	/**
	 * Set to true or false, to override the default setting in the container factory.
	 * @return true to auto start, false to not auto start.
	 * @since 2.0
	 */
	String autoStartup() default "";

	/**
	 * Set the task executor bean name to use for this listener's container; overrides
	 * any executor set on the container factory.
	 * @return the executor bean name.
	 * @since 2.2
	 */
	String executor() default "";

	/**
	 * Override the container factory
	 * {@link org.springframework.amqp.core.AcknowledgeMode} property. Must be one of the
	 * valid enumerations. If a SpEL expression is provided, it must evaluate to a
	 * {@link String} or {@link org.springframework.amqp.core.AcknowledgeMode}.
	 * @return the acknowledgement mode.
	 * @since 2.2
	 */
	String ackMode() default "";

	/**
	 * The bean name of a
	 * {@link org.springframework.amqp.rabbit.listener.adapter.ReplyPostProcessor} to post
	 * process a response before it is sent.
	 * @return the bean name.
	 * @since 2.2.5
	 * @see org.springframework.amqp.rabbit.listener.adapter.AbstractAdaptableMessageListener#setReplyPostProcessor(org.springframework.amqp.rabbit.listener.adapter.ReplyPostProcessor)
	 */
	String replyPostProcessor() default "";

}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187

1.1、 Queue[] queuesToDeclare() default {};

@RabbitListener(queuesToDeclare = @Queue("simple_queue"))

  • 1
  • 2
  1. 如果simple_queue队列不存在,则创建simple_queue队列。
  2. 队列默认
    1. 是持久化
    2. 非独占式的

1.2、 String[] queues() default {};

@RabbitListener(queues = {"simple_queue"}

  • 1
  • 2
  1. MQ中simple_queue队列必须存在,否则就会报错

2、使用

  1. 可以标记在类上
  2. 可以标记在方法上

2.1、标记在类上

  1. 需配合 @RabbitHandler 注解一起使用
  2. 当有收到消息的时候,就交给 @RabbitHandler标记的方法处理

在这里插入图片描述

2.2、可以标记在方法上

就由指定的方法进行处理

@Component
public class Simple_Consumer {
    // MQ中code_simple_queue1队列必须存在,否则就会报错
    // 默认队列是持久化,非独占式的
    @RabbitListener(queues = { "code_simple_queue1" })
    public void receive(Message message, Channel channel) throws Exception {
        // 消息id
        long deliveryTag = message.getMessageProperties().getDeliveryTag();
        
        System.out.println("Simple_Consumer:" + new String(message.getBody()));
    }
}


  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

在这里插入图片描述

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/IT小白/article/detail/716869
推荐阅读
相关标签
  

闽ICP备14008679号