Search in sources :

Example 6 with PollerMetadata

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

the class SourcePollingChannelAdapterFactoryBeanTests method testTransactionalAdviceChain.

@Test
public void testTransactionalAdviceChain() throws Throwable {
    SourcePollingChannelAdapterFactoryBean factoryBean = new SourcePollingChannelAdapterFactoryBean();
    QueueChannel outputChannel = new QueueChannel();
    TestApplicationContext context = TestUtils.createTestApplicationContext();
    factoryBean.setBeanFactory(context.getBeanFactory());
    factoryBean.setBeanClassLoader(ClassUtils.getDefaultClassLoader());
    factoryBean.setOutputChannel(outputChannel);
    factoryBean.setSource(() -> new GenericMessage<>("test"));
    PollerMetadata pollerMetadata = new PollerMetadata();
    List<Advice> adviceChain = new ArrayList<Advice>();
    final AtomicBoolean adviceApplied = new AtomicBoolean(false);
    adviceChain.add((MethodInterceptor) invocation -> {
        adviceApplied.set(true);
        return invocation.proceed();
    });
    pollerMetadata.setTrigger(new PeriodicTrigger(5000));
    pollerMetadata.setMaxMessagesPerPoll(1);
    final AtomicInteger count = new AtomicInteger();
    final MethodInterceptor txAdvice = mock(MethodInterceptor.class);
    adviceChain.add((MethodInterceptor) invocation -> {
        count.incrementAndGet();
        return invocation.proceed();
    });
    when(txAdvice.invoke(any(MethodInvocation.class))).thenAnswer(invocation -> {
        count.incrementAndGet();
        return ((MethodInvocation) invocation.getArgument(0)).proceed();
    });
    pollerMetadata.setAdviceChain(adviceChain);
    factoryBean.setPollerMetadata(pollerMetadata);
    factoryBean.setAutoStartup(true);
    factoryBean.afterPropertiesSet();
    context.registerEndpoint("testPollingEndpoint", factoryBean.getObject());
    context.refresh();
    Message<?> message = outputChannel.receive(5000);
    assertEquals("test", message.getPayload());
    assertEquals(1, count.get());
    assertTrue("adviceChain was not applied", adviceApplied.get());
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) QueueChannel(org.springframework.integration.channel.QueueChannel) MessagingException(org.springframework.messaging.MessagingException) PollerMetadata(org.springframework.integration.scheduling.PollerMetadata) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) TestApplicationContext(org.springframework.integration.test.util.TestUtils.TestApplicationContext) DirectFieldAccessor(org.springframework.beans.DirectFieldAccessor) ArgumentMatchers.contains(org.mockito.ArgumentMatchers.contains) Mockito.spy(org.mockito.Mockito.spy) TestUtils(org.springframework.integration.test.util.TestUtils) MessagePublishingErrorHandler(org.springframework.integration.channel.MessagePublishingErrorHandler) MessageSource(org.springframework.integration.core.MessageSource) Mockito.verifyZeroInteractions(org.mockito.Mockito.verifyZeroInteractions) ArrayList(java.util.ArrayList) MethodInvocation(org.aopalliance.intercept.MethodInvocation) NullChannel(org.springframework.integration.channel.NullChannel) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Advice(org.aopalliance.aop.Advice) Message(org.springframework.messaging.Message) ThreadPoolTaskScheduler(org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler) PeriodicTrigger(org.springframework.scheduling.support.PeriodicTrigger) ClassUtils(org.springframework.util.ClassUtils) Trigger(org.springframework.scheduling.Trigger) Assert.assertNotNull(org.junit.Assert.assertNotNull) Assert.assertTrue(org.junit.Assert.assertTrue) SourcePollingChannelAdapter(org.springframework.integration.endpoint.SourcePollingChannelAdapter) TaskScheduler(org.springframework.scheduling.TaskScheduler) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) BDDMockito.willAnswer(org.mockito.BDDMockito.willAnswer) Mockito.verify(org.mockito.Mockito.verify) Lifecycle(org.springframework.context.Lifecycle) TimeUnit(java.util.concurrent.TimeUnit) CountDownLatch(java.util.concurrent.CountDownLatch) MethodInterceptor(org.aopalliance.intercept.MethodInterceptor) List(java.util.List) BeanFactory(org.springframework.beans.factory.BeanFactory) Log(org.apache.commons.logging.Log) GenericMessage(org.springframework.messaging.support.GenericMessage) Assert.assertEquals(org.junit.Assert.assertEquals) Mockito.mock(org.mockito.Mockito.mock) QueueChannel(org.springframework.integration.channel.QueueChannel) ArrayList(java.util.ArrayList) MethodInvocation(org.aopalliance.intercept.MethodInvocation) PeriodicTrigger(org.springframework.scheduling.support.PeriodicTrigger) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) MethodInterceptor(org.aopalliance.intercept.MethodInterceptor) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Advice(org.aopalliance.aop.Advice) PollerMetadata(org.springframework.integration.scheduling.PollerMetadata) TestApplicationContext(org.springframework.integration.test.util.TestUtils.TestApplicationContext) Test(org.junit.Test)

