If you're interested in functional programming, you might also want to checkout my second blog which i'm actively working on!!

Wednesday, December 15, 2010

A new free troubleshooting tool: AppDynamics Lite

Today I received an email about a new tool called AppDynamics to track down performance issues for applications running on a production environment. Maybe nice to test this one out next time I run into slow performance.

Friday, December 10, 2010

Another example why we all love IE !!

Besides from the usual frustrations I encounter in my day to day live developing webapplications which SHOULD also work on Internet Explorer, I just found another little reason to drop IE from my support list.

Why? I just had a discussion with my customer explaining that the reason why some stuff failed to work was the trailing space in a @id attribute. Firefox for instance shows the data as it should.. untouched. The customer however was a bit frustrated because when he opened the xml file in IE, he did not see the trailing space so we ended up in a small yes-no discussion ;-)



Lesson learned: Be carefull when debugging issues using Internet Explorer.

Do you really consider this a feature... or is this just one of many bugs?

Robby Pelssers

Monday, November 22, 2010

Benchmark on Synchronization in Java

The article mentions several ways of synchronizing a method and shows performance vs. number of threads. So if the solution has to scale you are better off using semaphores or explicit locks.

Saturday, November 13, 2010

Persevere, a NoSQL database

I've read quite a bit about client-server architecture but the fundamental problem seems like a scalability issue. Maintaining session state and generating not only the model(data) but also the view on the server side is asking for problems. And de(serialization) of your data between a relational database and your domain model sounds like a time consuming operation. Some databases like CouchDB and Persevere skip this tedious task and since the data is stored as JSON, it's immediately usable in the browser. So it's a matter of providing efficient ways to store and query the data from within the browser.

I was reading through Persevere and Pintura documentation and it's definitely worthwile to build a little sample app with these technologies to get a better understanding. To be continued real soon...

Wednesday, November 10, 2010

From XML to JSON using SAX

As a die hard Apache Cocoon user I deal with lots of XML data. And there is more then way to transform your data into another output format like xhtml.
  • use xslt to transform directly into xhtml -> serialize to xhtml -> render page
  • use xslt to transform to json -> serialize to text -> render the page template -> make an asynchronous request to fetch the json data -> preferably instantiate some javascript widget and populate it with the data
For my last project I created a new search module which was working from top-to-bottom asynchronously and fetched only the minimal amount of data at the time it was needed.

One use case was where I needed to transform a large xml document describing some tree like structure and the customer wanted that tree to be displayed nicely as a tree.

I'd like to describe how you can generically convert any xml document into json.

Let's first start taking a quick look at what JSON is all about.

You basically have to deal with arrays, objects, dates, booleans, numbers and strings.

Based on the above we can come up with a generic XML way of describing the above:

So we can easily write a generic transformer for this use case:

Now you only need to convert your input xml file into the generic JSON xml structure and transform it with the above stylesheet.

PDOM: Yet another XML parser

People keep being creative and xml is here to stay for a long time. So we already had SAX (not memory expensive but hard to program against) and (J)DOM which loads the complete document in memory but is easier to use.

Now they released a new parser somewhere in between: PDOM

Tuesday, November 9, 2010

Selectors API

I never really used the selectors API's untill my customer wanted to add some special behaviour on the first tree cells of all rows of a particular table body.

In combination with YUI this proves to be a really cool feature.

Some new specs for my TO-READ list:

The snippet below selects the first 3 cells from all rows inside tbody[id='fileresults']

Below the macro which generates my table using jx-template (Apache Cocoon)

YUI3 using YUI2 TreeView

I was pleased to find out that the YUI team offers functionality to access YUI2 modules from within YUI3. Anyway... just sharing some code I wrote which offers me reusable Tree functionality:

YUI 3 transaction management simplified

After using YUI3 for a while I was quite satisfied with most of the API but the IO module was still quite cumbersome to use. I came up with following user-friendly function.

Some examples of how i use them in my project:

An example of executing a synchronous transaction

An example of using a SimpleContainerTransaction

An example of a ContainerTransaction where I override the onComplete method

Sunday, November 7, 2010

WebP as replacement for JPEG

Google always is trying to cut down on loading time of webpages. Since most webpages contain many images in JPEG format they decided to use a newer compression technique called WebP and they provide a conversion tool to convert your JPEG images to WebP format. They claim an average reduction in filesize of 39% based on converting 900.000 random images.

Tuesday, October 12, 2010

example of connection url for oracle 10g (loadbalanced)

Just in case I ever need to connect to oracle again.. below an example how to configure a readable property spread out over multiple lines where the values for server1 to 4 and servicename are depending on the use case.

