Search in sources :

Example 1 with SourcePollingChannelAdapterFactoryBean

use of org.springframework.integration.config.SourcePollingChannelAdapterFactoryBean in project spring-integration by spring-projects.

the class JpaTestUtils method getSourcePollingChannelAdapter.

public static SourcePollingChannelAdapter getSourcePollingChannelAdapter(MessageSource<?> adapter, MessageChannel channel, PollerMetadata poller, GenericApplicationContext context, ClassLoader beanClassLoader) throws Exception {
    SourcePollingChannelAdapterFactoryBean fb = new SourcePollingChannelAdapterFactoryBean();
    fb.setSource(adapter);
    fb.setOutputChannel(channel);
    fb.setPollerMetadata(poller);
    fb.setBeanClassLoader(beanClassLoader);
    fb.setAutoStartup(false);
    fb.setBeanFactory(context.getBeanFactory());
    fb.afterPropertiesSet();
    return fb.getObject();
}
Also used : SourcePollingChannelAdapterFactoryBean(org.springframework.integration.config.SourcePollingChannelAdapterFactoryBean)

Example 2 with SourcePollingChannelAdapterFactoryBean

use of org.springframework.integration.config.SourcePollingChannelAdapterFactoryBean in project spring-integration by spring-projects.

the class PollingLifecycleTests method ensurePollerTaskStopsForAdapterWithInterruptible.

@Test
public void ensurePollerTaskStopsForAdapterWithInterruptible() throws Exception {
    final CountDownLatch latch = new CountDownLatch(2);
    QueueChannel channel = new QueueChannel();
    SourcePollingChannelAdapterFactoryBean adapterFactory = new SourcePollingChannelAdapterFactoryBean();
    PollerMetadata pollerMetadata = new PollerMetadata();
    pollerMetadata.setMaxMessagesPerPoll(-1);
    pollerMetadata.setTrigger(new PeriodicTrigger(2000));
    adapterFactory.setPollerMetadata(pollerMetadata);
    final Runnable coughtInterrupted = mock(Runnable.class);
    MessageSource<String> source = () -> {
        try {
            for (int i = 0; i < 10; i++) {
                Thread.sleep(1000);
                latch.countDown();
            }
        } catch (InterruptedException e) {
            coughtInterrupted.run();
        }
        return new GenericMessage<String>("hello");
    };
    adapterFactory.setSource(source);
    adapterFactory.setOutputChannel(channel);
    adapterFactory.setBeanFactory(mock(ConfigurableBeanFactory.class));
    SourcePollingChannelAdapter adapter = adapterFactory.getObject();
    adapter.setTaskScheduler(taskScheduler);
    adapter.afterPropertiesSet();
    adapter.start();
    assertTrue(latch.await(3000, TimeUnit.SECONDS));
    // 
    adapter.stop();
    Thread.sleep(1000);
    Mockito.verify(coughtInterrupted, times(1)).run();
}
Also used : ConfigurableBeanFactory(org.springframework.beans.factory.config.ConfigurableBeanFactory) QueueChannel(org.springframework.integration.channel.QueueChannel) SourcePollingChannelAdapterFactoryBean(org.springframework.integration.config.SourcePollingChannelAdapterFactoryBean) CountDownLatch(java.util.concurrent.CountDownLatch) PollerMetadata(org.springframework.integration.scheduling.PollerMetadata) PeriodicTrigger(org.springframework.scheduling.support.PeriodicTrigger) Test(org.junit.Test)

Example 3 with SourcePollingChannelAdapterFactoryBean

use of org.springframework.integration.config.SourcePollingChannelAdapterFactoryBean in project spring-integration by spring-projects.

the class PollingLifecycleTests method testAdapterLifecycleIsPropagatedToMessageSource.

