Example usage for org.springframework.amqp AmqpIllegalStateException AmqpIllegalStateException

List of usage examples for org.springframework.amqp AmqpIllegalStateException AmqpIllegalStateException

Introduction

In this page you can find the example usage for org.springframework.amqp AmqpIllegalStateException AmqpIllegalStateException.

Prototype

public AmqpIllegalStateException(String message, Throwable cause) 

Source Link

Usage

From source file:org.kurento.rabbitmq.RabbitTemplate.java

@Override
public void onMessage(Message message) {
    try {//from   w w  w .j  a va2 s .  c o m
        String messageTag;
        if (this.correlationKey == null) { // using standard correlationId
            // property
            messageTag = new String(message.getMessageProperties().getCorrelationId(), this.encoding);
        } else {
            messageTag = (String) message.getMessageProperties().getHeaders().get(this.correlationKey);
        }
        if (messageTag == null) {
            logger.error("No correlation header in reply");
            return;
        }

        PendingReply pendingReply = this.replyHolder.get(messageTag);
        if (pendingReply == null) {
            if (logger.isWarnEnabled()) {
                logger.warn("Reply received after timeout for " + messageTag);
            }
        } else {
            // Restore the inbound correlation data
            String savedCorrelation = pendingReply.getSavedCorrelation();
            if (this.correlationKey == null) {
                if (savedCorrelation == null) {
                    message.getMessageProperties().setCorrelationId(null);
                } else {
                    message.getMessageProperties().setCorrelationId(savedCorrelation.getBytes(this.encoding));
                }
            } else {
                if (savedCorrelation != null) {
                    message.getMessageProperties().setHeader(this.correlationKey, savedCorrelation);
                } else {
                    message.getMessageProperties().getHeaders().remove(this.correlationKey);
                }
            }
            // Restore any inbound replyTo
            String savedReplyTo = pendingReply.getSavedReplyTo();
            message.getMessageProperties().setReplyTo(savedReplyTo);
            LinkedBlockingQueue<Message> queue = pendingReply.getQueue();
            queue.add(message);
            if (logger.isDebugEnabled()) {
                logger.debug("Reply received for " + messageTag);
                if (savedReplyTo != null) {
                    logger.debug("Restored replyTo to " + savedReplyTo);
                }
            }
        }
    } catch (UnsupportedEncodingException e) {
        throw new AmqpIllegalStateException("Invalid Character Set:" + this.encoding, e);
    }
}

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

/**
 * Re-initializes this container's Rabbit message consumers, if not initialized already. Then submits each consumer
 * to this container's task executor.//from  www  .  j av a  2  s .c  om
 * @throws Exception Any Exception.
 */
@Override
protected void doStart() throws Exception {
    if (getMessageListener() instanceof ListenerContainerAware) {
        Collection<String> expectedQueueNames = ((ListenerContainerAware) getMessageListener())
                .expectedQueueNames();
        if (expectedQueueNames != null) {
            String[] queueNames = getQueueNames();
            Assert.state(expectedQueueNames.size() == queueNames.length,
                    "Listener expects us to be listening on '" + expectedQueueNames + "'; our queues: "
                            + Arrays.asList(queueNames));
            boolean found = false;
            for (String queueName : queueNames) {
                if (expectedQueueNames.contains(queueName)) {
                    found = true;
                } else {
                    found = false;
                    break;
                }
            }
            Assert.state(found, "Listener expects us to be listening on '" + expectedQueueNames
                    + "'; our queues: " + Arrays.asList(queueNames));
        }
    }
    if (this.rabbitAdmin == null && this.getApplicationContext() != null) {
        Map<String, RabbitAdmin> admins = this.getApplicationContext().getBeansOfType(RabbitAdmin.class);
        if (admins.size() == 1) {
            this.rabbitAdmin = admins.values().iterator().next();
        } else {
            if (this.autoDeclare || this.mismatchedQueuesFatal) {
                if (logger.isDebugEnabled()) {
                    logger.debug(
                            "For 'autoDeclare' and 'mismatchedQueuesFatal' to work, there must be exactly one "
                                    + "RabbitAdmin in the context or you must inject one into this container; found: "
                                    + admins.size() + " for container " + this.toString());
                }
            }
            if (this.mismatchedQueuesFatal) {
                throw new IllegalStateException("When 'mismatchedQueuesFatal' is 'true', there must be exactly "
                        + "one RabbitAdmin in the context or you must inject one into this container; found: "
                        + admins.size() + " for container " + this.toString());
            }
        }
    }
    checkMismatchedQueues();
    super.doStart();
    synchronized (this.consumersMonitor) {
        int newConsumers = initializeConsumers();
        if (this.consumers == null) {
            if (logger.isInfoEnabled()) {
                logger.info("Consumers were initialized and then cleared "
                        + "(presumably the container was stopped concurrently)");
            }
            return;
        }
        if (newConsumers <= 0) {
            if (logger.isInfoEnabled()) {
                logger.info("Consumers are already running");
            }
            return;
        }
        Set<AsyncMessageProcessingConsumer> processors = new HashSet<AsyncMessageProcessingConsumer>();
        for (BlockingQueueConsumer consumer : this.consumers.keySet()) {
            AsyncMessageProcessingConsumer processor = new AsyncMessageProcessingConsumer(consumer);
            processors.add(processor);
            this.taskExecutor.execute(processor);
        }
        for (AsyncMessageProcessingConsumer processor : processors) {
            FatalListenerStartupException startupException = processor.getStartupException();
            if (startupException != null) {
                throw new AmqpIllegalStateException("Fatal exception on listener startup", startupException);
            }
        }
    }
}

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

protected void addAndStartConsumers(int delta) {
    synchronized (this.consumersMonitor) {
        if (this.consumers != null) {
            for (int i = 0; i < delta; i++) {
                BlockingQueueConsumer consumer = createBlockingQueueConsumer();
                this.consumers.put(consumer, true);
                AsyncMessageProcessingConsumer processor = new AsyncMessageProcessingConsumer(consumer);
                if (logger.isDebugEnabled()) {
                    logger.debug("Starting a new consumer: " + consumer);
                }/*  w  w w  .j  a  v a 2s  . c o m*/
                this.taskExecutor.execute(processor);
                try {
                    FatalListenerStartupException startupException = processor.getStartupException();
                    if (startupException != null) {
                        this.consumers.remove(consumer);
                        throw new AmqpIllegalStateException("Fatal exception on listener startup",
                                startupException);
                    }
                } catch (InterruptedException ie) {
                    Thread.currentThread().interrupt();
                } catch (Exception e) {
                    consumer.stop();
                    logger.error("Error starting new consumer", e);
                    this.cancellationLock.release(consumer);
                    this.consumers.remove(consumer);
                }
            }
        }
    }
}