Search in sources :

Example 1 with WorkflowRuntimeException

use of org.jbpm.workflow.instance.WorkflowRuntimeException in project jBPM5-Developer-Guide by Salaboy.

the class PersistentProcessTest method processInstancesAndLocalHTPlusFailTest.

@Test
public void processInstancesAndLocalHTPlusFailTest() throws Exception {
    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kbuilder.add(new ClassPathResource("process-async-interactions.bpmn"), ResourceType.BPMN2);
    if (kbuilder.hasErrors()) {
        for (KnowledgeBuilderError error : kbuilder.getErrors()) {
            System.out.println(">>> Error:" + error.getMessage());
        }
        fail(">>> Knowledge couldn't be parsed! ");
    }
    KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    kbase.addKnowledgePackages(kbuilder.getKnowledgePackages());
    Environment env = EnvironmentFactory.newEnvironment();
    EntityManagerFactory emf = Persistence.createEntityManagerFactory("org.jbpm.runtime.ht");
    env.set(EnvironmentName.ENTITY_MANAGER_FACTORY, emf);
    env.set(EnvironmentName.TRANSACTION_MANAGER, TransactionManagerServices.getTransactionManager());
    // Let's create a Persistence Knowledge Session
    System.out.println(" >>> Let's create a Persistent Knowledge Session");
    final StatefulKnowledgeSession ksession = JPAKnowledgeService.newStatefulKnowledgeSession(kbase, null, env);
    int sessionId = ksession.getId();
    assertNotNull(sessionId);
    assertTrue(sessionId != 0);
    // We need to register the WorkItems and Listeners that the session will use
    TaskService client = createTaskService(emf);
    LocalHTWorkItemHandler localHTWorkItemHandler = new LocalHTWorkItemHandler(client, ksession);
    MockFaultWorkItemHandler mockFaultWorkItemHandler = new MockFaultWorkItemHandler();
    ksession.getWorkItemManager().registerWorkItemHandler("Human Task", localHTWorkItemHandler);
    ksession.getWorkItemManager().registerWorkItemHandler("External Service Call", mockFaultWorkItemHandler);
    //KnowledgeRuntimeLoggerFactory.newConsoleLogger(ksession);
    // Let's create a Process Instance
    Person person = new Person("Salaboy", 29);
    Map<String, Object> params = new HashMap<String, Object>();
    params.put("person", person);
    //Each Command will generate an interaction
    System.out.println(">>> Let's Create Process Instance");
    ProcessInstance processInstance = ksession.createProcessInstance("com.salaboy.process.AsyncInteractions", params);
    System.out.println(">>> Let's Start the Process Instance");
    UserTransaction ut = (UserTransaction) new InitialContext().lookup("java:comp/UserTransaction");
    ut.begin();
    ksession.startProcessInstance(processInstance.getId());
    ut.commit();
    System.out.println(" >>> Looking for Salaboy's Tasks");
    List<TaskSummary> salaboysTasks = client.getTasksAssignedAsPotentialOwner("salaboy", "en-UK");
    assertTrue(salaboysTasks.size() == 1);
    TaskSummary salaboyTask = salaboysTasks.get(0);
    System.out.println(" >>> Starting Salaboy's Task");
    client.start(salaboyTask.getId(), "salaboy");
    System.out.println(" >>> Completing Salaboy's Task");
    ut.begin();
    try {
        client.complete(salaboyTask.getId(), "salaboy", null);
        ut.commit();
    } catch (WorkflowRuntimeException e) {
        System.out.println("Rolling back transaction" + e);
        ut.rollback();
    }
    System.out.println(" >>> Looking for Salaboy's Tasks");
    Task salaboysTask = client.getTask(salaboyTask.getId());
    assertNotNull(salaboysTask);
    assertEquals("InProgress", salaboysTask.getTaskData().getStatus().name());
    // We need to dispose the session, because the reference to this ksession object will no longer be valid
    //  because another thread could load the same session and execute a different command.
    System.out.println(" >>> Disposing Session");
    ksession.dispose();
}
Also used : UserTransaction(javax.transaction.UserTransaction) KnowledgeBuilderError(org.drools.builder.KnowledgeBuilderError) Task(org.jbpm.task.Task) HashMap(java.util.HashMap) LocalTaskService(org.jbpm.task.service.local.LocalTaskService) TaskService(org.jbpm.task.TaskService) WorkflowRuntimeException(org.jbpm.workflow.instance.WorkflowRuntimeException) ClassPathResource(org.drools.io.impl.ClassPathResource) InitialContext(javax.naming.InitialContext) KnowledgeBuilder(org.drools.builder.KnowledgeBuilder) KnowledgeBase(org.drools.KnowledgeBase) EntityManagerFactory(javax.persistence.EntityManagerFactory) TaskSummary(org.jbpm.task.query.TaskSummary) ProcessInstance(org.drools.runtime.process.ProcessInstance) Person(com.salaboy.model.Person) LocalHTWorkItemHandler(org.jbpm.process.workitem.wsht.LocalHTWorkItemHandler)

