Example usage for org.springframework.amqp.rabbit.core RabbitAdmin deleteExchange

List of usage examples for org.springframework.amqp.rabbit.core RabbitAdmin deleteExchange

Introduction

In this page you can find the example usage for org.springframework.amqp.rabbit.core RabbitAdmin deleteExchange.

Prototype

@Override
    @ManagedOperation(description = "Delete an exchange from the broker")
    public boolean deleteExchange(final String exchangeName) 

Source Link

Usage

From source file:multibinder.RabbitAndKafkaBinderApplicationTests.java

@After
public void cleanUp() {
    RabbitAdmin admin = new RabbitAdmin(rabbitTestSupport.getResource());
    admin.deleteQueue("binder.dataOut.default");
    admin.deleteQueue("binder.dataOut." + this.randomGroup);
    admin.deleteExchange("binder.dataOut");
}

From source file:org.springframework.cloud.stream.binder.rabbit.RabbitBinderCleanerTests.java

@Test
public void testCleanStream() {
    final RabbitBindingCleaner cleaner = new RabbitBindingCleaner();
    final RestTemplate template = RabbitManagementUtils.buildRestTemplate("http://localhost:15672", "guest",
            "guest");
    final String stream1 = UUID.randomUUID().toString();
    String stream2 = stream1 + "-1";
    String firstQueue = null;//from www. j a  va2  s  .  c om
    CachingConnectionFactory connectionFactory = rabbitWithMgmtEnabled.getResource();
    RabbitAdmin rabbitAdmin = new RabbitAdmin(connectionFactory);
    for (int i = 0; i < 5; i++) {
        String queue1Name = AbstractBinder.applyPrefix(BINDER_PREFIX, stream1 + ".default." + i);
        String queue2Name = AbstractBinder.applyPrefix(BINDER_PREFIX, stream2 + ".default." + i);
        if (firstQueue == null) {
            firstQueue = queue1Name;
        }
        URI uri = UriComponentsBuilder.fromUriString("http://localhost:15672/api/queues")
                .pathSegment("{vhost}", "{queue}").buildAndExpand("/", queue1Name).encode().toUri();
        template.put(uri, new AmqpQueue(false, true));
        uri = UriComponentsBuilder.fromUriString("http://localhost:15672/api/queues")
                .pathSegment("{vhost}", "{queue}").buildAndExpand("/", queue2Name).encode().toUri();
        template.put(uri, new AmqpQueue(false, true));
        uri = UriComponentsBuilder.fromUriString("http://localhost:15672/api/queues")
                .pathSegment("{vhost}", "{queue}")
                .buildAndExpand("/", AbstractBinder.constructDLQName(queue1Name)).encode().toUri();
        template.put(uri, new AmqpQueue(false, true));
        TopicExchange exchange = new TopicExchange(queue1Name);
        rabbitAdmin.declareExchange(exchange);
        rabbitAdmin.declareBinding(BindingBuilder.bind(new Queue(queue1Name)).to(exchange).with(queue1Name));
        exchange = new TopicExchange(queue2Name);
        rabbitAdmin.declareExchange(exchange);
        rabbitAdmin.declareBinding(BindingBuilder.bind(new Queue(queue2Name)).to(exchange).with(queue2Name));
    }
    final TopicExchange topic1 = new TopicExchange(
            AbstractBinder.applyPrefix(BINDER_PREFIX, stream1 + ".foo.bar"));
    rabbitAdmin.declareExchange(topic1);
    rabbitAdmin.declareBinding(BindingBuilder.bind(new Queue(firstQueue)).to(topic1).with("#"));
    String foreignQueue = UUID.randomUUID().toString();
    rabbitAdmin.declareQueue(new Queue(foreignQueue));
    rabbitAdmin.declareBinding(BindingBuilder.bind(new Queue(foreignQueue)).to(topic1).with("#"));
    final TopicExchange topic2 = new TopicExchange(
            AbstractBinder.applyPrefix(BINDER_PREFIX, stream2 + ".foo.bar"));
    rabbitAdmin.declareExchange(topic2);
    rabbitAdmin.declareBinding(BindingBuilder.bind(new Queue(firstQueue)).to(topic2).with("#"));
    new RabbitTemplate(connectionFactory).execute(new ChannelCallback<Void>() {

        @Override
        public Void doInRabbit(Channel channel) throws Exception {
            String queueName = AbstractBinder.applyPrefix(BINDER_PREFIX, stream1 + ".default." + 4);
            String consumerTag = channel.basicConsume(queueName, new DefaultConsumer(channel));
            try {
                waitForConsumerStateNot(queueName, 0);
                cleaner.clean(stream1, false);
                fail("Expected exception");
            } catch (RabbitAdminException e) {
                assertThat(e).hasMessageContaining("Queue " + queueName + " is in use");
            }
            channel.basicCancel(consumerTag);
            waitForConsumerStateNot(queueName, 1);
            try {
                cleaner.clean(stream1, false);
                fail("Expected exception");
            } catch (RabbitAdminException e) {
                assertThat(e).hasMessageContaining("Cannot delete exchange ");
                assertThat(e).hasMessageContaining("; it has bindings:");
            }
            return null;
        }

        private void waitForConsumerStateNot(String queueName, int state) throws InterruptedException {
            int n = 0;
            URI uri = UriComponentsBuilder.fromUriString("http://localhost:15672/api/queues")
                    .pathSegment("{vhost}", "{queue}").buildAndExpand("/", queueName).encode().toUri();
            while (n++ < 100) {
                @SuppressWarnings("unchecked")
                Map<String, Object> queueInfo = template.getForObject(uri, Map.class);
                if (!queueInfo.get("consumers").equals(Integer.valueOf(state))) {
                    break;
                }
                Thread.sleep(100);
            }
            assertThat(n < 100).withFailMessage("Consumer state remained at " + state + " after 10 seconds");
        }

    });
    rabbitAdmin.deleteExchange(topic1.getName()); // easier than deleting the binding
    rabbitAdmin.declareExchange(topic1);
    rabbitAdmin.deleteQueue(foreignQueue);
    connectionFactory.destroy();
    Map<String, List<String>> cleanedMap = cleaner.clean(stream1, false);
    assertThat(cleanedMap).hasSize(2);
    List<String> cleanedQueues = cleanedMap.get("queues");
    // should *not* clean stream2
    assertThat(cleanedQueues).hasSize(10);
    for (int i = 0; i < 5; i++) {
        assertThat(cleanedQueues.get(i * 2)).isEqualTo(BINDER_PREFIX + stream1 + ".default." + i);
        assertThat(cleanedQueues.get(i * 2 + 1)).isEqualTo(BINDER_PREFIX + stream1 + ".default." + i + ".dlq");
    }
    List<String> cleanedExchanges = cleanedMap.get("exchanges");
    assertThat(cleanedExchanges).hasSize(6);

    // wild card *should* clean stream2
    cleanedMap = cleaner.clean(stream1 + "*", false);
    assertThat(cleanedMap).hasSize(2);
    cleanedQueues = cleanedMap.get("queues");
    assertThat(cleanedQueues).hasSize(5);
    for (int i = 0; i < 5; i++) {
        assertThat(cleanedQueues.get(i)).isEqualTo(BINDER_PREFIX + stream2 + ".default." + i);
    }
    cleanedExchanges = cleanedMap.get("exchanges");
    assertThat(cleanedExchanges).hasSize(6);
}

