@SuppressWarnings("unchecked") private void waitForNewConsumer(PublishSubscribeAmqpChannel channel, BlockingQueueConsumer consumer) throws Exception { final Object consumersMonitor = TestUtils.getPropertyValue(channel, "container.consumersMonitor"); int n = 0; while (n++ < 100) { Set<BlockingQueueConsumer> consumers = TestUtils.getPropertyValue(channel, "container.consumers", Set.class); synchronized (consumersMonitor) { if (!consumers.isEmpty()) { BlockingQueueConsumer newConsumer = consumers.iterator().next(); if (newConsumer != consumer && newConsumer.getConsumerTags().size() > 0) { break; } } } Thread.sleep(100); } assertTrue("Failed to restart consumer", n < 100); }
@Override public String toString() { return "Consumer@" + ObjectUtils.getIdentityHexString(this) + ": " + "tags=[" + getConsumerTags() + "], channel=" + this.channel + ", acknowledgeMode=" + this.acknowledgeMode + " local queue size=" + this.queue.size(); }
@Override public String toString() { return "Consumer@" + ObjectUtils.getIdentityHexString(this) + ": " + "tags=[" + getConsumerTags() + "], channel=" + this.channel + ", acknowledgeMode=" + this.acknowledgeMode + " local queue size=" + this.queue.size(); }
protected void basicCancel(boolean expected) { this.normalCancel = expected; getConsumerTags().forEach(consumerTag -> { try { if (this.channel.isOpen()) { this.channel.basicCancel(consumerTag); } } catch (IOException | IllegalStateException e) { if (logger.isDebugEnabled()) { logger.debug("Error performing 'basicCancel'", e); } } catch (AlreadyClosedException e) { if (logger.isTraceEnabled()) { logger.trace(this.channel + " is already closed"); } } }); this.cancelled.set(true); this.abortStarted = System.currentTimeMillis(); }
protected void basicCancel(boolean expected) { this.normalCancel = expected; getConsumerTags().forEach(consumerTag -> { try { if (this.channel.isOpen()) { this.channel.basicCancel(consumerTag); } } catch (IOException | IllegalStateException e) { if (logger.isDebugEnabled()) { logger.debug("Error performing 'basicCancel'", e); } } catch (AlreadyClosedException e) { if (logger.isTraceEnabled()) { logger.trace(this.channel + " is already closed"); } } }); this.cancelled.set(true); this.abortStarted = System.currentTimeMillis(); }
public synchronized void stop() { if (this.abortStarted == 0) { // signal handle delivery to use offer this.abortStarted = System.currentTimeMillis(); } if (!this.cancelled()) { try { RabbitUtils.closeMessageConsumer(this.channel, getConsumerTags(), this.transactional); } catch (Exception e) { if (logger.isDebugEnabled()) { logger.debug("Error closing consumer " + this, e); } } } if (logger.isDebugEnabled()) { logger.debug("Closing Rabbit Channel: " + this.channel); } RabbitUtils.setPhysicalCloseRequired(this.channel, true); ConnectionFactoryUtils.releaseResources(this.resourceHolder); this.deliveryTags.clear(); this.consumers.clear(); this.queue.clear(); // in case we still have a client thread blocked }
public synchronized void stop() { if (this.abortStarted == 0) { // signal handle delivery to use offer this.abortStarted = System.currentTimeMillis(); } if (!this.cancelled()) { try { RabbitUtils.closeMessageConsumer(this.channel, getConsumerTags(), this.transactional); } catch (Exception e) { if (logger.isDebugEnabled()) { logger.debug("Error closing consumer " + this, e); } } } if (logger.isDebugEnabled()) { logger.debug("Closing Rabbit Channel: " + this.channel); } RabbitUtils.setPhysicalCloseRequired(this.channel, true); ConnectionFactoryUtils.releaseResources(this.resourceHolder); this.deliveryTags.clear(); this.consumers.clear(); this.queue.clear(); // in case we still have a client thread blocked }
private BlockingQueueConsumer createConsumer(RabbitAccessor accessor) { BlockingQueueConsumer consumer = new BlockingQueueConsumer( accessor.getConnectionFactory(), new DefaultMessagePropertiesConverter(), new ActiveObjectCounter<BlockingQueueConsumer>(), AcknowledgeMode.AUTO, true, 1, queue.getName()); consumer.start(); // wait for consumeOk... int n = 0; while (n++ < 100) { if (consumer.getConsumerTags().size() == 0) { try { Thread.sleep(100); } catch (InterruptedException e) { Thread.currentThread().interrupt(); break; } } } return consumer; }
String tag = consumer.getConsumerTags().iterator().next(); assertNotNull(tag); String tag = consumer.getConsumerTags().iterator().next(); assertNotNull(tag);
@Test public void testSendAndReceiveWithFanout() throws Exception { RabbitAdmin admin = new RabbitAdmin(connectionFactory); FanoutExchange exchange = new FanoutExchange("fanout"); admin.declareExchange(exchange); template.setExchange(exchange.getName()); admin.declareBinding(BindingBuilder.bind(queue).to(exchange)); template.execute(channel -> { BlockingQueueConsumer consumer = createConsumer(template); String tag = consumer.getConsumerTags().iterator().next(); assertNotNull(tag); try { template.convertAndSend("message"); String result = getResult(consumer); assertEquals("message", result); } finally { consumer.stop(); } return null; }); admin.deleteExchange("fanout"); }
@Test public void testSendAndReceiveWithNonDefaultExchange() throws Exception { final RabbitAdmin admin = new RabbitAdmin(connectionFactory); final TopicExchange exchange = new TopicExchange("topic"); admin.declareExchange(exchange); admin.declareBinding(BindingBuilder.bind(queue).to(exchange).with("*.end")); template.execute(channel -> { BlockingQueueConsumer consumer = createConsumer(template); String tag = consumer.getConsumerTags().iterator().next(); assertNotNull(tag); template.convertAndSend("topic", "foo", "message"); try { String result = getResult(consumer); assertEquals(null, result); template.convertAndSend("topic", "foo.end", "message"); result = getResult(consumer); assertEquals("message", result); } finally { consumer.getChannel().basicCancel(tag); } return null; }); admin.deleteExchange("topic"); }
@Test public void testSendAndReceiveWithTopicSingleCallback() throws Exception { final RabbitAdmin admin = new RabbitAdmin(connectionFactory); final TopicExchange exchange = new TopicExchange("topic"); admin.declareExchange(exchange); template.setExchange(exchange.getName()); admin.declareBinding(BindingBuilder.bind(queue).to(exchange).with("*.end")); template.execute(channel -> { BlockingQueueConsumer consumer = createConsumer(template); String tag = consumer.getConsumerTags().iterator().next(); assertNotNull(tag); template.convertAndSend("foo", "message"); try { String result = getResult(consumer); assertEquals(null, result); template.convertAndSend("foo.end", "message"); result = getResult(consumer); assertEquals("message", result); } finally { consumer.getChannel().basicCancel(tag); } return null; }); admin.deleteExchange("topic"); }
@Test // @Ignore("Not sure yet if we need to support a use case like this") public void testSendAndReceiveWithTopicConsumeInBackground() throws Exception { RabbitAdmin admin = new RabbitAdmin(connectionFactory); TopicExchange exchange = new TopicExchange("topic"); admin.declareExchange(exchange); template.setExchange(exchange.getName()); admin.declareBinding(BindingBuilder.bind(queue).to(exchange).with("*.end")); final CachingConnectionFactory cachingConnectionFactory = new CachingConnectionFactory(); cachingConnectionFactory.setHost("localhost"); final RabbitTemplate template = new RabbitTemplate(cachingConnectionFactory); template.setExchange(exchange.getName()); BlockingQueueConsumer consumer = template.execute(channel -> { BlockingQueueConsumer consumer1 = createConsumer(template); String tag = consumer1.getConsumerTags().iterator().next(); assertNotNull(tag); return consumer1; }); template.convertAndSend("foo", "message"); String result = getResult(consumer); assertEquals(null, result); template.convertAndSend("foo.end", "message"); result = getResult(consumer); assertEquals("message", result); consumer.stop(); admin.deleteExchange("topic"); cachingConnectionFactory.destroy(); }