Example 2 with WorkflowRuntimeException

use of org.jbpm.workflow.instance.WorkflowRuntimeException in project jBPM5-Developer-Guide by Salaboy.

the class MultiProcessEvaluationTest method testMultiProcessEvaluation.

@Test
public void testMultiProcessEvaluation() {
    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kbuilder.add(new ClassPathResource("multi_process_evaluation/resources.drl"), ResourceType.DRL);
    kbuilder.add(new ClassPathResource("multi_process_evaluation/multi-process-decision.bpmn"), ResourceType.BPMN2);
    if (kbuilder.hasErrors()) {
        for (KnowledgeBuilderError error : kbuilder.getErrors()) {
            System.out.println(">>> Error:" + error.getMessage());
        }
        fail(">>> Knowledge couldn't be parsed! ");
    }
    KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    kbase.addKnowledgePackages(kbuilder.getKnowledgePackages());
    final StatefulKnowledgeSession ksession = kbase.newStatefulKnowledgeSession();
    // Uncomment to see all the logs
    KnowledgeRuntimeLoggerFactory.newConsoleLogger(ksession);
    try {
        ksession.addEventListener(new DefaultAgendaEventListener() {

            @Override
            public void activationCreated(ActivationCreatedEvent event) {
                System.out.println(">>> Firing All the Rules on Activation Created " + event);
                ((StatefulKnowledgeSession) event.getKnowledgeRuntime()).fireAllRules();
            }
        });
        ksession.addEventListener(new DefaultProcessEventListener() {

            @Override
            public void beforeNodeTriggered(ProcessNodeTriggeredEvent event) {
                System.out.println(">>> Firing All the Rules before node triggered! " + event);
                ((StatefulKnowledgeSession) event.getKnowledgeRuntime()).fireAllRules();
            }

            @Override
            public void beforeNodeLeft(ProcessNodeLeftEvent event) {
                System.out.println(">>> Firing All the Rules before node triggered! " + event);
                ((StatefulKnowledgeSession) event.getKnowledgeRuntime()).fireAllRules();
            }
        });
        Map<String, Object> params = new HashMap<String, Object>();
        ksession.insert(new Resources(50));
        for (int i = 1; i < 16; i++) {
            ProcessInstance processInstance = ksession.createProcessInstance("com.salaboy.process.SimpleDecision", params);
            ksession.insert(processInstance);
            assertEquals(processInstance.getState(), ProcessInstance.STATE_PENDING);
            System.out.println(" ----------------- ##### -----------------");
            System.out.println(" ----- Starting Process Number : " + i + " ----");
            System.out.println(" ----------------- ##### -----------------");
            ksession.startProcessInstance(processInstance.getId());
            assertEquals(processInstance.getState(), ProcessInstance.STATE_COMPLETED);
            System.out.println(" ----------------- ##### -----------------");
            System.out.println(" ----- Process Number : " + i + " Completed ----");
            System.out.println(" ----------------- ##### -----------------");
        }
    } catch (WorkflowRuntimeException e) {
        assertEquals(true, e.getCause().getMessage().contains("No More Resources Available = "));
    }
}
Also used : KnowledgeBuilderError(org.drools.builder.KnowledgeBuilderError) HashMap(java.util.HashMap) StatefulKnowledgeSession(org.drools.runtime.StatefulKnowledgeSession) WorkflowRuntimeException(org.jbpm.workflow.instance.WorkflowRuntimeException) ClassPathResource(org.drools.io.impl.ClassPathResource) KnowledgeBuilder(org.drools.builder.KnowledgeBuilder) KnowledgeBase(org.drools.KnowledgeBase) DefaultProcessEventListener(org.drools.event.process.DefaultProcessEventListener) ProcessNodeTriggeredEvent(org.drools.event.process.ProcessNodeTriggeredEvent) ProcessInstance(org.drools.runtime.process.ProcessInstance) Resources(com.salaboy.model.Resources) ProcessNodeLeftEvent(org.drools.event.process.ProcessNodeLeftEvent) Test(org.junit.Test)

Example 3 with WorkflowRuntimeException

use of org.jbpm.workflow.instance.WorkflowRuntimeException in project jBPM5-Developer-Guide by Salaboy.

the class PersistentProcessTest method processInstancesPersistenceFaultTest.