Saturday, October 9, 2010

creating higher order functions in javascript

For the ones who haven't noticed yet... I tend to lean more and more towards functional programming because it allows you to create higher-order functions and get more done with less code.

Below a first draft version of a javascript Collection API which shows the immediate usage of higher-order functions.

Building webservices with Spring-WS (part 9)

Part 9 (final part) will explain how we can create the client for our movie webservice.

The interface acts as a facade and hides any webservice related details from the end user.

The implementation uses Springs webservicetemplate to (un)marshall the request and response objects and do a bidirectional send and receive.

The application-context.xml below will show you how easy it is to configure your client. For demo purpose I hardcoded the defaultUri.

And finally a little unit test which can be executed succesfully if you start the movieService application.

I left out the pom.xml but the dependencies are mostly the same as for the movieService project itself.

All source files for this demo webservice can be downloaded:

One remark:
You will need to install the default-value plugin in your local maven repository which is used by the movieDomain project and you should use following groupId 'org.jvnet.jaxb2-commons' and artifactId 'defaultvalueplugin' since I configured the plugin like that.

Kind regards,

Building webservices with Spring-WS (part 8)

In part 8 we are going to take a quick look at some unit tests of our webservice.

Please take a good look at the generic return type of the findByTitle method on both the movieDao and movieService. They are not the same!!

Since our mock MovieDao only had 2 movies both tests should lightup green.

Part 9 will explain how we can quickly write an easy client (using a little facade) based on the movieDomain project from part 1.

Building webservices with Spring-WS (part 7)

A big part of our applicationContext was already configured in demo-ws-servlet.xml mentioned in part 3.


The bean 'movieWSDL' takes care of autogenerating our WSDL which will be available at http://[servername]:[port]/webservicedemo/services/movieWSDL.wsdl and it's completely based on our xml schema which we defined as our first step. As you can remember I proposed to create a separate project 'movieDomain'. Hence i reference the schema with value="classpath:webservicedemo.xsd".


As you can see I used the mocked MovieDao implementation so we can do run some unit tests quickly.

Building webservices with Spring-WS (part 6)

Part 6 will take a closer look at the movieService interface/implementation and the endpoint.

Since all our service offers is finding movies by title the interface is very basic.

The implementation uses the DAO layer to fetch the collection of movies by title provided. We only have to take care of adapting our Movie to MovieWS classes.

I created a generic Adapter interface:

And a Abstract class DomainAdapterImpl which implements 1 method.

The adapters are easy 1 on 1 mappers from our application domain objects onto our webservice domain objects.

Part 7 will take a look at our applicationContext.

Building webservices with Spring-WS (part 5)

Since for our use case we only need to retrieve data from the database I only added following methods to our generic DAO interface:

A real implementation could look like the one below.

But in order to quickly test our application I wrote a little mock implementation for the MovieDao interface.

Part 6 will take a closer look at our MovieService interface/implementation and the endpoint and I will shortly explain why I had to use domain object adapters.

Building webservices with Spring-WS (part 4)

Part 4 of this series will take a look at the domain model used by our application.
I think it's good practice to always start out your domain model with a basic interface and abstract implementation. Not only offers this the opportunity to add some generic methods/properties but it will also allow you to code in a generic way.

Part 5 will take a look at our DAO layer.

Building webservices with Spring-WS (part 3)

Let us first take a look at our pom.xml since we will have declare quite a few dependencies to do the heavy lifting.

Important to notice is we have declared a dependency on the project 'movieDomain' we created in part 1.

My web.xml:

One thing important to remember is that your main application context file has to end with '-ws-servlet.xml' and in my case it's mapped to WEB-INF/demo-ws-servlet.xml. If you rename it you will find out the hard way your project will run into some exceptions.

I decided to have all webservice related requests mapped onto the url-pattern '/services/*', but that's upto you.

In part 4 of this serie we'll take a look at some other files.

Building webservices with Spring-WS (part 2)

Now it's time to create the actual webservice and below are 2 screenshots of the completed version.

The project has been setup under the assumption the webservice will be using a database so I created a DAO layer which will be used by our MovieService implementation.

In the next article we will take a deeper look into the files listed above.

Building webservices with Spring-WS (part 1)

The next series of articles describe my experiences and my personal believe about some best practives using Spring-WS to build a webservice and a corresponding client.

The Spring documentation itself promotes using a Contract-First approach. This means you first think about your Request - Response objects and the corresponding service objects used to interchange information.

For this hands-on tutorial I wrote a webservice which allows you to find movies based on their title.

Below the xml schema used for this demo 'webservicedemo.xsd'.