Example 7 with PollerMetadata

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

the class SourcePollingChannelAdapterFactoryBeanTests method testAdviceChain.

@Test
public void testAdviceChain() throws Exception {
    SourcePollingChannelAdapterFactoryBean factoryBean = new SourcePollingChannelAdapterFactoryBean();
    QueueChannel outputChannel = new QueueChannel();
    TestApplicationContext context = TestUtils.createTestApplicationContext();
    factoryBean.setBeanFactory(context.getBeanFactory());
    factoryBean.setBeanClassLoader(ClassUtils.getDefaultClassLoader());
    factoryBean.setOutputChannel(outputChannel);
    factoryBean.setSource(() -> new GenericMessage<>("test"));
    PollerMetadata pollerMetadata = new PollerMetadata();
    List<Advice> adviceChain = new ArrayList<Advice>();
    final AtomicBoolean adviceApplied = new AtomicBoolean(false);
    adviceChain.add((MethodInterceptor) invocation -> {
        adviceApplied.set(true);
        return invocation.proceed();
    });
    pollerMetadata.setTrigger(new PeriodicTrigger(5000));
    pollerMetadata.setMaxMessagesPerPoll(1);
    pollerMetadata.setAdviceChain(adviceChain);
    factoryBean.setPollerMetadata(pollerMetadata);
    factoryBean.setAutoStartup(true);
    factoryBean.afterPropertiesSet();
    context.registerEndpoint("testPollingEndpoint", factoryBean.getObject());
    context.refresh();
    Message<?> message = outputChannel.receive(5000);
    assertEquals("test", message.getPayload());
    assertTrue("adviceChain was not applied", adviceApplied.get());
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) QueueChannel(org.springframework.integration.channel.QueueChannel) MessagingException(org.springframework.messaging.MessagingException) PollerMetadata(org.springframework.integration.scheduling.PollerMetadata) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) TestApplicationContext(org.springframework.integration.test.util.TestUtils.TestApplicationContext) DirectFieldAccessor(org.springframework.beans.DirectFieldAccessor) ArgumentMatchers.contains(org.mockito.ArgumentMatchers.contains) Mockito.spy(org.mockito.Mockito.spy) TestUtils(org.springframework.integration.test.util.TestUtils) MessagePublishingErrorHandler(org.springframework.integration.channel.MessagePublishingErrorHandler) MessageSource(org.springframework.integration.core.MessageSource) Mockito.verifyZeroInteractions(org.mockito.Mockito.verifyZeroInteractions) ArrayList(java.util.ArrayList) MethodInvocation(org.aopalliance.intercept.MethodInvocation) NullChannel(org.springframework.integration.channel.NullChannel) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Advice(org.aopalliance.aop.Advice) Message(org.springframework.messaging.Message) ThreadPoolTaskScheduler(org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler) PeriodicTrigger(org.springframework.scheduling.support.PeriodicTrigger) ClassUtils(org.springframework.util.ClassUtils) Trigger(org.springframework.scheduling.Trigger) Assert.assertNotNull(org.junit.Assert.assertNotNull) Assert.assertTrue(org.junit.Assert.assertTrue) SourcePollingChannelAdapter(org.springframework.integration.endpoint.SourcePollingChannelAdapter) TaskScheduler(org.springframework.scheduling.TaskScheduler) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) BDDMockito.willAnswer(org.mockito.BDDMockito.willAnswer) Mockito.verify(org.mockito.Mockito.verify) Lifecycle(org.springframework.context.Lifecycle) TimeUnit(java.util.concurrent.TimeUnit) CountDownLatch(java.util.concurrent.CountDownLatch) MethodInterceptor(org.aopalliance.intercept.MethodInterceptor) List(java.util.List) BeanFactory(org.springframework.beans.factory.BeanFactory) Log(org.apache.commons.logging.Log) GenericMessage(org.springframework.messaging.support.GenericMessage) Assert.assertEquals(org.junit.Assert.assertEquals) Mockito.mock(org.mockito.Mockito.mock) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) QueueChannel(org.springframework.integration.channel.QueueChannel) ArrayList(java.util.ArrayList) Advice(org.aopalliance.aop.Advice) 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 8 with PollerMetadata

