Example usage for org.springframework.amqp.core Queue Queue

List of usage examples for org.springframework.amqp.core Queue Queue

Introduction

In this page you can find the example usage for org.springframework.amqp.core Queue Queue.

Prototype

public Queue(String name) 

Source Link

Document

The queue is durable, non-exclusive and non auto-delete.

Usage

From source file:org.cloudfoundry.workers.stocks.integration.service.config.ServiceConfiguration.java

@Bean
public Queue customerQueue() throws Throwable {
    Queue q = new Queue(this.stocks);
    amqpAdmin().declareQueue(q);/*from   ww w  .  ja  v  a 2  s. c om*/
    return q;
}

From source file:com.noriental.solr.config.test.BrokerRunning.java

private BrokerRunning(boolean assumeOnline, boolean purge, boolean management) {
    this(assumeOnline, purge, management, new Queue(DEFAULT_QUEUE_NAME));
}

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  w ww  .j  av  a2s . com
    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:io.acme.solution.query.conf.EventBusConfigurer.java

@PostConstruct
private void setup() {

    Queue currentQueue = null;// w  ww. ja  v a2 s . co  m
    SimpleMessageListenerContainer currentContainer = null;
    MessageListenerAdapter currentAdapter = null;

    final RabbitAdmin rabbitAdmin = this.context.getBean("eventBusRabbitAdmin", RabbitAdmin.class);
    final ConnectionFactory connectionFactory = this.context.getBean("eventBusConnectionFactory",
            ConnectionFactory.class);
    final TopicExchange exchange = this.context.getBean("eventExchange", TopicExchange.class);
    final MessageConverter converter = this.context.getBean("eventBusMessageConverter", MessageConverter.class);
    final Map<String, EventHandler> eventHandlersRegistry = EventHandlerUtils
            .buildEventHandlersRegistry(this.handlerBasePackage, this.context);

    for (String eventType : eventHandlersRegistry.keySet()) {

        rabbitAdmin.declareQueue(currentQueue = new Queue(this.queuePrefix + eventType));
        rabbitAdmin.declareBinding(BindingBuilder.bind(currentQueue).to(exchange).with(eventType));

        currentAdapter = new MessageListenerAdapter(eventHandlersRegistry.get(eventType), converter);

        currentContainer = new SimpleMessageListenerContainer(connectionFactory);
        currentContainer.setMessageListener(currentAdapter);
        currentContainer.setQueues(currentQueue);
        currentContainer.start();
    }
}

From source file:com.sample.amqp.RabbitConfiguration.java

/**
 * Let's assume that the queue name is the same as the routing_key  
 */
@Bean
public Queue requestQueue() {
    return new Queue(requestQueueName);
}

From source file:com.sample.amqp.RabbitConfiguration.java

@Bean
public Queue responseQueue() {
    return new Queue(responseQueueName);
}

From source file:com.jbrisbin.vpc.jobsched.QueueConfiguration.java

@Bean
public Queue sqlQueue() {
    Queue q = new Queue(sqlQueueName);
    q.setDurable(true);
    q.setAutoDelete(false);
    return q;
}

From source file:org.cloudfoundry.workers.stocks.integration.client.ClientConfiguration.java

@Bean
public Queue customerQueue() throws Throwable {
    Queue q = new Queue(this.tickers);
    amqpAdmin().declareQueue(q);//  w ww  . j a v  a 2s  .  c o m
    return q;
}

From source file:com.jbrisbin.vpc.jobsched.QueueConfiguration.java

@Bean
public Queue requeueQueue() {
    Queue q = new Queue(requeueQueueName);
    q.setDurable(true);
    q.setAutoDelete(false);
    return q;
}

From source file:io.acme.solution.application.conf.CommandBusConfigurer.java

@PostConstruct
private void setup() {

    Queue currentQueue = null;/*from   www  .  ja  v a 2s  . c  om*/
    String currentCommandType = null;
    SimpleMessageListenerContainer currentContainer = null;
    MessageListenerAdapter currentAdapter = null;

    final RabbitAdmin rabbitAdmin = this.context.getBean("commandBusRabbitAdmin", RabbitAdmin.class);
    final ConnectionFactory connectionFactory = this.context.getBean("commandBusConnectionFactory",
            ConnectionFactory.class);
    final TopicExchange exchange = this.context.getBean("commandExchange", TopicExchange.class);
    final MessageConverter converter = this.context.getBean("commandBusMessageConverter",
            MessageConverter.class);
    final Map<String, CommandHandler> commandHandlersRegistry = CommandHandlerUtils
            .buildCommandHandlersRegistry(this.handlerBasePackage, this.context);
    final ClassPathScanningCandidateComponentProvider scanner = new ClassPathScanningCandidateComponentProvider(
            false);
    scanner.addIncludeFilter(new AssignableTypeFilter(Command.class));

    for (BeanDefinition bean : scanner.findCandidateComponents(this.commandBasePackage)) {
        currentCommandType = bean.getBeanClassName().substring(bean.getBeanClassName().lastIndexOf('.') + 1);
        rabbitAdmin.declareQueue(currentQueue = new Queue(this.queuePrefix + currentCommandType));
        rabbitAdmin.declareBinding(BindingBuilder.bind(currentQueue).to(exchange).with(currentCommandType));

        if (commandHandlersRegistry.containsKey(bean.getBeanClassName())) {
            currentAdapter = new MessageListenerAdapter(commandHandlersRegistry.get(bean.getBeanClassName()),
                    converter);

            currentContainer = new SimpleMessageListenerContainer(connectionFactory);
            currentContainer.setMessageListener(currentAdapter);
            currentContainer.setQueues(currentQueue);
            currentContainer.start();
        }
    }

}