@Test
public void testAdapterLifecycleIsPropagatedToMessageSource() throws Exception {
    SourcePollingChannelAdapterFactoryBean adapterFactory = new SourcePollingChannelAdapterFactoryBean();
    adapterFactory.setOutputChannel(new NullChannel());
    adapterFactory.setBeanFactory(mock(ConfigurableBeanFactory.class));
    PollerMetadata pollerMetadata = new PollerMetadata();
    pollerMetadata.setTrigger(new PeriodicTrigger(2000));
    adapterFactory.setPollerMetadata(pollerMetadata);
    final AtomicBoolean startInvoked = new AtomicBoolean();
    final AtomicBoolean stopInvoked = new AtomicBoolean();
    MethodInvokingMessageSource source = new MethodInvokingMessageSource();
    source.setObject(new Lifecycle() {

        @Override
        public void start() {
            startInvoked.set(true);
        }

        @Override
        public void stop() {
            stopInvoked.set(true);
        }

        @Override
        public boolean isRunning() {
            return false;
        }
    });
    source.setMethodName("isRunning");
    adapterFactory.setSource(source);
    SourcePollingChannelAdapter adapter = adapterFactory.getObject();
    adapter.setTaskScheduler(this.taskScheduler);
    adapter.start();
    adapter.stop();
    assertTrue(startInvoked.get());
    assertTrue(stopInvoked.get());
}
Also used : ConfigurableBeanFactory(org.springframework.beans.factory.config.ConfigurableBeanFactory) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) SourcePollingChannelAdapterFactoryBean(org.springframework.integration.config.SourcePollingChannelAdapterFactoryBean) Lifecycle(org.springframework.context.Lifecycle) NullChannel(org.springframework.integration.channel.NullChannel) PollerMetadata(org.springframework.integration.scheduling.PollerMetadata) PeriodicTrigger(org.springframework.scheduling.support.PeriodicTrigger) Test(org.junit.Test)

Example 4 with SourcePollingChannelAdapterFactoryBean

use of org.springframework.integration.config.SourcePollingChannelAdapterFactoryBean in project spring-integration by spring-projects.

the class PollingLifecycleTests method ensurePollerTaskStopsForAdapter.

@Test
public void ensurePollerTaskStopsForAdapter() throws Exception {
    final CountDownLatch latch = new CountDownLatch(1);
    QueueChannel channel = new QueueChannel();
    SourcePollingChannelAdapterFactoryBean adapterFactory = new SourcePollingChannelAdapterFactoryBean();
    PollerMetadata pollerMetadata = new PollerMetadata();
    pollerMetadata.setTrigger(new PeriodicTrigger(2000));
    adapterFactory.setPollerMetadata(pollerMetadata);
    MessageSource<String> source = spy(new MessageSource<String>() {

        @Override
        public Message<String> receive() {
            latch.countDown();
            return new GenericMessage<String>("hello");
        }
    });
    adapterFactory.setSource(source);
    adapterFactory.setOutputChannel(channel);
    adapterFactory.setBeanFactory(mock(ConfigurableBeanFactory.class));
    SourcePollingChannelAdapter adapter = adapterFactory.getObject();
    adapter.setTaskScheduler(taskScheduler);
    adapter.afterPropertiesSet();
    adapter.start();
    assertTrue(latch.await(20, TimeUnit.SECONDS));
    assertNotNull(channel.receive(100));
    adapter.stop();
    assertNull(channel.receive(1000));
    Mockito.verify(source, times(1)).receive();
}
Also used : ConfigurableBeanFactory(org.springframework.beans.factory.config.ConfigurableBeanFactory) QueueChannel(org.springframework.integration.channel.QueueChannel) Message(org.springframework.messaging.Message) GenericMessage(org.springframework.messaging.support.GenericMessage) SourcePollingChannelAdapterFactoryBean(org.springframework.integration.config.SourcePollingChannelAdapterFactoryBean) CountDownLatch(java.util.concurrent.CountDownLatch) PollerMetadata(org.springframework.integration.scheduling.PollerMetadata) PeriodicTrigger(org.springframework.scheduling.support.PeriodicTrigger) Test(org.junit.Test)

Example 5 with SourcePollingChannelAdapterFactoryBean

use of org.springframework.integration.config.SourcePollingChannelAdapterFactoryBean in project spring-integration by spring-projects.

the class IntegrationFlowDefinition method registerOutputChannelIfCan.

