Search in sources :

Example 11 with SourcePollingChannelAdapter

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

the class SourcePollingChannelAdapterFactoryBeanTests method testInterrupted.

@Test
public void testInterrupted() throws Exception {
    final CountDownLatch startLatch = new CountDownLatch(1);
    MessageSource<Object> ms = () -> {
        startLatch.countDown();
        try {
            Thread.sleep(10000);
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
            throw new MessagingException("Interrupted awaiting stopLatch", e);
        }
        return null;
    };
    SourcePollingChannelAdapter pollingChannelAdapter = new SourcePollingChannelAdapter();
    ThreadPoolTaskScheduler taskScheduler = new ThreadPoolTaskScheduler();
    taskScheduler.setWaitForTasksToCompleteOnShutdown(true);
    taskScheduler.setAwaitTerminationSeconds(1);
    taskScheduler.afterPropertiesSet();
    pollingChannelAdapter.setTaskScheduler(taskScheduler);
    MessagePublishingErrorHandler errorHandler = new MessagePublishingErrorHandler();
    Log errorHandlerLogger = TestUtils.getPropertyValue(errorHandler, "logger", Log.class);
    errorHandlerLogger = spy(errorHandlerLogger);
    DirectFieldAccessor dfa = new DirectFieldAccessor(errorHandler);
    dfa.setPropertyValue("logger", errorHandlerLogger);
    pollingChannelAdapter.setErrorHandler(errorHandler);
    pollingChannelAdapter.setSource(ms);
    pollingChannelAdapter.setOutputChannel(new NullChannel());
    pollingChannelAdapter.setBeanFactory(mock(BeanFactory.class));
    pollingChannelAdapter.afterPropertiesSet();
    Log adapterLogger = TestUtils.getPropertyValue(pollingChannelAdapter, "logger", Log.class);
    adapterLogger = spy(adapterLogger);
    when(adapterLogger.isDebugEnabled()).thenReturn(true);
    dfa = new DirectFieldAccessor(pollingChannelAdapter);
    dfa.setPropertyValue("logger", adapterLogger);
    pollingChannelAdapter.start();
    assertTrue(startLatch.await(10, TimeUnit.SECONDS));
    pollingChannelAdapter.stop();
    taskScheduler.shutdown();
    verifyZeroInteractions(errorHandlerLogger);
    verify(adapterLogger).debug(contains("Poll interrupted - during stop()?"));
}
Also used : MessagePublishingErrorHandler(org.springframework.integration.channel.MessagePublishingErrorHandler) MessagingException(org.springframework.messaging.MessagingException) Log(org.apache.commons.logging.Log) DirectFieldAccessor(org.springframework.beans.DirectFieldAccessor) BeanFactory(org.springframework.beans.factory.BeanFactory) SourcePollingChannelAdapter(org.springframework.integration.endpoint.SourcePollingChannelAdapter) CountDownLatch(java.util.concurrent.CountDownLatch) NullChannel(org.springframework.integration.channel.NullChannel) ThreadPoolTaskScheduler(org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler) Test(org.junit.Test)

Example 12 with SourcePollingChannelAdapter

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

the class SourcePollingChannelAdapterFactoryBeanTests method testStartSourceBeforeRunPollingTask.

@Test
public void testStartSourceBeforeRunPollingTask() {
    TaskScheduler taskScheduler = mock(TaskScheduler.class);
    willAnswer(invocation -> {
        Runnable task = invocation.getArgument(0);
        task.run();
        return null;
    }).given(taskScheduler).schedule(any(Runnable.class), any(Trigger.class));
    SourcePollingChannelAdapter pollingChannelAdapter = new SourcePollingChannelAdapter();
    pollingChannelAdapter.setTaskScheduler(taskScheduler);
    pollingChannelAdapter.setSource(new LifecycleMessageSource());
    pollingChannelAdapter.setMaxMessagesPerPoll(1);
    QueueChannel outputChannel = new QueueChannel();
    pollingChannelAdapter.setOutputChannel(outputChannel);
    pollingChannelAdapter.setBeanFactory(mock(BeanFactory.class));
    pollingChannelAdapter.afterPropertiesSet();
    pollingChannelAdapter.start();
    Message<?> receive = outputChannel.receive(10_000);
    assertNotNull(receive);
    assertEquals(true, receive.getPayload());
    pollingChannelAdapter.stop();
}
Also used : PeriodicTrigger(org.springframework.scheduling.support.PeriodicTrigger) Trigger(org.springframework.scheduling.Trigger) QueueChannel(org.springframework.integration.channel.QueueChannel) BeanFactory(org.springframework.beans.factory.BeanFactory) SourcePollingChannelAdapter(org.springframework.integration.endpoint.SourcePollingChannelAdapter) ThreadPoolTaskScheduler(org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler) TaskScheduler(org.springframework.scheduling.TaskScheduler) Test(org.junit.Test)

