public void initTransactionManager(Environment env) {
    Object tm = env.get(EnvironmentName.TRANSACTION_MANAGER);
    if (env.get(EnvironmentName.PERSISTENCE_CONTEXT_MANAGER) != null
        && env.get(EnvironmentName.TRANSACTION_MANAGER) != null) {
      this.txm = (TransactionManager) tm;
      this.jpm = (PersistenceContextManager) env.get(EnvironmentName.PERSISTENCE_CONTEXT_MANAGER);
    } else {
      if (tm != null && tm.getClass().getName().startsWith("org.springframework")) {
        try {
          Class<?> cls =
              Class.forName(
                  "org.drools.container.spring.beans.persistence.DroolsSpringTransactionManager");
          Constructor<?> con = cls.getConstructors()[0];
          this.txm = (TransactionManager) con.newInstance(tm);
          logger.debug("Instantiating  DroolsSpringTransactionManager");

          //                    if ( tm.getClass().getName().toLowerCase().contains( "jpa" ) ) {
          // configure spring for JPA and local transactions
          cls =
              Class.forName("org.drools.container.spring.beans.persistence.DroolsSpringJpaManager");
          con = cls.getConstructors()[0];
          this.jpm = (PersistenceContextManager) con.newInstance(new Object[] {this.env});
          //                    } else {
          //                        // configure spring for JPA and distributed transactions
          //                    }
        } catch (Exception e) {
          logger.warn("Could not instatiate DroolsSpringTransactionManager");
          throw new RuntimeException(
              "Could not instatiate org.drools.container.spring.beans.persistence.DroolsSpringTransactionManager",
              e);
        }
      } else {
        logger.debug("Instantiating  JtaTransactionManager");
        this.txm =
            new JtaTransactionManager(
                env.get(EnvironmentName.TRANSACTION),
                env.get(EnvironmentName.TRANSACTION_SYNCHRONIZATION_REGISTRY),
                tm);
        try {
          Class<?> jpaPersistenceCtxMngrClass =
              Class.forName("org.jbpm.persistence.JpaProcessPersistenceContextManager");
          Constructor<?> jpaPersistenceCtxMngrCtor =
              jpaPersistenceCtxMngrClass.getConstructors()[0];
          this.jpm =
              (PersistenceContextManager)
                  jpaPersistenceCtxMngrCtor.newInstance(new Object[] {this.env});
        } catch (ClassNotFoundException e) {
          this.jpm = new JpaPersistenceContextManager(this.env);
        } catch (Exception e) {
          throw new RuntimeException("Error creating JpaProcessPersistenceContextManager", e);
        }
      }
      env.set(EnvironmentName.PERSISTENCE_CONTEXT_MANAGER, this.jpm);
      env.set(EnvironmentName.TRANSACTION_MANAGER, this.txm);
    }
  }
 public void checkEnvironment(Environment env) {
   if (env.get(EnvironmentName.ENTITY_MANAGER_FACTORY) == null
       && env.get(EnvironmentName.PERSISTENCE_CONTEXT_MANAGER) == null) {
     throw new IllegalArgumentException(
         "Environment must have an EntityManagerFactory "
             + "or a PersistenceContextManager instance");
   }
 }
 private Environment createEnvironment() {
   Environment env = KnowledgeBaseFactory.newEnvironment();
   env.set(EnvironmentName.ENTITY_MANAGER_FACTORY, emf);
   // env.set(EnvironmentName.TRANSACTION_MANAGER,
   // TransactionManagerServices.getTransactionManager());
   env.set(EnvironmentName.GLOBALS, new MapGlobalResolver());
   return env;
 }
  public void testLogger1() throws Exception {

    // load the process
    KnowledgeBase kbase = createKnowledgeBase();
    // create a new session
    Environment env = KnowledgeBaseFactory.newEnvironment();
    env.set(EnvironmentName.ENTITY_MANAGER_FACTORY, emf);
    env.set(
        EnvironmentName.TRANSACTION_MANAGER, TransactionManagerServices.getTransactionManager());
    Properties properties = new Properties();
    properties.put(
        "drools.processInstanceManagerFactory",
        "org.jbpm.persistence.processinstance.JPAProcessInstanceManagerFactory");
    properties.put(
        "drools.processSignalManagerFactory",
        "org.jbpm.persistence.processinstance.JPASignalManagerFactory");
    KnowledgeSessionConfiguration config =
        KnowledgeBaseFactory.newKnowledgeSessionConfiguration(properties);
    StatefulKnowledgeSession session =
        JPAKnowledgeService.newStatefulKnowledgeSession(kbase, config, env);
    new JPAWorkingMemoryDbLogger(session);
    JPAProcessInstanceDbLog log = new JPAProcessInstanceDbLog(env);
    session
        .getWorkItemManager()
        .registerWorkItemHandler("Human Task", new SystemOutWorkItemHandler());

    // record the initial count to compare to later
    List<ProcessInstanceLog> processInstances = log.findProcessInstances("com.sample.ruleflow");
    int initialProcessInstanceSize = processInstances.size();

    // start process instance
    long processInstanceId = session.startProcess("com.sample.ruleflow").getId();

    System.out.println("Checking process instances for process 'com.sample.ruleflow'");
    processInstances = log.findProcessInstances("com.sample.ruleflow");
    assertEquals(initialProcessInstanceSize + 1, processInstances.size());
    ProcessInstanceLog processInstance = processInstances.get(0);
    System.out.print(processInstance);
    System.out.println(" -> " + processInstance.getStart() + " - " + processInstance.getEnd());
    assertNotNull(processInstance.getStart());
    assertNotNull(processInstance.getEnd());
    assertEquals(processInstanceId, processInstance.getProcessInstanceId());
    assertEquals("com.sample.ruleflow", processInstance.getProcessId());
    List<NodeInstanceLog> nodeInstances = log.findNodeInstances(processInstanceId);
    assertEquals(6, nodeInstances.size());
    for (NodeInstanceLog nodeInstance : nodeInstances) {
      System.out.println(nodeInstance);
      assertEquals(processInstanceId, processInstance.getProcessInstanceId());
      assertEquals("com.sample.ruleflow", processInstance.getProcessId());
      assertNotNull(nodeInstance.getDate());
    }
    log.clear();
    processInstances = log.findProcessInstances("com.sample.ruleflow");
    assertEquals(0, processInstances.size());
    log.dispose();
  }
