List of usage examples for org.springframework.amqp.rabbit.listener SimpleMessageListenerContainer setAdviceChain
public void setAdviceChain(Advice... adviceChain)
From source file:jp.co.ctc_g.rack.resource_manager.config.RackResourceManagerConsumerContextConfig.java
private SimpleMessageListenerContainer makeContainer() { SimpleMessageListenerContainer container = new SimpleMessageListenerContainer(); container.setConnectionFactory(factory()); container.setErrorHandler(errorHandler()); container.setAdviceChain(advice()); return container; }
From source file:org.springframework.amqp.rabbit.retry.MissingIdRetryTests.java
@SuppressWarnings("rawtypes") @Test/*from w ww . jav a2 s. c o m*/ public void testWithNoId() throws Exception { // 2 messages; each retried once by missing id interceptor this.latch = new CountDownLatch(4); ConfigurableApplicationContext ctx = new ClassPathXmlApplicationContext("retry-context.xml", this.getClass()); RabbitTemplate template = ctx.getBean(RabbitTemplate.class); ConnectionFactory connectionFactory = ctx.getBean(ConnectionFactory.class); SimpleMessageListenerContainer container = new SimpleMessageListenerContainer(connectionFactory); container.setStatefulRetryFatalWithNullMessageId(false); container.setMessageListener(new MessageListenerAdapter(new POJO())); container.setQueueNames("retry.test.queue"); StatefulRetryOperationsInterceptorFactoryBean fb = new StatefulRetryOperationsInterceptorFactoryBean(); // use an external template so we can share his cache RetryTemplate retryTemplate = new RetryTemplate(); RetryContextCache cache = spy(new MapRetryContextCache()); retryTemplate.setRetryContextCache(cache); fb.setRetryOperations(retryTemplate); Advice retryInterceptor = fb.getObject(); container.setAdviceChain(retryInterceptor); container.start(); template.convertAndSend("retry.test.exchange", "retry.test.binding", "Hello, world!"); template.convertAndSend("retry.test.exchange", "retry.test.binding", "Hello, world!"); try { assertTrue(latch.await(30, TimeUnit.SECONDS)); Map map = (Map) new DirectFieldAccessor(cache).getPropertyValue("map"); int n = 0; while (n++ < 100 && map.size() != 0) { Thread.sleep(100); } verify(cache, never()).put(any(), any(RetryContext.class)); verify(cache, never()).remove(any()); assertEquals("Expected map.size() = 0, was: " + map.size(), 0, map.size()); } finally { container.stop(); ctx.close(); } }
From source file:org.springframework.amqp.rabbit.retry.MissingIdRetryTests.java
@SuppressWarnings("rawtypes") @Test// ww w .j av a2 s. com public void testWithId() throws Exception { // 2 messages; each retried twice by retry interceptor this.latch = new CountDownLatch(6); ConfigurableApplicationContext ctx = new ClassPathXmlApplicationContext("retry-context.xml", this.getClass()); RabbitTemplate template = ctx.getBean(RabbitTemplate.class); ConnectionFactory connectionFactory = ctx.getBean(ConnectionFactory.class); SimpleMessageListenerContainer container = new SimpleMessageListenerContainer(connectionFactory); container.setMessageListener(new MessageListenerAdapter(new POJO())); container.setQueueNames("retry.test.queue"); StatefulRetryOperationsInterceptorFactoryBean fb = new StatefulRetryOperationsInterceptorFactoryBean(); // use an external template so we can share his cache RetryTemplate retryTemplate = new RetryTemplate(); RetryContextCache cache = spy(new MapRetryContextCache()); retryTemplate.setRetryContextCache(cache); fb.setRetryOperations(retryTemplate); fb.setMessageRecoverer(new RejectAndDontRequeueRecoverer()); Advice retryInterceptor = fb.getObject(); container.setAdviceChain(retryInterceptor); container.start(); MessageProperties messageProperties = new MessageProperties(); messageProperties.setContentType("text/plain"); messageProperties.setMessageId("foo"); Message message = new Message("Hello, world!".getBytes(), messageProperties); template.send("retry.test.exchange", "retry.test.binding", message); template.send("retry.test.exchange", "retry.test.binding", message); try { assertTrue(latch.await(30, TimeUnit.SECONDS)); Map map = (Map) new DirectFieldAccessor(cache).getPropertyValue("map"); int n = 0; while (n++ < 100 && map.size() != 0) { Thread.sleep(100); } ArgumentCaptor putCaptor = ArgumentCaptor.forClass(Object.class); ArgumentCaptor getCaptor = ArgumentCaptor.forClass(Object.class); ArgumentCaptor removeCaptor = ArgumentCaptor.forClass(Object.class); verify(cache, times(6)).put(putCaptor.capture(), any(RetryContext.class)); verify(cache, times(6)).get(getCaptor.capture()); verify(cache, atLeast(2)).remove(removeCaptor.capture()); verify(cache, atMost(4)).remove(removeCaptor.capture()); logger.debug("puts:" + putCaptor.getAllValues()); logger.debug("gets:" + putCaptor.getAllValues()); logger.debug("removes:" + removeCaptor.getAllValues()); assertEquals("Expected map.size() = 0, was: " + map.size(), 0, map.size()); } finally { container.stop(); ctx.close(); } }
From source file:org.springframework.amqp.rabbit.retry.MissingIdRetryTests.java
@SuppressWarnings("rawtypes") @Test//ww w . j av a2 s. c om public void testWithIdAndSuccess() throws Exception { // 2 messages; each retried twice by retry interceptor this.latch = new CountDownLatch(6); ConfigurableApplicationContext ctx = new ClassPathXmlApplicationContext("retry-context.xml", this.getClass()); RabbitTemplate template = ctx.getBean(RabbitTemplate.class); ConnectionFactory connectionFactory = ctx.getBean(ConnectionFactory.class); SimpleMessageListenerContainer container = new SimpleMessageListenerContainer(connectionFactory); final Set<String> processed = new HashSet<>(); final CountDownLatch latch = new CountDownLatch(4); container.setMessageListener(m -> { latch.countDown(); if (!processed.contains(m.getMessageProperties().getMessageId())) { processed.add(m.getMessageProperties().getMessageId()); throw new RuntimeException("fail"); } }); container.setQueueNames("retry.test.queue"); StatefulRetryOperationsInterceptorFactoryBean fb = new StatefulRetryOperationsInterceptorFactoryBean(); // use an external template so we can share his cache RetryTemplate retryTemplate = new RetryTemplate(); RetryContextCache cache = spy(new MapRetryContextCache()); retryTemplate.setRetryContextCache(cache); fb.setRetryOperations(retryTemplate); fb.setMessageRecoverer(new RejectAndDontRequeueRecoverer()); Advice retryInterceptor = fb.getObject(); container.setAdviceChain(retryInterceptor); container.start(); MessageProperties messageProperties = new MessageProperties(); messageProperties.setContentType("text/plain"); messageProperties.setMessageId("foo"); Message message = new Message("Hello, world!".getBytes(), messageProperties); template.send("retry.test.exchange", "retry.test.binding", message); messageProperties.setMessageId("bar"); template.send("retry.test.exchange", "retry.test.binding", message); try { assertTrue(latch.await(30, TimeUnit.SECONDS)); Map map = (Map) new DirectFieldAccessor(cache).getPropertyValue("map"); int n = 0; while (n++ < 100 && map.size() != 0) { Thread.sleep(100); } ArgumentCaptor putCaptor = ArgumentCaptor.forClass(Object.class); ArgumentCaptor getCaptor = ArgumentCaptor.forClass(Object.class); ArgumentCaptor removeCaptor = ArgumentCaptor.forClass(Object.class); verify(cache, times(2)).put(putCaptor.capture(), any(RetryContext.class)); verify(cache, times(2)).get(getCaptor.capture()); verify(cache, atLeast(2)).remove(removeCaptor.capture()); verify(cache, atMost(4)).remove(removeCaptor.capture()); logger.debug("puts:" + putCaptor.getAllValues()); logger.debug("gets:" + putCaptor.getAllValues()); logger.debug("removes:" + removeCaptor.getAllValues()); assertEquals("Expected map.size() = 0, was: " + map.size(), 0, map.size()); } finally { container.stop(); ctx.close(); } }
From source file:org.springframework.integration.x.rabbit.RabbitMessageBus.java
private void doRegisterConsumer(String name, MessageChannel moduleInputChannel, Queue queue) { SimpleMessageListenerContainer listenerContainer = new SimpleMessageListenerContainer( this.connectionFactory); if (this.concurrentConsumers != null) { listenerContainer.setConcurrentConsumers(this.concurrentConsumers); }/*from www .j a va 2 s.co m*/ listenerContainer.setQueues(queue); Advice advice = new StatelessRetryOperationsInterceptorFactoryBean().getObject(); listenerContainer.setAdviceChain(new Advice[] { advice }); listenerContainer.afterPropertiesSet(); AmqpInboundChannelAdapter adapter = new AmqpInboundChannelAdapter(listenerContainer); DirectChannel bridgeToModuleChannel = new DirectChannel(); bridgeToModuleChannel.setBeanName(name + ".bridge"); adapter.setOutputChannel(bridgeToModuleChannel); adapter.setHeaderMapper(this.mapper); adapter.setBeanName("inbound." + name); adapter.afterPropertiesSet(); addBinding(Binding.forConsumer(adapter, moduleInputChannel)); ReceivingHandler convertingBridge = new ReceivingHandler(); convertingBridge.setOutputChannel(moduleInputChannel); convertingBridge.setBeanName(name + ".convert.bridge"); convertingBridge.afterPropertiesSet(); bridgeToModuleChannel.subscribe(convertingBridge); adapter.start(); }
From source file:org.springframework.xd.dirt.integration.rabbit.RabbitMessageBus.java
private void doRegisterConsumer(String name, MessageChannel moduleInputChannel, Queue queue, RabbitPropertiesAccessor properties, boolean isPubSub) { // Fix for XD-2503 // Temporarily overrides the thread context classloader with the one where the SimpleMessageListenerContainer is defined // This allows for the proxying that happens while initializing the SimpleMessageListenerContainer to work correctly ClassLoader originalClassloader = Thread.currentThread().getContextClassLoader(); try {/*from w w w. j a v a 2 s . co m*/ ClassUtils.overrideThreadContextClassLoader(SimpleMessageListenerContainer.class.getClassLoader()); SimpleMessageListenerContainer listenerContainer = new SimpleMessageListenerContainer( this.connectionFactory); listenerContainer.setAcknowledgeMode(properties.getAcknowledgeMode(this.defaultAcknowledgeMode)); listenerContainer.setChannelTransacted(properties.getTransacted(this.defaultChannelTransacted)); listenerContainer .setDefaultRequeueRejected(properties.getRequeueRejected(this.defaultDefaultRequeueRejected)); if (!isPubSub) { int concurrency = properties.getConcurrency(this.defaultConcurrency); concurrency = concurrency > 0 ? concurrency : 1; listenerContainer.setConcurrentConsumers(concurrency); int maxConcurrency = properties.getMaxConcurrency(this.defaultMaxConcurrency); if (maxConcurrency > concurrency) { listenerContainer.setMaxConcurrentConsumers(maxConcurrency); } } listenerContainer.setPrefetchCount(properties.getPrefetchCount(this.defaultPrefetchCount)); listenerContainer.setTxSize(properties.getTxSize(this.defaultTxSize)); listenerContainer.setTaskExecutor(new SimpleAsyncTaskExecutor(queue.getName() + "-")); listenerContainer.setQueues(queue); int maxAttempts = properties.getMaxAttempts(this.defaultMaxAttempts); if (maxAttempts > 1) { RetryOperationsInterceptor retryInterceptor = RetryInterceptorBuilder.stateless() .maxAttempts(maxAttempts) .backOffOptions(properties.getBackOffInitialInterval(this.defaultBackOffInitialInterval), properties.getBackOffMultiplier(this.defaultBackOffMultiplier), properties.getBackOffMaxInterval(this.defaultBackOffMaxInterval)) .recoverer(new RejectAndDontRequeueRecoverer()).build(); listenerContainer.setAdviceChain(new Advice[] { retryInterceptor }); } listenerContainer.setAfterReceivePostProcessors(this.decompressingPostProcessor); listenerContainer.setMessagePropertiesConverter(RabbitMessageBus.inboundMessagePropertiesConverter); listenerContainer.afterPropertiesSet(); AmqpInboundChannelAdapter adapter = new AmqpInboundChannelAdapter(listenerContainer); adapter.setBeanFactory(this.getBeanFactory()); DirectChannel bridgeToModuleChannel = new DirectChannel(); bridgeToModuleChannel.setBeanFactory(this.getBeanFactory()); bridgeToModuleChannel.setBeanName(name + ".bridge"); adapter.setOutputChannel(bridgeToModuleChannel); adapter.setBeanName("inbound." + name); DefaultAmqpHeaderMapper mapper = new DefaultAmqpHeaderMapper(); mapper.setRequestHeaderNames(properties.getRequestHeaderPattens(this.defaultRequestHeaderPatterns)); mapper.setReplyHeaderNames(properties.getReplyHeaderPattens(this.defaultReplyHeaderPatterns)); adapter.setHeaderMapper(mapper); adapter.afterPropertiesSet(); Binding consumerBinding = Binding.forConsumer(name, adapter, moduleInputChannel, properties); addBinding(consumerBinding); ReceivingHandler convertingBridge = new ReceivingHandler(); convertingBridge.setOutputChannel(moduleInputChannel); convertingBridge.setBeanName(name + ".convert.bridge"); convertingBridge.afterPropertiesSet(); bridgeToModuleChannel.subscribe(convertingBridge); consumerBinding.start(); } finally { Thread.currentThread().setContextClassLoader(originalClassloader); } }