Friday, August 31, 2012

JSF2 Byte Boolean Checkbox

A few days ago I needed to convert old Icefaces 1.8.2 (JSF1.2) project to Icefaces 3.1 (JSF2) and found a problem when converting Byte to Boolean for the checkboxes. Byte value was generated by Hibernate from database and that is the model that already exists so we really cannot change that. The option with JSF1.2 was to implement Byte to Boolean converter and override rendering kit to call it. JSF2 does not allow this anymore or it is not working (expression library only coerces String to Boolean). After some research, the only option was to automatically generate Boolean getters and setters for existing files. Since there is quite a bit of them spread through different projects, I needed an utility to do this. I ended up writing a small Ruby application for this job. Here is the code:


This is by no means perfect code, but it did the job. If you have a better suggestion, please let me know!

Thanks

Tuesday, August 21, 2012

Why use jBPM and Drools


I just wanted to give some intro to the people that are considering using jBPM but still need some picture as where this would fit in their system.

1. What is BPM?


Taken from Wiki:
Business process management (BPM) is a holistic management approach focused on aligning all aspects of an organization with the wants and needs of clients. It promotes business effectiveness and efficiency while striving for innovation, flexibility, and integration with technology. BPM attempts to improve processes continuously. It can therefore be described as a "process optimization process." It is argued that BPM enables organizations to be more efficient, more effective and more capable of change than a functionally focused, traditional hierarchical management approach.

From my perspecitve, simply said, BPM should allow company to circumvent complex development cycles, give more control to customer over what is happening in the process itself and significanetly cut the cost of creating new solutions for the customer as most of components should be reusable. Documentation and help are readily available and development is constant as jBPM is open source.

2. Why use it?


To develop a custom solution is sometimes needed but when exapanding usage and bringing in new technologies where companies are trying to cut costs, we need to be focused on how to implement what we have with anything that might potentialy benefit us and customers at the same time. This means using the knowledge of majority of the people that are alreay experts in the matter but they are not directly working for your company. Maintating and testing such complex sultions takes a lot of time and effort. Also, constantly thinking about new ways to improve functionality and tying the resources into this is not always the best idea, unless your budget allowes you to do it. Contributing to the community (should your company allow it) would be a much better and cost effective way of doing it.

3. What was the need in our company?


We are always faced with the question of how to improve the functionality of the system to allow it to easily integrate with various tools, but on the other hand, to make it so it is stable and reliable as our solution needs to be used in insurance industry. Most of the problems that we are solving are common for the insurance industry but going from customer to customer there are frequent requests to implement a specific functionality that separates one customer from another in the market. Having some competition just makes this process more intense as legacy systems are not so easily replaced.

BPM came into story as we needed more flexibility in our back end systems to allow customers to create and shape processes using only Desinger where we would create custom sevices that can be assembled into different processes with minimal help from development team.

4. How to implement and take the most from this technology?


This is the scenario that we envisioned on how BPM and Drools would be used:


  • Development team creates processes and back-end logic with custom work items where most of the logic that we already have is reused and fortified so it can be called independently
  • Development team creates initial processes to show how BPM should be used 
  • BA team creates initial rules using BRL and DRL file packed into the PKG so we can version every release and use agents to reload resources dynamically 
  • Development team creates integration of the BPM engine into the system
  • Customer starts modifying processes and submit them to Guvnor. These resources are automatically applied (after QA testing and busniess approvals)
  • Customer starts modifying rules by changing and removing old ones and introducing new rules as needed. Rules are dynamically loaded into application (no need for restart or recompiling)
  • Process is iterative and has minimal involvement from IT team (only for implementing new functionality)