Example #5
0
  public static Environment createEnvironment(HashMap<String, Object> context) {
    Environment env = EnvironmentFactory.newEnvironment();

    UserTransaction ut = (UserTransaction) context.get(TRANSACTION);
    if (ut != null) {
      env.set(TRANSACTION, ut);
    }

    env.set(ENTITY_MANAGER_FACTORY, context.get(ENTITY_MANAGER_FACTORY));
    env.set(TRANSACTION_MANAGER, TransactionManagerServices.getTransactionManager());
    env.set(GLOBALS, new MapGlobalResolver());

    return env;
  }
  @Test
  @Ignore("Probably expects H2 do delete everything on shutdown?")
  public void testPersistenceTimer() throws Exception {
    final Environment env = KnowledgeBaseFactory.newEnvironment();
    env.set(EnvironmentName.ENTITY_MANAGER_FACTORY, emf);
    env.set(
        EnvironmentName.TRANSACTION_MANAGER, TransactionManagerServices.getTransactionManager());

    final Properties properties = new Properties();
    properties.setProperty("drools.commandService", SingleSessionCommandService.class.getName());
    properties.setProperty(
        "drools.processInstanceManagerFactory", JPAProcessInstanceManagerFactory.class.getName());
    properties.setProperty(
        "drools.workItemManagerFactory", JPAWorkItemManagerFactory.class.getName());
    properties.setProperty(
        "drools.processSignalManagerFactory", JPASignalManagerFactory.class.getName());
    properties.setProperty("drools.timerService", JpaJDKTimerService.class.getName());
    final SessionConfiguration config = new SessionConfiguration(properties);

    final KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    final Collection<KnowledgePackage> kpkgs = getProcessTimer();
    kbase.addKnowledgePackages(kpkgs);

    SingleSessionCommandService service = new SingleSessionCommandService(kbase, config, env);
    final int sessionId = service.getSessionId();
    final StartProcessCommand startProcessCommand = new StartProcessCommand();
    startProcessCommand.setProcessId("org.drools.test.TestProcess");
    ProcessInstance processInstance = service.execute(startProcessCommand);
    System.out.println("Started process instance " + processInstance.getId());
    service.dispose();

    service = new SingleSessionCommandService(sessionId, kbase, config, env);
    GetProcessInstanceCommand getProcessInstanceCommand = new GetProcessInstanceCommand();
    getProcessInstanceCommand.setProcessInstanceId(processInstance.getId());
    processInstance = service.execute(getProcessInstanceCommand);
    assertNotNull(processInstance);
    service.dispose();

    service = new SingleSessionCommandService(sessionId, kbase, config, env);
    Thread.sleep(3000);
    getProcessInstanceCommand = new GetProcessInstanceCommand();
    getProcessInstanceCommand.setProcessInstanceId(processInstance.getId());
    processInstance = service.execute(getProcessInstanceCommand);
    assertNull(processInstance);
  }
  public void testLogger4LargeVariable() throws Exception {
    // load the process
    KnowledgeBase kbase = createKnowledgeBase();
    // create a new session
    Environment env = KnowledgeBaseFactory.newEnvironment();
    env.set(EnvironmentName.ENTITY_MANAGER_FACTORY, emf);
    env.set(
        EnvironmentName.TRANSACTION_MANAGER, TransactionManagerServices.getTransactionManager());
    Properties properties = new Properties();
    properties.put(
        "drools.processInstanceManagerFactory",
        "org.jbpm.persistence.processinstance.JPAProcessInstanceManagerFactory");
    properties.put(
        "drools.processSignalManagerFactory",
        "org.jbpm.persistence.processinstance.JPASignalManagerFactory");
    KnowledgeSessionConfiguration config =
        KnowledgeBaseFactory.newKnowledgeSessionConfiguration(properties);
    StatefulKnowledgeSession session =
        JPAKnowledgeService.newStatefulKnowledgeSession(kbase, config, env);
    new JPAWorkingMemoryDbLogger(session);
    JPAProcessInstanceDbLog log = new JPAProcessInstanceDbLog(env);
    session
        .getWorkItemManager()
        .registerWorkItemHandler(
            "Human Task",
            new WorkItemHandler() {
              public void executeWorkItem(WorkItem workItem, WorkItemManager manager) {
                Map<String, Object> results = new HashMap<String, Object>();
                results.put("Result", "ResultValue");
                manager.completeWorkItem(workItem.getId(), results);
              }

              public void abortWorkItem(WorkItem workItem, WorkItemManager manager) {}
            });

    // record the initial count to compare to later
    List<ProcessInstanceLog> processInstances = log.findProcessInstances("com.sample.ruleflow");
    int initialProcessInstanceSize = processInstances.size();

    // start process instance
    Map<String, Object> params = new HashMap<String, Object>();
    List<String> list = new ArrayList<String>();
    list.add("One");
    list.add("Two");
    String three = "";
    for (int i = 0; i < 1024; i++) {
      three += "*";
    }
    list.add(three);
    params.put("list", list);
    long processInstanceId = session.startProcess("com.sample.ruleflow3", params).getId();

    System.out.println("Checking process instances for process 'com.sample.ruleflow3'");
    processInstances = log.findProcessInstances("com.sample.ruleflow3");
    assertEquals(initialProcessInstanceSize + 1, processInstances.size());
    ProcessInstanceLog processInstance = processInstances.get(0);
    System.out.print(processInstance);
    System.out.println(" -> " + processInstance.getStart() + " - " + processInstance.getEnd());
    assertNotNull(processInstance.getStart());
    assertNotNull(processInstance.getEnd());
    assertEquals(processInstanceId, processInstance.getProcessInstanceId());
    assertEquals("com.sample.ruleflow3", processInstance.getProcessId());
    List<VariableInstanceLog> variableInstances = log.findVariableInstances(processInstanceId);
    assertEquals(6, variableInstances.size());
    for (VariableInstanceLog variableInstance : variableInstances) {
      System.out.println(variableInstance);
      assertEquals(processInstanceId, processInstance.getProcessInstanceId());
      assertEquals("com.sample.ruleflow3", processInstance.getProcessId());
      assertNotNull(variableInstance.getDate());
    }
    log.clear();
    processInstances = log.findProcessInstances("com.sample.ruleflow3");
    assertEquals(0, processInstances.size());
    log.dispose();
  }
  @Test
  public void testPersistenceSubProcess() {
    final Environment env = KnowledgeBaseFactory.newEnvironment();
    env.set(EnvironmentName.ENTITY_MANAGER_FACTORY, emf);
    env.set(
        EnvironmentName.TRANSACTION_MANAGER, TransactionManagerServices.getTransactionManager());

    final Properties properties = new Properties();
    properties.setProperty("drools.commandService", SingleSessionCommandService.class.getName());
    properties.setProperty(
        "drools.processInstanceManagerFactory", JPAProcessInstanceManagerFactory.class.getName());
    properties.setProperty(
        "drools.workItemManagerFactory", JPAWorkItemManagerFactory.class.getName());
    properties.setProperty(
        "drools.processSignalManagerFactory", JPASignalManagerFactory.class.getName());
    properties.setProperty("drools.timerService", JpaJDKTimerService.class.getName());
    final SessionConfiguration config = new SessionConfiguration(properties);

    final RuleBase ruleBase = RuleBaseFactory.newRuleBase();
    final Package pkg = getProcessSubProcess();
    ruleBase.addPackage(pkg);

    SingleSessionCommandService service = new SingleSessionCommandService(ruleBase, config, env);
    final int sessionId = service.getSessionId();
    final StartProcessCommand startProcessCommand = new StartProcessCommand();
    startProcessCommand.setProcessId("org.drools.test.TestProcess");
    RuleFlowProcessInstance processInstance =
        (RuleFlowProcessInstance) service.execute(startProcessCommand);
    System.out.println("Started process instance " + processInstance.getId());
    final long processInstanceId = processInstance.getId();

    final TestWorkItemHandler handler = TestWorkItemHandler.getInstance();
    final WorkItem workItem = handler.getWorkItem();
    assertNotNull(workItem);
    service.dispose();

    service = new SingleSessionCommandService(sessionId, ruleBase, config, env);
    GetProcessInstanceCommand getProcessInstanceCommand = new GetProcessInstanceCommand();
    getProcessInstanceCommand.setProcessInstanceId(processInstanceId);
    processInstance = (RuleFlowProcessInstance) service.execute(getProcessInstanceCommand);
    assertNotNull(processInstance);

    final Collection<NodeInstance> nodeInstances = processInstance.getNodeInstances();
    assertEquals(1, nodeInstances.size());
    final SubProcessNodeInstance subProcessNodeInstance =
        (SubProcessNodeInstance) nodeInstances.iterator().next();
    final long subProcessInstanceId = subProcessNodeInstance.getProcessInstanceId();
    getProcessInstanceCommand = new GetProcessInstanceCommand();
    getProcessInstanceCommand.setProcessInstanceId(subProcessInstanceId);
    RuleFlowProcessInstance subProcessInstance =
        (RuleFlowProcessInstance) service.execute(getProcessInstanceCommand);
    assertNotNull(subProcessInstance);
    service.dispose();

    service = new SingleSessionCommandService(sessionId, ruleBase, config, env);
    final CompleteWorkItemCommand completeWorkItemCommand = new CompleteWorkItemCommand();
    completeWorkItemCommand.setWorkItemId(workItem.getId());
    service.execute(completeWorkItemCommand);
    service.dispose();

    service = new SingleSessionCommandService(sessionId, ruleBase, config, env);
    getProcessInstanceCommand = new GetProcessInstanceCommand();
    getProcessInstanceCommand.setProcessInstanceId(subProcessInstanceId);
    subProcessInstance = (RuleFlowProcessInstance) service.execute(getProcessInstanceCommand);
    assertNull(subProcessInstance);

    getProcessInstanceCommand = new GetProcessInstanceCommand();
    getProcessInstanceCommand.setProcessInstanceId(processInstanceId);
    processInstance = (RuleFlowProcessInstance) service.execute(getProcessInstanceCommand);
    assertNull(processInstance);
    service.dispose();
  }
  @Test
  public void testPersistenceWorkItemsUserTransaction() throws Exception {
    final Environment env = KnowledgeBaseFactory.newEnvironment();
    env.set(EnvironmentName.ENTITY_MANAGER_FACTORY, emf);
    env.set(
        EnvironmentName.TRANSACTION_MANAGER, TransactionManagerServices.getTransactionManager());

    final KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    final Collection<KnowledgePackage> kpkgs = getProcessWorkItems();
    kbase.addKnowledgePackages(kpkgs);

    final Properties properties = new Properties();
    properties.setProperty("drools.commandService", SingleSessionCommandService.class.getName());
    properties.setProperty(
        "drools.processInstanceManagerFactory", JPAProcessInstanceManagerFactory.class.getName());
    properties.setProperty(
        "drools.workItemManagerFactory", JPAWorkItemManagerFactory.class.getName());
    properties.setProperty(
        "drools.processSignalManagerFactory", JPASignalManagerFactory.class.getName());
    properties.setProperty("drools.timerService", JpaJDKTimerService.class.getName());
    final SessionConfiguration config = new SessionConfiguration(properties);

    SingleSessionCommandService service = new SingleSessionCommandService(kbase, config, env);
    final int sessionId = service.getSessionId();

    final UserTransaction ut =
        (UserTransaction) new InitialContext().lookup("java:comp/UserTransaction");
    ut.begin();
    final StartProcessCommand startProcessCommand = new StartProcessCommand();
    startProcessCommand.setProcessId("org.drools.test.TestProcess");
    ProcessInstance processInstance = service.execute(startProcessCommand);
    System.out.println("Started process instance " + processInstance.getId());
    ut.commit();

    final TestWorkItemHandler handler = TestWorkItemHandler.getInstance();
    WorkItem workItem = handler.getWorkItem();
    assertNotNull(workItem);
    service.dispose();

    service = new SingleSessionCommandService(sessionId, kbase, config, env);
    ut.begin();
    GetProcessInstanceCommand getProcessInstanceCommand = new GetProcessInstanceCommand();
    getProcessInstanceCommand.setProcessInstanceId(processInstance.getId());
    processInstance = service.execute(getProcessInstanceCommand);
    assertNotNull(processInstance);
    ut.commit();
    service.dispose();

    service = new SingleSessionCommandService(sessionId, kbase, config, env);
    ut.begin();
    CompleteWorkItemCommand completeWorkItemCommand = new CompleteWorkItemCommand();
    completeWorkItemCommand.setWorkItemId(workItem.getId());
    service.execute(completeWorkItemCommand);
    ut.commit();

    workItem = handler.getWorkItem();
    assertNotNull(workItem);
    service.dispose();

    service = new SingleSessionCommandService(sessionId, kbase, config, env);
    ut.begin();
    getProcessInstanceCommand = new GetProcessInstanceCommand();
    getProcessInstanceCommand.setProcessInstanceId(processInstance.getId());
    processInstance = service.execute(getProcessInstanceCommand);
    ut.commit();
    assertNotNull(processInstance);
    service.dispose();

    service = new SingleSessionCommandService(sessionId, kbase, config, env);
    ut.begin();
    completeWorkItemCommand = new CompleteWorkItemCommand();
    completeWorkItemCommand.setWorkItemId(workItem.getId());
    service.execute(completeWorkItemCommand);
    ut.commit();

    workItem = handler.getWorkItem();
    assertNotNull(workItem);
    service.dispose();

    service = new SingleSessionCommandService(sessionId, kbase, config, env);
    ut.begin();
    getProcessInstanceCommand = new GetProcessInstanceCommand();
    getProcessInstanceCommand.setProcessInstanceId(processInstance.getId());
    processInstance = service.execute(getProcessInstanceCommand);
    ut.commit();
    assertNotNull(processInstance);
    service.dispose();

    service = new SingleSessionCommandService(sessionId, kbase, config, env);
    ut.begin();
    completeWorkItemCommand = new CompleteWorkItemCommand();
    completeWorkItemCommand.setWorkItemId(workItem.getId());
    service.execute(completeWorkItemCommand);
    ut.commit();

    workItem = handler.getWorkItem();
    assertNull(workItem);
    service.dispose();

    service = new SingleSessionCommandService(sessionId, kbase, config, env);
    ut.begin();
    getProcessInstanceCommand = new GetProcessInstanceCommand();
    getProcessInstanceCommand.setProcessInstanceId(processInstance.getId());
    processInstance = service.execute(getProcessInstanceCommand);
    ut.commit();
    assertNull(processInstance);
    service.dispose();
  }