Search in sources :

Example 11 with PollerMetadata

use of org.springframework.integration.scheduling.PollerMetadata in project spring-integration by spring-projects.

the class CronTriggerParserTests method checkConfigWithAttribute.

@Test
public void checkConfigWithAttribute() {
    Object poller = context.getBean("pollerWithAttribute");
    assertEquals(PollerMetadata.class, poller.getClass());
    PollerMetadata metadata = (PollerMetadata) poller;
    Trigger trigger = metadata.getTrigger();
    assertEquals(CronTrigger.class, trigger.getClass());
    DirectFieldAccessor accessor = new DirectFieldAccessor(trigger);
    String expression = (String) new DirectFieldAccessor(accessor.getPropertyValue("sequenceGenerator")).getPropertyValue("expression");
    assertEquals("*/10 * 9-17 * * MON-FRI", expression);
}
Also used : CronTrigger(org.springframework.scheduling.support.CronTrigger) Trigger(org.springframework.scheduling.Trigger) DirectFieldAccessor(org.springframework.beans.DirectFieldAccessor) PollerMetadata(org.springframework.integration.scheduling.PollerMetadata) Test(org.junit.Test)

Example 12 with PollerMetadata

use of org.springframework.integration.scheduling.PollerMetadata in project spring-integration by spring-projects.

the class ApplicationContextMessageBusTests method errorChannelWithFailedDispatch.

@Test
public void errorChannelWithFailedDispatch() throws InterruptedException {
    TestApplicationContext context = TestUtils.createTestApplicationContext();
    QueueChannel errorChannel = new QueueChannel();
    QueueChannel outputChannel = new QueueChannel();
    context.registerChannel("errorChannel", errorChannel);
    CountDownLatch latch = new CountDownLatch(1);
    SourcePollingChannelAdapter channelAdapter = new SourcePollingChannelAdapter();
    channelAdapter.setSource(new FailingSource(latch));
    PollerMetadata pollerMetadata = new PollerMetadata();
    pollerMetadata.setTrigger(new PeriodicTrigger(1000));
    channelAdapter.setOutputChannel(outputChannel);
    context.registerEndpoint("testChannel", channelAdapter);
    context.refresh();
    latch.await(2000, TimeUnit.MILLISECONDS);
    Message<?> message = errorChannel.receive(5000);
    context.stop();
    assertNull(outputChannel.receive(100));
    assertNotNull("message should not be null", message);
    assertTrue(message instanceof ErrorMessage);
    Throwable exception = ((ErrorMessage) message).getPayload();
    assertEquals("intentional test failure", exception.getCause().getMessage());
}
Also used : QueueChannel(org.springframework.integration.channel.QueueChannel) SourcePollingChannelAdapter(org.springframework.integration.endpoint.SourcePollingChannelAdapter) CountDownLatch(java.util.concurrent.CountDownLatch) ErrorMessage(org.springframework.messaging.support.ErrorMessage) PollerMetadata(org.springframework.integration.scheduling.PollerMetadata) TestApplicationContext(org.springframework.integration.test.util.TestUtils.TestApplicationContext) PeriodicTrigger(org.springframework.scheduling.support.PeriodicTrigger) Test(org.junit.Test)

Example 13 with PollerMetadata

use of org.springframework.integration.scheduling.PollerMetadata 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 14 with PollerMetadata

use of org.springframework.integration.scheduling.PollerMetadata 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 15 with PollerMetadata

use of org.springframework.integration.scheduling.PollerMetadata 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)

Aggregations

PollerMetadata (org.springframework.integration.scheduling.PollerMetadata)15 Test (org.junit.Test)12 PeriodicTrigger (org.springframework.scheduling.support.PeriodicTrigger)12 Trigger (org.springframework.scheduling.Trigger)6 CountDownLatch (java.util.concurrent.CountDownLatch)5 DirectFieldAccessor (org.springframework.beans.DirectFieldAccessor)5 QueueChannel (org.springframework.integration.channel.QueueChannel)5 SourcePollingChannelAdapter (org.springframework.integration.endpoint.SourcePollingChannelAdapter)4 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)3 Advice (org.aopalliance.aop.Advice)3 ConfigurableBeanFactory (org.springframework.beans.factory.config.ConfigurableBeanFactory)3 Lifecycle (org.springframework.context.Lifecycle)3 MessagePublishingErrorHandler (org.springframework.integration.channel.MessagePublishingErrorHandler)3 NullChannel (org.springframework.integration.channel.NullChannel)3 SourcePollingChannelAdapterFactoryBean (org.springframework.integration.config.SourcePollingChannelAdapterFactoryBean)3 TestApplicationContext (org.springframework.integration.test.util.TestUtils.TestApplicationContext)3 Message (org.springframework.messaging.Message)3 GenericMessage (org.springframework.messaging.support.GenericMessage)3 ArrayList (java.util.ArrayList)2 List (java.util.List)2