protected AbstractEndpoint doCreateEndpoint(MessageHandler handler, MessageChannel inputChannel, List<Annotation> annotations) { AbstractEndpoint endpoint; if (inputChannel instanceof PollableChannel) { PollingConsumer pollingConsumer = new PollingConsumer((PollableChannel) inputChannel, handler); configurePollingEndpoint(pollingConsumer, annotations); endpoint = pollingConsumer; } else { Poller[] pollers = MessagingAnnotationUtils.resolveAttribute(annotations, "poller", Poller[].class); Assert.state(ObjectUtils.isEmpty(pollers), "A '@Poller' should not be specified for Annotation-based " + "endpoint, since '" + inputChannel + "' is a SubscribableChannel (not pollable)."); if (inputChannel instanceof Publisher) { endpoint = new ReactiveStreamsConsumer(inputChannel, handler); } else { endpoint = new EventDrivenConsumer((SubscribableChannel) inputChannel, handler); } } return endpoint; }
@Bean public PollingConsumer polling() { PollingConsumer pollingConsumer = new PollingConsumer(four(), new BareHandler()); pollingConsumer.setAutoStartup(false); return pollingConsumer; }
@Test public void testPollingConsumerWithExecutorInterceptor() throws InterruptedException { TestUtils.TestApplicationContext testApplicationContext = TestUtils.createTestApplicationContext(); QueueChannel channel = new QueueChannel(); final CountDownLatch latch1 = new CountDownLatch(1); final CountDownLatch latch2 = new CountDownLatch(2); final List<Message<?>> messages = new ArrayList<>(); PollingConsumer consumer = new PollingConsumer(channel, message -> { messages.add(message); latch1.countDown(); latch2.countDown(); }); testApplicationContext.registerBean("consumer", consumer); testApplicationContext.refresh(); channel.send(new GenericMessage<>("foo")); assertTrue(latch1.await(10, TimeUnit.SECONDS)); channel.addInterceptor(new TestExecutorInterceptor()); channel.send(new GenericMessage<>("foo")); assertTrue(latch2.await(10, TimeUnit.SECONDS)); assertEquals(2, messages.size()); assertEquals("foo", messages.get(0).getPayload()); assertEquals("FOO", messages.get(1).getPayload()); testApplicationContext.close(); }
PollingConsumer consumer = new PollingConsumer(inputChannel, message -> { }); consumer.setAdviceChain(Collections.singletonList(advice)); ExecutorService exec = Executors.newSingleThreadExecutor();
@Before public void init() { this.channelMock = mock(PollableChannel.class); this.endpoint = new PollingConsumer(this.channelMock, this.consumer); this.taskScheduler.setPoolSize(5); this.endpoint.setErrorHandler(this.errorHandler); this.endpoint.setTaskScheduler(this.taskScheduler); this.endpoint.setTrigger(this.trigger); this.endpoint.setBeanFactory(mock(BeanFactory.class)); this.endpoint.setReceiveTimeout(-1); this.endpoint.afterPropertiesSet(); this.taskScheduler.afterPropertiesSet(); }
this.gatherEndpoint = new PollingConsumer((PollableChannel) this.gatherChannel, this.gatherer); ((PollingConsumer) this.gatherEndpoint).setReceiveTimeout(this.gatherTimeout);
@Test public void consumerSubscribedToErrorChannel() throws InterruptedException { TestApplicationContext context = TestUtils.createTestApplicationContext(); QueueChannel errorChannel = new QueueChannel(); context.registerChannel(IntegrationContextUtils.ERROR_CHANNEL_BEAN_NAME, errorChannel); final CountDownLatch latch = new CountDownLatch(1); AbstractReplyProducingMessageHandler handler = new AbstractReplyProducingMessageHandler() { @Override public Object handleRequestMessage(Message<?> message) { latch.countDown(); return null; } }; PollingConsumer endpoint = new PollingConsumer(errorChannel, handler); endpoint.setBeanFactory(mock(BeanFactory.class)); context.registerEndpoint("testEndpoint", endpoint); context.refresh(); errorChannel.send(new ErrorMessage(new RuntimeException("test-exception"))); latch.await(1000, TimeUnit.MILLISECONDS); assertEquals("handler should have received error message", 0, latch.getCount()); context.close(); }
@Before public void initialize() { stream = new ByteArrayOutputStream(); handler = new ByteStreamWritingMessageHandler(stream); this.channel = new QueueChannel(10); this.endpoint = new PollingConsumer(channel, handler); scheduler = new ThreadPoolTaskScheduler(); this.endpoint.setTaskScheduler(scheduler); scheduler.afterPropertiesSet(); trigger.reset(); endpoint.setTrigger(trigger); endpoint.setBeanFactory(mock(BeanFactory.class)); }
@Before public void initialize() { writer = new StringWriter(); handler = new CharacterStreamWritingMessageHandler(writer); this.channel = new QueueChannel(10); trigger.reset(); this.endpoint = new PollingConsumer(channel, handler); scheduler = new ThreadPoolTaskScheduler(); this.endpoint.setTaskScheduler(scheduler); scheduler.afterPropertiesSet(); trigger.reset(); endpoint.setTrigger(trigger); endpoint.setBeanFactory(mock(BeanFactory.class)); }
@Test public void ensurePollerTaskStops() throws Exception { final CountDownLatch latch = new CountDownLatch(1); QueueChannel channel = new QueueChannel(); channel.send(new GenericMessage<>("foo")); //Has to be an explicit implementation - Mockito cannot mock/spy lambdas MessageHandler handler = Mockito.spy(new MessageHandler() { @Override public void handleMessage(Message<?> message) throws MessagingException { latch.countDown(); } }); PollingConsumer consumer = new PollingConsumer(channel, handler); consumer.setTrigger(new PeriodicTrigger(0)); consumer.setErrorHandler(errorHandler); consumer.setTaskScheduler(taskScheduler); consumer.setBeanFactory(mock(BeanFactory.class)); consumer.afterPropertiesSet(); consumer.start(); assertTrue(latch.await(2, TimeUnit.SECONDS)); Mockito.verify(handler, times(1)).handleMessage(Mockito.any(Message.class)); consumer.stop(); Mockito.reset(handler); for (int i = 0; i < 10; i++) { channel.send(new GenericMessage<>("foo")); } Mockito.verify(handler, atMost(1)).handleMessage(Mockito.any(Message.class)); }
PollingConsumer pollingConsumer = new PollingConsumer((PollableChannel) channel, this.handler); if (this.pollerMetadata == null) { this.pollerMetadata = PollerMetadata.getDefaultPollerMetadata(this.beanFactory);
PollingConsumer endpoint = new PollingConsumer((PollableChannel) replyChan, handler); endpoint.setBeanFactory(getBeanFactory()); endpoint.setReceiveTimeout(this.replyTimeout);
/** * {@inheritDoc} */ public void afterPropertiesSet() { Assert.notNull(this.messageChannel, "MessageChannel must not be null"); if (this.messageChannel instanceof PollableChannel) { this.consumerEndpoint = new PollingConsumer((PollableChannel) this.messageChannel, this); } else if (this.messageChannel instanceof SubscribableChannel) { this.consumerEndpoint = new EventDrivenConsumer((SubscribableChannel) this.messageChannel, this); } }
handler1.setOutputChannel(outputChannel1); handler2.setOutputChannel(outputChannel2); PollingConsumer endpoint1 = new PollingConsumer(inputChannel, handler1); endpoint1.setBeanFactory(mock(BeanFactory.class)); PollingConsumer endpoint2 = new PollingConsumer(inputChannel, handler2); endpoint2.setBeanFactory(mock(BeanFactory.class)); context.registerEndpoint("testEndpoint1", endpoint1);
@Test public void endpointRegistrationWithInputChannelReference() { TestApplicationContext context = TestUtils.createTestApplicationContext(); QueueChannel sourceChannel = new QueueChannel(); QueueChannel targetChannel = new QueueChannel(); context.registerChannel("sourceChannel", sourceChannel); context.registerChannel("targetChannel", targetChannel); Message<String> message = MessageBuilder.withPayload("test") .setReplyChannelName("targetChannel").build(); sourceChannel.send(message); AbstractReplyProducingMessageHandler handler = new AbstractReplyProducingMessageHandler() { @Override public Object handleRequestMessage(Message<?> message) { return message; } }; handler.setBeanFactory(context); handler.afterPropertiesSet(); PollingConsumer endpoint = new PollingConsumer(sourceChannel, handler); endpoint.setBeanFactory(mock(BeanFactory.class)); context.registerEndpoint("testEndpoint", endpoint); context.refresh(); Message<?> result = targetChannel.receive(10000); assertEquals("test", result.getPayload()); context.close(); }
@Test public void subscription() throws Exception { TestApplicationContext context = TestUtils.createTestApplicationContext(); SynchronousQueue<String> queue = new SynchronousQueue<>(); TestBean testBean = new TestBean(queue); QueueChannel channel = new QueueChannel(); context.registerChannel("channel", channel); Message<String> message = new GenericMessage<>("testing"); channel.send(message); assertNull(queue.poll()); MethodInvokingMessageHandler handler = new MethodInvokingMessageHandler(testBean, "foo"); handler.setBeanFactory(context); PollingConsumer endpoint = new PollingConsumer(channel, handler); endpoint.setTrigger(new PeriodicTrigger(10)); context.registerEndpoint("testEndpoint", endpoint); context.refresh(); String result = queue.poll(2000, TimeUnit.MILLISECONDS); assertNotNull(result); assertEquals("testing", result); context.close(); }
handler.afterPropertiesSet(); final PollingConsumer consumer = new PollingConsumer(requestChannel, handler); final TestErrorHandler errorHandler = new TestErrorHandler();
protected AbstractEndpoint doCreateEndpoint(MessageHandler handler, MessageChannel inputChannel, List<Annotation> annotations) { AbstractEndpoint endpoint; if (inputChannel instanceof PollableChannel) { PollingConsumer pollingConsumer = new PollingConsumer((PollableChannel) inputChannel, handler); configurePollingEndpoint(pollingConsumer, annotations); endpoint = pollingConsumer; } else { Poller[] pollers = MessagingAnnotationUtils.resolveAttribute(annotations, "poller", Poller[].class); Assert.state(ObjectUtils.isEmpty(pollers), "A '@Poller' should not be specified for Annotation-based " + "endpoint, since '" + inputChannel + "' is a SubscribableChannel (not pollable)."); if (inputChannel instanceof Publisher) { endpoint = new ReactiveStreamsConsumer(inputChannel, handler); } else { endpoint = new EventDrivenConsumer((SubscribableChannel) inputChannel, handler); } } return endpoint; }
this.gatherEndpoint = new PollingConsumer((PollableChannel) this.gatherChannel, this.gatherer); ((PollingConsumer) this.gatherEndpoint).setReceiveTimeout(this.gatherTimeout);
private void registerReplyMessageCorrelator() { synchronized (this.replyMessageCorrelatorMonitor) { if (this.replyMessageCorrelator != null) { return; } AbstractEndpoint correlator = null; MessageHandler handler = new AbstractReplyProducingMessageHandler() { @Override protected void handleRequestMessage(Message<?> message, ReplyMessageHolder replyHolder) { replyHolder.set(message); } }; if (this.replyChannel instanceof SubscribableChannel) { correlator = new EventDrivenConsumer( (SubscribableChannel) this.replyChannel, handler); } else if (this.replyChannel instanceof PollableChannel) { PollingConsumer endpoint = new PollingConsumer( (PollableChannel) this.replyChannel, handler); endpoint.setTrigger(new IntervalTrigger(10)); endpoint.setBeanFactory(this.getBeanFactory()); endpoint.afterPropertiesSet(); correlator = endpoint; } if (this.isRunning() && correlator instanceof Lifecycle) { ((Lifecycle) correlator).start(); } this.replyMessageCorrelator = correlator; } }