10 Replies Latest reply on Dec 28, 2011 1:26 PM by murthy516

    Human task client on a different user GUI

    pierx83

      Hi to all.

      I need to develop a JSP-based GUI to integrate with JBPM. Actually I have two possibilities:

      - using the class org.jbpm.task.service.TaskClient as task client

      - using the REST interface.

       

      Which are the differences between such kind of communications?

      Which is the better way to do that?

      Which is the best way to call a REST interface from JAVA?

       

      Thanks in advance,

      Pierpaolo

        • 1. Re: Human task client on a different user GUI
          salaboy21

          Hi Pierpaolo,

          You can use both tecniques to interact with the human task service. If you should choose the underlaying transport communication based on your needs. If you want to interact with the human task component with an application that was written in a different language than java you don't have too much options. REST is the only way to go. But if you are planning to use Java there is no best way, it really depends on the extra features that you are looking for. Right now the TaskClient has three flavors: Local (Embedded), Mina and JMS/HornetQ.

          For calling a REST interface there is a lot of frameworks that do the work for you, you should investigate about that but in no way is related with how jBPM or the Human Task service works, it's just an interface.

          Cheers

          1 of 1 people found this helpful
          • 2. Re: Human task client on a different user GUI
            pierx83

            Hi!

             

            Thanks a lot for these informations.

            Actually I started to do training on TaskClient but I'm experiencing a problem for the case in which a process variable has to be passed to the task.

            Do you have some suggestion to do that?

             

            Thanks in advance,

            Pierpaolo

            • 3. Re: Human task client on a different user GUI
              salaboy21

              what kind of problem are you experiencing? Remember that if you are using the Mina or HornetQ implementation your variables should be Serializable.

              Can you please mark the question as answered?

              Cheers

              • 4. Re: Human task client on a different user GUI
                kita

                I want to build jsp ui for task management using rest api. I would like to know how I post data back to process to go to next node ( task).

                For example, user gets a task to update something, after done updating how I can post data back to process?

                 

                Also I can launch a process from jsp?  I saw a rest api to launch new process instance but it did not work for me.

                 

                http://localhost:8080/gwt-console-server/rs/process/definition/test/new_instance

                • 5. Re: Human task client on a different user GUI
                  pierx83

                  Hi,

                   

                  actually I use the REST interface just to start a process. The Task management activities are done by using the TaskClient library provided with jbpm.

                  About the start process, here my code:

                   

                  public void startProcess(String username,String password,String processName, Map<String, Object> parameters) {

                   

                  JBPMRestOperations jbpmRestOperations = new JBPMRestOperations ("localhost","8080");

                   

                  String responseString = this.jbpmRestOperations.requestPostService(processName, parameters);

                   

                  if(responseString.substring(101).startsWith("j_security_check")) //REST needs authentication

                   

                  this.jbpmRestOperations.authenticate(username, password);

                   

                   

                  this.jbpmRestOperations.requestPostService(processName, parameters);

                  }

                  and the JBPMRestOperations class is:

                    

                  import java.io.BufferedReader;

                  import java.io.InputStream;

                  import java.io.InputStreamReader;

                  import java.util.ArrayList;

                  import java.util.HashMap;

                  import java.util.Iterator;

                  import java.util.List;

                  import java.util.Map;

                  import java.util.Set;

                  import org.apache.http.HttpResponse;

                  import org.apache.http.NameValuePair;

                  import org.apache.http.client.entity.UrlEncodedFormEntity;

                  import org.apache.http.client.methods.HttpPost;

                  import org.apache.http.impl.client.DefaultHttpClient;

                  import org.apache.http.message.BasicNameValuePair;

                  public class JBPMRestOperations {

                  public String KEY_USERNAME = "j_username";

                  public String KEY_PASSWORD = "j_password";

                  private DefaultHttpClient httpClient; // keep this out of the method in order to reuse the object for calling other services without losing session

                  private String address;

                  public JBPMRestOperations(String host,String port){

                  httpClient = new DefaultHttpClient();

                  this.address = host+":"+port;

                  }

                  public String authenticate(String username, String password) {

                  String responseString = "";

                  List<NameValuePair> formparams = new ArrayList<NameValuePair>();

                  formparams.add(new BasicNameValuePair(KEY_USERNAME, username));

                  formparams.add(new BasicNameValuePair(KEY_PASSWORD, password));

                  HttpPost httpPost = new HttpPost("http://" + address + "/gwt-console-server/rs/process/j_security_check");

                  InputStreamReader inputStreamReader = null;

                  BufferedReader bufferedReader = null;

                  try {

                  UrlEncodedFormEntity entity = new UrlEncodedFormEntity(formparams, "UTF-8");//UrlEncodedFormEntity(formparams, "multipart/form-data");

                  httpPost.setEntity(entity);

                  HttpResponse response = httpClient.execute(httpPost);

                  InputStream inputStream = response.getEntity().getContent();

                  inputStreamReader = new InputStreamReader(inputStream);

                  bufferedReader = new BufferedReader(inputStreamReader);

                  StringBuilder stringBuilder = new StringBuilder();

                  String line = bufferedReader.readLine();

                  while (line != null) {

                  stringBuilder.append(line);

                  line = bufferedReader.readLine();

                  }

                  responseString = stringBuilder.toString();

                  } catch (Exception e) {

                  throw new RuntimeException(e);

                  } finally {

                  if (inputStreamReader != null) {

                  try {

                  inputStreamReader.close();

                  } catch (Exception e) {

                  throw new RuntimeException(e);

                  }

                  }

                  if (bufferedReader != null) {

                  try {

                  bufferedReader.close();

                  } catch (Exception e) {

                  throw new RuntimeException(e);

                  }

                  }

                  }

                  return responseString;

                  }

                    

                  public String requestPostService(String processName, Map<String, Object> parameters) {

                  String responseString = "";

                  List<NameValuePair> formparams = new ArrayList<NameValuePair>();

                  if (parameters == null) {

                  parameters = new HashMap<String, Object>();

                  }

                  Set<String> keys = parameters.keySet();

                  for (Iterator<String> keysIterator = keys.iterator(); keysIterator.hasNext();) {

                  String keyString = keysIterator.next();

                  String value = parameters.get(keyString).toString();

                  formparams.add(new BasicNameValuePair(keyString, value));

                  }

                  HttpPost httpPost = new HttpPost("http://" + address + "/gwt-console-server/rs/process/definition/"+processName+"/new_instance_params");

                  InputStreamReader inputStreamReader = null;

                  BufferedReader bufferedReader = null;

                    

                  try {

                  UrlEncodedFormEntity entity = new UrlEncodedFormEntity(formparams, "UTF-8");

                  httpPost.setEntity(entity);

                  HttpResponse response = httpClient.execute(httpPost);

                  InputStream inputStream = response.getEntity().getContent();

                  inputStreamReader = new InputStreamReader(inputStream);

                  bufferedReader = new BufferedReader(inputStreamReader);

                  StringBuilder stringBuilder = new StringBuilder();

                  String line = bufferedReader.readLine();

                  while (line != null) {

                  stringBuilder.append(line);

                  line = bufferedReader.readLine();

                  }

                  responseString = stringBuilder.toString();

                    

                  } catch (Exception e) {

                  throw new RuntimeException(e);

                  } finally {

                  if (inputStreamReader != null) {

                  try {

                  inputStreamReader.close();

                  } catch (Exception e) {

                  throw new RuntimeException(e);

                  }

                  }

                  if (bufferedReader != null) {

                  try {

                  bufferedReader.close();

                  } catch (Exception e) {

                  throw new RuntimeException(e);

                  }

                  }

                  }

                  return responseString;

                  }

                  }

                   

                  • 6. Re: Human task client on a different user GUI
                    kita

                    I will try your code. Were you able to post data to a task?

                    • 7. Re: Human task client on a different user GUI
                      pierx83

                      Yes I was.

                      I use the TaskClient utilities. Here my code to start and to complete a task (the start method reads the input parameters and the complete one write the output parameters). I hope this can help you

                       

                       

                      public Object start(String user,Task task) throws Exception {

                       

                      this.taskClient.start(task.getId(),user, null);

                       

                      BlockingGetTaskResponseHandler handlerT = new BlockingGetTaskResponseHandler();

                      this.taskClient.getTask(id, handlerT);

                      org.jbpm.task.Task t = handlerT.getTask();

                      TaskData taskData = t.getTaskData();

                      // System.out.println("TaskData = "+taskData);

                      BlockingGetContentResponseHandler handlerC = new BlockingGetContentResponseHandler();

                      this.taskClient.getContent(taskData.getDocumentContentId(), handlerC);

                      Content content = handlerC.getContent();

                      // System.out.println("Content= "+content);

                      ByteArrayInputStream bais = new ByteArrayInputStream(content.getContent());

                      ObjectInputStream ois = new ObjectInputStream(bais);

                       

                      //parameter può essere un parametro di tipo primitivo o un oggetto

                      //a seconda del task che esegue il cast

                      Object parameter =ois.readObject();

                      // System.out.println("parameter = "+parameter);

                       

                      return parameter;   //input parameter of the task

                      }

                       

                      /**

                      * Complete the task execution

                      */

                      public void complete(String user,Task task,Map<String,String> parameters){

                      ContentData contentData = null;

                      if (parameters != null) {

                      ByteArrayOutputStream bos = new ByteArrayOutputStream();

                      ObjectOutputStream out;

                      try {

                      out = new ObjectOutputStream(bos);

                      out.writeObject(parameters);

                      out.close();

                      contentData = new ContentData();

                      contentData.setContent(bos.toByteArray());

                      contentData.setAccessType(AccessType.Inline);

                      } catch (IOException e) {

                      e.printStackTrace();

                      }

                      }

                       

                      BlockingTaskOperationResponseHandler responseHandler = new BlockingTaskOperationResponseHandler();

                      this.taskClient.complete(task.getId(), user, contentData, responseHandler);

                      responseHandler.waitTillDone(5000);

                      }

                      • 8. Re: Human task client on a different user GUI
                        salaboy21

                        That's right, you can take a look at all the test provided inside the jbpm-human-task module to find how to use the component

                        Cheers

                        • 9. Re: Human task client on a different user GUI
                          kita

                          Pierpaolo,

                           

                          I really appreciated your help. I was able to start process using rest code. My question related about task was how to use rest to post data to task.

                          My project is to evaluate how to build custom struts application to leverage jbpm5 rest api.

                           

                          Were you able to post data to task via rest?

                          • 10. Re: Human task client on a different user GUI
                            murthy516

                            Hi All,

                             

                            When I've developed the above classes,it is displaying errors even if i added required jars.Could you please suggest me to develop

                            human task client on a GUI.Is it through REST API??How?/the above code is not working for me..Please provide required jars,processes to include.

                             

                            Thanks