private void testStartProcess(SessionManagerFactory factory) throws Exception { SessionManager sessionManager = factory.getSessionManager(); long taskId; synchronized ( (SingleSessionCommandService) ((CommandBasedStatefulKnowledgeSession) sessionManager.getKnowledgeSession()) .getCommandService()) { UserTransaction ut = (UserTransaction) new InitialContext().lookup("java:comp/UserTransaction"); ut.begin(); System.out.println( "Starting process on ksession " + sessionManager.getKnowledgeSession().getId()); ProcessInstance processInstance = sessionManager.getKnowledgeSession().startProcess("com.sample.bpmn.hello", null); System.out.println( "Started process instance " + processInstance.getId() + " on ksession " + sessionManager.getKnowledgeSession().getId()); long workItemId = ((HumanTaskNodeInstance) ((WorkflowProcessInstance) processInstance).getNodeInstances().iterator().next()) .getWorkItemId(); taskId = sessionManager.getTaskService().getTaskByWorkItemId(workItemId).getId(); System.out.println("Created task " + taskId); ut.commit(); } sessionManager.getTaskService().claim(taskId, "mary"); sessionManager.dispose(); }
public void testNewSessionFailBefore() throws Exception { KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder(); kbuilder.add(ResourceFactory.newClassPathResource("sampleFailBefore.bpmn"), ResourceType.BPMN2); KnowledgeBase kbase = kbuilder.newKnowledgeBase(); SessionManagerFactory factory = new NewSessionSessionManagerFactory(kbase); SessionManager sessionManager = factory.getSessionManager(); try { ProcessInstance processInstance = sessionManager.getKnowledgeSession().startProcess("com.sample.bpmn.hello", null); fail("Started process instance " + processInstance.getId()); } catch (RuntimeException e) { // do nothing } // TODO: whenever transaction fails, do we need to dispose? can we? // sessionManager.dispose(); List<Status> statusses = new ArrayList<Status>(); statusses.add(Status.Reserved); sessionManager = factory.getSessionManager(); List<TaskSummary> tasks = sessionManager.getTaskService().getTasksAssignedAsPotentialOwner("mary", "en-UK"); assertEquals(0, tasks.size()); sessionManager.dispose(); factory.dispose(); }
public void testNewSessionFailAfter() throws Exception { KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder(); kbuilder.add(ResourceFactory.newClassPathResource("sampleFailAfter.bpmn"), ResourceType.BPMN2); KnowledgeBase kbase = kbuilder.newKnowledgeBase(); SessionManagerFactory factory = new NewSessionSessionManagerFactory(kbase); SessionManager sessionManager = factory.getSessionManager(); ProcessInstance processInstance = sessionManager.getKnowledgeSession().startProcess("com.sample.bpmn.hello", null); long workItemId = ((HumanTaskNodeInstance) ((WorkflowProcessInstance) processInstance).getNodeInstances().iterator().next()) .getWorkItemId(); long taskId = sessionManager.getTaskService().getTaskByWorkItemId(workItemId).getId(); sessionManager.getTaskService().claim(taskId, "mary"); List<Status> statusses = new ArrayList<Status>(); statusses.add(Status.Reserved); List<TaskSummary> tasks = sessionManager.getTaskService().getTasksOwned("mary", statusses, "en-UK"); assertEquals(1, tasks.size()); taskId = tasks.get(0).getId(); UserTransaction ut = (UserTransaction) new InitialContext().lookup("java:comp/UserTransaction"); try { ut.begin(); sessionManager.getTaskService().start(taskId, "mary"); sessionManager.getTaskService().complete(taskId, "mary", null); fail("Task completed"); } catch (RuntimeException e) { // do nothing } ut.rollback(); // TODO: whenever transaction fails, do we need to dispose? can we? // sessionManager.dispose(); sessionManager = factory.getSessionManager(); tasks = sessionManager.getTaskService().getTasksOwned("mary", statusses, "en-UK"); assertEquals(1, tasks.size()); sessionManager.dispose(); factory.dispose(); }
private boolean testCompleteTask(SessionManagerFactory factory) throws InterruptedException, Exception { boolean result = false; List<Status> statusses = new ArrayList<Status>(); statusses.add(Status.Reserved); SessionManager sessionManager = factory.getSessionManager(); List<TaskSummary> tasks = null; tasks = sessionManager.getTaskService().getTasksOwned("mary", statusses, "en-UK"); if (tasks.isEmpty()) { System.out.println("Task thread found no tasks"); Thread.sleep(1000); } else { long taskId = tasks.get(0).getId(); System.out.println("Completing task " + taskId); boolean success = false; try { sessionManager.getTaskService().start(taskId, "mary"); success = true; } catch (PermissionDeniedException e) { // TODO can we avoid these by doing it all in one transaction? System.out.println("Task thread was too late for starting task " + taskId); } catch (RuntimeException e) { if (e.getCause() instanceof OptimisticLockException) { System.out.println("Task thread got in conflict when starting task " + taskId); } else { throw e; } } if (success) { sessionManager.getTaskService().complete(taskId, "mary", null); System.out.println("Completed task " + taskId); result = true; } } sessionManager.dispose(); return result; }