public static StatefulKnowledgeSession deserializeKnowledgeSession(Marshaller marshaller, byte [] serializedKsession) throws Exception { ByteArrayInputStream bais = new ByteArrayInputStream( serializedKsession ); StatefulKnowledgeSession deserializedKsession = (StatefulKnowledgeSession) marshaller.unmarshall( bais, SessionConfiguration.newInstance(), EnvironmentFactory.newEnvironment() ); bais.close(); return deserializedKsession; }
@Override public ProcessInstance startProcess(String processId) { if (session == null) { // load the process KieBase kbase = createKnowledgeBase(); // create a new session Properties properties = new Properties(); properties.put("drools.processInstanceManagerFactory", "org.jbpm.process.instance.impl.DefaultProcessInstanceManagerFactory"); properties.put("drools.processSignalManagerFactory", "org.jbpm.process.instance.event.DefaultSignalManagerFactory"); SessionConfiguration config = SessionConfiguration.newInstance(properties); session = kbase.newKieSession(config, createEnvironment(context)); new JPAWorkingMemoryDbLogger(session); session.getWorkItemManager().registerWorkItemHandler("Human Task", new SystemOutWorkItemHandler()); } return session.startProcess(processId); }
@Override public ProcessInstance startProcess(String processName) { if( ksession == null ) { KieBase kbase = createKnowledgeBase(); Properties properties = new Properties(); properties.put("drools.processInstanceManagerFactory", "org.jbpm.persistence.processinstance.JPAProcessInstanceManagerFactory"); properties.put("drools.processSignalManagerFactory", "org.jbpm.persistence.processinstance.JPASignalManagerFactory"); KieSessionConfiguration conf = SessionConfiguration.newInstance(properties); Environment env = createEnvironment(context); env.set("IS_JTA_TRANSACTION", false); ksession = JPAKnowledgeService.newStatefulKnowledgeSession(kbase, conf, env); ksession.addEventListener(new JPAWorkingMemoryDbLogger(emf, env)); ksession.getWorkItemManager().registerWorkItemHandler("Human Task", new SystemOutWorkItemHandler()); } return ksession.startProcess(processName); }
JPASignalManagerFactory.class.getName() ); SessionConfiguration config = SessionConfiguration.newInstance( properties ); config.setOption( TimerJobFactoryOption.get(TimerJobFactoryType.JPA.getId()) );
defaultProps.setProperty("drools.processInstanceManagerFactory", DefaultProcessInstanceManagerFactory.class.getName()); conf = SessionConfiguration.newInstance(defaultProps); conf.setOption(ForceEagerActivationOption.YES); result = (StatefulKnowledgeSession) kbase.newKieSession(conf, env);
JPASignalManagerFactory.class.getName() ); SessionConfiguration config = SessionConfiguration.newInstance( properties ); config.setOption( TimerJobFactoryOption.get(TimerJobFactoryType.JPA.getId()) );
properties.setProperty( "drools.timerService", JpaJDKTimerService.class.getName() ); SessionConfiguration config = SessionConfiguration.newInstance( properties );
kbase.addPackages(Arrays.asList(builder.getPackages())); SessionConfiguration conf = SessionConfiguration.newInstance(); conf.setOption( ClockTypeOption.get( ClockType.PSEUDO_CLOCK.getId() ) );
properties.setProperty( "drools.timerService", JpaJDKTimerService.class.getName() ); SessionConfiguration config = SessionConfiguration.newInstance( properties );
properties.setProperty( "drools.timerService", JpaJDKTimerService.class.getName() ); SessionConfiguration config = SessionConfiguration.newInstance( properties );
@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() ); }
/** * This test can take a while (> 1 minute). * @throws Exception */ @Test @Ignore public void testStatefulSessionsCreation() throws Exception { KieBase kbase = loadKnowledgeBase("test_OutOfMemoryError.drl"); int i = 0; SessionConfiguration conf = SessionConfiguration.newInstance(); conf.setKeepReference( true ); // this is just for documentation purposes, since the default value is "true" try { for ( i = 0; i < 300000; i++ ) { KieSession ksession = kbase.newKieSession( conf, null ); ksession.dispose(); } } catch ( Throwable e ) { logger.info( "Error at: " + i ); e.printStackTrace(); fail( "Should not raise any error or exception." ); } }
@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 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() ); }
KieSessionConfiguration ksc = SessionConfiguration.newInstance();
KieSessionConfiguration ksc = SessionConfiguration.newInstance();
@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 );
SessionConfiguration config = SessionConfiguration.newInstance(); config.setClockType( ClockType.PSEUDO_CLOCK ); KieSession ksession = knowledgeBase.newKieSession( config, KieServices.get().newEnvironment() );
@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); } }