RabbitMQ--集成Springboot--02--RabbitListener注解

RabbitMQ–集成Springboot–02–RabbitListener注解


代码位置

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

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.1、 Queue[] queuesToDeclare() default {};

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

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

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

@RabbitListener(queues = {"simple_queue"}

  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()));
    }
}


在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值