Search in sources :

Example 1 with PollingConsumer

use of org.springframework.integration.endpoint.PollingConsumer in project spring-integration by spring-projects.

the class MessagingGatewaySupport method registerReplyMessageCorrelatorIfNecessary.

protected void registerReplyMessageCorrelatorIfNecessary() {
    MessageChannel replyChannel = getReplyChannel();
    if (replyChannel != null && this.replyMessageCorrelator == null) {
        boolean shouldStartCorrelator;
        synchronized (this.replyMessageCorrelatorMonitor) {
            if (this.replyMessageCorrelator != null) {
                return;
            }
            AbstractEndpoint correlator;
            BridgeHandler handler = new BridgeHandler();
            if (getBeanFactory() != null) {
                handler.setBeanFactory(getBeanFactory());
            }
            handler.afterPropertiesSet();
            if (replyChannel instanceof SubscribableChannel) {
                correlator = new EventDrivenConsumer((SubscribableChannel) replyChannel, handler);
            } else if (replyChannel instanceof PollableChannel) {
                PollingConsumer endpoint = new PollingConsumer((PollableChannel) replyChannel, handler);
                endpoint.setBeanFactory(getBeanFactory());
                endpoint.setReceiveTimeout(this.replyTimeout);
                endpoint.afterPropertiesSet();
                correlator = endpoint;
            } else if (replyChannel instanceof ReactiveStreamsSubscribableChannel) {
                ReactiveStreamsConsumer endpoint = new ReactiveStreamsConsumer(replyChannel, (Subscriber<Message<?>>) handler);
                endpoint.afterPropertiesSet();
                correlator = endpoint;
            } else {
                throw new MessagingException("Unsupported 'replyChannel' type [" + replyChannel.getClass() + "]." + "SubscribableChannel or PollableChannel type are supported.");
            }
            this.replyMessageCorrelator = correlator;
            shouldStartCorrelator = true;
        }
        if (shouldStartCorrelator && isRunning()) {
            if (isRunning()) {
                this.replyMessageCorrelator.start();
            }
        }
    }
}
Also used : AbstractEndpoint(org.springframework.integration.endpoint.AbstractEndpoint) EventDrivenConsumer(org.springframework.integration.endpoint.EventDrivenConsumer) PollingConsumer(org.springframework.integration.endpoint.PollingConsumer) ReactiveStreamsConsumer(org.springframework.integration.endpoint.ReactiveStreamsConsumer) MessageChannel(org.springframework.messaging.MessageChannel) ErrorMessage(org.springframework.messaging.support.ErrorMessage) Message(org.springframework.messaging.Message) BridgeHandler(org.springframework.integration.handler.BridgeHandler) MessagingException(org.springframework.messaging.MessagingException) PollableChannel(org.springframework.messaging.PollableChannel) ReactiveStreamsSubscribableChannel(org.springframework.integration.channel.ReactiveStreamsSubscribableChannel) SubscribableChannel(org.springframework.messaging.SubscribableChannel) ReactiveStreamsSubscribableChannel(org.springframework.integration.channel.ReactiveStreamsSubscribableChannel)

Example 2 with PollingConsumer

use of org.springframework.integration.endpoint.PollingConsumer in project spring-integration by spring-projects.

the class IntegrationGraphServer method consumers.

private void consumers(Collection<IntegrationNode> nodes, Collection<LinkNode> links, Map<String, MessageChannelNode> channelNodes) {
    Map<String, IntegrationConsumer> consumers = this.applicationContext.getBeansOfType(IntegrationConsumer.class);
    for (Entry<String, IntegrationConsumer> entry : consumers.entrySet()) {
        IntegrationConsumer consumer = entry.getValue();
        MessageHandlerNode handlerNode = consumer instanceof PollingConsumer ? this.nodeFactory.polledHandlerNode(entry.getKey(), (PollingConsumer) consumer) : this.nodeFactory.handlerNode(entry.getKey(), consumer);
        nodes.add(handlerNode);
        MessageChannelNode channelNode = channelNodes.get(handlerNode.getInput());
        if (channelNode != null) {
            links.add(new LinkNode(channelNode.getNodeId(), handlerNode.getNodeId(), LinkNode.Type.input));
        }
        producerLink(links, channelNodes, handlerNode);
    }
}
Also used : IntegrationConsumer(org.springframework.integration.endpoint.IntegrationConsumer) PollingConsumer(org.springframework.integration.endpoint.PollingConsumer)

Example 3 with PollingConsumer

use of org.springframework.integration.endpoint.PollingConsumer in project spring-integration by spring-projects.

the class AdvisedMessageHandlerTests method testInappropriateAdvice.

