Search in sources :

Example 16 with PeriodicTrigger

use of org.springframework.scheduling.support.PeriodicTrigger 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 17 with PeriodicTrigger

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

the class WebServiceOutboundGatewayParserTests method simpleGatewayWithPoller.

@Test
public void simpleGatewayWithPoller() {
    AbstractEndpoint endpoint = this.context.getBean("gatewayWithPoller", AbstractEndpoint.class);
    assertEquals(PollingConsumer.class, endpoint.getClass());
    Object triggerObject = new DirectFieldAccessor(endpoint).getPropertyValue("trigger");
    assertEquals(PeriodicTrigger.class, triggerObject.getClass());
    PeriodicTrigger trigger = (PeriodicTrigger) triggerObject;
    DirectFieldAccessor accessor = new DirectFieldAccessor(trigger);
    assertEquals("PeriodicTrigger had wrong period", 5000, ((Long) accessor.getPropertyValue("period")).longValue());
}
Also used : AbstractEndpoint(org.springframework.integration.endpoint.AbstractEndpoint) DirectFieldAccessor(org.springframework.beans.DirectFieldAccessor) PeriodicTrigger(org.springframework.scheduling.support.PeriodicTrigger) Test(org.junit.Test)

Example 18 with PeriodicTrigger

use of org.springframework.scheduling.support.PeriodicTrigger in project spring-cloud-stream by spring-cloud.

the class DefaultPollerProperties method getPollerMetadata.

public PollerMetadata getPollerMetadata() {
    PollerMetadata pollerMetadata = new PollerMetadata();
    pollerMetadata.setTrigger(new PeriodicTrigger(this.fixedDelay));
    pollerMetadata.setMaxMessagesPerPoll(this.maxMessagesPerPoll);
    return pollerMetadata;
}
Also used : PollerMetadata(org.springframework.integration.scheduling.PollerMetadata) PeriodicTrigger(org.springframework.scheduling.support.PeriodicTrigger)

Example 19 with PeriodicTrigger

use of org.springframework.scheduling.support.PeriodicTrigger in project webofneeds by researchstudio-sat.

the class BotRunnerApp method main.

public static void main(String[] args) throws Exception {
    if (args.length != 1) {
        System.err.println("arguments: [bot class name]");
        System.exit(1);
    }
    String botClass = args[0];
    SpringApplication app = new SpringApplication(new Object[] { "classpath:/spring/app/botRunner.xml" });
    app.setWebEnvironment(false);
    ConfigurableApplicationContext applicationContext = app.run(args);
    Bot bot = null;
    // create a bot instance and auto-wire it
    AutowireCapableBeanFactory beanFactory = applicationContext.getAutowireCapableBeanFactory();
    bot = (Bot) beanFactory.autowire(Class.forName(botClass), AutowireCapableBeanFactory.AUTOWIRE_BY_TYPE, false);
    Object botBean = beanFactory.initializeBean(bot, "theBot");
    bot = (Bot) botBean;
    // (there is no trigger bean in the context)
    if (bot instanceof TriggeredBot) {
        PeriodicTrigger trigger = new PeriodicTrigger(5000, TimeUnit.MILLISECONDS);
        trigger.setInitialDelay(1000);
        ((TriggeredBot) bot).setTrigger(trigger);
    }
    BotManager botManager = (BotManager) applicationContext.getBean("botManager");
    // adding the bot to the bot manager will cause it to be initialized.
    // at that point, the trigger starts.
    botManager.addBot(bot);
}
Also used : ConfigurableApplicationContext(org.springframework.context.ConfigurableApplicationContext) BotManager(won.bot.framework.manager.BotManager) SpringApplication(org.springframework.boot.SpringApplication) TriggeredBot(won.bot.framework.bot.base.TriggeredBot) Bot(won.bot.framework.bot.Bot) AutowireCapableBeanFactory(org.springframework.beans.factory.config.AutowireCapableBeanFactory) TriggeredBot(won.bot.framework.bot.base.TriggeredBot) PeriodicTrigger(org.springframework.scheduling.support.PeriodicTrigger)

Example 20 with PeriodicTrigger

use of org.springframework.scheduling.support.PeriodicTrigger in project webofneeds by researchstudio-sat.

the class NeedInactivityChecker method afterPropertiesSet.

@Override
public void afterPropertiesSet() throws Exception {
    if (this.taskScheduler == null)
        throw new IllegalStateException("taskScheduler must be set");
    if (this.inactivityCheckInterval > 0) {
        PeriodicTrigger periodicTrigger = new PeriodicTrigger(this.inactivityCheckInterval, TimeUnit.SECONDS);
        periodicTrigger.setInitialDelay(this.inactivityCheckInterval);
        this.trigger = periodicTrigger;
    }
    this.inactivityCheckTask = new InactivityCheckTask();
    taskScheduler.schedule(this.inactivityCheckTask, trigger);
    if (logger.isDebugEnabled()) {
        logger.debug("setting up inactivity checker to check inactivity every {} seconds, warn after {} seconds and deactivate after {} seconds", new Object[] { inactivityCheckInterval, warnTimeout, deactivateTimeout });
    }
}
Also used : PeriodicTrigger(org.springframework.scheduling.support.PeriodicTrigger)

Aggregations

PeriodicTrigger (org.springframework.scheduling.support.PeriodicTrigger)42 AutowireCapableBeanFactory (org.springframework.beans.factory.config.AutowireCapableBeanFactory)20 Before (org.junit.Before)19 BrokenBarrierException (java.util.concurrent.BrokenBarrierException)14 Test (org.junit.Test)14 QueueChannel (org.springframework.integration.channel.QueueChannel)10 PollerMetadata (org.springframework.integration.scheduling.PollerMetadata)10 CountDownLatch (java.util.concurrent.CountDownLatch)8 BeanFactory (org.springframework.beans.factory.BeanFactory)6 Message (org.springframework.messaging.Message)6 SourcePollingChannelAdapter (org.springframework.integration.endpoint.SourcePollingChannelAdapter)5 GenericMessage (org.springframework.messaging.support.GenericMessage)5 NullChannel (org.springframework.integration.channel.NullChannel)4 TestApplicationContext (org.springframework.integration.test.util.TestUtils.TestApplicationContext)4 Trigger (org.springframework.scheduling.Trigger)4 ThreadPoolTaskScheduler (org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler)4 ArrayList (java.util.ArrayList)3 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)3 DirectFieldAccessor (org.springframework.beans.DirectFieldAccessor)3 ConfigurableBeanFactory (org.springframework.beans.factory.config.ConfigurableBeanFactory)3