Example usage for org.springframework.integration.endpoint PollingConsumer setTaskExecutor

List of usage examples for org.springframework.integration.endpoint PollingConsumer setTaskExecutor

Introduction

In this page you can find the example usage for org.springframework.integration.endpoint PollingConsumer setTaskExecutor.

Prototype

public void setTaskExecutor(Executor taskExecutor) 

Source Link

Usage

From source file:org.springframework.integration.config.ConsumerEndpointFactoryBean.java

private void initializeEndpoint() throws Exception {
    synchronized (this.initializationMonitor) {
        if (this.initialized) {
            return;
        }//ww w.ja  v  a  2  s.com
        MessageChannel channel = null;
        if (StringUtils.hasText(this.inputChannelName)) {
            Assert.isTrue(this.beanFactory.containsBean(this.inputChannelName), "no such input channel '"
                    + this.inputChannelName + "' for endpoint '" + this.beanName + "'");
            channel = this.beanFactory.getBean(this.inputChannelName, MessageChannel.class);
        }
        if (this.inputChannel != null) {
            channel = this.inputChannel;
        }
        Assert.state(channel != null, "one of inputChannelName or inputChannel is required");
        if (channel instanceof SubscribableChannel) {
            Assert.isNull(this.pollerMetadata, "A poller should not be specified for endpoint '" + this.beanName
                    + "', since '" + channel + "' is a SubscribableChannel (not pollable).");
            this.endpoint = new EventDrivenConsumer((SubscribableChannel) channel, this.handler);
        } else if (channel instanceof PollableChannel) {
            PollingConsumer pollingConsumer = new PollingConsumer((PollableChannel) channel, this.handler);
            if (this.pollerMetadata == null) {
                this.pollerMetadata = PollerMetadata.getDefaultPollerMetadata(this.beanFactory);
                Assert.notNull(this.pollerMetadata, "No poller has been defined for endpoint '" + this.beanName
                        + "', 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(beanClassLoader);
            pollingConsumer.setBeanFactory(beanFactory);
            this.endpoint = pollingConsumer;
        } else {
            throw new IllegalArgumentException("unsupported channel type: [" + channel.getClass() + "]");
        }
        this.endpoint.setBeanName(this.beanName);
        this.endpoint.setBeanFactory(this.beanFactory);
        this.endpoint.setAutoStartup(this.autoStartup);
        this.endpoint.afterPropertiesSet();
        this.initialized = true;
    }
}

From source file:org.springframework.integration.handler.advice.AdvisedMessageHandlerTests.java

@Test
public void testInappropriateAdvice() throws Exception {
    final AtomicBoolean called = new AtomicBoolean(false);
    Advice advice = new AbstractRequestHandlerAdvice() {
        @Override/*from   www.j a va  2 s . c o  m*/
        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, new MessageHandler() {
        @Override
        public void handleMessage(Message<?> message) throws MessagingException {
        }
    });
    consumer.setAdviceChain(Collections.singletonList(advice));
    consumer.setTaskExecutor(
            new ErrorHandlingTaskExecutor(Executors.newSingleThreadExecutor(), new ErrorHandler() {
                @Override
                public void handleError(Throwable t) {
                }
            }));
    consumer.afterPropertiesSet();

    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(new Answer<Object>() {
        @Override
        public Object answer(InvocationOnMock invocation) throws Throwable {
            logMessage.set((String) invocation.getArguments()[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());
    assertTrue(logMessage.get()
            .endsWith("can only be used for MessageHandlers; " + "an attempt to advise method 'call' in "
                    + "'org.springframework.integration.endpoint.AbstractPollingEndpoint$1' is ignored"));
}