AnsweredAssumed Answered

repositoryService is null?

Question asked by xiegs2007 on Dec 12, 2011
Latest reply on Apr 29, 2016 by logicallimit
I'm test activiti58 integrate with Spring3.0.6,and after ref activiti58's example activiti-context.xml,and copy the runtime libs ,test libs to my Eclipse SpringMVC project.
1,here is my junit4 test,it successed.
public class Deploy {
   private static ApplicationContext ctx;
   private static ProcessEngine processEngine;
   private static RepositoryService repositoryService;
   private static RuntimeService runtimeService;
   private static TaskService taskService;
   private static HistoryService historyService;
   private static ManagementService managementService;
   
   @BeforeClass
   public static void setUpBeforeClass() throws Exception{
      try{
         ctx = new ClassPathXmlApplicationContext("activitiContext.xml");
         processEngine = (ProcessEngine)ctx.getBean("processEngine");
         repositoryService = (RepositoryService)ctx.getBean("repositoryService");
         runtimeService = (RuntimeService)ctx.getBean("runtimeService");
         taskService = (TaskService)ctx.getBean("taskService");
         historyService = (HistoryService)ctx.getBean("historyService");
         managementService = (ManagementService)ctx.getBean("managementService");
      }catch(RuntimeException e){
         e.printStackTrace();
      }
   }
   
   @Test
   public void OpenDatabase(){
      System.out.println("—start test—-");
      
      // assert bean no null
      assertNotNull(processEngine);
      assertNotNull(repositoryService);
      assertNotNull(runtimeService);
      assertNotNull(taskService);
      assertNotNull(historyService);
      assertNotNull(managementService);
      
      //use repositoryService deploy to DB
      //repositoryService.createDeployment().name("TestReport.bar")
      //   .addClasspathResource("com/xiegs/activity/test/bpmn20/TestReport.bpmn20.xml")
      //   .addClasspathResource("com/xiegs/activity/test/bpmn20/TestReport.png")
      //   .deploy();
      // get deployed processDefinition list
      List<ProcessDefinition> processDefinitions = repositoryService
            .createProcessDefinitionQuery().orderByProcessDefinitionKey()
            .asc().orderByProcessDefinitionVersion().desc().list();
      assertNotNull(processDefinitions);
      assertEquals(2, processDefinitions.size());
      
      //show definition.because singleResult() returned exception when morethan one result.
      //ProcessDefinition processDefinition = repositoryService
      //      .createProcessDefinitionQuery().processDefinitionKey("TestReport")
      //      .singleResult();
      ProcessDefinition processDefinition = processDefinitions.get(1);
      String processResourceName = processDefinition.getResourceName();
      String diagramResourceName = processDefinition.getDiagramResourceName();
      System.out.println("bpmn's name:" + processResourceName + ",diagram name:" + diagramResourceName);
      System.out.println("—- ");
      
      //start a instance
      ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("TestReport");
      assertNotNull(processInstance);
      System.out.println("—-instance started—");
   
      //query and exec task
      Task task = taskService.createTaskQuery().singleResult();
      assertEquals("fill report",task.getName());
      taskService.complete(task.getId());
      assertEquals(1,taskService.createTaskQuery().count());
      System.out.println("—-finished fill report task—–");
      
      //query and exec task again
      task = taskService.createTaskQuery().singleResult();
      assertEquals("manager sign",task.getName());
      taskService.complete(task.getId());
      assertEquals(0,taskService.createTaskQuery().count());
      System.out.println("——finished manager asign task—–");
      
      // now the instance is finished
      assertEquals(0,runtimeService.createProcessInstanceQuery().count());
      System.out.println("——instance finished—–");
   }
}
2,then, in my springMVC's controller,I write code:
public class HelloController implements Controller {

   /**
    * Logger for this class and subclasses
    */
   protected final Log logger = LogFactory.getLog(getClass());

   public ModelAndView handleRequest(HttpServletRequest request,
         HttpServletResponse arg1) throws Exception {
      // add two return parameters
      request.setAttribute("helloEntity01", "hello,xiegs2007");
      request.setAttribute("helloEntity02", "this is a httpRequest param");
      request.setAttribute("helloEntity03", "use ${} get the result");

      if(pe == null){
         logger.info("—-processInstance isn't established—–");
      }
      
      // deploy a processInstance
      if (repositoryService != null) {
         repositoryService
               .createDeployment()
               .name("Test-Report.bar")
               .addClasspathResource(
                     "main/resources/diagrams/TestReport.bpmn20.xml")
               .addClasspathResource(
                     "main/resources/diagrams/TestReport.png").deploy();
         // get deployed instance list
         List<ProcessDefinition> processDefinitions = repositoryService
               .createProcessDefinitionQuery()
               .orderByProcessDefinitionKey().asc()
               .orderByProcessDefinitionVersion().desc().list();
         request.setAttribute("processlist", processDefinitions);

         // display deployed processDefinition
         ProcessDefinition processDefinition = repositoryService
               .createProcessDefinitionQuery()
               .processDefinitionKey("TestReport").singleResult();
         request.setAttribute("processdefinition", processDefinition);

         // start a processInstance

         // query and exec first task

         // query and exec second task

         // ensure instance completed and is deleted
      }
      else{
         request.setAttribute("processlist", " ");
         request.setAttribute("processdefinition", " ");
      }

      return new ModelAndView("WEB-INF/page/hello.jsp");
   }

   @Autowired
   protected ProcessEngine pe;
   @Autowired
   protected RepositoryService repositoryService;
}

to be continued

Outcomes