private B registerOutputChannelIfCan(MessageChannel outputChannel) {
    if (!(outputChannel instanceof FixedSubscriberChannelPrototype)) {
        this.integrationComponents.put(outputChannel, null);
        if (this.currentComponent != null) {
            String channelName = null;
            if (outputChannel instanceof MessageChannelReference) {
                channelName = ((MessageChannelReference) outputChannel).getName();
            }
            Object currentComponent = this.currentComponent;
            if (AopUtils.isAopProxy(currentComponent)) {
                currentComponent = extractProxyTarget(currentComponent);
            }
            if (currentComponent instanceof MessageProducer) {
                MessageProducer messageProducer = (MessageProducer) currentComponent;
                checkReuse(messageProducer);
                if (channelName != null) {
                    if (messageProducer instanceof AbstractMessageProducingHandler) {
                        ((AbstractMessageProducingHandler) messageProducer).setOutputChannelName(channelName);
                    } else {
                        throw new BeanCreationException("The 'currentComponent' (" + currentComponent + ") must extend 'AbstractMessageProducingHandler' " + "for message channel resolution by name.\n" + "Your handler should extend 'AbstractMessageProducingHandler', " + "its subclass 'AbstractReplyProducingMessageHandler', or you should " + "reference a 'MessageChannel' bean instead of its name.");
                    }
                } else {
                    messageProducer.setOutputChannel(outputChannel);
                }
            } else if (currentComponent instanceof SourcePollingChannelAdapterSpec) {
                SourcePollingChannelAdapterFactoryBean pollingChannelAdapterFactoryBean = ((SourcePollingChannelAdapterSpec) currentComponent).get().getT1();
                if (channelName != null) {
                    pollingChannelAdapterFactoryBean.setOutputChannelName(channelName);
                } else {
                    pollingChannelAdapterFactoryBean.setOutputChannel(outputChannel);
                }
            } else {
                throw new BeanCreationException("The 'currentComponent' (" + currentComponent + ") is a one-way 'MessageHandler' and it isn't appropriate to configure 'outputChannel'. " + "This is the end of the integration flow.");
            }
            this.currentComponent = null;
        }
    }
    return _this();
}
Also used : BeanCreationException(org.springframework.beans.factory.BeanCreationException) MessageChannelReference(org.springframework.integration.dsl.support.MessageChannelReference) FixedSubscriberChannelPrototype(org.springframework.integration.dsl.support.FixedSubscriberChannelPrototype) SourcePollingChannelAdapterFactoryBean(org.springframework.integration.config.SourcePollingChannelAdapterFactoryBean) AbstractMessageProducingHandler(org.springframework.integration.handler.AbstractMessageProducingHandler) MessageProducer(org.springframework.integration.core.MessageProducer)

Aggregations

SourcePollingChannelAdapterFactoryBean (org.springframework.integration.config.SourcePollingChannelAdapterFactoryBean)6 Test (org.junit.Test)3 ConfigurableBeanFactory (org.springframework.beans.factory.config.ConfigurableBeanFactory)3 PollerMetadata (org.springframework.integration.scheduling.PollerMetadata)3 PeriodicTrigger (org.springframework.scheduling.support.PeriodicTrigger)3 CountDownLatch (java.util.concurrent.CountDownLatch)2 QueueChannel (org.springframework.integration.channel.QueueChannel)2 MessageChannelReference (org.springframework.integration.dsl.support.MessageChannelReference)2 Collection (java.util.Collection)1 LinkedHashMap (java.util.LinkedHashMap)1 Map (java.util.Map)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 BeansException (org.springframework.beans.BeansException)1 BeanCreationException (org.springframework.beans.factory.BeanCreationException)1 BeanCreationNotAllowedException (org.springframework.beans.factory.BeanCreationNotAllowedException)1 BeanFactory (org.springframework.beans.factory.BeanFactory)1 BeanFactoryAware (org.springframework.beans.factory.BeanFactoryAware)1 BeanFactoryUtils (org.springframework.beans.factory.BeanFactoryUtils)1 SmartInitializingSingleton (org.springframework.beans.factory.SmartInitializingSingleton)1 BeanDefinition (org.springframework.beans.factory.config.BeanDefinition)1