List of usage examples for org.springframework.amqp.rabbit.core RabbitAdmin getQueueProperties
@Override @ManagedOperation(description = "Get queue name, message count and consumer count") public Properties getQueueProperties(final String queueName)
From source file:org.springframework.amqp.rabbit.core.RabbitAdminTests.java
@Test public void testProperties() throws Exception { SingleConnectionFactory connectionFactory = new SingleConnectionFactory(); connectionFactory.setHost("localhost"); RabbitAdmin rabbitAdmin = new RabbitAdmin(connectionFactory); String queueName = "test.properties." + System.currentTimeMillis(); try {/* w w w. ja v a2 s. c om*/ rabbitAdmin.declareQueue(new Queue(queueName)); new RabbitTemplate(connectionFactory).convertAndSend(queueName, "foo"); int n = 0; while (n++ < 100 && messageCount(rabbitAdmin, queueName) == 0) { Thread.sleep(100); } assertTrue("Message count = 0", n < 100); Channel channel = connectionFactory.createConnection().createChannel(false); DefaultConsumer consumer = new DefaultConsumer(channel); channel.basicConsume(queueName, true, consumer); n = 0; while (n++ < 100 && messageCount(rabbitAdmin, queueName) > 0) { Thread.sleep(100); } assertTrue("Message count > 0", n < 100); Properties props = rabbitAdmin.getQueueProperties(queueName); assertNotNull(props.get(RabbitAdmin.QUEUE_CONSUMER_COUNT)); assertEquals(1, props.get(RabbitAdmin.QUEUE_CONSUMER_COUNT)); channel.close(); } finally { rabbitAdmin.deleteQueue(queueName); connectionFactory.destroy(); } }
From source file:org.springframework.amqp.rabbit.core.RabbitAdminTests.java
private int messageCount(RabbitAdmin rabbitAdmin, String queueName) { Properties props = rabbitAdmin.getQueueProperties(queueName); assertNotNull(props);/*from w w w . j ava2 s. c om*/ assertNotNull(props.get(RabbitAdmin.QUEUE_MESSAGE_COUNT)); return (Integer) props.get(RabbitAdmin.QUEUE_MESSAGE_COUNT); }
From source file:org.springframework.amqp.rabbit.core.RabbitAdminTests.java
@Test public void testMultiEntities() { ConfigurableApplicationContext ctx = new AnnotationConfigApplicationContext(Config.class); RabbitTemplate template = ctx.getBean(RabbitTemplate.class); template.convertAndSend("e1", "k1", "foo"); template.convertAndSend("e2", "k2", "bar"); template.convertAndSend("e3", "k3", "baz"); template.convertAndSend("e4", "k4", "qux"); assertEquals("foo", template.receiveAndConvert("q1")); assertEquals("bar", template.receiveAndConvert("q2")); assertEquals("baz", template.receiveAndConvert("q3")); assertEquals("qux", template.receiveAndConvert("q4")); RabbitAdmin admin = ctx.getBean(RabbitAdmin.class); admin.deleteQueue("q1"); admin.deleteQueue("q2"); admin.deleteQueue("q3"); admin.deleteQueue("q4"); admin.deleteExchange("e1"); admin.deleteExchange("e2"); admin.deleteExchange("e3"); admin.deleteExchange("e4"); assertNull(admin.getQueueProperties(ctx.getBean(Config.class).protypeQueueName)); ctx.close();/*from w ww . j ava 2s .c o m*/ }
From source file:org.springframework.amqp.rabbit.core.RabbitAdminTests.java
@Test public void testAvoidHangAMQP_508() { CachingConnectionFactory cf = new CachingConnectionFactory("localhost"); RabbitAdmin admin = new RabbitAdmin(cf); String longName = new String(new byte[300]).replace('\u0000', 'x'); try {/* w w w . j av a 2s . c o m*/ admin.declareQueue(new Queue(longName)); } catch (Exception e) { e.printStackTrace(); } String goodName = "foobar"; admin.declareQueue(new Queue(goodName)); assertNull(admin.getQueueProperties(longName)); assertNotNull(admin.getQueueProperties(goodName)); admin.deleteQueue(goodName); cf.destroy(); }
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 w w w . ja va 2 s. c o 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); } }
From source file:org.springframework.amqp.rabbit.listener.DirectMessageListenerContainer.java
private void checkMissingQueues(String[] queueNames) { if (isMissingQueuesFatal()) { RabbitAdmin checkAdmin = getRabbitAdmin(); if (checkAdmin == null) { /*/* ww w . j av a2 s . com*/ * Checking queue existence doesn't require an admin in the context or injected into * the container. If there's no such admin, just create a local one here. */ checkAdmin = new RabbitAdmin(getConnectionFactory()); } for (String queue : queueNames) { Properties queueProperties = checkAdmin.getQueueProperties(queue); if (queueProperties == null && isMissingQueuesFatal()) { throw new IllegalStateException("At least one of the configured queues is missing"); } } } }
From source file:org.springframework.amqp.rabbit.listener.DirectMessageListenerContainerTests.java
private boolean consumersOnQueue(String queue, int expected) throws Exception { int n = 0;// w w w . ja v a 2 s . co m RabbitAdmin admin = brokerRunning.getAdmin(); Properties queueProperties = admin.getQueueProperties(queue); LogFactory.getLog(getClass()).debug(queue + " waiting for " + expected + " : " + queueProperties); while (n++ < 600 && (queueProperties == null || !queueProperties.get(RabbitAdmin.QUEUE_CONSUMER_COUNT).equals(expected))) { Thread.sleep(100); queueProperties = admin.getQueueProperties(queue); LogFactory.getLog(getClass()).debug(queue + " waiting for " + expected + " : " + queueProperties); } return queueProperties.get(RabbitAdmin.QUEUE_CONSUMER_COUNT).equals(expected); }
From source file:org.springframework.cloud.stream.binder.rabbit.RabbitBinderTests.java
@Test public void testDurablePubSubWithAutoBindDLQ() throws Exception { RabbitAdmin admin = new RabbitAdmin(this.rabbitAvailableRule.getResource()); RabbitTestBinder binder = getBinder(); ExtendedConsumerProperties<RabbitConsumerProperties> consumerProperties = createConsumerProperties(); consumerProperties.getExtension().setPrefix(TEST_PREFIX); consumerProperties.getExtension().setAutoBindDlq(true); consumerProperties.getExtension().setDurableSubscription(true); consumerProperties.setMaxAttempts(1); // disable retry DirectChannel moduleInputChannel = createBindableChannel("input", createConsumerBindingProperties(consumerProperties)); moduleInputChannel.setBeanName("durableTest"); moduleInputChannel.subscribe(new MessageHandler() { @Override//w w w .j a v a 2 s . c o m public void handleMessage(Message<?> message) throws MessagingException { throw new RuntimeException("foo"); } }); Binding<MessageChannel> consumerBinding = binder.bindConsumer("durabletest.0", "tgroup", moduleInputChannel, consumerProperties); RabbitTemplate template = new RabbitTemplate(this.rabbitAvailableRule.getResource()); template.convertAndSend(TEST_PREFIX + "durabletest.0", "", "foo"); int n = 0; while (n++ < 100) { Object deadLetter = template.receiveAndConvert(TEST_PREFIX + "durabletest.0.tgroup.dlq"); if (deadLetter != null) { assertThat(deadLetter).isEqualTo("foo"); break; } Thread.sleep(100); } assertThat(n).isLessThan(100); consumerBinding.unbind(); assertThat(admin.getQueueProperties(TEST_PREFIX + "durabletest.0.tgroup.dlq")).isNotNull(); }
From source file:org.springframework.cloud.stream.binder.rabbit.RabbitBinderTests.java
@Test public void testNonDurablePubSubWithAutoBindDLQ() throws Exception { RabbitAdmin admin = new RabbitAdmin(this.rabbitAvailableRule.getResource()); RabbitTestBinder binder = getBinder(); ExtendedConsumerProperties<RabbitConsumerProperties> consumerProperties = createConsumerProperties(); consumerProperties.getExtension().setPrefix(TEST_PREFIX); consumerProperties.getExtension().setAutoBindDlq(true); consumerProperties.getExtension().setDurableSubscription(false); consumerProperties.setMaxAttempts(1); // disable retry BindingProperties bindingProperties = createConsumerBindingProperties(consumerProperties); DirectChannel moduleInputChannel = createBindableChannel("input", bindingProperties); moduleInputChannel.setBeanName("nondurabletest"); moduleInputChannel.subscribe(new MessageHandler() { @Override/*from w w w . j a v a 2 s .c o m*/ public void handleMessage(Message<?> message) throws MessagingException { throw new RuntimeException("foo"); } }); Binding<MessageChannel> consumerBinding = binder.bindConsumer("nondurabletest.0", "tgroup", moduleInputChannel, consumerProperties); consumerBinding.unbind(); assertThat(admin.getQueueProperties(TEST_PREFIX + "nondurabletest.0.dlq")).isNull(); }
From source file:org.springframework.xd.dirt.integration.bus.rabbit.RabbitMessageBusTests.java
@Test public void testDurablePubSubWithAutoBindDLQ() throws Exception { RabbitAdmin admin = new RabbitAdmin(this.rabbitAvailableRule.getResource()); MessageBus bus = getMessageBus();//from w ww . ja v a 2 s . co m Properties properties = new Properties(); properties.put("prefix", "xdbustest."); properties.put("autoBindDLQ", "true"); properties.put("durableSubscription", "true"); properties.put("maxAttempts", "1"); // disable retry properties.put("requeue", "false"); DirectChannel moduleInputChannel = new DirectChannel(); moduleInputChannel.setBeanName("durableTest"); moduleInputChannel.subscribe(new MessageHandler() { @Override public void handleMessage(Message<?> message) throws MessagingException { throw new RuntimeException("foo"); } }); bus.bindPubSubConsumer("teststream.tap:stream:durabletest.0", moduleInputChannel, properties); RabbitTemplate template = new RabbitTemplate(this.rabbitAvailableRule.getResource()); template.convertAndSend("xdbustest.topic.tap:stream:durabletest.0", "", "foo"); int n = 0; while (n++ < 100) { Object deadLetter = template.receiveAndConvert("xdbustest.teststream.tap:stream:durabletest.0.dlq"); if (deadLetter != null) { assertEquals("foo", deadLetter); break; } Thread.sleep(100); } assertTrue(n < 100); bus.unbindConsumer("teststream.tap:stream:durabletest.0", moduleInputChannel); assertNotNull(admin.getQueueProperties("xdbustest.teststream.tap:stream:durabletest.0.dlq")); admin.deleteQueue("xdbustest.teststream.tap:stream:durabletest.0.dlq"); admin.deleteQueue("xdbustest.teststream.tap:stream:durabletest.0"); admin.deleteExchange("xdbustest.topic.tap:stream:durabletest.0"); admin.deleteExchange("xdbustest.DLX"); }