[jboss-svn-commits] JBL Code SVN: r34612 - in labs/jbossrules/branches/5_1_20100802_esteban_diega/drools-process/drools-persistent-timer: drools-timer-executor/src/main/java/org/drools/job/executor and 6 other directories.
jboss-svn-commits at lists.jboss.org
jboss-svn-commits at lists.jboss.org
Mon Aug 9 18:43:16 EDT 2010
Author: diegoll
Date: 2010-08-09 18:43:15 -0400 (Mon, 09 Aug 2010)
New Revision: 34612
Added:
labs/jbossrules/branches/5_1_20100802_esteban_diega/drools-process/drools-persistent-timer/drools-timer-executor/src/main/resources/executor.properties
labs/jbossrules/branches/5_1_20100802_esteban_diega/drools-process/drools-persistent-timer/drools-timer-executor/src/test/resources/META-INF/
labs/jbossrules/branches/5_1_20100802_esteban_diega/drools-process/drools-persistent-timer/drools-timer-executor/src/test/resources/META-INF/orm.xml
labs/jbossrules/branches/5_1_20100802_esteban_diega/drools-process/drools-persistent-timer/drools-timer-executor/src/test/resources/META-INF/persistence.xml
labs/jbossrules/branches/5_1_20100802_esteban_diega/drools-process/drools-persistent-timer/drools-timer-executor/src/test/resources/completedProcessSession.xml
labs/jbossrules/branches/5_1_20100802_esteban_diega/drools-process/drools-persistent-timer/drools-timer-executor/src/test/resources/mockContext.xml
labs/jbossrules/branches/5_1_20100802_esteban_diega/drools-process/drools-persistent-timer/drools-timer-executor/src/test/resources/persistentTimerProcess.rf
labs/jbossrules/branches/5_1_20100802_esteban_diega/drools-process/drools-persistent-timer/drools-timer-executor/src/test/resources/sessionContext.xml
labs/jbossrules/branches/5_1_20100802_esteban_diega/drools-process/drools-persistent-timer/drools-timer-executor/src/test/resources/shortProcess.rf
Modified:
labs/jbossrules/branches/5_1_20100802_esteban_diega/drools-process/drools-persistent-timer/drools-timer-executor/pom.xml
labs/jbossrules/branches/5_1_20100802_esteban_diega/drools-process/drools-persistent-timer/drools-timer-executor/src/main/java/org/drools/job/executor/JobExecutorService.java
labs/jbossrules/branches/5_1_20100802_esteban_diega/drools-process/drools-persistent-timer/drools-timer-executor/src/main/java/org/drools/job/executor/timer/TimerJobExecutor.java
labs/jbossrules/branches/5_1_20100802_esteban_diega/drools-process/drools-persistent-timer/drools-timer-executor/src/main/resources/executorContext.xml
labs/jbossrules/branches/5_1_20100802_esteban_diega/drools-process/drools-persistent-timer/drools-timer-executor/src/test/java/org/drools/job/executor/timer/TimerExecutorServiceTest.java
labs/jbossrules/branches/5_1_20100802_esteban_diega/drools-process/drools-persistent-timer/drools-timer-executor/src/test/resources/executorContextTest.xml
labs/jbossrules/branches/5_1_20100802_esteban_diega/drools-process/drools-persistent-timer/drools-timer-messages/src/main/java/org/drools/timer/message/TimerJob.java
Log:
[JBRULES-2616] added test for timer job executor, must polish TimerJob session revivification information
Modified: labs/jbossrules/branches/5_1_20100802_esteban_diega/drools-process/drools-persistent-timer/drools-timer-executor/pom.xml
===================================================================
--- labs/jbossrules/branches/5_1_20100802_esteban_diega/drools-process/drools-persistent-timer/drools-timer-executor/pom.xml 2010-08-09 22:40:43 UTC (rev 34611)
+++ labs/jbossrules/branches/5_1_20100802_esteban_diega/drools-process/drools-persistent-timer/drools-timer-executor/pom.xml 2010-08-09 22:43:15 UTC (rev 34612)
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
-xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
+ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<artifactId>drools-persistent-timer</artifactId>
@@ -10,13 +10,13 @@
<artifactId>drools-timer-executor</artifactId>
<packaging>jar</packaging>
<name>Drools :: Timers :: Executor</name>
-
+
<dependencies>
<dependency>
<groupId>org.drools</groupId>
<artifactId>drools-timer-messages</artifactId>
</dependency>
-
+
<!-- HornetQ dependencies -->
<dependency>
<groupId>org.hornetq</groupId>
@@ -33,10 +33,14 @@
<artifactId>netty</artifactId>
<version>${netty.version}</version>
</dependency>
-
-
+
+
<!-- Spring Dependencies -->
<dependency>
+ <groupId>org.drools</groupId>
+ <artifactId>drools-spring</artifactId>
+ </dependency>
+ <dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
</dependency>
@@ -48,7 +52,26 @@
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
</dependency>
-
+
+ <!-- persistence dependencies -->
+ <dependency>
+ <groupId>org.drools</groupId>
+ <artifactId>drools-persistence-jpa</artifactId>
+ </dependency>
+ <dependency>
+ <groupId>org.springframework</groupId>
+ <artifactId>spring-orm</artifactId>
+ </dependency>
+ <dependency>
+ <groupId>org.springframework</groupId>
+ <artifactId>spring-jdbc</artifactId>
+ </dependency>
+ <dependency>
+ <groupId>com.h2database</groupId>
+ <artifactId>h2</artifactId>
+ <scope>test</scope>
+ </dependency>
+
<!-- SL4J dependencies -->
<dependency>
<groupId>org.slf4j</groupId>
@@ -61,7 +84,7 @@
<version>1.4.2</version>
<scope>runtime</scope>
</dependency>
-
+
<!-- test dependencies -->
<dependency>
<groupId>junit</groupId>
@@ -70,5 +93,4 @@
<scope>test</scope>
</dependency>
</dependencies>
-
</project>
Modified: labs/jbossrules/branches/5_1_20100802_esteban_diega/drools-process/drools-persistent-timer/drools-timer-executor/src/main/java/org/drools/job/executor/JobExecutorService.java
===================================================================
--- labs/jbossrules/branches/5_1_20100802_esteban_diega/drools-process/drools-persistent-timer/drools-timer-executor/src/main/java/org/drools/job/executor/JobExecutorService.java 2010-08-09 22:40:43 UTC (rev 34611)
+++ labs/jbossrules/branches/5_1_20100802_esteban_diega/drools-process/drools-persistent-timer/drools-timer-executor/src/main/java/org/drools/job/executor/JobExecutorService.java 2010-08-09 22:43:15 UTC (rev 34612)
@@ -3,6 +3,7 @@
import java.util.TimerTask;
import java.util.concurrent.atomic.AtomicBoolean;
+import org.hornetq.api.core.HornetQException;
import org.hornetq.api.core.client.ClientConsumer;
import org.hornetq.api.core.client.ClientMessage;
import org.slf4j.Logger;
@@ -27,8 +28,12 @@
executor.execute(job);
}
}
- } catch (Exception e) {
+ } catch(HornetQException hqe) {
+ logger.error("HornetQ error code: "+hqe.getCode());
+ running.set(false);
+ }catch (Exception e) {
logger.error("Error running job executor service", e);
+ running.set(false);
}
}
Modified: labs/jbossrules/branches/5_1_20100802_esteban_diega/drools-process/drools-persistent-timer/drools-timer-executor/src/main/java/org/drools/job/executor/timer/TimerJobExecutor.java
===================================================================
--- labs/jbossrules/branches/5_1_20100802_esteban_diega/drools-process/drools-persistent-timer/drools-timer-executor/src/main/java/org/drools/job/executor/timer/TimerJobExecutor.java 2010-08-09 22:40:43 UTC (rev 34611)
+++ labs/jbossrules/branches/5_1_20100802_esteban_diega/drools-process/drools-persistent-timer/drools-timer-executor/src/main/java/org/drools/job/executor/timer/TimerJobExecutor.java 2010-08-09 22:43:15 UTC (rev 34612)
@@ -1,15 +1,70 @@
package org.drools.job.executor.timer;
+import org.drools.KnowledgeBase;
+import org.drools.KnowledgeBaseFactory;
+import org.drools.base.MapGlobalResolver;
+import org.drools.command.impl.CommandBasedStatefulKnowledgeSession;
+import org.drools.command.impl.KnowledgeCommandContext;
+import org.drools.impl.StatefulKnowledgeSessionImpl;
import org.drools.job.executor.JobExecutor;
+import org.drools.persistence.jpa.KnowledgeStoreService;
+import org.drools.process.instance.event.SignalManager;
+import org.drools.process.instance.timer.TimerInstance;
+import org.drools.runtime.Environment;
+import org.drools.runtime.EnvironmentName;
+import org.drools.runtime.StatefulKnowledgeSession;
+import org.drools.runtime.process.ProcessInstance;
import org.drools.timer.message.TimerJob;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
+import org.springframework.context.ApplicationContext;
+import org.springframework.context.support.ClassPathXmlApplicationContext;
+import org.springframework.transaction.PlatformTransactionManager;
public class TimerJobExecutor implements JobExecutor<TimerJob> {
private static Logger logger = LoggerFactory.getLogger(TimerJobExecutor.class);
+ private ApplicationContext sessionContext;
+
public void execute(TimerJob job) {
- logger.info("executing timer");
+ long processInstanceId = job.getProcessId();
+ logger.info("executing timer");
+ logger.debug("Process instance to signal: " + processInstanceId);
+ StatefulKnowledgeSession ksession = loadSession(job.getSessionId(), job.getKnowledgeStoreServiceBeanName(), getSessionContext());
+ ProcessInstance processInstance = ksession.getProcessInstance(processInstanceId);
+ if(null == processInstance){
+ throw new IllegalArgumentException("unexistent process instance with ID: " + processInstanceId);
+ }
+
+ StatefulKnowledgeSessionImpl sessionImpl = (StatefulKnowledgeSessionImpl) ((KnowledgeCommandContext) ((CommandBasedStatefulKnowledgeSession)ksession).getCommandService().getContext()).getStatefulKnowledgesession();
+ SignalManager signalManager = sessionImpl.getInternalWorkingMemory().getSignalManager();
+ TimerInstance timerInstance = null; //FIXME serialize timer instance information
+ signalManager.signalEvent( processInstanceId, "timerTriggered", timerInstance );
+ ksession.dispose();
+
}
+
+ public void setSessionContextLocation(String sessionContextLocation) {
+ logger.debug("creating session context at " + sessionContextLocation);
+ sessionContext = new ClassPathXmlApplicationContext(sessionContextLocation);
+ logger.debug("session context created");
+ }
+
+ private ApplicationContext getSessionContext(){
+ return sessionContext;
+ }
+
+ private StatefulKnowledgeSession loadSession(int sessionId, String kstoreName, ApplicationContext sessionContext) {
+ PlatformTransactionManager txManager = (PlatformTransactionManager) sessionContext.getBean( "transactionManager" );
+ Environment env = KnowledgeBaseFactory.newEnvironment();
+ env.set( EnvironmentName.ENTITY_MANAGER_FACTORY, sessionContext.getBean( "entityManagerFactory" ));
+ env.set( EnvironmentName.TRANSACTION_MANAGER, txManager);
+ env.set( EnvironmentName.GLOBALS, new MapGlobalResolver() );
+ KnowledgeStoreService kstore = (KnowledgeStoreService) sessionContext.getBean(kstoreName);
+ KnowledgeBase kbase = (KnowledgeBase) sessionContext.getBean("kbase");
+ StatefulKnowledgeSession ksession = kstore.loadStatefulKnowledgeSession(sessionId, kbase, null, env);
+ return ksession;
+ }
+
}
Added: labs/jbossrules/branches/5_1_20100802_esteban_diega/drools-process/drools-persistent-timer/drools-timer-executor/src/main/resources/executor.properties
===================================================================
--- labs/jbossrules/branches/5_1_20100802_esteban_diega/drools-process/drools-persistent-timer/drools-timer-executor/src/main/resources/executor.properties (rev 0)
+++ labs/jbossrules/branches/5_1_20100802_esteban_diega/drools-process/drools-persistent-timer/drools-timer-executor/src/main/resources/executor.properties 2010-08-09 22:43:15 UTC (rev 34612)
@@ -0,0 +1 @@
+sessioncontext.location=classpath:/dummy.xml
\ No newline at end of file
Property changes on: labs/jbossrules/branches/5_1_20100802_esteban_diega/drools-process/drools-persistent-timer/drools-timer-executor/src/main/resources/executor.properties
___________________________________________________________________
Name: svn:eol-style
+ native
Modified: labs/jbossrules/branches/5_1_20100802_esteban_diega/drools-process/drools-persistent-timer/drools-timer-executor/src/main/resources/executorContext.xml
===================================================================
--- labs/jbossrules/branches/5_1_20100802_esteban_diega/drools-process/drools-persistent-timer/drools-timer-executor/src/main/resources/executorContext.xml 2010-08-09 22:40:43 UTC (rev 34611)
+++ labs/jbossrules/branches/5_1_20100802_esteban_diega/drools-process/drools-persistent-timer/drools-timer-executor/src/main/resources/executorContext.xml 2010-08-09 22:43:15 UTC (rev 34612)
@@ -1,34 +1,32 @@
<?xml version="1.0" encoding="UTF-8"?>
-<beans
- xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xmlns:context="http://www.springframework.org/schema/context"
- xmlns:util="http://www.springframework.org/schema/util"
+<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xmlns:context="http://www.springframework.org/schema/context" xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
- http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd" >
+ http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd">
<bean id="timerJobExecutorService" class="org.drools.job.executor.JobExecutorService">
<property name="consumer">
- <bean id="timerMessagesConsumer" factory-bean="clientSession" factory-method="createConsumer" destroy-method="close">
- <constructor-arg value="executeTimerMessages"/>
+ <bean id="timerMessagesConsumer" factory-bean="clientSession" factory-method="createConsumer" destroy-method="close">
+ <constructor-arg value="executeTimerMessages" />
</bean>
- </property>
- <property name="translator" ref="messageTranslator"/>
- <property name="executor">
- <bean class="org.drools.job.executor.timer.TimerJobExecutor"/>
- </property>
+ </property>
+ <property name="translator" ref="messageTranslator" />
+ <property name="executor">
+ <bean class="org.drools.job.executor.timer.TimerJobExecutor">
+ <property name="sessionContextLocation" value="${sessioncontext.location}" />
+ </bean>
+ </property>
</bean>
-
+
<bean id="messageTranslator" class="org.drools.job.executor.utils.SerializedMessageTranslator">
- <property name="session" ref="clientSession"/>
+ <property name="session" ref="clientSession" />
</bean>
-
-
<!-- client connection with the HornetQ server -->
- <bean id="clientSession" factory-bean="clientSessionFactory" factory-method="createSession" init-method="start"/>
- <bean id="clientSessionFactory" class="org.hornetq.api.core.client.HornetQClient" factory-method="createClientSessionFactory" depends-on="hornetQServer" >
+ <bean id="clientSession" factory-bean="clientSessionFactory" factory-method="createSession" init-method="start" />
+ <bean id="clientSessionFactory" class="org.hornetq.api.core.client.HornetQClient" factory-method="createClientSessionFactory"
+ depends-on="hornetQServer">
<constructor-arg>
<bean class="org.hornetq.api.core.TransportConfiguration">
<constructor-arg>
@@ -37,29 +35,39 @@
<constructor-arg>
<map>
<entry>
- <key><util:constant static-field="org.hornetq.integration.transports.netty.TransportConstants.PORT_PROP_NAME"/></key>
+ <key>
+ <util:constant static-field="org.hornetq.integration.transports.netty.TransportConstants.PORT_PROP_NAME" />
+ </key>
<value>5446</value>
</entry>
</map>
</constructor-arg>
</bean>
- </constructor-arg>
+ </constructor-arg>
</bean>
+ <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
+ <property name="location" value="classpath:executor.properties" />
+ </bean>
+
+
<!-- start HornetQ server -->
- <bean id="hornetQServer" class="org.hornetq.core.server.HornetQServers" factory-method="newHornetQServer" init-method="start" destroy-method="stop">
+ <bean id="hornetQServer" class="org.hornetq.core.server.HornetQServers" factory-method="newHornetQServer"
+ init-method="start" destroy-method="stop">
<constructor-arg>
<bean class="org.hornetq.core.config.impl.ConfigurationImpl">
- <property name="persistenceEnabled" value="false"/>
- <property name="securityEnabled" value="false"/>
- <property name="clustered" value="false"/>
+ <property name="persistenceEnabled" value="false" />
+ <property name="securityEnabled" value="false" />
+ <property name="clustered" value="false" />
<property name="queueConfigurations">
<list>
<bean class="org.hornetq.core.server.cluster.QueueConfiguration">
- <constructor-arg name="address" value="executeTimerMessages"/>
- <constructor-arg name="name" value="executeTimerMessages"/>
- <constructor-arg name="filterString"><null/></constructor-arg>
- <constructor-arg name="durable" value="true"/>
+ <constructor-arg name="address" value="executeTimerMessages" />
+ <constructor-arg name="name" value="executeTimerMessages" />
+ <constructor-arg name="filterString">
+ <null />
+ </constructor-arg>
+ <constructor-arg name="durable" value="true" />
</bean>
</list>
</property>
@@ -73,7 +81,7 @@
<map>
<entry>
<key>
- <util:constant static-field="org.hornetq.integration.transports.netty.TransportConstants.PORT_PROP_NAME"/>
+ <util:constant static-field="org.hornetq.integration.transports.netty.TransportConstants.PORT_PROP_NAME" />
</key>
<value>5446</value>
</entry>
Modified: labs/jbossrules/branches/5_1_20100802_esteban_diega/drools-process/drools-persistent-timer/drools-timer-executor/src/test/java/org/drools/job/executor/timer/TimerExecutorServiceTest.java
===================================================================
--- labs/jbossrules/branches/5_1_20100802_esteban_diega/drools-process/drools-persistent-timer/drools-timer-executor/src/test/java/org/drools/job/executor/timer/TimerExecutorServiceTest.java 2010-08-09 22:40:43 UTC (rev 34611)
+++ labs/jbossrules/branches/5_1_20100802_esteban_diega/drools-process/drools-persistent-timer/drools-timer-executor/src/test/java/org/drools/job/executor/timer/TimerExecutorServiceTest.java 2010-08-09 22:43:15 UTC (rev 34612)
@@ -1,40 +1,80 @@
package org.drools.job.executor.timer;
+import java.util.Properties;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
+import org.drools.KnowledgeBase;
+import org.drools.KnowledgeBaseFactory;
+import org.drools.base.MapGlobalResolver;
+import org.drools.core.util.KnowledgeSessionRuntimeConfigurator;
import org.drools.job.executor.JobExecutorService;
import org.drools.job.executor.utils.SerializedMessageTranslator;
+import org.drools.persistence.jpa.KnowledgeStoreService;
+import org.drools.runtime.Environment;
+import org.drools.runtime.EnvironmentName;
+import org.drools.runtime.StatefulKnowledgeSession;
import org.drools.timer.message.TimerJob;
+import org.h2.tools.DeleteDbFiles;
+import org.h2.tools.Server;
import org.hornetq.api.core.HornetQException;
import org.hornetq.api.core.client.ClientProducer;
+import org.junit.AfterClass;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;
-import org.springframework.context.ApplicationContext;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
+import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
-import org.springframework.core.task.TaskExecutor;
+import org.springframework.transaction.PlatformTransactionManager;
+import org.springframework.transaction.TransactionStatus;
+import org.springframework.transaction.support.TransactionCallback;
+import org.springframework.transaction.support.TransactionTemplate;
public class TimerExecutorServiceTest {
- private static ApplicationContext context;
+ private static final Logger logger = LoggerFactory.getLogger(TimerExecutorServiceTest.class);
+ private static final String H2_DB_NAME = "persistentTimersTest";
+
+ private static Server h2Server;
- @BeforeClass
- public static void loadContext(){
- context = new ClassPathXmlApplicationContext(new String[]{
- "classpath:/executorContext.xml",
- "classpath:/executorContextTest.xml",
- });
- }
-
+ @BeforeClass
+ public static void startH2Database() throws Exception {
+ DeleteDbFiles.execute("", H2_DB_NAME, true);
+ h2Server = Server.createTcpServer(new String[0]);
+ h2Server.start();
+ }
+
+ @AfterClass
+ public static void stopH2Database() throws Exception {
+ logger .info( "stoping database" );
+ h2Server.stop();
+ DeleteDbFiles.execute( "", H2_DB_NAME, true );
+ }
+
@Test
@SuppressWarnings("unchecked")
public void triggerTimerJob() throws HornetQException, InterruptedException{
+ PropertyPlaceholderConfigurer configurer = new PropertyPlaceholderConfigurer();
+ Properties properties = new Properties();
+ properties.setProperty( "sessioncontext.location", "classpath:/mockContext.xml" );
+ configurer.setProperties( properties );
+
+ ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext();
+ context.addBeanFactoryPostProcessor(configurer);
+ context.setConfigLocations(new String[]{
+ "classpath:/executorContext.xml",
+ "classpath:/executorContextTest.xml",
+ });
+ context.refresh();
+
//start task service
JobExecutorService<TimerJob> timerJobExecutorService = (JobExecutorService<TimerJob>) context.getBean("timerJobExecutorService");
ExecutorService executor = Executors.newSingleThreadExecutor();
executor.execute(timerJobExecutorService);
- Thread.sleep(500);
+ Thread.sleep(1500); //executor doesn't guarantee you it'll run immediately
Assert.assertTrue(timerJobExecutorService.isRunning());
//create timer job to consume
@@ -44,5 +84,82 @@
producer.send(timerJobMessageTranslator.writeMessage(timerJob));
//check timer succesfully executed
+ timerJobExecutorService.cancel();
+ context.destroy();
}
+
+ @Test
+ public void checkTriggerCompletedProcess() throws InterruptedException, HornetQException{
+ String sessionContextLocation = "classpath:/completedProcessSession.xml";
+ String sessionConfiguratorBeanName = "sessionConfigurator";
+ String knowledgeStoreServiceBeanName = "kstore";
+
+ //start the executor
+ AbstractApplicationContext executorContext = createExecutorContext(sessionContextLocation);
+ JobExecutorService<TimerJob> timerJobExecutorService = (JobExecutorService<TimerJob>) executorContext.getBean("timerJobExecutorService");
+ ExecutorService executor = Executors.newSingleThreadExecutor();
+ executor.execute(timerJobExecutorService);
+ Thread.sleep(1500); //executor doesn't guarantee you it'll run immediately
+ Assert.assertTrue(timerJobExecutorService.isRunning());
+
+ //start process
+ AbstractApplicationContext sessionContext = new ClassPathXmlApplicationContext(sessionContextLocation);
+ final StatefulKnowledgeSession ksession = createSession(knowledgeStoreServiceBeanName, sessionContext);
+ KnowledgeSessionRuntimeConfigurator sessionConfigurator = (KnowledgeSessionRuntimeConfigurator) sessionContext.getBean(sessionConfiguratorBeanName);
+ sessionConfigurator.configure(ksession);
+
+ PlatformTransactionManager txManager = (PlatformTransactionManager) sessionContext.getBean("transactionManager");
+ TransactionTemplate txTemplate = new TransactionTemplate(txManager);
+ Long processId = (Long) txTemplate.execute(new TransactionCallback() {
+ public Object doInTransaction(TransactionStatus status) {
+ long processId = ksession.startProcess("shortProcess").getId();
+ ksession.dispose();
+ return processId;
+ }
+ });
+
+ //send message to executor (emulates scheduler)
+ ClientProducer producer = (ClientProducer)executorContext.getBean("timerMessageProducer");
+ SerializedMessageTranslator<TimerJob> timerJobMessageTranslator = (SerializedMessageTranslator<TimerJob>) executorContext.getBean("messageTranslator");
+ TimerJob timerJob = new TimerJob();
+ timerJob.setSessionId(ksession.getId());
+ timerJob.setProcessId(processId);
+ timerJob.setKnowledgeStoreServiceBeanName(knowledgeStoreServiceBeanName);
+ timerJob.setRuntimeSessionConfiguratorBeanName(sessionConfiguratorBeanName);
+ producer.send(timerJobMessageTranslator.writeMessage(timerJob));
+
+ //close everything
+ timerJobExecutorService.cancel();
+ producer.close();
+ sessionContext.close();
+ executorContext.close();
+ }
+
+ private AbstractApplicationContext createExecutorContext(String sessionContextLocation) {
+ PropertyPlaceholderConfigurer configurer = new PropertyPlaceholderConfigurer();
+ Properties properties = new Properties();
+ properties.setProperty( "sessioncontext.location", sessionContextLocation );
+ configurer.setProperties( properties );
+ ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext();
+ context.addBeanFactoryPostProcessor(configurer);
+ context.setConfigLocations(new String[]{
+ "classpath:/executorContext.xml",
+ "classpath:/executorContextTest.xml",
+ });
+ context.refresh();
+
+ return context;
+ }
+
+ private StatefulKnowledgeSession createSession(String kstoreName, AbstractApplicationContext sessionContext) {
+ PlatformTransactionManager txManager = (PlatformTransactionManager) sessionContext.getBean( "transactionManager" );
+ Environment env = KnowledgeBaseFactory.newEnvironment();
+ env.set( EnvironmentName.ENTITY_MANAGER_FACTORY, sessionContext.getBean( "entityManagerFactory" ));
+ env.set( EnvironmentName.TRANSACTION_MANAGER, txManager);
+ env.set( EnvironmentName.GLOBALS, new MapGlobalResolver() );
+ KnowledgeStoreService kstore = (KnowledgeStoreService) sessionContext.getBean(kstoreName);
+ KnowledgeBase kbase = (KnowledgeBase) sessionContext.getBean("kbase");
+ StatefulKnowledgeSession ksession = kstore.newStatefulKnowledgeSession(kbase, null, env);
+ return ksession;
+ }
}
Added: labs/jbossrules/branches/5_1_20100802_esteban_diega/drools-process/drools-persistent-timer/drools-timer-executor/src/test/resources/META-INF/orm.xml
===================================================================
--- labs/jbossrules/branches/5_1_20100802_esteban_diega/drools-process/drools-persistent-timer/drools-timer-executor/src/test/resources/META-INF/orm.xml (rev 0)
+++ labs/jbossrules/branches/5_1_20100802_esteban_diega/drools-process/drools-persistent-timer/drools-timer-executor/src/test/resources/META-INF/orm.xml 2010-08-09 22:43:15 UTC (rev 34612)
@@ -0,0 +1,16 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<entity-mappings xmlns="http://java.sun.com/xml/ns/persistence/orm"
+ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xsi:schemaLocation="http://java.sun.com/xml/ns/persistence/orm orm_1_0.xsd"
+ version="1.0">
+ <named-query name="ProcessInstancesWaitingForEvent">
+ <query>
+select
+ processInstanceInfo.processInstanceId
+from
+ ProcessInstanceInfo processInstanceInfo
+where
+ :type in elements(processInstanceInfo.eventTypes)
+ </query>
+ </named-query>
+</entity-mappings>
Property changes on: labs/jbossrules/branches/5_1_20100802_esteban_diega/drools-process/drools-persistent-timer/drools-timer-executor/src/test/resources/META-INF/orm.xml
___________________________________________________________________
Name: svn:eol-style
+ native
Added: labs/jbossrules/branches/5_1_20100802_esteban_diega/drools-process/drools-persistent-timer/drools-timer-executor/src/test/resources/META-INF/persistence.xml
===================================================================
--- labs/jbossrules/branches/5_1_20100802_esteban_diega/drools-process/drools-persistent-timer/drools-timer-executor/src/test/resources/META-INF/persistence.xml (rev 0)
+++ labs/jbossrules/branches/5_1_20100802_esteban_diega/drools-process/drools-persistent-timer/drools-timer-executor/src/test/resources/META-INF/persistence.xml 2010-08-09 22:43:15 UTC (rev 34612)
@@ -0,0 +1,36 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<persistence version="1.0" xmlns="http://java.sun.com/xml/ns/persistence"
+ xmlns:orm="http://java.sun.com/xml/ns/persistence/orm"
+ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xsi:schemaLocation="
+ http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd
+ http://java.sun.com/xml/ns/persistence/orm http://java.sun.com/xml/ns/persistence/orm_1_0.xsd">
+
+ <persistence-unit name="org.drools.persistence.jpa.local" transaction-type="RESOURCE_LOCAL">
+ <provider>org.hibernate.ejb.HibernatePersistence</provider>
+ <!--
+ <jta-data-source>jdbc/testDS1</jta-data-source>
+ -->
+ <class>org.drools.persistence.session.SessionInfo</class>
+ <class>org.drools.persistence.processinstance.ProcessInstanceInfo</class>
+ <class>org.drools.persistence.processinstance.variabletypes.JPAPersistedVariable</class>
+ <class>org.drools.persistence.processinstance.variabletypes.VariableInstanceInfo</class>
+ <class>org.drools.persistence.processinstance.variabletypes.SerializablePersistedVariable</class>
+ <class>org.drools.persistence.processinstance.WorkItemInfo</class>
+
+ <properties>
+ <property name="hibernate.dialect" value="org.hibernate.dialect.H2Dialect" />
+ <!--property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect"/-->
+ <!--property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect"/-->
+
+ <property name="hibernate.max_fetch_depth" value="3" />
+ <property name="hibernate.hbm2ddl.auto" value="create" />
+ <property name="hibernate.show_sql" value="true" />
+
+ <!--
+ <property name="hibernate.connection.autocommit" value="false" />
+ <property name="hibernate.transaction.manager_lookup_class" value="org.hibernate.transaction.BTMTransactionManagerLookup" />
+ -->
+ </properties>
+ </persistence-unit>
+</persistence>
Property changes on: labs/jbossrules/branches/5_1_20100802_esteban_diega/drools-process/drools-persistent-timer/drools-timer-executor/src/test/resources/META-INF/persistence.xml
___________________________________________________________________
Name: svn:eol-style
+ native
Added: labs/jbossrules/branches/5_1_20100802_esteban_diega/drools-process/drools-persistent-timer/drools-timer-executor/src/test/resources/completedProcessSession.xml
===================================================================
--- labs/jbossrules/branches/5_1_20100802_esteban_diega/drools-process/drools-persistent-timer/drools-timer-executor/src/test/resources/completedProcessSession.xml (rev 0)
+++ labs/jbossrules/branches/5_1_20100802_esteban_diega/drools-process/drools-persistent-timer/drools-timer-executor/src/test/resources/completedProcessSession.xml 2010-08-09 22:43:15 UTC (rev 34612)
@@ -0,0 +1,50 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<beans
+ xmlns="http://www.springframework.org/schema/beans"
+ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
+ xmlns:util="http://www.springframework.org/schema/util"
+ xmlns:drools="http://drools.org/schema/drools-spring"
+ xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
+ http://drools.org/schema/drools-spring http://drools.org/schema/drools-spring.xsd">
+
+
+ <!-- drools configuration -->
+
+ <bean id="sessionConfigurator" class="org.drools.core.util.KnowledgeSessionRuntimeConfiguratorImpl">
+ <property name="workItemHandlers">
+ <map>
+ <entry key="nothing">
+ <bean class="org.drools.process.instance.impl.demo.DoNothingWorkItemHandler" />
+ </entry>
+ </map>
+ </property>
+ </bean>
+
+ <drools:kstore id="kstore" />
+
+ <drools:connection id="connection1" type="local" />
+ <drools:execution-node id="node1" connection="connection1" />
+
+ <drools:kbase id="kbase">
+ <drools:resources>
+ <drools:resource type="DRF" source="classpath:shortProcess.rf" />
+ </drools:resources>
+ </drools:kbase>
+
+ <!-- persistence configuration -->
+ <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
+ <property name="driverClassName" value="org.h2.Driver" />
+ <property name="url" value="jdbc:h2:tcp://localhost/persistentTimersTest" />
+ <property name="username" value="sa" />
+ <property name="password" value="" />
+ </bean>
+
+ <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
+ <property name="dataSource" ref="dataSource" />
+ <property name="persistenceUnitName" value="org.drools.persistence.jpa.local" />
+ </bean>
+
+ <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
+ <property name="entityManagerFactory" ref="entityManagerFactory" />
+ </bean>
+</beans>
\ No newline at end of file
Property changes on: labs/jbossrules/branches/5_1_20100802_esteban_diega/drools-process/drools-persistent-timer/drools-timer-executor/src/test/resources/completedProcessSession.xml
___________________________________________________________________
Name: svn:eol-style
+ native
Modified: labs/jbossrules/branches/5_1_20100802_esteban_diega/drools-process/drools-persistent-timer/drools-timer-executor/src/test/resources/executorContextTest.xml
===================================================================
--- labs/jbossrules/branches/5_1_20100802_esteban_diega/drools-process/drools-persistent-timer/drools-timer-executor/src/test/resources/executorContextTest.xml 2010-08-09 22:40:43 UTC (rev 34611)
+++ labs/jbossrules/branches/5_1_20100802_esteban_diega/drools-process/drools-persistent-timer/drools-timer-executor/src/test/resources/executorContextTest.xml 2010-08-09 22:43:15 UTC (rev 34612)
@@ -1,13 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
-<beans
- xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xmlns:context="http://www.springframework.org/schema/context"
- xmlns:util="http://www.springframework.org/schema/util"
- xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
- http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
- http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd" >
-
+<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
+
<bean id="timerMessageProducer" factory-bean="clientSession" factory-method="createProducer" destroy-method="close">
<constructor-arg value="executeTimerMessages" />
</bean>
Added: labs/jbossrules/branches/5_1_20100802_esteban_diega/drools-process/drools-persistent-timer/drools-timer-executor/src/test/resources/mockContext.xml
===================================================================
--- labs/jbossrules/branches/5_1_20100802_esteban_diega/drools-process/drools-persistent-timer/drools-timer-executor/src/test/resources/mockContext.xml (rev 0)
+++ labs/jbossrules/branches/5_1_20100802_esteban_diega/drools-process/drools-persistent-timer/drools-timer-executor/src/test/resources/mockContext.xml 2010-08-09 22:43:15 UTC (rev 34612)
@@ -0,0 +1,8 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<beans
+ xmlns="http://www.springframework.org/schema/beans"
+ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
+ xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
+
+ <bean id="ksession" class="java.lang.Object"></bean>
+</beans>
\ No newline at end of file
Property changes on: labs/jbossrules/branches/5_1_20100802_esteban_diega/drools-process/drools-persistent-timer/drools-timer-executor/src/test/resources/mockContext.xml
___________________________________________________________________
Name: svn:eol-style
+ native
Added: labs/jbossrules/branches/5_1_20100802_esteban_diega/drools-process/drools-persistent-timer/drools-timer-executor/src/test/resources/persistentTimerProcess.rf
===================================================================
--- labs/jbossrules/branches/5_1_20100802_esteban_diega/drools-process/drools-persistent-timer/drools-timer-executor/src/test/resources/persistentTimerProcess.rf (rev 0)
+++ labs/jbossrules/branches/5_1_20100802_esteban_diega/drools-process/drools-persistent-timer/drools-timer-executor/src/test/resources/persistentTimerProcess.rf 2010-08-09 22:43:15 UTC (rev 34612)
@@ -0,0 +1,23 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<process xmlns="http://drools.org/drools-5.0/process"
+ xmlns:xs="http://www.w3.org/2001/XMLSchema-instance"
+ xs:schemaLocation="http://drools.org/drools-5.0/process drools-processes-5.0.xsd"
+ type="RuleFlow" name="PersistentTimer" id="org.drools.timer.PersistentTimer" package-name="org.drools.timer" >
+
+ <header>
+ </header>
+
+ <nodes>
+ <start id="1" name="Start" x="72" y="108" width="48" height="48" />
+ <actionNode id="2" name="Script" x="180" y="108" width="80" height="48" >
+ <action type="expression" dialect="java" >System.out.println("capo");</action>
+ </actionNode>
+ <end id="3" name="End" x="324" y="108" width="48" height="48" />
+ </nodes>
+
+ <connections>
+ <connection from="1" to="2" />
+ <connection from="2" to="3" />
+ </connections>
+
+</process>
\ No newline at end of file
Property changes on: labs/jbossrules/branches/5_1_20100802_esteban_diega/drools-process/drools-persistent-timer/drools-timer-executor/src/test/resources/persistentTimerProcess.rf
___________________________________________________________________
Name: svn:eol-style
+ native
Added: labs/jbossrules/branches/5_1_20100802_esteban_diega/drools-process/drools-persistent-timer/drools-timer-executor/src/test/resources/sessionContext.xml
===================================================================
--- labs/jbossrules/branches/5_1_20100802_esteban_diega/drools-process/drools-persistent-timer/drools-timer-executor/src/test/resources/sessionContext.xml (rev 0)
+++ labs/jbossrules/branches/5_1_20100802_esteban_diega/drools-process/drools-persistent-timer/drools-timer-executor/src/test/resources/sessionContext.xml 2010-08-09 22:43:15 UTC (rev 34612)
@@ -0,0 +1,48 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<beans
+ xmlns="http://www.springframework.org/schema/beans"
+ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
+ xmlns:util="http://www.springframework.org/schema/util"
+ xmlns:drools="http://drools.org/schema/drools-spring"
+ xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
+ http://drools.org/schema/drools-spring http://drools.org/schema/drools-spring.xsd">
+
+
+ <!-- drools configuration -->
+
+ <drools:connection id="connection1" type="local" />
+ <drools:execution-node id="node1" connection="connection1" />
+ <drools:kstore id="kstore" />
+
+ <drools:kbase id="kbase">
+ <drools:resources>
+ <drools:resource type="DRF" source="classpath:persistentTimerProcess.rf" />
+ </drools:resources>
+ </drools:kbase>
+
+ <drools:ksession id="ksession" type="stateful" kbase="kbase">
+ <drools:configuration>
+ <drools:jpa-persistence>
+ <drools:transaction-manager ref="transactionManager" />
+ <drools:entity-manager-factory ref="entityManagerFactory" />
+ </drools:jpa-persistence>
+ </drools:configuration>
+ </drools:ksession>
+
+ <!-- persistence configuration -->
+ <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
+ <property name="driverClassName" value="org.h2.Driver" />
+ <property name="url" value="jdbc:h2:tcp://localhost/persistentTimersTest" />
+ <property name="username" value="sa" />
+ <property name="password" value="" />
+ </bean>
+
+ <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
+ <property name="dataSource" ref="dataSource" />
+ <property name="persistenceUnitName" value="org.drools.persistence.jpa.local" />
+ </bean>
+
+ <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
+ <property name="entityManagerFactory" ref="entityManagerFactory" />
+ </bean>
+</beans>
\ No newline at end of file
Property changes on: labs/jbossrules/branches/5_1_20100802_esteban_diega/drools-process/drools-persistent-timer/drools-timer-executor/src/test/resources/sessionContext.xml
___________________________________________________________________
Name: svn:eol-style
+ native
Added: labs/jbossrules/branches/5_1_20100802_esteban_diega/drools-process/drools-persistent-timer/drools-timer-executor/src/test/resources/shortProcess.rf
===================================================================
--- labs/jbossrules/branches/5_1_20100802_esteban_diega/drools-process/drools-persistent-timer/drools-timer-executor/src/test/resources/shortProcess.rf (rev 0)
+++ labs/jbossrules/branches/5_1_20100802_esteban_diega/drools-process/drools-persistent-timer/drools-timer-executor/src/test/resources/shortProcess.rf 2010-08-09 22:43:15 UTC (rev 34612)
@@ -0,0 +1,19 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<process xmlns="http://drools.org/drools-5.0/process"
+ xmlns:xs="http://www.w3.org/2001/XMLSchema-instance"
+ xs:schemaLocation="http://drools.org/drools-5.0/process drools-processes-5.0.xsd"
+ type="RuleFlow" name="org.drools.timer.ShortProcess" id="shortProcess" package-name="org.drools.timer" >
+
+ <header>
+ </header>
+
+ <nodes>
+ <start id="1" name="Start" x="96" y="96" width="48" height="48" />
+ <end id="2" name="End" x="204" y="96" width="48" height="48" />
+ </nodes>
+
+ <connections>
+ <connection from="1" to="2" />
+ </connections>
+
+</process>
\ No newline at end of file
Property changes on: labs/jbossrules/branches/5_1_20100802_esteban_diega/drools-process/drools-persistent-timer/drools-timer-executor/src/test/resources/shortProcess.rf
___________________________________________________________________
Name: svn:eol-style
+ native
Modified: labs/jbossrules/branches/5_1_20100802_esteban_diega/drools-process/drools-persistent-timer/drools-timer-messages/src/main/java/org/drools/timer/message/TimerJob.java
===================================================================
--- labs/jbossrules/branches/5_1_20100802_esteban_diega/drools-process/drools-persistent-timer/drools-timer-messages/src/main/java/org/drools/timer/message/TimerJob.java 2010-08-09 22:40:43 UTC (rev 34611)
+++ labs/jbossrules/branches/5_1_20100802_esteban_diega/drools-process/drools-persistent-timer/drools-timer-messages/src/main/java/org/drools/timer/message/TimerJob.java 2010-08-09 22:43:15 UTC (rev 34612)
@@ -3,5 +3,41 @@
import java.io.Serializable;
public class TimerJob implements Serializable{
+
+ private long processId;
+ private String runtimeSessionConfiguratorBeanName;
+ private String knowledgeStoreServiceBeanName;
+ private int sessionId;
+ public void setProcessId(long processId) {
+ this.processId = processId;
+ }
+
+ public long getProcessId() {
+ return processId;
+ }
+
+ public void setRuntimeSessionConfiguratorBeanName(String sessionConfiguratorBeanName) {
+ this.runtimeSessionConfiguratorBeanName = sessionConfiguratorBeanName;
+ }
+
+ public String getRuntimeSessionConfiguratorBeanName() {
+ return runtimeSessionConfiguratorBeanName;
+ }
+
+ public void setKnowledgeStoreServiceBeanName(String knowledgeStoreServiceBeanName) {
+ this.knowledgeStoreServiceBeanName = knowledgeStoreServiceBeanName;
+ }
+
+ public String getKnowledgeStoreServiceBeanName() {
+ return knowledgeStoreServiceBeanName;
+ }
+
+ public void setSessionId(int id) {
+ this.sessionId = id;
+ }
+
+ public int getSessionId() {
+ return sessionId;
+ }
}
More information about the jboss-svn-commits
mailing list