When integrating BPM, there are several things to consider:


  1. Is your system stateful or stateless? BPM generaly uses stateful session, so you need to know the bounds and lifecycle of this session even if it is executed briefly.
  2. Where to create your Knowledge Base. Crating knowledge base can take time, so you can consider either creating it in some application scoped object that is initiated once or creating singleton object for the Knowledge Base as you access BPM engine.
  3. How to invoke rules? There is an option to invoke rules through BPM directly, but we chose to do it in the work item code by creating another stateless session. This gives us much more control over which objects are passed into the session and we can reuse code if we want to use Drools without BPM.
  4. How to load resources? KnowledgeBuilderFactory vs KnowledgeAgentFactory. Static vs dynamic. We chose this to be configurable to allow us to load resources from packages and to connect to Guvnor and load resources from there. If you are loading resources from Guvnor, you need to consider that the Guvnor will not only become your source control but integral part of the application. On the other hand, resources can be loaded dynamically from files too, if you choose not to use Guvnor. This allows you to change the process and deploy the resources without touching the application. You should not forget to start the listeners though, if using agent to check for changes in packages.
  5. Transport and security of the user tasks. If you need user tasks, you can use several different types of handlers (WorkItemHandler). Please note that you will need to build/configure some security around those. Sine the source is open, you can change queries that are executed through already defines interface by defining custom mapping-file in persistence.xml. 
  6. Versioning. When you deploy one ID of the process, you will still need the old one as you might have some processes persisted and in need of continuing. You will then copy process and create new version with different ID. When starting new process you will need to use new ID and also have both BPMN resources loaded as the one that is deserializing and continuing will need old definition. The other option is to fail the process or manually convert to new process.
  7. Transaction management. We needed to implement 2 data sources (one XA) to make this work, so plan where your transaction will start, join and where it will commit/rollback.
There are probably many more, but these were important to me.


I hope that I gave you some ideas on why would you choose jBPM in your application and hopefully this will make your customers happy. As always, to find extensive information, please use JBoss help site(s)/forums and manual as there is plenty of information there. You can get a lot of help from IRC too.

Thanks!

Thursday, July 12, 2012

Hunting in Canada

A few weeks ago I passed my Ontario Hunter Education course. I decided to take the course as I felt that I needed to connect more with nature through hunting as something that was common for people to do in the past centuries. Becoming a hunter and hunting is something that I have always wanted to do but somehow never found the time to do it. Well, if you decide to start with this and live in GTA, these resources might help:

1. Ontario Hunters Education Program
2. Ready AIM

The second link is from the company that offers hunting and firearms courses. I have had a great time listening to the courses as people working in Ready AIM are great and knowledgeable. A word of advice, book your courses soon as they get sold quickly.

I decided that I wanted to Bow-hunt but even for that, to get a hunter license, you will need to pass non-restricted firearms course (Canadian Firearms Safety Course). Course is not difficult and you will get a lot of help there.

All the best to the future hunters and please hunt safely and ethically.

Working with jBPM 5


I just wanted to say a few words about jBPM. jBPM is a great product that comes from JBoss and used with several other technologies we can use it to make effective and easy to understand workflow. Our task was to replace current Java based back-end and integrate it loosely with jBPM with possibility to replace BPM engine with some other vendor in the future, if needed. Together with this we would need to modularize our back-end and expose it to BPM adding greatly to the ability for our customers to custom craft and change processes to the way that best fits their business. We already have a great platform for insurance and BPM should make it even better and more versatile. Time to change the process flow and to apply new design should be dramatically cut after BPM is introduced.

After some time of using it I have to say that it is a great product that comes free and that will add a lot of value to already established backend.

In this blog I want to point out to several problems that costed me a lot of time to figure it out and hopefuly this should make understanding jBPM easier.

To start of we needed some documentation and found some basic usages on the JBoss site. It should be enough to start using jBPM but I have to say that initially I was not pleased with the amount of details and some complex usages requirements that we had. We needed following components to make this work:

  1. jBPM libraries
  2. Drools libraries 
  3. Guvnor and Designer setup
  4. Database setup for jBPM

Our deployment and usage was for JBoss 5.1 and Lifray 6. Database version was not much important except that we needed three extra data sources.

  1. Data source to load users from Liferay into BPM database so we can use it to assign human tasks to our users and verify user roles and groups when accessing MINA server
  2. Data source for BPM
  3. Data source for Human Task Service

Please note that all three data sources were implemented using JPA as this is JBoss implementation. The difference in our application is that we needed to combine such approach with Hibernate mapping files that we already had. One of the big issues that I have faced was initiating EntityManagerFactory. Initiating this factory in the middle of the EJB call was not an option, so creating JNDI connection when JBoss started was a way to go.

Sample code:

In persistence.xml:


In your code:



Our code structure was also a bit different from the examples as we had this call structure: Web->Business Delegate->EJB (Stateless)->BPM->Java back-end. The problem is that command based listener is coded in such a way that is has asynchronous code inside that assumes that your session is stateful. In our case this was not good and we needed to rewrite this listener into two classes, one that would create human tasks and other that would process messages with blocking handler. Otherwise, you would need to create some loop to wait for the execution of the code (checking status of the process) in some cases or your EJB call would end up sooner than BPM call.

