@Test public void testActiveCount() throws Exception { final Object object1 = new Object(); final Object object2 = new Object(); counter.add(object1); counter.add(object2); assertEquals(2, counter.getCount()); counter.release(object2); assertEquals(1, counter.getCount()); counter.release(object1); counter.release(object1); assertEquals(0, counter.getCount()); }
@Test public void testWaitForLocks() throws Exception { final Object object1 = new Object(); final Object object2 = new Object(); counter.add(object1); counter.add(object2); Future<Boolean> future = Executors.newSingleThreadExecutor().submit(() -> { counter.release(object1); counter.release(object2); counter.release(object2); return true; }); assertEquals(true, counter.await(1000L, TimeUnit.MILLISECONDS)); assertEquals(true, future.get()); }
boolean finished = this.cancellationLock.await(getShutdownTimeout(), TimeUnit.MILLISECONDS); if (finished) { logger.info("Successfully waited for workers to finish."); this.cancellationLock.deactivate();
@Override public void handleShutdownSignal(String consumerTag, ShutdownSignalException sig) { if (logger.isDebugEnabled()) { if (RabbitUtils.isNormalShutdown(sig)) { logger.debug("Received shutdown signal for consumer tag=" + consumerTag + ": " + sig.getMessage()); } else { logger.debug("Received shutdown signal for consumer tag=" + consumerTag, sig); } } BlockingQueueConsumer.this.shutdown = sig; // The delivery tags will be invalid if the channel shuts down BlockingQueueConsumer.this.deliveryTags.clear(); BlockingQueueConsumer.this.activeObjectCounter.release(BlockingQueueConsumer.this); }
public void start() throws AmqpException { if (logger.isDebugEnabled()) { logger.debug("Starting consumer " + this); } this.thread = Thread.currentThread(); try { this.resourceHolder = ConnectionFactoryUtils.getTransactionalResourceHolder(this.connectionFactory, this.transactional); this.channel = this.resourceHolder.getChannel(); ClosingRecoveryListener.addRecoveryListenerIfNecessary(this.channel); // NOSONAR never null here } catch (AmqpAuthenticationException e) { throw new FatalListenerStartupException("Authentication failure", e); } this.deliveryTags.clear(); this.activeObjectCounter.add(this); passiveDeclarations(); setQosAndreateConsumers(); }
@ManagedMetric(metricType = MetricType.GAUGE) public int getActiveConsumerCount() { return this.cancellationLock.getCount(); }
if (this.cancellationLock.await(getShutdownTimeout(), TimeUnit.MILLISECONDS)) { this.logger.info("Successfully waited for consumers to finish.");
protected int initializeConsumers() { int count = 0; synchronized (this.consumersMonitor) { if (this.consumers == null) { this.cancellationLock.reset(); this.consumers = new HashSet<BlockingQueueConsumer>(this.concurrentConsumers); for (int i = 0; i < this.concurrentConsumers; i++) { BlockingQueueConsumer consumer = createBlockingQueueConsumer(); this.consumers.add(consumer); count++; } } } return count; }
private void testRequeueOrNotDefaultYes(Exception ex, boolean expectedRequeue) throws Exception { ConnectionFactory connectionFactory = mock(ConnectionFactory.class); Channel channel = mock(Channel.class); BlockingQueueConsumer blockingQueueConsumer = new BlockingQueueConsumer(connectionFactory, new DefaultMessagePropertiesConverter(), new ActiveObjectCounter<BlockingQueueConsumer>(), AcknowledgeMode.AUTO, true, 1, "testQ"); testRequeueOrNotGuts(ex, expectedRequeue, channel, blockingQueueConsumer); }
protected boolean cancelled() { return this.cancelled.get() || (this.abortStarted > 0 && this.abortStarted + this.shutdownTimeout > System.currentTimeMillis()) || !this.activeObjectCounter.isActive(); }
@Override public void handleShutdownSignal(String consumerTag, ShutdownSignalException sig) { if (logger.isDebugEnabled()) { if (RabbitUtils.isNormalShutdown(sig)) { logger.debug("Received shutdown signal for consumer tag=" + consumerTag + ": " + sig.getMessage()); } else { logger.debug("Received shutdown signal for consumer tag=" + consumerTag, sig); } } BlockingQueueConsumer.this.shutdown = sig; // The delivery tags will be invalid if the channel shuts down BlockingQueueConsumer.this.deliveryTags.clear(); BlockingQueueConsumer.this.activeObjectCounter.release(BlockingQueueConsumer.this); }
public void start() throws AmqpException { if (logger.isDebugEnabled()) { logger.debug("Starting consumer " + this); } this.thread = Thread.currentThread(); try { this.resourceHolder = ConnectionFactoryUtils.getTransactionalResourceHolder(this.connectionFactory, this.transactional); this.channel = this.resourceHolder.getChannel(); ClosingRecoveryListener.addRecoveryListenerIfNecessary(this.channel); // NOSONAR never null here } catch (AmqpAuthenticationException e) { throw new FatalListenerStartupException("Authentication failure", e); } this.deliveryTags.clear(); this.activeObjectCounter.add(this); passiveDeclarations(); setQosAndreateConsumers(); }
@ManagedMetric(metricType = MetricType.GAUGE) public int getActiveConsumerCount() { return this.cancellationLock.getCount(); }
if (this.cancellationLock.await(getShutdownTimeout(), TimeUnit.MILLISECONDS)) { this.logger.info("Successfully waited for consumers to finish.");
protected int initializeConsumers() { int count = 0; synchronized (this.consumersMonitor) { if (this.consumers == null) { this.cancellationLock.reset(); this.consumers = new HashSet<BlockingQueueConsumer>(this.concurrentConsumers); for (int i = 0; i < this.concurrentConsumers; i++) { BlockingQueueConsumer consumer = createBlockingQueueConsumer(); this.consumers.add(consumer); count++; } } } return count; }
private void testRequeueOrNotDefaultNo(Exception ex, boolean expectedRequeue) throws Exception { ConnectionFactory connectionFactory = mock(ConnectionFactory.class); Channel channel = mock(Channel.class); BlockingQueueConsumer blockingQueueConsumer = new BlockingQueueConsumer(connectionFactory, new DefaultMessagePropertiesConverter(), new ActiveObjectCounter<BlockingQueueConsumer>(), AcknowledgeMode.AUTO, true, 1, false, "testQ"); testRequeueOrNotGuts(ex, expectedRequeue, channel, blockingQueueConsumer); }
protected boolean cancelled() { return this.cancelled.get() || (this.abortStarted > 0 && this.abortStarted + this.shutdownTimeout > System.currentTimeMillis()) || !this.activeObjectCounter.isActive(); }
private void handleDeclarationException(int passiveDeclareRetries, DeclarationException e) { if (passiveDeclareRetries > 0 && this.channel.isOpen()) { if (logger.isWarnEnabled()) { logger.warn("Queue declaration failed; retries left=" + (passiveDeclareRetries), e); try { Thread.sleep(this.failedDeclarationRetryInterval); } catch (InterruptedException e1) { this.declaring = false; Thread.currentThread().interrupt(); this.activeObjectCounter.release(this); throw RabbitExceptionTranslator.convertRabbitAccessException(e1); // NOSONAR stack trace loss } } } else if (e.getFailedQueues().size() < this.queues.length) { if (logger.isWarnEnabled()) { logger.warn("Not all queues are available; only listening on those that are - configured: " + Arrays.asList(this.queues) + "; not available: " + e.getFailedQueues()); } this.missingQueues.addAll(e.getFailedQueues()); this.lastRetryDeclaration = System.currentTimeMillis(); } else { this.declaring = false; this.activeObjectCounter.release(this); throw new QueuesNotAvailableException("Cannot prepare queue for listener. " + "Either the queue doesn't exist or the broker will not allow us to use it.", e); } }
boolean finished = this.cancellationLock.await(getShutdownTimeout(), TimeUnit.MILLISECONDS); if (finished) { logger.info("Successfully waited for workers to finish."); this.cancellationLock.deactivate();