Class RabbitAdmin
- java.lang.Object
-
- org.springframework.amqp.rabbit.core.RabbitAdmin
-
- All Implemented Interfaces:
AmqpAdmin,Aware,BeanNameAware,InitializingBean,ApplicationContextAware,ApplicationEventPublisherAware
@ManagedResource(description="Admin Tasks") public class RabbitAdmin extends Object implements AmqpAdmin, ApplicationContextAware, ApplicationEventPublisherAware, BeanNameAware, InitializingBean
RabbitMQ implementation of portable AMQP administrative operations for AMQP >= 0.9.1.- Author:
- Mark Pollack, Mark Fisher, Dave Syer, Ed Scriven, Gary Russell, Artem Bilan
-
-
Field Summary
Fields Modifier and Type Field Description static StringDEFAULT_EXCHANGE_NAMEThe default exchange name.protected LogloggerLogger available to subclasses.static ObjectQUEUE_CONSUMER_COUNTProperty key for the consumer count in thePropertiesreturned bygetQueueProperties(String).static ObjectQUEUE_MESSAGE_COUNTProperty key for the message count in thePropertiesreturned bygetQueueProperties(String).static ObjectQUEUE_NAMEProperty key for the queue name in thePropertiesreturned bygetQueueProperties(String).
-
Constructor Summary
Constructors Constructor Description RabbitAdmin(ConnectionFactory connectionFactory)Construct an instance using the providedConnectionFactory.RabbitAdmin(RabbitTemplate rabbitTemplate)Construct an instance using the providedRabbitTemplate.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidafterPropertiesSet()IfautoStartupis set to true, registers a callback on theConnectionFactoryto declare all exchanges and queues in the enclosing application context.voiddeclareBinding(Binding binding)Declare a binding of a queue to an exchange.voiddeclareExchange(Exchange exchange)Declare an exchange.QueuedeclareQueue()Declares a server-named exclusive, autodelete, non-durable queue.StringdeclareQueue(Queue queue)Declare the given queue.booleandeleteExchange(String exchangeName)Delete an exchange.booleandeleteQueue(String queueName)Delete a queue, without regard for whether it is in use or has messages on it.voiddeleteQueue(String queueName, boolean unused, boolean empty)Delete a queue.StringgetBeanName()DeclarationExceptionEventgetLastDeclarationExceptionEvent()QueueInformationgetQueueInfo(String queueName)Returns information about the queue, if it exists.PropertiesgetQueueProperties(String queueName)Returns 3 propertiesQUEUE_NAME,QUEUE_MESSAGE_COUNT,QUEUE_CONSUMER_COUNT, or null if the queue doesn't exist.RabbitTemplategetRabbitTemplate()voidinitialize()Declares all the exchanges, queues and bindings in the enclosing application context, if any.booleanisAutoStartup()booleanisRedeclareManualDeclarations()Normally, when a connection is recovered, the admin only recovers auto-delete queues, etc, that are declared as beans in the application context.intpurgeQueue(String queueName)Purges the contents of the given queue.voidpurgeQueue(String queueName, boolean noWait)Purges the contents of the given queue.voidremoveBinding(Binding binding)Remove a binding of a queue to an exchange.voidresetAllManualDeclarations()Invoke this method to prevent the admin from recovering any declarations made by calls todeclare*()methods.voidsetApplicationContext(ApplicationContext applicationContext)voidsetApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher)voidsetAutoStartup(boolean autoStartup)voidsetBeanName(String name)voidsetExplicitDeclarationsOnly(boolean explicitDeclarationsOnly)Set to true to only declareDeclarablebeans that are explicitly configured to be declared by this admin.voidsetIgnoreDeclarationExceptions(boolean ignoreDeclarationExceptions)voidsetRedeclareManualDeclarations(boolean redeclareManualDeclarations)Normally, when a connection is recovered, the admin only recovers auto-delete queues, etc, that are declared as beans in the application context.voidsetRetryTemplate(org.springframework.retry.support.RetryTemplate retryTemplate)Set a retry template for auto declarations.voidsetTaskExecutor(TaskExecutor taskExecutor)Set a task executor to use for async operations.
-
-
-
Field Detail
-
DEFAULT_EXCHANGE_NAME
public static final String DEFAULT_EXCHANGE_NAME
The default exchange name.- See Also:
- Constant Field Values
-
QUEUE_NAME
public static final Object QUEUE_NAME
Property key for the queue name in thePropertiesreturned bygetQueueProperties(String).
-
QUEUE_MESSAGE_COUNT
public static final Object QUEUE_MESSAGE_COUNT
Property key for the message count in thePropertiesreturned bygetQueueProperties(String).
-
QUEUE_CONSUMER_COUNT
public static final Object QUEUE_CONSUMER_COUNT
Property key for the consumer count in thePropertiesreturned bygetQueueProperties(String).
-
logger
protected final Log logger
Logger available to subclasses.
-
-
Constructor Detail
-
RabbitAdmin
public RabbitAdmin(ConnectionFactory connectionFactory)
Construct an instance using the providedConnectionFactory.- Parameters:
connectionFactory- the connection factory - must not be null.
-
RabbitAdmin
public RabbitAdmin(RabbitTemplate rabbitTemplate)
Construct an instance using the providedRabbitTemplate. Use this constructor when, for example, you want the admin operations to be performed within the scope of the provided template'sinvoke()method.- Parameters:
rabbitTemplate- the template - must not be null and must have a connection factory.- Since:
- 2.0
-
-
Method Detail
-
setAutoStartup
public void setAutoStartup(boolean autoStartup)
-
setApplicationContext
public void setApplicationContext(ApplicationContext applicationContext)
- Specified by:
setApplicationContextin interfaceApplicationContextAware
-
setApplicationEventPublisher
public void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher)
- Specified by:
setApplicationEventPublisherin interfaceApplicationEventPublisherAware
-
setIgnoreDeclarationExceptions
public void setIgnoreDeclarationExceptions(boolean ignoreDeclarationExceptions)
-
getLastDeclarationExceptionEvent
public DeclarationExceptionEvent getLastDeclarationExceptionEvent()
- Returns:
- the last
DeclarationExceptionEventthat was detected in this admin. - Since:
- 1.6
-
setTaskExecutor
public void setTaskExecutor(TaskExecutor taskExecutor)
Set a task executor to use for async operations. Currently only used withpurgeQueue(String, boolean).- Parameters:
taskExecutor- the executor to use.- Since:
- 2.1
-
getRabbitTemplate
public RabbitTemplate getRabbitTemplate()
-
declareExchange
public void declareExchange(Exchange exchange)
Description copied from interface:AmqpAdminDeclare an exchange.- Specified by:
declareExchangein interfaceAmqpAdmin- Parameters:
exchange- the exchange to declare.
-
deleteExchange
@ManagedOperation(description="Delete an exchange from the broker") public boolean deleteExchange(String exchangeName)
Description copied from interface:AmqpAdminDelete an exchange. Look at implementation specific subclass for implementation specific behavior, for example for RabbitMQ this will delete the exchange without regard for whether it is in use or not.- Specified by:
deleteExchangein interfaceAmqpAdmin- Parameters:
exchangeName- the name of the exchange- Returns:
- true if the exchange existed and was deleted
-
declareQueue
@ManagedOperation(description="Declare a queue on the broker (this operation is not available remotely)") @Nullable public String declareQueue(Queue queue)
Declare the given queue. If the queue doesn't have a value for 'name' property, the queue name will be generated by Broker and returned from this method. The declaredName property of the queue will be updated to reflect this value.- Specified by:
declareQueuein interfaceAmqpAdmin- Parameters:
queue- the queue- Returns:
- the queue name if successful, null if not successful and
ignoreDeclarationExceptionsis true.
-
declareQueue
@ManagedOperation(description="Declare a queue with a broker-generated name (this operation is not available remotely)") @Nullable public Queue declareQueue()
Declares a server-named exclusive, autodelete, non-durable queue.- Specified by:
declareQueuein interfaceAmqpAdmin- Returns:
- the queue or null if an exception occurred and
ignoreDeclarationExceptionsis true.
-
deleteQueue
@ManagedOperation(description="Delete a queue from the broker") public boolean deleteQueue(String queueName)
Description copied from interface:AmqpAdminDelete a queue, without regard for whether it is in use or has messages on it.- Specified by:
deleteQueuein interfaceAmqpAdmin- Parameters:
queueName- the name of the queue.- Returns:
- true if the queue existed and was deleted.
-
deleteQueue
@ManagedOperation(description="Delete a queue from the broker if unused and empty (when corresponding arguments are true") public void deleteQueue(String queueName, boolean unused, boolean empty)
Description copied from interface:AmqpAdminDelete a queue.- Specified by:
deleteQueuein interfaceAmqpAdmin- Parameters:
queueName- the name of the queue.unused- true if the queue should be deleted only if not in use.empty- true if the queue should be deleted only if empty.
-
purgeQueue
@ManagedOperation(description="Purge a queue and optionally don\'t wait for the purge to occur") public void purgeQueue(String queueName, boolean noWait)
Description copied from interface:AmqpAdminPurges the contents of the given queue.- Specified by:
purgeQueuein interfaceAmqpAdmin- Parameters:
queueName- the name of the queue.noWait- true to not await completion of the purge.
-
purgeQueue
@ManagedOperation(description="Purge a queue and return the number of messages purged") public int purgeQueue(String queueName)
Description copied from interface:AmqpAdminPurges the contents of the given queue.- Specified by:
purgeQueuein interfaceAmqpAdmin- Parameters:
queueName- the name of the queue.- Returns:
- the number of messages purged.
-
declareBinding
@ManagedOperation(description="Declare a binding on the broker (this operation is not available remotely)") public void declareBinding(Binding binding)
Description copied from interface:AmqpAdminDeclare a binding of a queue to an exchange.- Specified by:
declareBindingin interfaceAmqpAdmin- Parameters:
binding- a description of the binding to declare.
-
removeBinding
@ManagedOperation(description="Remove a binding from the broker (this operation is not available remotely)") public void removeBinding(Binding binding)
Description copied from interface:AmqpAdminRemove a binding of a queue to an exchange. Note unbindQueue/removeBinding was not introduced until 0.9 of the specification.- Specified by:
removeBindingin interfaceAmqpAdmin- Parameters:
binding- a description of the binding to remove.
-
getQueueProperties
@ManagedOperation(description="Get queue name, message count and consumer count") public Properties getQueueProperties(String queueName)
Returns 3 propertiesQUEUE_NAME,QUEUE_MESSAGE_COUNT,QUEUE_CONSUMER_COUNT, or null if the queue doesn't exist.- Specified by:
getQueuePropertiesin interfaceAmqpAdmin- Parameters:
queueName- the name of the queue.- Returns:
- the properties or null if the queue doesn't exist.
-
getQueueInfo
public QueueInformation getQueueInfo(String queueName)
Description copied from interface:AmqpAdminReturns information about the queue, if it exists.- Specified by:
getQueueInfoin interfaceAmqpAdmin- Parameters:
queueName- the name of the queue.- Returns:
- the information or null if the queue doesn't exist.
-
setExplicitDeclarationsOnly
public void setExplicitDeclarationsOnly(boolean explicitDeclarationsOnly)
Set to true to only declareDeclarablebeans that are explicitly configured to be declared by this admin.- Parameters:
explicitDeclarationsOnly- true to ignore beans with no admin declaration configuration.- Since:
- 2.1.9
-
isRedeclareManualDeclarations
public boolean isRedeclareManualDeclarations()
Normally, when a connection is recovered, the admin only recovers auto-delete queues, etc, that are declared as beans in the application context. When this is true, it will also redeclare any manually declaredDeclarables via admin methods.- Returns:
- true to redeclare.
- Since:
- 2.4
-
setRedeclareManualDeclarations
public void setRedeclareManualDeclarations(boolean redeclareManualDeclarations)
Normally, when a connection is recovered, the admin only recovers auto-delete queues, etc, that are declared as beans in the application context. When this is true, it will also redeclare any manually declaredDeclarables via admin methods. When a queue or exhange is deleted, it will not longer be recovered, nor will any corresponding bindings.- Parameters:
redeclareManualDeclarations- true to redeclare.- Since:
- 2.4
- See Also:
declareQueue(Queue),declareExchange(Exchange),declareBinding(Binding),deleteQueue(String),deleteExchange(String),removeBinding(Binding),resetAllManualDeclarations()
-
setRetryTemplate
public void setRetryTemplate(@Nullable org.springframework.retry.support.RetryTemplate retryTemplate)
Set a retry template for auto declarations. There is a race condition with auto-delete, exclusive queues in that the queue might still exist for a short time, preventing the redeclaration. The default retry configuration will try 5 times with an exponential backOff starting at 1 second a multiplier of 2.0 and a max interval of 5 seconds. To disable retry, set the argument tonull. Note that this retry is at the macro level - all declarations will be retried within the scope of this template. If you supplied aRabbitTemplatethat is configured with aRetryTemplate, its template will retry each individual declaration.- Parameters:
retryTemplate- the retry template.- Since:
- 1.7.8
-
setBeanName
public void setBeanName(String name)
- Specified by:
setBeanNamein interfaceBeanNameAware
-
getBeanName
public String getBeanName()
-
isAutoStartup
public boolean isAutoStartup()
-
afterPropertiesSet
public void afterPropertiesSet()
IfautoStartupis set to true, registers a callback on theConnectionFactoryto declare all exchanges and queues in the enclosing application context. If the callback fails then it may cause other clients of the connection factory to fail, but since only exchanges, queues and bindings are declared failure is not expected.- Specified by:
afterPropertiesSetin interfaceInitializingBean- See Also:
InitializingBean.afterPropertiesSet(),initialize()
-
initialize
public void initialize()
Declares all the exchanges, queues and bindings in the enclosing application context, if any. It should be safe (but unnecessary) to call this method more than once.- Specified by:
initializein interfaceAmqpAdmin
-
resetAllManualDeclarations
public void resetAllManualDeclarations()
Invoke this method to prevent the admin from recovering any declarations made by calls todeclare*()methods.- Since:
- 2.4
- See Also:
setRedeclareManualDeclarations(boolean)
-
-