@Test
public void processInstancesPersistenceFaultTest() throws Exception {
    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kbuilder.add(new ClassPathResource("process-async-interactions.bpmn"), ResourceType.BPMN2);
    if (kbuilder.hasErrors()) {
        for (KnowledgeBuilderError error : kbuilder.getErrors()) {
            System.out.println(">>> Error:" + error.getMessage());
        }
        fail(">>> Knowledge couldn't be parsed! ");
    }
    KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    kbase.addKnowledgePackages(kbuilder.getKnowledgePackages());
    Environment env = EnvironmentFactory.newEnvironment();
    EntityManagerFactory emf = Persistence.createEntityManagerFactory("org.jbpm.runtime");
    env.set(EnvironmentName.ENTITY_MANAGER_FACTORY, emf);
    env.set(EnvironmentName.TRANSACTION_MANAGER, TransactionManagerServices.getTransactionManager());
    // Let's create a Persistence Knowledge Session
    System.out.println(" >>> Let's create a Persistent Knowledge Session");
    final StatefulKnowledgeSession ksession = JPAKnowledgeService.newStatefulKnowledgeSession(kbase, null, env);
    int sessionId = ksession.getId();
    assertNotNull(sessionId);
    assertTrue(sessionId != 0);
    // We need to register the WorkItems and Listeners that the session will use
    MockHTWorkItemHandler mockHTWorkItemHandler = new MockHTWorkItemHandler();
    MockFaultWorkItemHandler mockFaultWorkItemHandler = new MockFaultWorkItemHandler();
    ksession.getWorkItemManager().registerWorkItemHandler("Human Task", mockHTWorkItemHandler);
    ksession.getWorkItemManager().registerWorkItemHandler("External Service Call", mockFaultWorkItemHandler);
    //KnowledgeRuntimeLoggerFactory.newConsoleLogger(ksession);
    // Let's create a Process Instance
    Person person = new Person("Salaboy", 29);
    Map<String, Object> params = new HashMap<String, Object>();
    params.put("person", person);
    //Each Command will generate an interaction
    System.out.println(">>> Let's Create Process Instance");
    ProcessInstance processInstance = ksession.createProcessInstance("com.salaboy.process.AsyncInteractions", params);
    System.out.println(">>> Let's Start the Process Instance");
    try {
        ksession.startProcessInstance(processInstance.getId());
    } catch (Exception e) {
        assertTrue(e instanceof WorkflowRuntimeException);
        System.out.println(">>> Disposing Session");
        // We need to dispose the session, because the reference to this ksession object will no longer be valid
        //  because another thread could load the same session and execute a different command.
        ksession.dispose();
        // The startProcess transaction never gets commited.
        EntityManager em = emf.createEntityManager();
        // The ProcessInstance is in the same state as when it was created
        List resultList = em.createQuery("select p from ProcessInstanceInfo p").getResultList();
        assertEquals(1, resultList.size());
        assertEquals(0, ((WorkflowProcessInstanceImpl) processInstance).getNodeInstances().size());
        // No WorkItemInfo was commited.
        resultList = em.createQuery("select w from WorkItemInfo w").getResultList();
        assertEquals(0, resultList.size());
    }
}
Also used : KnowledgeBuilderError(org.drools.builder.KnowledgeBuilderError) HashMap(java.util.HashMap) WorkflowRuntimeException(org.jbpm.workflow.instance.WorkflowRuntimeException) ClassPathResource(org.drools.io.impl.ClassPathResource) WorkflowRuntimeException(org.jbpm.workflow.instance.WorkflowRuntimeException) EntityManager(javax.persistence.EntityManager) KnowledgeBuilder(org.drools.builder.KnowledgeBuilder) KnowledgeBase(org.drools.KnowledgeBase) EntityManagerFactory(javax.persistence.EntityManagerFactory) ProcessInstance(org.drools.runtime.process.ProcessInstance) List(java.util.List) Person(com.salaboy.model.Person)

Example 4 with WorkflowRuntimeException

use of org.jbpm.workflow.instance.WorkflowRuntimeException in project jBPM5-Developer-Guide by Salaboy.

the class PersistentProcessTest method processInstancesPersistenceFaultRecoveryTest.