Some of the other things that need to be watched for is that when you create knowledge base (talking about KnowledgeBuilderFactory), you would preferably do it as a singleton, as creating and importing resources every time takes valuable time. We chose not to bind our application to Guvnor and we decided to use Guvnor only as a development resource (we already have plenty of repositories to worry about).

JBPMHelper is a nice class to start with and I used most of it while rewriting some parts to offer better separation when doing unit tests (during Maven builds) and production usage. We also needed to integrate our configuration engine there and to apply custom created command based listener.

When developing for jBPM pay close attention to ksession.dispose();. You need to call this when you no longer want to work with BPM as resources are not automatically disposed.

Creating custom work item handler is a must in BPM when you have much back-end code, so here are couple of tips that might save you some time. When handling Exceptions, pay close attention to nesting in BPM depending on your structure as you might end up messing up some of the transactions. BPM code executes like this A->B->C->code->C->B->A. In the middle of that you should properly completeWorkItem or abortWorkItem. It is important that you catch your exception and know what to do with it rather that let BPM decide. In our case letting BPM decide what to do was not a good option.

One of the things that I like to do is to group work items so I do not have to write same code over and over again. This means that I would have one input parameter called "function" that would indicate which peace of code would I like to execute once the work item is called. If you have dozens of function, this will save you a lot of time.

Finally, I would like to say a few words about a Designer. A developer from JBoss helped me a lot (Tihomir Surdilovic) with understanding and I wish to thank him for that. For a free product, it offers great possibilities and it is very easy to work with. Your developers and customers will enjoy using it. I say customers as you can expose this directly to them to create shell of a processes that can be later passed on to developer to implement some basic "plumbing". Customer->BA->Developer integration was never more easier.

Designer comes as separate WAR that is deployed together with Guvnor. Guvnor is a repository and the Designer is a tool to craft your processes. Here are a few tips for using and deploying it. When deploying, rename wars to guvnor.war and designer.war Guvnor can find Designer. When creating processes, you can rollback to previous version. Just open process, go to Attributes->Version History and select which version do you want. If you need to delete anything, you need to archive it first and then delete it from the archive. When deploying your resources, I strongly advise you to deploy them as a package (you need to compile them and then package all resources). This way, it will be easy to bundle all the data in a single file and give it a version. The only problem that you might have is the migration of the current processes to the new version if there are saved processes in the database. This means that you will need to convert those manually as they will not be able to deserialize into a new process structure.

These are just a few tips and I intend to write more about BPM and Drools as we progress with development and integration of the BPM into our own platform.

Here are some of the useful links:

  1. BPM User Guide
  2. Guvnor Manual
  3. Drools Introduction
  4. Tihomir's Blog


Thanks

Sunday, May 20, 2012

Shihan Fred Haynes 7th Dan - Aikido Georgetown

On May 7th, we had a fantastic seminar held by Fred Haynes from Victoria, BC. Seminar was held in Georgetown Aikido club. It was my first visit there but, I have to say that the club and people training there are great. We had a good time and there were two senior Dan grading held (4th and 5th Dan). 4th Dan grading was for my Sensei Fred Springer from Seikeikan Yoshinkai Aikido. I have to say that it was a great grading and we all enjoyed watching. These are some of the photographs taken by Dan (he trains with us too :)):


Anyway, grading was great, training even better. Most of the training was focused on center line when doing technique and what Shite needs to do to maintain control of the opponent and keep focus on that line. One of the techniques that we touched was Sankajo (Sankyo) and effectiveness of the pin regarding the position of the Uke shoulder when finishing the technique. This one in particular was focusing on making space between yourself and Uke and keeping Uke's shoulder pinned to the ground. A very nice and effective way to finish the technique. 

I do hope to attend more seminars from Fred H as he has very effective and entertaining way to teach Aikido. If you were at the seminar, please let me know what you think...


Thursday, April 12, 2012

Working with .NET

I have been working with Java most of my professional life, and I have to say that not always has it been easy. In fact, most of the times it was rather difficult to build up knowledge, find and use dependencies, write unit tests and apply correct patterns.

A few days ago, I needed to build an application in .NET as we needed to print and process some Word documents. I have been using C# before but it is not my area of expertise. Obviously, I needed to refresh my knowledge and build an application that can perform well. I was simply amazed how easy was it to pick up basics, start development in Visual Studio and create variations of the application be it, console, form or web. It took mere hours to understand basic concepts, get to know Visual Studio and to be productive. For Java, to achieve the same for the person coming from .NET would take days to achieve same results (think Swing)! I am not a big fan of Microsoft products (being able to run only on Windows machines) but BRAVO Microsoft for making development so easy! It is no wonder that this platform is so widely accepted.