use of org.springframework.integration.scheduling.PollerMetadata in project paascloud-master by paascloud.

the class CustomPollerConfiguration method customPoller.

/**
 * Custom poller poller metadata.
 *
 * @return the poller metadata
 */
@Bean(name = StreamSpanReporter.POLLER)
PollerMetadata customPoller() {
    PollerMetadata poller = new PollerMetadata();
    poller.setMaxMessagesPerPoll(500);
    poller.setTrigger(new PeriodicTrigger(5000L));
    return poller;
}
Also used : PollerMetadata(org.springframework.integration.scheduling.PollerMetadata) PeriodicTrigger(org.springframework.scheduling.support.PeriodicTrigger) Bean(org.springframework.context.annotation.Bean)

Example 9 with PollerMetadata

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

the class PollerParserTests method pollerWithReceiveTimeoutAndTimeunit.

@Test
public void pollerWithReceiveTimeoutAndTimeunit() {
    ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("pollerWithReceiveTimeout.xml", PollerParserTests.class);
    Object poller = context.getBean("poller");
    assertNotNull(poller);
    PollerMetadata metadata = (PollerMetadata) poller;
    assertEquals(1234, metadata.getReceiveTimeout());
    PeriodicTrigger trigger = (PeriodicTrigger) metadata.getTrigger();
    assertEquals(TimeUnit.SECONDS.toString(), TestUtils.getPropertyValue(trigger, "timeUnit").toString());
    context.close();
}
Also used : ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) PollerMetadata(org.springframework.integration.scheduling.PollerMetadata) PeriodicTrigger(org.springframework.scheduling.support.PeriodicTrigger) Test(org.junit.Test)

Example 10 with PollerMetadata

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

the class PollerParserTests method pollerWithTriggerReference.

@Test
public void pollerWithTriggerReference() {
    ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("pollerWithTriggerReference.xml", PollerParserTests.class);
    Object poller = context.getBean("poller");
    assertNotNull(poller);
    PollerMetadata metadata = (PollerMetadata) poller;
    assertTrue(metadata.getTrigger() instanceof TestTrigger);
    context.close();
}
Also used : TestTrigger(org.springframework.integration.config.TestTrigger) ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) PollerMetadata(org.springframework.integration.scheduling.PollerMetadata) 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