Example usage for org.springframework.amqp.rabbit.listener BlockingQueueConsumer basicCancel

List of usage examples for org.springframework.amqp.rabbit.listener BlockingQueueConsumer basicCancel

Introduction

In this page you can find the example usage for org.springframework.amqp.rabbit.listener BlockingQueueConsumer basicCancel.

Prototype

protected void basicCancel() 

Source Link

Usage

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

/**
 * Specify the number of concurrent consumers to create. Default is 1.
 * <p>//from  w  ww.  j a  v a2s  .  c  o  m
 * Raising the number of concurrent consumers is recommended in order to scale the consumption of messages coming in
 * from a queue. However, note that any ordering guarantees are lost once multiple consumers are registered. In
 * general, stick with 1 consumer for low-volume queues. Cannot be more than {@link #maxConcurrentConsumers} (if set).
 * @param concurrentConsumers the minimum number of consumers to create.
 * @see #setMaxConcurrentConsumers(int)
 */
public void setConcurrentConsumers(final int concurrentConsumers) {
    Assert.isTrue(concurrentConsumers > 0, "'concurrentConsumers' value must be at least 1 (one)");
    Assert.isTrue(!isExclusive() || concurrentConsumers == 1,
            "When the consumer is exclusive, the concurrency must be 1");
    if (this.maxConcurrentConsumers != null) {
        Assert.isTrue(concurrentConsumers <= this.maxConcurrentConsumers,
                "'concurrentConsumers' cannot be more than 'maxConcurrentConsumers'");
    }
    synchronized (this.consumersMonitor) {
        if (logger.isDebugEnabled()) {
            logger.debug("Changing consumers from " + this.concurrentConsumers + " to " + concurrentConsumers);
        }
        int delta = this.concurrentConsumers - concurrentConsumers;
        this.concurrentConsumers = concurrentConsumers;
        if (isActive() && this.consumers != null) {
            if (delta > 0) {
                Iterator<Entry<BlockingQueueConsumer, Boolean>> entryIterator = this.consumers.entrySet()
                        .iterator();
                while (entryIterator.hasNext() && delta > 0) {
                    Entry<BlockingQueueConsumer, Boolean> entry = entryIterator.next();
                    if (entry.getValue()) {
                        BlockingQueueConsumer consumer = entry.getKey();
                        consumer.basicCancel();
                        this.consumers.put(consumer, false);
                        delta--;
                    }
                }

            } else {
                addAndStartConsumers(-delta);
            }
        }
    }
}

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

@Override
protected void doShutdown() {

    if (!this.isRunning()) {
        return;/*  w w  w. j  a  v  a2  s . c  o m*/
    }

    try {
        synchronized (this.consumersMonitor) {
            if (this.consumers != null) {
                for (BlockingQueueConsumer consumer : this.consumers.keySet()) {
                    consumer.basicCancel();
                }
            }
        }
        logger.info("Waiting for workers to finish.");
        boolean finished = this.cancellationLock.await(getShutdownTimeout(), TimeUnit.MILLISECONDS);
        if (finished) {
            logger.info("Successfully waited for workers to finish.");
        } else {
            logger.info("Workers not finished.");
        }
    } catch (InterruptedException e) {
        Thread.currentThread().interrupt();
        logger.warn("Interrupted waiting for workers.  Continuing with shutdown.");
    }

    synchronized (this.consumersMonitor) {
        this.consumers = null;
    }

}

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

private void considerStoppingAConsumer(BlockingQueueConsumer consumer) {
    synchronized (this.consumersMonitor) {
        if (this.consumers != null && this.consumers.size() > this.concurrentConsumers) {
            long now = System.currentTimeMillis();
            if (this.lastConsumerStopped + this.stopConsumerMinInterval < now) {
                consumer.basicCancel();
                this.consumers.put(consumer, false);
                if (logger.isDebugEnabled()) {
                    logger.debug("Idle consumer terminating: " + consumer);
                }//from   ww w.j a va 2s. c o m
                this.lastConsumerStopped = now;
            }
        }
    }
}