Coding the Task Server

Ok, so we need to create at first a TaskServer where Human Task will be registered.
The Human Task service needs to know what all the possible valid user and group ids are (to make sure tasks are assigned to existing users and/or groups to avoid errors and tasks that end up assigned to non-existing users). You need to make sure to register all users and groups before tasks can be assigned to them.

Finally the MinaTaskServer is used to start the TaskServer on the default port.

Let’s code this TaskServer in a Startup Servlet so it will be started when you deploy your application:

package com.mastertheboss.jbpm5;

import java.io.IOException;

import javax.naming.InitialContext;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.transaction.TransactionManager;

import org.drools.KnowledgeBaseFactory;
import org.drools.SystemEventListenerFactory;
import org.drools.base.MapGlobalResolver;
import org.drools.runtime.Environment;
import org.drools.runtime.EnvironmentName;

import org.jbpm.task.User;
import org.jbpm.task.service.TaskService;
import org.jbpm.task.service.TaskServiceSession;
import org.jbpm.task.service.mina.MinaTaskServer;

/**
* Servlet implementation class StartupServlet
*/
@WebServlet(value="/StartupServlet", loadOnStartup=1)

public class StartupServlet extends HttpServlet {
   private static final long serialVersionUID = 1L;

   public void init(ServletConfig config) throws ServletException
   {
       
       EntityManagerFactory emfTask = Persistence.createEntityManagerFactory( "org.jbpm.task" );


       Environment env = KnowledgeBaseFactory.newEnvironment();
       env.set( EnvironmentName.ENTITY_MANAGER_FACTORY, emfTask );    
       env.set( EnvironmentName.GLOBALS, new MapGlobalResolver() );


       TaskService taskService = new TaskService(emfTask, SystemEventListenerFactory.getSystemEventListener());

       TaskServiceSession taskSession = taskService.createSession();
       taskSession.addUser(new User("Administrator"));
       taskSession.addUser(new User("frank"));
       taskSession.addUser(new User("mary"));


       MinaTaskServer server = new MinaTaskServer( taskService );
       Thread thread = new Thread( server );
       thread.start();

       System.out.println("Started task service!");
   }
   public StartupServlet() {
       super();
       
   }
   

   protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
       // TODO Auto-generated method stub
   }

   protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
       // TODO Auto-generated method stub
   }

}

0
0
0
s2sdefault