From source file:org.springframework.amqp.rabbit.core.RabbitAdminTests.java

private void cleanQueuesAndExchanges(RabbitAdmin rabbitAdmin) {
    rabbitAdmin.deleteQueue("testq.nonDur");
    rabbitAdmin.deleteQueue("testq.ad");
    rabbitAdmin.deleteQueue("testq.excl");
    rabbitAdmin.deleteQueue("testq.all");
    rabbitAdmin.deleteExchange("testex.nonDur");
    rabbitAdmin.deleteExchange("testex.ad");
    rabbitAdmin.deleteExchange("testex.all");
}

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();/*w ww  .ja va 2  s.  co m*/
}

From source file:org.springframework.amqp.rabbit.retry.MissingIdRetryTests.java

@BeforeClass
@AfterClass/* www .  j  a v  a2s. com*/
public static void setupAndCleanUp() {
    RabbitAdmin admin = brokerIsRunning.getAdmin();
    admin.deleteQueue("retry.test.queue");
    admin.deleteExchange("retry.test.exchange");
}

From source file:org.springframework.amqp.rabbit.test.BrokerFederated.java

@SuppressWarnings("deprecation")
@Override//ww w.j  ava  2 s.c  om
public Statement apply(Statement base, FrameworkMethod method, Object target) {

    // Check at the beginning, so this can be used as a static field
    if (assumeOnline) {
        Assume.assumeTrue(brokerOnline.get(port));
    } else {
        Assume.assumeTrue(brokerOffline.get(port));
    }

    CachingConnectionFactory connectionFactory = new CachingConnectionFactory();

    try {

        connectionFactory.setPort(port);
        if (StringUtils.hasText(hostName)) {
            connectionFactory.setHost(hostName);
        }
        RabbitAdmin admin = new RabbitAdmin(connectionFactory);
        org.springframework.amqp.core.FederatedExchange exchange = new org.springframework.amqp.core.FederatedExchange(
                "fedDirectRuleTest");
        exchange.setBackingType("direct");
        exchange.setUpstreamSet("upstream-set");
        admin.declareExchange(exchange);
        admin.deleteExchange("fedDirectRuleTest");

        brokerOffline.put(port, false);

        if (!assumeOnline) {
            Assume.assumeTrue(brokerOffline.get(port));
        }

    } catch (Exception e) {
        logger.warn("Not executing tests because federated connectivity test failed", e);
        brokerOnline.put(port, false);
        if (assumeOnline) {
            Assume.assumeNoException(e);
        }
    } finally {
        connectionFactory.destroy();
    }

    return super.apply(base, method, target);

}

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  ww  w  .j  a  v  a  2s . c  o  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");
}

