@Test public void testRepeatedExecutionJob() throws Exception { SessionConfiguration config = SessionConfiguration.newInstance(); config.setClockType(ClockType.REALTIME_CLOCK); TimerService timeService = TimerServiceFactory.getTimerService( config ); Trigger trigger = new DelayedTrigger( new long[] { 100, 100, 100} ); HelloWorldJobContext ctx = new HelloWorldJobContext( "hello world", timeService); timeService.scheduleJob( new HelloWorldJob(), ctx, trigger); Thread.sleep( 500 ); timeService.shutdown(); assertEquals( 3, ctx.getList().size() ); }
@Test public void testSingleExecutionJob() throws Exception { SessionConfiguration config = SessionConfiguration.newInstance(); config.setClockType(ClockType.REALTIME_CLOCK); TimerService timeService = TimerServiceFactory.getTimerService( config ); Trigger trigger = new DelayedTrigger( 100 ); HelloWorldJobContext ctx = new HelloWorldJobContext( "hello world", timeService); timeService.scheduleJob( new HelloWorldJob(), ctx, trigger); Thread.sleep( 500 ); timeService.shutdown(); assertEquals( 1, ctx.getList().size() ); }
@Test public void testRepeatedExecutionJobWithRemove() throws Exception { SessionConfiguration config = SessionConfiguration.newInstance(); config.setClockType(ClockType.REALTIME_CLOCK); TimerService timeService = TimerServiceFactory.getTimerService( config ); Trigger trigger = new DelayedTrigger( new long[] {100, 100, 100, 100, 100, 100, 100, 100} ); HelloWorldJobContext ctx = new HelloWorldJobContext( "hello world", timeService); ctx.setLimit( 3 ); timeService.scheduleJob( new HelloWorldJob(), ctx, trigger); Thread.sleep( 1000 ); timeService.shutdown(); assertEquals( 5, ctx.getList().size() ); }
@Test public void testTimeMachine() { SessionConfiguration conf = SessionConfiguration.newInstance(); conf.setClockType( ClockType.PSEUDO_CLOCK ); WorkingMemory wm = (WorkingMemory) new KnowledgeBaseImpl("x", null).newKieSession(conf, null); final Calendar future = Calendar.getInstance(); ((PseudoClockScheduler)wm.getSessionClock()).setStartupTime( future.getTimeInMillis() ); final RuleImpl rule = new RuleImpl( "myrule" ); rule.setEnabled( EnabledBoolean.ENABLED_TRUE ); assertTrue( rule.isEffective(null, new RuleTerminalNode(), wm ) ); future.setTimeInMillis( future.getTimeInMillis() + 100000000 ); rule.setDateEffective(future); assertFalse( rule.isEffective(null, new RuleTerminalNode(), wm ) ); ((PseudoClockScheduler)wm.getSessionClock()).advanceTime( 1000000000000L, TimeUnit.MILLISECONDS ); assertTrue(rule.isEffective(null, new RuleTerminalNode(), wm )); }
@Test public void testCronTriggerJob() throws Exception { SessionConfiguration config = SessionConfiguration.newInstance(); config.setClockType( ClockType.PSEUDO_CLOCK ); PseudoClockScheduler timeService = (PseudoClockScheduler) TimerServiceFactory.getTimerService( config );
config.setClockType( ClockType.PSEUDO_CLOCK ); KieSession ksession = knowledgeBase.newKieSession( config, KieServices.get().newEnvironment() ); PseudoClockScheduler scheduler = (PseudoClockScheduler) ksession.<SessionClock> getSessionClock();
@Override public KieSession newDefaultKieSessionWithPseudoClock(final KieModule project) { KieContainer kieContainer = buildInfoService.getBuildInfo(project).getKieContainer(); //If a KieContainer could not be built there is a build error somewhere; so return null to be handled elsewhere if (kieContainer == null) { return null; } //We always need a pseudo clock final SessionConfiguration conf = SessionConfiguration.newInstance(); conf.setClockType(ClockType.PSEUDO_CLOCK); return kieContainer.getKieBase().newKieSession(conf, null); } }
config.setClockType( ClockType.PSEUDO_CLOCK ); StatefulKnowledgeSession ksession = knowledgeBase.newStatefulKnowledgeSession( config, KnowledgeBaseFactory.newEnvironment() ); PseudoClockScheduler scheduler = (PseudoClockScheduler) ksession.<SessionClock> getSessionClock();