@Test
public void testInappropriateAdvice() throws Exception {
    final AtomicBoolean called = new AtomicBoolean(false);
    Advice advice = new AbstractRequestHandlerAdvice() {

        @Override
        protected Object doInvoke(ExecutionCallback callback, Object target, Message<?> message) throws Exception {
            called.set(true);
            return callback.execute();
        }
    };
    PollableChannel inputChannel = new QueueChannel();
    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();
    Callable<?> pollingTask = TestUtils.getPropertyValue(consumer, "poller.pollingTask", Callable.class);
    assertTrue(AopUtils.isAopProxy(pollingTask));
    Log logger = TestUtils.getPropertyValue(advice, "logger", Log.class);
    logger = spy(logger);
    when(logger.isWarnEnabled()).thenReturn(Boolean.TRUE);
    final AtomicReference<String> logMessage = new AtomicReference<String>();
    doAnswer(invocation -> {
        logMessage.set(invocation.getArgument(0));
        return null;
    }).when(logger).warn(Mockito.anyString());
    DirectFieldAccessor accessor = new DirectFieldAccessor(advice);
    accessor.setPropertyValue("logger", logger);
    pollingTask.call();
    assertFalse(called.get());
    assertNotNull(logMessage.get());
    assertThat(logMessage.get(), Matchers.containsString("can only be used for MessageHandlers; " + "an attempt to advise method 'call' in " + "'org.springframework.integration.endpoint.AbstractPollingEndpoint"));
    consumer.stop();
    exec.shutdownNow();
}
Also used : DirtiesContext(org.springframework.test.annotation.DirtiesContext) DefaultRetryState(org.springframework.retry.support.DefaultRetryState) AopUtils(org.springframework.aop.support.AopUtils) Autowired(org.springframework.beans.factory.annotation.Autowired) ErrorMessage(org.springframework.messaging.support.ErrorMessage) PollingConsumer(org.springframework.integration.endpoint.PollingConsumer) CoreMatchers.instanceOf(org.hamcrest.CoreMatchers.instanceOf) Assert.assertThat(org.junit.Assert.assertThat) MethodInvocation(org.aopalliance.intercept.MethodInvocation) SpringJUnit4ClassRunner(org.springframework.test.context.junit4.SpringJUnit4ClassRunner) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) MessageHandlingException(org.springframework.messaging.MessageHandlingException) Mockito.doAnswer(org.mockito.Mockito.doAnswer) Map(java.util.Map) Assert.fail(org.junit.Assert.fail) PollableChannel(org.springframework.messaging.PollableChannel) Method(java.lang.reflect.Method) AdviceMessage(org.springframework.integration.message.AdviceMessage) TaskScheduler(org.springframework.scheduling.TaskScheduler) MessageChannel(org.springframework.messaging.MessageChannel) Executors(java.util.concurrent.Executors) List(java.util.List) Assert.assertFalse(org.junit.Assert.assertFalse) Matchers.containsString(org.hamcrest.Matchers.containsString) Mockito.mock(org.mockito.Mockito.mock) QueueChannel(org.springframework.integration.channel.QueueChannel) MessagingException(org.springframework.messaging.MessagingException) CoreMatchers.equalTo(org.hamcrest.CoreMatchers.equalTo) RunWith(org.junit.runner.RunWith) AbstractReplyProducingMessageHandler(org.springframework.integration.handler.AbstractReplyProducingMessageHandler) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) HashMap(java.util.HashMap) Callable(java.util.concurrent.Callable) DirectFieldAccessor(org.springframework.beans.DirectFieldAccessor) Mockito.spy(org.mockito.Mockito.spy) TestUtils(org.springframework.integration.test.util.TestUtils) AtomicReference(java.util.concurrent.atomic.AtomicReference) ArrayList(java.util.ArrayList) Assert.assertSame(org.junit.Assert.assertSame) MessageFilter(org.springframework.integration.filter.MessageFilter) Advice(org.aopalliance.aop.Advice) Message(org.springframework.messaging.Message) ExecutorService(java.util.concurrent.ExecutorService) ErrorHandlingTaskExecutor(org.springframework.integration.util.ErrorHandlingTaskExecutor) MessageHandlingExpressionEvaluatingAdviceException(org.springframework.integration.handler.advice.ExpressionEvaluatingRequestHandlerAdvice.MessageHandlingExpressionEvaluatingAdviceException) SimpleRetryPolicy(org.springframework.retry.policy.SimpleRetryPolicy) Assert.assertNotNull(org.junit.Assert.assertNotNull) Assert.assertTrue(org.junit.Assert.assertTrue) Matchers(org.hamcrest.Matchers) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) MethodInterceptor(org.aopalliance.intercept.MethodInterceptor) Mockito(org.mockito.Mockito) Assert.assertNull(org.junit.Assert.assertNull) RetryContext(org.springframework.retry.RetryContext) BeanFactory(org.springframework.beans.factory.BeanFactory) ContextConfiguration(org.springframework.test.context.ContextConfiguration) ProxyFactory(org.springframework.aop.framework.ProxyFactory) Log(org.apache.commons.logging.Log) GenericMessage(org.springframework.messaging.support.GenericMessage) RetryTemplate(org.springframework.retry.support.RetryTemplate) Collections(java.util.Collections) Assert.assertEquals(org.junit.Assert.assertEquals) PollingConsumer(org.springframework.integration.endpoint.PollingConsumer) ErrorMessage(org.springframework.messaging.support.ErrorMessage) AdviceMessage(org.springframework.integration.message.AdviceMessage) Message(org.springframework.messaging.Message) GenericMessage(org.springframework.messaging.support.GenericMessage) QueueChannel(org.springframework.integration.channel.QueueChannel) Log(org.apache.commons.logging.Log) AtomicReference(java.util.concurrent.atomic.AtomicReference) Matchers.containsString(org.hamcrest.Matchers.containsString) TaskScheduler(org.springframework.scheduling.TaskScheduler) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) DirectFieldAccessor(org.springframework.beans.DirectFieldAccessor) PollableChannel(org.springframework.messaging.PollableChannel) ExecutorService(java.util.concurrent.ExecutorService) BeanFactory(org.springframework.beans.factory.BeanFactory) Advice(org.aopalliance.aop.Advice) ErrorHandlingTaskExecutor(org.springframework.integration.util.ErrorHandlingTaskExecutor) Test(org.junit.Test)