From source file:org.springframework.xd.dirt.integration.bus.rabbit.RabbitMessageBusTests.java

@Test
public void testNonDurablePubSubWithAutoBindDLQ() throws Exception {
    RabbitAdmin admin = new RabbitAdmin(this.rabbitAvailableRule.getResource());

    MessageBus bus = getMessageBus();/*from  w  w  w.j a  v  a2 s  .c  o  m*/
    Properties properties = new Properties();
    properties.put("prefix", "xdbustest.");
    properties.put("autoBindDLQ", "true");
    properties.put("durableSubscription", "false");
    properties.put("maxAttempts", "1"); // disable retry
    properties.put("requeue", "false");
    DirectChannel moduleInputChannel = new DirectChannel();
    moduleInputChannel.setBeanName("nondurabletest");
    moduleInputChannel.subscribe(new MessageHandler() {

        @Override
        public void handleMessage(Message<?> message) throws MessagingException {
            throw new RuntimeException("foo");
        }

    });
    bus.bindPubSubConsumer("teststream.tap:stream:nondurabletest.0", moduleInputChannel, properties);

    bus.unbindConsumer("teststream.tap:stream:nondurabletest.0", moduleInputChannel);
    assertNull(admin.getQueueProperties("xdbustest.teststream.tap:stream:nondurabletest.0.dlq"));
    admin.deleteQueue("xdbustest.teststream.tap:stream:nondurabletest.0");
    admin.deleteExchange("xdbustest.topic.tap:stream:nondurabletest.0");
}

From source file:org.springframework.xd.dirt.integration.bus.rabbit.RabbitMessageBusTests.java

