PollingConsumer pollingConsumer = new PollingConsumer((PollableChannel) channel, this.handler); if (this.pollerMetadata == null) { this.pollerMetadata = PollerMetadata.getDefaultPollerMetadata(this.beanFactory); + "', and no default poller is available within the context."); pollingConsumer.setTaskExecutor(this.pollerMetadata.getTaskExecutor()); pollingConsumer.setTrigger(this.pollerMetadata.getTrigger()); pollingConsumer.setAdviceChain(this.pollerMetadata.getAdviceChain()); pollingConsumer.setMaxMessagesPerPoll(this.pollerMetadata.getMaxMessagesPerPoll()); pollingConsumer.setErrorHandler(this.pollerMetadata.getErrorHandler()); pollingConsumer.setReceiveTimeout(this.pollerMetadata.getReceiveTimeout()); pollingConsumer.setTransactionSynchronizationFactory( this.pollerMetadata.getTransactionSynchronizationFactory()); pollingConsumer.setBeanClassLoader(this.beanClassLoader); pollingConsumer.setBeanFactory(this.beanFactory); this.endpoint = pollingConsumer;
@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(); }
@Test public void blockingSourceNotTimedOut() { Mockito.when(this.channelMock.receive(Mockito.eq(1L))).thenReturn(this.message); this.endpoint.setReceiveTimeout(1); this.endpoint.setMaxMessagesPerPoll(1); this.endpoint.start(); this.trigger.await(); this.endpoint.stop(); assertEquals(1, this.consumer.counter.get()); }
@Test public void multipleMessages() { Mockito.when(this.channelMock.receive()) .thenReturn(this.message, this.message, this.message, this.message, this.message); this.endpoint.setMaxMessagesPerPoll(5); this.endpoint.start(); this.trigger.await(); this.endpoint.stop(); assertEquals(5, this.consumer.counter.get()); }
@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 consumer = new PollingConsumer(inputChannel, message -> { }); consumer.setAdviceChain(Collections.singletonList(advice)); ExecutorService exec = Executors.newSingleThreadExecutor(); consumer.setTaskExecutor(new ErrorHandlingTaskExecutor(exec, t -> { })); consumer.setBeanFactory(mock(BeanFactory.class)); consumer.afterPropertiesSet(); consumer.setTaskScheduler(mock(TaskScheduler.class)); consumer.start(); "an attempt to advise method 'call' in " + "'org.springframework.integration.endpoint.AbstractPollingEndpoint")); consumer.stop(); exec.shutdownNow();
PollingConsumer pollingConsumer = new PollingConsumer( (PollableChannel) channel, this.handler); if (this.pollerMetadata == null) { + this.beanName + "', and no default poller is available within the context."); pollingConsumer.setTrigger(this.pollerMetadata.getTrigger()); pollingConsumer.setMaxMessagesPerPoll(this.pollerMetadata.getMaxMessagesPerPoll()); pollingConsumer.setReceiveTimeout(this.pollerMetadata.getReceiveTimeout()); pollingConsumer.setTaskExecutor(this.pollerMetadata.getTaskExecutor()); pollingConsumer.setTransactionManager(this.pollerMetadata.getTransactionManager()); pollingConsumer.setTransactionDefinition(this.pollerMetadata.getTransactionDefinition()); pollingConsumer.setAdviceChain(this.pollerMetadata.getAdviceChain()); this.endpoint = pollingConsumer;
PollingConsumer endpoint = new PollingConsumer((PollableChannel) replyChan, handler); endpoint.setBeanFactory(getBeanFactory()); endpoint.setReceiveTimeout(this.replyTimeout); endpoint.afterPropertiesSet(); correlator = endpoint;
@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)); }
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; } }
@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(); }
this.gatherEndpoint = new PollingConsumer((PollableChannel) this.gatherChannel, this.gatherer); ((PollingConsumer) this.gatherEndpoint).setReceiveTimeout(this.gatherTimeout);
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; }
@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(); }
@Test public void blockingSourceTimedOut() { // we don't need to await the timeout, returning null suffices Mockito.when(this.channelMock.receive()).thenReturn(null); this.endpoint.setReceiveTimeout(1); this.endpoint.start(); this.trigger.await(); this.endpoint.stop(); assertEquals(0, this.consumer.counter.get()); }
@Bean public PollingConsumer polling() { PollingConsumer pollingConsumer = new PollingConsumer(four(), new BareHandler()); pollingConsumer.setAutoStartup(false); return pollingConsumer; }
pollingEndpoint.setErrorHandler(pollerMetadata.getErrorHandler()); if (pollingEndpoint instanceof PollingConsumer) { ((PollingConsumer) pollingEndpoint).setReceiveTimeout(pollerMetadata.getReceiveTimeout());
@Test public void singleNonStringObject() { endpoint.setTrigger(trigger); endpoint.setMaxMessagesPerPoll(1); TestObject testObject = new TestObject("foo"); channel.send(new GenericMessage<TestObject>(testObject)); endpoint.start(); trigger.await(); endpoint.stop(); assertEquals("foo", writer.toString()); }
handler.afterPropertiesSet(); final PollingConsumer consumer = new PollingConsumer(requestChannel, handler); final TestErrorHandler errorHandler = new TestErrorHandler(); consumer.setTrigger(new PeriodicTrigger(0)); consumer.setErrorHandler(errorHandler); taskScheduler.afterPropertiesSet(); consumer.setTaskScheduler(taskScheduler); consumer.setBeanFactory(mock(BeanFactory.class)); consumer.afterPropertiesSet(); consumer.start(); consumer.stop(); taskScheduler.destroy();
PollingConsumer endpoint = new PollingConsumer((PollableChannel) replyChan, handler); endpoint.setBeanFactory(getBeanFactory()); endpoint.setReceiveTimeout(this.replyTimeout); endpoint.afterPropertiesSet(); correlator = endpoint;