Java development tools are getting there, but having so much diversity can be good and bad at the same time and we will just have to wait and see. Until then, listen to this song (my friend sent this to me) We Code Hard :).

Wednesday, March 07, 2012

Moving from Ant to Maven


I have to admit that I have not used Maven a lot until perhaps mid last year, when I realised how difficult it is becoming to maintain dependencies and builds for J2EE projects using Ant. Many of my colleagues were faced with same problems but most of them still choose to stay with Ant.

Ant was here, it seems, forever. It is well established and mature product and almost all developers understand it and know how to finish the job with it. On the other hand, when Maven came on the first look it seems that developers could not define explicitly what needs to happen when building the project (or at least this was my experience). Everything was implicit with an option to override the default settings. This was a confusing part opposing to the Ant where you needed to define everything explicitly. Not all understood what a project lifecycle was and why it was important. What Maven is today is described here, "What is Maven".  In simple words, these are few benefits that Maven provided to me:
  • Default setting out of the box with need to change build and deployment setting where needed
  • Dependency management, where if I need one dependency, Maven will automatically include all other needed dependency files and will exclude conflicted dependencies
  • Ability to define which dependencies are used for deployment and which are used for test (for example, you need to start in memory database for unit tests and do not want to deploy those jar files to production)
  • If we want to change version of the main dependency, all other relative dependencies will be updated automatically
  • Standardizes project builds through build lifecycle
  • Allows easy separation of code and unit tests

Maven does a lot more, but if we take only items listed, it will be clear how much more your life will become easier. One of the major problems in switching to Maven is how we are thinking about referenced projects (for example if we have dependencies on other projects in the workspace). Maven will not support this (there are workarounds but the only proper way is to create local repository). Maven needs to work with artifacts (basically versions of jar files). To achieve this, all you need to do is to setup in-house Maven repository (for example Sonatype Nexus or Artifactory from JFrog Artifactory) for local files and proxy repositories that are on the Internet. This will enable you to have you own repository with your artifacts (libraries). Many developers will usually avoid this (versioning) as when including them with it would mean that every time they change library project updates with dependencies will need to happen (for every library). On the other hand, working with same version would help avoid updating references. Developers would usually track libraries in the version control systems (or similar) where files are always given the same version under a different release. This, of course, has to change if we want to use Maven, and we need to start versioning the libraries and then uploading them to a local Maven repository. Then we can start including those dependencies in our project, but not as a referenced projects, but as a Maven dependencies. In the case that we need a quick build of the library, we can always install it in our local repository (in Win 7 \Users\User\.m2). This way, it will become available to be included in a project that needs this dependency and later on, it can be published in centralized repository for the rest of the team. This, of course, requires change of culture in the organization. In the end, it pays off big time.

One of the other problems developers are faced with is setting up local environment when starting a new development and making it easy to start being productive (business-wise) and not spending time resolving technical issues with environments and dependencies. Maven does this in an easy way. It not only saves you trouble of uploading you jar files to CVS or SVN, it has different plugins (like Eclipse plugin) that can start projects very easily and where it will setup dependencies from repository based on what is in the pom.xml file. Try it one, and you will never go back to Ant.

So basically, for Maven to work correctly, we need correct directory structure and correctly setup pom.xml file. For directory structure, you can reference this Maven directory layout and for POM setup, this Maven POM. Please note that POM can be simple as just as adding a few lines to describe your artifact and group. (This is like the project name and the package). If you have, for example, m2e plugin for Eclipse, you can see an example of Effective POM that will have a lot of information that you can use to customize your POM. It is up to you how much customization you want or need.

In my experience, even though Maven has an Ant plugin, mixing two of them is not a good idea as it might interfere with Maven build lifecycle (Maven build lifecycle).

And for the end, just to mention that Maven helps with one of the biggest problems that developers have when debugging the code. What happens when the source code from the 3rd party libraries is missing? Maven gets this for you automatically. That means that you can now see what you are debugging when going in depth to figure out you problems.

Please note that this text was written in very simple terms and if you would like to fully understand how Maven works, please refer to Maven site for further documentation. This article is not comparison of Ant vs Maven, but rather it reflects on benefits of having Maven builds in your environment. All projects can be built with both Maven and Ant.