@Test
public void testAutoBindDLQ() throws Exception {
    RabbitAdmin admin = new RabbitAdmin(this.rabbitAvailableRule.getResource());

    MessageBus bus = getMessageBus();//from   ww w .  j a  v  a  2  s .  co m
    Properties properties = new Properties();
    properties.put("prefix", "xdbustest.");
    properties.put("autoBindDLQ", "true");
    properties.put("maxAttempts", "1"); // disable retry
    properties.put("requeue", "false");
    DirectChannel moduleInputChannel = new DirectChannel();
    moduleInputChannel.setBeanName("dlqTest");
    moduleInputChannel.subscribe(new MessageHandler() {

        @Override
        public void handleMessage(Message<?> message) throws MessagingException {
            throw new RuntimeException("foo");
        }

    });
    bus.bindConsumer("dlqtest", moduleInputChannel, properties);

    RabbitTemplate template = new RabbitTemplate(this.rabbitAvailableRule.getResource());
    template.convertAndSend("", "xdbustest.dlqtest", "foo");

    int n = 0;
    while (n++ < 100) {
        Object deadLetter = template.receiveAndConvert("xdbustest.dlqtest.dlq");
        if (deadLetter != null) {
            assertEquals("foo", deadLetter);
            break;
        }
        Thread.sleep(100);
    }
    assertTrue(n < 100);

    bus.unbindConsumer("dlqtest", moduleInputChannel);
    admin.deleteQueue("xdbustest.dlqtest.dlq");
    admin.deleteQueue("xdbustest.dlqtest");
    admin.deleteExchange("xdbustest.DLX");
}

From source file:org.springframework.xd.dirt.integration.bus.rabbit.RabbitMessageBusTests.java

@Test
public void testAutoBindDLQwithRepublish() throws Exception {
    // pre-declare the queue with dead-lettering, users can also use a policy
    RabbitAdmin admin = new RabbitAdmin(this.rabbitAvailableRule.getResource());
    Map<String, Object> args = new HashMap<String, Object>();
    args.put("x-dead-letter-exchange", "xdbustest.DLX");
    Queue queue = new Queue("xdbustest.dlqpubtest", true, false, false, args);
    admin.declareQueue(queue);//from w ww .  j  a v a2  s  .c o m

    MessageBus bus = getMessageBus();
    Properties properties = new Properties();
    properties.put("prefix", "xdbustest.");
    properties.put("autoBindDLQ", "true");
    properties.put("republishToDLQ", "true");
    properties.put("maxAttempts", "1"); // disable retry
    properties.put("requeue", "false");
    DirectChannel moduleInputChannel = new DirectChannel();
    moduleInputChannel.setBeanName("dlqPubTest");
    moduleInputChannel.subscribe(new MessageHandler() {

        @Override
        public void handleMessage(Message<?> message) throws MessagingException {
            throw new RuntimeException("foo");
        }

    });
    bus.bindConsumer("dlqpubtest", moduleInputChannel, properties);

    RabbitTemplate template = new RabbitTemplate(this.rabbitAvailableRule.getResource());
    template.convertAndSend("", "xdbustest.dlqpubtest", "foo");

    int n = 0;
    while (n++ < 100) {
        org.springframework.amqp.core.Message deadLetter = template.receive("xdbustest.dlqpubtest.dlq");
        if (deadLetter != null) {
            assertEquals("foo", new String(deadLetter.getBody()));
            assertNotNull(deadLetter.getMessageProperties().getHeaders().get("x-exception-stacktrace"));
            break;
        }
        Thread.sleep(100);
    }
    assertTrue(n < 100);

    bus.unbindConsumer("dlqpubtest", moduleInputChannel);
    admin.deleteQueue("xdbustest.dlqpubtest.dlq");
    admin.deleteQueue("xdbustest.dlqpubtest");
    admin.deleteExchange("xdbustest.DLX");
}