@Test
public void processInstancesPersistenceFaultRecoveryTest() throws Exception {
    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kbuilder.add(new ClassPathResource("process-async-interactions.bpmn"), ResourceType.BPMN2);
    if (kbuilder.hasErrors()) {
        for (KnowledgeBuilderError error : kbuilder.getErrors()) {
            System.out.println(">>> Error:" + error.getMessage());
        }
        fail(">>> Knowledge couldn't be parsed! ");
    }
    KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    kbase.addKnowledgePackages(kbuilder.getKnowledgePackages());
    Environment env = EnvironmentFactory.newEnvironment();
    EntityManagerFactory emf = Persistence.createEntityManagerFactory("org.jbpm.runtime");
    env.set(EnvironmentName.ENTITY_MANAGER_FACTORY, emf);
    env.set(EnvironmentName.TRANSACTION_MANAGER, TransactionManagerServices.getTransactionManager());
    // Let's create a Persistence Knowledge Session
    System.out.println(" >>> Let's create a Persistent Knowledge Session");
    final StatefulKnowledgeSession ksession = JPAKnowledgeService.newStatefulKnowledgeSession(kbase, null, env);
    int sessionId = ksession.getId();
    assertNotNull(sessionId);
    assertTrue(sessionId != 0);
    // We need to register the WorkItems and Listeners that the session will use
    MockAsyncHTWorkItemHandler mockAsyncHTWorkItemHandler = new MockAsyncHTWorkItemHandler();
    MockFaultWorkItemHandler mockFaultWorkItemHandler = new MockFaultWorkItemHandler();
    ksession.getWorkItemManager().registerWorkItemHandler("Human Task", mockAsyncHTWorkItemHandler);
    ksession.getWorkItemManager().registerWorkItemHandler("External Service Call", mockFaultWorkItemHandler);
    //KnowledgeRuntimeLoggerFactory.newConsoleLogger(ksession);
    // Let's create a Process Instance
    Person person = new Person("Salaboy", 29);
    Map<String, Object> params = new HashMap<String, Object>();
    params.put("person", person);
    //Each Command will generate an interaction
    System.out.println(">>> Let's Create Process Instance");
    ProcessInstance processInstance = ksession.createProcessInstance("com.salaboy.process.AsyncInteractions", params);
    System.out.println(">>> Let's Start the Process Instance");
    try {
        ksession.startProcessInstance(processInstance.getId());
        System.out.println(">>> Completing the first Human Interaction");
        ksession.getWorkItemManager().completeWorkItem(mockAsyncHTWorkItemHandler.getId(), null);
    } catch (Exception e) {
        assertTrue(e instanceof WorkflowRuntimeException);
        System.out.println(">>> Disposing Session");
        // We need to dispose the session, because the reference to this ksession object will no longer be valid
        //  because another thread could load the same session and execute a different command.
        ksession.dispose();
        // The startProcess transaction never gets commited.
        EntityManager em = emf.createEntityManager();
        // The ProcessInstance is in the same state as when it was created
        List resultList = em.createQuery("select p from ProcessInstanceInfo p").getResultList();
        assertEquals(1, resultList.size());
        assertEquals(0, ((WorkflowProcessInstanceImpl) processInstance).getNodeInstances().size());
        // No WorkItemInfo was commited.
        resultList = em.createQuery("select w from WorkItemInfo w").getResultList();
        assertEquals(1, resultList.size());
    }
}
Also used : KnowledgeBuilderError(org.drools.builder.KnowledgeBuilderError) HashMap(java.util.HashMap) WorkflowRuntimeException(org.jbpm.workflow.instance.WorkflowRuntimeException) ClassPathResource(org.drools.io.impl.ClassPathResource) WorkflowRuntimeException(org.jbpm.workflow.instance.WorkflowRuntimeException) EntityManager(javax.persistence.EntityManager) KnowledgeBuilder(org.drools.builder.KnowledgeBuilder) KnowledgeBase(org.drools.KnowledgeBase) EntityManagerFactory(javax.persistence.EntityManagerFactory) ProcessInstance(org.drools.runtime.process.ProcessInstance) List(java.util.List) Person(com.salaboy.model.Person)

Aggregations

HashMap (java.util.HashMap)4 KnowledgeBase (org.drools.KnowledgeBase)4 KnowledgeBuilder (org.drools.builder.KnowledgeBuilder)4 KnowledgeBuilderError (org.drools.builder.KnowledgeBuilderError)4 ClassPathResource (org.drools.io.impl.ClassPathResource)4 ProcessInstance (org.drools.runtime.process.ProcessInstance)4 WorkflowRuntimeException (org.jbpm.workflow.instance.WorkflowRuntimeException)4 Person (com.salaboy.model.Person)3 EntityManagerFactory (javax.persistence.EntityManagerFactory)3 List (java.util.List)2 EntityManager (javax.persistence.EntityManager)2 Resources (com.salaboy.model.Resources)1 InitialContext (javax.naming.InitialContext)1 UserTransaction (javax.transaction.UserTransaction)1 DefaultProcessEventListener (org.drools.event.process.DefaultProcessEventListener)1 ProcessNodeLeftEvent (org.drools.event.process.ProcessNodeLeftEvent)1 ProcessNodeTriggeredEvent (org.drools.event.process.ProcessNodeTriggeredEvent)1 StatefulKnowledgeSession (org.drools.runtime.StatefulKnowledgeSession)1 LocalHTWorkItemHandler (org.jbpm.process.workitem.wsht.LocalHTWorkItemHandler)1 Task (org.jbpm.task.Task)1