domingo, 26 de julho de 2015

A Parser work item handler for jbpm 6

One usual task we do in a process is to have to parse XML/JSON to objects and vice versa. I always look at sample work item handlers from jbpm and I want to create my own work item handlers because it is fun. Today I will quickly show the first public useful work item handler I have created: The parser.

What is and how to use it?

It is a simple parser task that you can use in your BPM Suite/ jbpm 6 process to parse from XML/JSON to some Java object and vice versa. You can, for example, read XML from a file and convert to your model object. It can be used with the REST Task for previous jbpm version, since recently support to transform was added to the REST Task

It accepts the following parameters:

Input: The input data. If you provide a string, the parser will try to parse the string to an object of type Type and format Format (see below), if not, it will try to parse the object to a String of format Format. It is a required parameter;
Format: It is a required parameter that can have the values JSON or XML;
Result: The result is an output parameter that will be a string if you provide an Input object that is not of String type; and an object of type Type if you provide  an Input object of type String.

Providing these parameters correctly is enough to use the parser task. Please bear in mind that it uses JAXB to parse the object, so it must have the @XmlRootElement annotation.


The steps to install it are similar to the steps we used to install the hello world work item handler. But since we are on BPM Suite 6.1, it will be slight different, see:

* Clone the source code from my github and run mvn clean install to have it in your maven repository (actually in the maven repository used by BPM Suite); Or download the JAR containing this work item handler and upload it as an artifact to your installation;

* Add the GAV information of the WIH(Group: org.jbpm.process.workitem; Artifact ID: parser-task; version: 1.0) as a dependency of your project:

* Now edit the new deployment descriptor to include a declaration of this WIH class. Notice I am sending the Classloader as a parameter. It is needed to load classes defintion to be parsed:

* Finally edit the wid file to include the declaration of the WIH. Now you can open the process and drag it to use in your process.

If you want to see more details about this WIH, please see  the project in github and also the tests for it.


It is easy to extend the jbpm 6 library to include your own custom tasks. The parser task was approved for the Service Repository in BPM Suite! See jbpm github!

sábado, 4 de abril de 2015

Creating custom Work Item Handler in BPM Suite/jBPM 6

Hi all! In this post I am going to share my experience creating a work item handler for BPM Suite 6.0.3 (which is very similar to jbpm as explained in a previous post).

The Hello World Work Item Handler

Let's create a really simple WorkItemHandler just to print "Hello World" in the console. Of course you can implement more feature and make it receive parameters, access database, web services, etc. But in this post we will keep it simple. Really simple.

First thing to do is to create a maven project for my WorkItemHandler. Here's the pom.xml of my Maven project:

Notice that we are importing jbpm dependency so we can find the java interface that is used to create work item handlers.

Now we can start coding. Let's create a class name HelloWorkItemHandler in package org.jugvale.jbpm, here is the simple code:

Now we can build our project and go to BPM Suite to configure it so we can use this workitem.

Registering the WorkItemHandler

The first thing to do is to make our artifact visible to BPM Suite. We can either import it in the maven used by BPM Suite or we can upload the JAR using the web application. In any case, the common step is to build our project.

After building it, decide if you want to install it in your maven repository so you can use it in the BPM Suite project as a dependency or you can upload the JAR to BPM Suite and make it a dependency of the BPM Suite project. That's what we are going to do.
  • So, after starting BPM Suite, and logging into business central, we can go to Authoring -> Artifact Repository  and upload the JAR with our Work Item Handler:

Now we create a simple project in BPM Suite. You can do this by going to Authoring -> Project Authoring and then New Item -> Project. Notice that I am considering that you have already a repository created.

Now that you have your project created, let's add the artifact we uploaded as a dependency of it. To do this, select the project and go to Tools -> Project Editor and on the Project Settings drop down menu, select Dependencies. Now you can specify the uploaded artifact as a dependency of your project using the Add from repository button and choose our artifact from the list that will show up and then we are done with this part. Don't forget the save the changes.

Remember that WorkItemHandlers are registered in the file META-INF/kmodule.xml of your project. However, we can do this visually! Go to Project Editor  and from the Project Settings dropdown button, choose Knowledge base and sessions. Add a new knowledge base, make it default and add a new ksession, make it default, edit it and add the work item handler. Here are some useful screenshots:

 Adding the kbase and the ksession:

Editing the ksession to add the work item handler:

Save it and now the project is already configured with a kbase and a ksession with our work item handler! See how our kmodule.xml looks like:

We are almost done.  Let's create a process to test the WorkItemHandler we created, use the menu New Item -> Business Process and do nothing for now, just close the process editor.Now let's simple edit the file WorkDefinitions.wid of our project to include metadata about the WorkItemHandler. Here we can set the icon and the name that will appears in the process editor. We simple only set a display name:

   "name" : "HelloWorkItemHandler",
    "displayName" : "Hello World!",
    "icon" : "defaultemailicon.gif"

Save the file, create a new process and MAGIC! Now you can use your custom work item handler and it will be appear in the process editor library under the Service Tasks pane. See my sample process:

If you build, deploy the project and run the process above, you see the "Hello World!" message in the logs:

00:25:16,904 INFO  [stdout] (http-localhost.localdomain/ Hello World!

That's our Hello World Work Item Handler! Of course you can also declare parameters for the Work Item and do more complex operations, such as access a Web Service, send email, send SMS, update a database....

Download the sources on github

domingo, 1 de fevereiro de 2015

Porting the StackerJavaFX game to Android

For too long I want to port a JavaFX application of mine to Android using JavaFX Ports. The day has come! In this post I will share with you my experiences porting the Stacker Game to Android. Actually at the time I write this sentence I just created a new project on Netbeans and it might even not work, I can even abandon this post, because at the moment I have 0% of the port done! UPDATE: It worked and was really easy!

From Java 8 to Java 7

Android doesn't support Java 8. We have projects to use Lambdas in Java 7, but I wanted to make it simple as possible to avoid any incompatibility, hence I decided to keep it only with Java 7 and JavaFX 2.2.
The saddest thing was to remove my beautiful lambdas to use the old anonymous classes from the code. Also, I noticed that JavaFX 8 is easier than JavaFX 2.2. The reason is that we have more constants and constructors in JavaFX 8 that we won't find in the 2.2 version. There were simple details to change, but Netbeans helped me, what I did was:

* Create a new JavaFX prject in Netbeans, but this time using Java 7;
* Copied the source from the JavaFX 8 project and pasted into the Java 7 project;
* Corrected the errors pointed by Netbeans.

I finished this in about 25 minutes. The main problem is that I ran into this Netbeans bug, but quickly solved by running it with Java 8, but compiling with Java 7.
I could continue programming in Netbeans, but as this was a first test, I deleted netbeans related files and created a gradle project.

First Port to Android

I carefully followed the instruction from the JavaFX ports site using the new build system, but I faced an issue with "Where Should I put my JAR?". This thread answered my question. The I noticed that I had to configure my project structure as a Gradle project. It worked very well!

After you configure your gradle application and correctly install Android tools, you just need to run too commands to have your application running in your phone:

$ gradle build
$ gradle installDebug

The harder part was to configure my phone to run in developer mode and then use the adb tool to configure the USB access.


I was really easy to port the JavaFX application to my Android phone and I am still impressed with with the performance while the app was running on the phone. Of course I would have to make a few adjustments if I want the app to be widely available, such as the size when the screen rotates...

* The source of the application ready to run on Android can be found on my github;
* A direct link to the APK can be found here.

sexta-feira, 23 de janeiro de 2015

Exporting JavaFX application as an animated GIF

To try to export an animation from a JavaFX app to an animated GIF, I started on this thread in StackOverflow, then this class from Eliot Kroo, which I am sharing on gist in case his site goes down.

Right now I am going to try his class on a JavaFX application and I will share the results here.

A sample animation

This is the animation I generated using my code :

The source for this animation is here:

Based on Eliot's class, I created one which will take an amount of screenshots based on the given period and write an animated GIF on the given path(well, right now I didn't create the source yet, hope I will be able to make it work):

This code have no great performance since the snapshot method is not "performatic". If we set a small period to collect the GIF's frames, we will face slowness.
That's all, folks! Now you can use the above code in your application to export GIT animations from your JavaFX applications!