Example usage for org.springframework.integration.config SourcePollingChannelAdapterFactoryBean setAutoStartup

List of usage examples for org.springframework.integration.config SourcePollingChannelAdapterFactoryBean setAutoStartup

Introduction

In this page you can find the example usage for org.springframework.integration.config SourcePollingChannelAdapterFactoryBean setAutoStartup.

Prototype

public void setAutoStartup(boolean autoStartup) 

Source Link

Usage

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

@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);/*from  w  ww.  jav  a2  s .  c o  m*/
        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());
}

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

@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);//from  w ww  .ja  v  a  2 s.  co m
        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());
}