Example 13 with SourcePollingChannelAdapter

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

the class ChannelAdapterParserTests method methodInvokingSourceNotStarted.

@Test
public void methodInvokingSourceNotStarted() {
    String beanName = "methodInvokingSource";
    PollableChannel channel = (PollableChannel) this.applicationContext.getBean("queueChannel");
    TestBean testBean = (TestBean) this.applicationContext.getBean("testBean");
    testBean.store("source test");
    Object adapter = this.applicationContext.getBean(beanName);
    assertNotNull(adapter);
    assertTrue(adapter instanceof SourcePollingChannelAdapter);
    Message<?> message = channel.receive(100);
    assertNull(message);
}
Also used : PollableChannel(org.springframework.messaging.PollableChannel) SourcePollingChannelAdapter(org.springframework.integration.endpoint.SourcePollingChannelAdapter) Test(org.junit.Test)

Example 14 with SourcePollingChannelAdapter

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

the class ChannelAdapterParserTests method methodInvokingSourceStopped.

@Test
public void methodInvokingSourceStopped() {
    String beanName = "methodInvokingSource";
    PollableChannel channel = (PollableChannel) this.applicationContext.getBean("queueChannel");
    TestBean testBean = (TestBean) this.applicationContext.getBean("testBean");
    testBean.store("source test");
    Object adapter = this.applicationContext.getBean(beanName);
    assertNotNull(adapter);
    assertTrue(adapter instanceof SourcePollingChannelAdapter);
    ((SourcePollingChannelAdapter) adapter).start();
    Message<?> message = channel.receive(10000);
    assertNotNull(message);
    assertEquals("source test", testBean.getMessage());
    ((SourcePollingChannelAdapter) adapter).stop();
    message = channel.receive(100);
    assertNull(message);
}
Also used : PollableChannel(org.springframework.messaging.PollableChannel) SourcePollingChannelAdapter(org.springframework.integration.endpoint.SourcePollingChannelAdapter) Test(org.junit.Test)

Example 15 with SourcePollingChannelAdapter

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

the class ChannelAdapterParserTests method methodInvokingSourceStoppedByApplicationContextInner.

@Test
public void methodInvokingSourceStoppedByApplicationContextInner() {
    String beanName = "methodInvokingSource";
    PollableChannel channel = (PollableChannel) this.applicationContextInner.getBean("queueChannel");
    // TestBean testBean = (TestBean) this.applicationContextInner.getBean("testBean");
    // testBean.store("source test");
    Object adapter = this.applicationContextInner.getBean(beanName);
    assertNotNull(adapter);
    assertTrue(adapter instanceof SourcePollingChannelAdapter);
    this.applicationContextInner.start();
    Message<?> message = channel.receive(10000);
    assertNotNull(message);
    // assertEquals("source test", testBean.getMessage());
    this.applicationContextInner.stop();
    message = channel.receive(100);
    assertNull(message);
}
Also used : PollableChannel(org.springframework.messaging.PollableChannel) SourcePollingChannelAdapter(org.springframework.integration.endpoint.SourcePollingChannelAdapter) Test(org.junit.Test)

Aggregations

SourcePollingChannelAdapter (org.springframework.integration.endpoint.SourcePollingChannelAdapter)66 Test (org.junit.Test)58 ClassPathXmlApplicationContext (org.springframework.context.support.ClassPathXmlApplicationContext)29 Message (org.springframework.messaging.Message)15 PollableChannel (org.springframework.messaging.PollableChannel)13 Collection (java.util.Collection)10 ArrayList (java.util.ArrayList)9 QueueChannel (org.springframework.integration.channel.QueueChannel)9 JpaExecutor (org.springframework.integration.jpa.core.JpaExecutor)9 Consumer (org.springframework.integration.jpa.test.Consumer)9 DirectFieldAccessor (org.springframework.beans.DirectFieldAccessor)8 PeriodicTrigger (org.springframework.scheduling.support.PeriodicTrigger)8 Trigger (org.springframework.scheduling.Trigger)7 ConfigurableApplicationContext (org.springframework.context.ConfigurableApplicationContext)6 Expression (org.springframework.expression.Expression)6 CountDownLatch (java.util.concurrent.CountDownLatch)5 BeanFactory (org.springframework.beans.factory.BeanFactory)5 RedisConnectionFactory (org.springframework.data.redis.connection.RedisConnectionFactory)5 RedisAvailable (org.springframework.integration.redis.rules.RedisAvailable)5 File (java.io.File)4