Example usage for org.springframework.amqp.rabbit.core RabbitAdmin initialize

List of usage examples for org.springframework.amqp.rabbit.core RabbitAdmin initialize

Introduction

In this page you can find the example usage for org.springframework.amqp.rabbit.core RabbitAdmin initialize.

Prototype

@Override 
public void initialize() 

Source Link

Document

Declares all the exchanges, queues and bindings in the enclosing application context, if any.

Usage

From source file:org.springframework.amqp.rabbit.listener.AbstractMessageListenerContainer.java

/**
 * Use {@link RabbitAdmin#initialize()} to redeclare everything if necessary.
 * Since auto deletion of a queue can cause upstream elements
 * (bindings, exchanges) to be deleted too, everything needs to be redeclared if
 * a queue is missing./*from   www  . ja  v a  2 s .  co m*/
 * Declaration is idempotent so, aside from some network chatter, there is no issue,
 * and we only will do it if we detect our queue is gone.
 * <p>
 * In general it makes sense only for the 'auto-delete' or 'expired' queues,
 * but with the server TTL policy we don't have ability to determine 'expiration'
 * option for the queue.
 * <p>
 * Starting with version 1.6, if
 * {@link #setMismatchedQueuesFatal(boolean) mismatchedQueuesFatal} is true,
 * the declarations are always attempted during restart so the listener will
 * fail with a fatal error if mismatches occur.
 */
protected synchronized void redeclareElementsIfNecessary() {
    RabbitAdmin rabbitAdmin = getRabbitAdmin();
    if (rabbitAdmin == null || !isAutoDeclare()) {
        return;
    }
    try {
        ApplicationContext applicationContext = this.getApplicationContext();
        if (applicationContext != null) {
            Set<String> queueNames = this.getQueueNamesAsSet();
            Map<String, Queue> queueBeans = applicationContext.getBeansOfType(Queue.class);
            for (Entry<String, Queue> entry : queueBeans.entrySet()) {
                Queue queue = entry.getValue();
                if (isMismatchedQueuesFatal() || (queueNames.contains(queue.getName())
                        && rabbitAdmin.getQueueProperties(queue.getName()) == null)) {
                    if (logger.isDebugEnabled()) {
                        logger.debug("Redeclaring context exchanges, queues, bindings.");
                    }
                    rabbitAdmin.initialize();
                    return;
                }
            }
        }
    } catch (Exception e) {
        if (RabbitUtils.isMismatchedQueueArgs(e)) {
            throw new FatalListenerStartupException("Mismatched queues", e);
        }
        logger.error("Failed to check/redeclare auto-delete queue(s).", e);
    }
}