-
15. Re: JBPM 6 exception while trying to start a process using API
pmehra1 Mar 6, 2014 10:34 AM (in response to pandyajayn)Here's the code I used with the REST calls and was able to start a process and control the tasks. I'll put the main snippets here. Note: I used the org.apache.http package classes for the REST calls. Also, I used the DocumentBuilder to parse the response to the REST calls (that part I haven't put the code for). Actually the REST calls return instances of JaxbProcessInstanceResponse and TaskSummaryImpl for process and task respectively. I would like to know if there are ways to get the response directly cast to these objects, or in general the way to go about making the REST calls from Java.
Let me know if this works for you.
private String deploymentId = "org.jbpm:Evaluation:1.0";
private String url = "http://localhost:8080/jbpm-console/";
private String user = "krisv";
private String password = "krisv";
-------------------------------------------------
//Initializing the HttpClient
private void initializeHttpClient() {
try {
httpclient = new DefaultHttpClient();
credentials = new UsernamePasswordCredentials(user, password);
scheme = new BasicScheme();
} catch (Exception ex) {
ex.printStackTrace();
}
}
---------------------------------------------------------------------
//starting the evaluation process
private void startProcessRestMethod() {
restCall = "http://" + "localhost" + ":" + "8080" + "/"
+ "jbpm-console" + "/rest/runtime/" + deploymentId
+ "/process/" + process + "/start";
try {
httppost = new HttpPost(restCall);
authorizationHeader = scheme.authenticate(credentials, httppost);
httppost.addHeader(authorizationHeader);
response = httpclient.execute(httppost);
if (response != null) {
log.info("Response status line: " + response.getStatusLine());
if (response.getStatusLine() != null) {
log.info("Response status code: "
+ response.getStatusLine().getStatusCode());
}
entity = response.getEntity();
if (entity != null) {
inst = entity.getContent();
if (inst != null) {
createProcessInstanceResponse(inst);
}
}
}
} catch (ClientProtocolException ex) {
ex.printStackTrace();
} catch (AuthenticationException ex) {
ex.printStackTrace();
} catch (Exception ex) {
ex.printStackTrace();
} finally {
closeHttpClient();
}
}
-------------------------------------------------
//getting the tasks for a user
private void getTasksForUser() {
// get list of tasks
restCall = "http://" + "localhost" + ":" + "8080" + "/"
+ "jbpm-console" + "/rest/task/query?taskOwner=" + user;
try {
httpget = new HttpGet(restCall);
authorizationHeader = scheme.authenticate(credentials, httpget);
httpget.addHeader(authorizationHeader);
response = httpclient.execute(httpget);
if (response != null) {
log.info("Task Response status line: "
+ response.getStatusLine());
if (response.getStatusLine() != null) {
log.info("Task Response status code: "
+ response.getStatusLine().getStatusCode());
}
entity = response.getEntity();
if (entity != null) {
inst = entity.getContent();
if (inst != null) {
createTaskSummaryResponse(inst);
}
}
}
} catch (ClientProtocolException ex) {
ex.printStackTrace();
} catch (AuthenticationException ex) {
ex.printStackTrace();
} catch (Exception ex) {
ex.printStackTrace();
} finally {
closeHttpClient();
}
}
----------------------------------------------------------
//starting a task
private void startTask(String taskId) {
restCall = "http://" + "localhost" + ":" + "8080" + "/"
+ "jbpm-console" + "/rest/task/" + taskId + "/start";
try {
httppost = new HttpPost(restCall);
authorizationHeader = scheme.authenticate(credentials, httppost);
httppost.addHeader(authorizationHeader);
response = httpclient.execute(httppost);
if (response != null) {
log.info("Task start Response status line: "
+ response.getStatusLine());
if (response.getStatusLine() != null) {
log.info("Task start Response status code: "
+ response.getStatusLine().getStatusCode());
}
}
} catch (ClientProtocolException ex) {
ex.printStackTrace();
} catch (AuthenticationException ex) {
ex.printStackTrace();
} catch (Exception ex) {
ex.printStackTrace();
} finally {
closeHttpClient();
}
}
---------------------------------------
//complete a task
private void completeTask(String taskId) {
restCall = "http://" + "localhost" + ":" + "8080" + "/"
+ "jbpm-console" + "/rest/task/" + taskId + "/complete";
try {
httppost = new HttpPost(restCall);
authorizationHeader = scheme.authenticate(credentials, httppost);
httppost.addHeader(authorizationHeader);
response = httpclient.execute(httppost);
if (response != null) {
log.info("Task complete Response status line: "
+ response.getStatusLine());
if (response.getStatusLine() != null) {
log.info("Task complete Response status code: "
+ response.getStatusLine().getStatusCode());
}
}
} catch (ClientProtocolException ex) {
ex.printStackTrace();
} catch (AuthenticationException ex) {
ex.printStackTrace();
} catch (Exception ex) {
ex.printStackTrace();
} finally {
closeHttpClient();
}
}
-
16. Re: JBPM 6 exception while trying to start a process using API
pandyajayn Mar 6, 2014 12:22 PM (in response to pmehra1)Prateek,
thanks a ton for sharing your hard work but still i am failed to make it success. please find below my code and also exception stack trace which i am getting. please help
import java.net.URL;
import org.apache.http.Header;
import org.apache.http.HttpResponse;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.auth.BasicScheme;
import org.apache.http.impl.client.DefaultHttpClient;
public class MyClass
{
private String deploymentId = "org.jbpm:Evaluation:1.0";
private String url = "http://localhost:8080/jbpm-console/";
private String user = "krisv";
private String password = "krisv";
private HttpClient httpclient;
private BasicScheme scheme;
private org.apache.http.auth.Credentials credentials;
private HttpPost httppost;
private Header authorizationHeader;
//Initializing the HttpClient
public void initializeHttpClient()
{
try
{
httpclient = new DefaultHttpClient();
credentials = new UsernamePasswordCredentials(user, password);
scheme = new BasicScheme();
}
catch (Exception ex)
{
ex.printStackTrace();
}
}
//starting the evaluation process
@SuppressWarnings("deprecation")
public void startProcessRestMethod()
{
String process="evaluation";
String restCall = "http://" + "localhost" + ":" + "8080" + "/"
+ "jbpm-console" + "/rest/runtime/" + deploymentId
+ "/process/" + process + "/start";
try {
httppost = new HttpPost(restCall);
authorizationHeader = scheme.authenticate(credentials, httppost);
httppost.addHeader(authorizationHeader);
HttpResponse response = (HttpResponse) httpclient.execute(httppost);
}
catch(Exception exc)
{
}
}
}
And Exception stack trace it
Exception in thread "main" java.lang.VerifyError: Cannot inherit from final class
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(Unknown Source)
at java.security.SecureClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.access$100(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at org.apache.http.impl.client.DefaultHttpClient.createHttpParams(DefaultHttpClient.java:157)
at org.apache.http.impl.client.AbstractHttpClient.getParams(AbstractHttpClient.java:447)
at org.apache.http.impl.client.AbstractHttpClient.createClientConnectionManager(AbstractHttpClient.java:308)
at org.apache.http.impl.client.AbstractHttpClient.getConnectionManager(AbstractHttpClient.java:465)
at org.apache.http.impl.client.AbstractHttpClient.createHttpContext(AbstractHttpClient.java:285)
at org.apache.http.impl.client.AbstractHttpClient.doExecute(AbstractHttpClient.java:799)
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:82)
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:106)
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:57)
at MyClass.startProcessRestMethod(MyClass.java:58)
at InteractjBPM.main(InteractjBPM.java:64)
-
17. Re: JBPM 6 exception while trying to start a process using API
pandyajayn Mar 6, 2014 4:50 PM (in response to pmehra1)Prateek,
I have mavenize my project and it is working fine with REST API Command and i can start the process. Thanks a lot for your support and help.
But still i have one more doubt, what is difference between REST API call and REST URL call to interact with jBPM6?
Regards,
Jay
-
18. Re: JBPM 6 exception while trying to start a process using API
pmehra1 Mar 6, 2014 7:27 PM (in response to pandyajayn)Glad that it worked for you as well!
I am also exploring jbpm at the moment so cannot give you a detailed answer to the difference between the 2 API but from my understanding the Rest API basically is like a wrapper around the rest calls and will give you the response having done all the serialization etc. The pure Rest calls urls are the regular rest calls but you will have to handle the response on your own way.
-
19. Re: JBPM 6 exception while trying to start a process using API
pandyajayn Mar 14, 2014 3:46 PM (in response to pmehra1)can you please guide me how i can get the content from response which you are getting after post request to jbpm as i am currently using REST API to interact with jBPM but we have to move to REST URL call
-
20. Re: JBPM 6 exception while trying to start a process using API
pmehra1 Mar 17, 2014 5:03 AM (in response to pandyajayn)Hi Jaykumar,
I had finally written an article on what I used to control the processes and tasks using the pure REST calls:
Using the REST URLs from a web application to control a process and its tasks in JBPM 6
In that I have also put the code for processing the response of starting a process and getting the tasks for that process. You can check it out at the link above.