Example 4 with PollingConsumer

use of org.springframework.integration.endpoint.PollingConsumer in project spring-integration by spring-projects.

the class MethodInvokingMessageHandlerTests method subscription.

@Test
public void subscription() throws Exception {
    TestApplicationContext context = TestUtils.createTestApplicationContext();
    SynchronousQueue<String> queue = new SynchronousQueue<String>();
    TestBean testBean = new TestBean(queue);
    QueueChannel channel = new QueueChannel();
    context.registerChannel("channel", channel);
    Message<String> message = new GenericMessage<String>("testing");
    channel.send(message);
    assertNull(queue.poll());
    MethodInvokingMessageHandler handler = new MethodInvokingMessageHandler(testBean, "foo");
    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.stop();
}
Also used : GenericMessage(org.springframework.messaging.support.GenericMessage) MethodInvokingMessageHandler(org.springframework.integration.handler.MethodInvokingMessageHandler) PollingConsumer(org.springframework.integration.endpoint.PollingConsumer) QueueChannel(org.springframework.integration.channel.QueueChannel) SynchronousQueue(java.util.concurrent.SynchronousQueue) TestApplicationContext(org.springframework.integration.test.util.TestUtils.TestApplicationContext) PeriodicTrigger(org.springframework.scheduling.support.PeriodicTrigger) Test(org.junit.Test)

Example 5 with PollingConsumer

use of org.springframework.integration.endpoint.PollingConsumer in project spring-integration by spring-projects.

the class AbstractMethodAnnotationPostProcessor method doCreateEndpoint.

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;
}
Also used : AbstractEndpoint(org.springframework.integration.endpoint.AbstractEndpoint) PollingConsumer(org.springframework.integration.endpoint.PollingConsumer) EventDrivenConsumer(org.springframework.integration.endpoint.EventDrivenConsumer) ReactiveStreamsConsumer(org.springframework.integration.endpoint.ReactiveStreamsConsumer) PollableChannel(org.springframework.messaging.PollableChannel) Publisher(org.reactivestreams.Publisher) SubscribableChannel(org.springframework.messaging.SubscribableChannel) Poller(org.springframework.integration.annotation.Poller)

Aggregations

PollingConsumer (org.springframework.integration.endpoint.PollingConsumer)25 Test (org.junit.Test)17 QueueChannel (org.springframework.integration.channel.QueueChannel)12 Message (org.springframework.messaging.Message)9 BeanFactory (org.springframework.beans.factory.BeanFactory)7 GenericMessage (org.springframework.messaging.support.GenericMessage)7 AbstractReplyProducingMessageHandler (org.springframework.integration.handler.AbstractReplyProducingMessageHandler)6 MessageChannel (org.springframework.messaging.MessageChannel)6 PollableChannel (org.springframework.messaging.PollableChannel)6 DirectFieldAccessor (org.springframework.beans.DirectFieldAccessor)5 ErrorMessage (org.springframework.messaging.support.ErrorMessage)5 AbstractEndpoint (org.springframework.integration.endpoint.AbstractEndpoint)4 EventDrivenConsumer (org.springframework.integration.endpoint.EventDrivenConsumer)4 TestApplicationContext (org.springframework.integration.test.util.TestUtils.TestApplicationContext)4 SubscribableChannel (org.springframework.messaging.SubscribableChannel)4 ReactiveStreamsConsumer (org.springframework.integration.endpoint.ReactiveStreamsConsumer)3 MessagingException (org.springframework.messaging.MessagingException)3 PeriodicTrigger (org.springframework.scheduling.support.PeriodicTrigger)3 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2