It's important to know that your request and response messsages HAVE to end with respectively uppercase 'Request' and 'Response' like in my case QueryByTitleRequest and QueryByTitleResponse.

To start we'll create a separate project which contains nothing more then the generated JAXB classes. The reason why I advise this approach is that you only have to generate the stubs once for both the webservice itself as well as the client and add this project as a dependency.

Create a new source folder src/main/resources and copy webservicedemo.xsd to this folder. Also create a bindings.xjb with the content below.

As you can see I created a SimpleTypeConverter which contains some methods to convert to and from java.util.Date to the XML String representation of a Date.

The pom.xml for this project is show below:

Now we can generate our jaxb stubs using the command below:

If we also run mvn eclipse:eclipse again the folder src/main/generated will be added as a source folder.

In part 2 I will start with explaining step-by-step how to create the webservice itself.

Monday, September 27, 2010

Conditional breakpoints in Eclipse

If you ever have to debug a large for-loop in your code and you only want to inspect an iteration if some condition holds true, you should set a conditional breakpoint.

Higher order functions in XSLT

I read this very interesting article which almost sounds like using clojures in java but then for xslt. Anyway... when i have the time i'll do a few tests and see if I can directly benefit from the approach mentioned over here.

Behaviour Driven Development

One of the latest hypes is BDD. In Scala there are quite a few BDD libraries available. E.g. Specs.

I was curious to see if there was any library available for Java as well and I found a few:

My personal favourite based on the examples would be EasyB which allows you to write stories with a combination of human understandable language and groovy's expressiveness to test the actual behaviour.

I will shortly try to explain what BDD tries to offer next to good ol' plain interfaces and what some shortcomings of normal interfaces are.

Let us start with discussing following interface:

So this interface should form 'the contract' for the concrete implementation. But what would prevent me from writing following implementation which does nothing from the behaviour we intended.

And for all we know the method could be invoked like addRecipient("Robby Pelssers") which is not exactly a valid email address.

So if we were to add behavioural conditions to our interface, we might be better off. Let us take a look at a revamped version of our interface. The syntax will of course depend on the BDD library, but the idea should be clear.

Sunday, September 26, 2010

Hibernate and Spring - Part 6

Everybody who uses hibernate in a project is bound to run into the famous LazyInitializationException problem. By default, collections are loaded lazy. That's in most cases also what you want. E.g.: If you query Facebook for users with firstname 'John', you don't want to fetch all available data about those users in 1 go. You only want to get their complete names. If you click on the name, you want more detailed information.

You should definitely read Hibernate Fetching Strategies

Below default behaviour (fetch=FetchType.LAZY) which can be omitted.

One way to make sure you never run into lazyinitalization exceptions is to fetch eagerly. But be aware for the penalty on performance and memory consumption. You might be fetching big parts of your database if you use this strategy.

One other way to solve this issue is to add following snippet to the web.xml

Hibernate and Spring - Part 5

So let's take a look at one of our domain object's DAO interface and implementation:
  • JobDao
  • JobDaoImpl

As you can see the only thing we still had to implement was the method getEntityClass(). I also added a method to find a job based on its workflowname and itemname and used my convenience methods.

It calls the findListByProperties on the DaoImpl which dynamically creates a HQL string and calls the findByNamedParam(String query, String[] parameters, Object[] values) on our hibernate template.

The querystring we end up with looks like

Friday, September 24, 2010

Hibernate and Spring - Part 4

Ok.. So now that we do have our Domain Objects annotated and ready to be used, we need to write our Service or DAO layer which provides following methods for our domain objects:
  • find/query
  • delete
  • insertOrUpdate
So we already have defined our SessionFactory and HibernateTemplate. Time to make good use of it. First we define a generic interface and next a generic implementation.

One remark: Many methods are just one-1-one mapped to the typical hibernate template methods but I added some extra convenience methods like
  • findUniqueByProperty(String propertyName, Object value)
  • findUniqueByProperties(String[] propertyNames, Object[]values)
  • findListByProperty(String propertyName, Object value)
  • findListByProperties(String[] propertyNames, Object[]values)

Hibernate and Spring - Part 3

Now let's take a look at 2 of my DomainObject's: Job and JobEvent.
The relationships between both domainobjects are simple:
  • A job has 1 to many JobEvents
  • A JobEvent belongs to 1 Job.

Hibernate and Spring - Part 2

I think it's always good practice to extend some generic superclass for your Domain Objects. Even if they don't inherit anything at first, you might decide otherwise later on and it becomes very easy to add common methods and variables. For my use case I decided that every Domain Object had to be uniquely identifiable.