Insane Programming Java ramblings and finds on the net

23Jan/124

Why I never use the Maven release plugin

Just about every 6 months or so an article appears cursing Maven, attracting both proponents as opponents to Maven and Ant. While it's real fun to watch (I really get a laugh when people start to advocate the return to Ant), most of the time it's always the same arguments. Maven lacks flexibility, the plugin system sucks (when will people learn to use plugin versions...), you can't use scripting and the all time favorite: the release plugin sucks. Well, I am a Maven addict and I'm happy to say: yes, I agree, the release plugin sucks. Big time. But here's something you may have forgotten: you don't need it! Even more: you shouldn't use it.

Filed under: Maven Continue reading
22Jan/123

Exploring Spring Integration 2.1

At my current assignment, one of the things that we do is reading XML files from an FTP site, do some magic on the contents and send the modified data to a REST service. If you've read some of my earlier posts, you've probably guessed this is done through an IBM product (Websphere Process Server). Given the fact that WPS is just an ESB with a BPM engine, I wanted to see how difficult it would be and how much faster I would be if I'd use an open source product for the ESB part (there's an article coming on the BPM part too). As I'm a real Spring fan, I decided to give Spring Integration (I'll refer to it as SI from now on) a go, as other products seemed a bit too much work to start with (Mule and ServiceMix have their own server implementation for starters...).

17Jan/120

Hibernate’s bidirectional @OneToOne and lazy loading

Just a small post now, about something I discovered when developing a JPA example project concerning one-to-one relationships and fetching them.

22Dec/110

Having fun with JSON and DbUnit, continued

The beauty of a blog like this is the fact that people read the articles and take the ideas in them to the next level. And that's exactly what Dan Haywood did.

He reused my JSONDataSet idea and combined it with JUnit 4's new Rule system to add configurable dataset loading to individual test methods. It's a basic, yet very functional implementation. But I thought, what the heck, let's take his idea a step further.

15Aug/117

Getting Hibernate out of your POJO’s

Anyone using Hibernate for the persistence layer in their program, knows that a JPA POJO contains traces of proxy class types from the Hibernate API. For example, if you have a one-to-many relationship between 2 entities expressed by a List in one of the entities, the corresponding implementation of that list when retrieving that object using Hibernate will be PersistentBag. The same goes for set, respectively represented by a PersistentSet. To-one lazy fetched object references are at runtime replaced with subclasses of the actual entity, which also implement Hibernate's lazy fetching. But so far for basic Hibernate POJO pollution.

In most cases, this isn't really an issue. Unless your transaction is already closed and lazy-fetching isn't possible anymore, in which case you'll get the dreaded LazyInitializationException. In web applications there are workarounds available to avoid this kind of issues, fetching the needed fields if they need to be show on screen. However, there are a couple of cases in which lazy fetching is a bad thing. For example, if you're exposing your entities through a web service, say REST, you need to marshal your entities to an XML or JSON format. In that case, you probably have retrieved the entities with the entities and their relationships you intend to marshal. In most cases, you also don't have an open JPA transaction at that moment. If you did, you would marshall the entire object graph, lazily fetching entities every step of the way, ending up with something possibly a lot larger than you wanted. If you don't, you'll will most certainly run into a LazyInitializationException. The same thing happens if you're using Dozer, mapping between data structures. Or when you just don't want pieces of Hibernate's API on the client side, for example if you're using a client-server architecture. All of this is the reason why DTO's are still present in some JPA-based systems.

After running into the issue more than I would like (in my case, mostly Dozer related problems), I looked to Google for answers. I found some interesting ideas, but none that fit my use cases. I hate the idea of a DTO, but my solutions needs to account for DTO usage. The solution also needed to be a one-size-fits-all with as little configuration as possible (in which case custom mappers were out as they required a lot of configuration). Then, I finally found it, thanks to a post on StackOverflow: tucked away and undocumented in the Dozer API is the support for custom field mappers. I took the solution and used it as a template for a more generic solution. Here it is:

public class HibernateFieldMapper implements CustomFieldMapper {
    public boolean mapField(Object source, Object destination, Object sourceFieldValue, ClassMap classMap, FieldMap fieldMapping) {
        if (!(sourceFieldValue instanceof PersistentCollection) && !(sourceFieldValue instanceof HibernateProxy)) {
            return false;
        } else {
            if(Hibernate.isInitialized(sourceFieldValue)) {
                return false;
            }
            else {
                destination = null;
                return true;
            }
        }
    }
}

It's so unbelievably simple and elegant. You put the custom field mapped on your DozerBeanMapper and voila, no more lazy fetching or LazyInitializationException. Finally a way to get Hibernate out of your POJO and safely be able to marshal a JPA POJO into anything you want.

Filed under: Frameworks 7 Comments
5Aug/113

How to avoid taking the Big Blue pill

Intro

As a Java consultant, I come in contact with IBM products quite often. More often than I would personally hope. I'm not saying that IBM makes bad products, they have their merits, but more often than not the choice for IBM products is more out of corporate compliance than it is because of the technical needs of the projects than are built on top of the projects.

Currently, I'm working on a project that uses a whole lot of the IBM stack: the DB2 AS/400 database, IBM Websphere 6.1 and 7, IBM Process Server, IBM Portal Server on top of a Websphere 6.1 installation, all developed on either RAD (front-end and back-end) or WID (the processes). It shouldn't come as a surprise that setting up the development workspace is a couple of days worth of work. And then you can only hope you haven't forgotten to apply the patches and that the integration between all those products has gone flawlessly. Off course, this kind of setup also means that you need one hell of a development machine when you want to develop through the entire chain of the application. 2 Gb of memory is not an option here, folks.

This got me thinking, since the last project I was on was also mainly IBM based. Is the choice for IBM projects a valid one from a technical point of view, or are there cheaper, more developer-friendly and above all, technically rivalling products out there in the open source world, whilst still keeping up an enterprise-like reputation?

19Jul/110

Atlassian scores another one: having your software set on fire with Bonfire

Perhaps a strange title, but it sums up the goal of Bonfire: making it easier for testers to make firewood out of your carefully crafted piece of software. One thing: I don't work for Atlassian, so this isn't an advertisement. Well... actually it is, just not one ordered by Atlassian.

23Mar/110

Series: Multi-threading in Java – Part 1: ExecutorService

Starting with Java 5, many multithreading and more specifically concurrency features have been added. We'll go over a couple of them in the next couple of articles.

ExecutorService

Before Java 5, you either used a 3rd party thread pool or implemented your own pooling in order to handle multiple threads. With the introduction of the ExecutorService implementations, this is a thing of the past.

Filed under: Java SE Continue reading
2Feb/110

MySQL managed connections in tcServer

MySQL connections configured as JNDI resources always have been a pain in the ass on Tomcat. Mostly because of the fact that the MySQL driver can't handle connection failures correctly (keep a connection idle longer than the socket timeout on your MySQL server and you'll see what I mean).
Recently, I've been hunting down a bug on our company's JIRA server: invalid database connections were still held in the connection pool. So I consulted the JIRA documentation and they had a specific section regarding this problem. So I applied the instructions and restarted the server. It worked once again. But not for long: the day after, I was confronted with the same problem. So back to the drawing board.
I started thinking once again. The instructions for the solution were meant for Tomcat 6+. tcServer is just a glorified version of Tomcat, sprinkled with some SpringSource sugar. Perhaps SpringSource did something to the connection pooling? They did. They replaced the standard DBCP pooling with a high-performance concurrent connection pool... with extra parameters. Instructions can be found here. So lesson learned: never assume.

27Dec/1027

Why I think web applications aren’t always the best choice

Web clients are hot. Hip. Sometimes even hype. But having worked on different projects, both fat clients and web applications, I'm starting to grow a consciousness on when a web application is appropriate and when the choice for a fat client would be a more sane one.

Fat clients have a bad reputation. Deployment issues being the main culprit here. With web applications, you just deploy it on a server and every user has instant access to the application. Fat clients, for example using Java Webstart, need to be installed on the local client. They also require a JVM. Updating is not instantly, but requires a restart in most cases (unless for example you're using some fancy modular architecture that allows updating on the fly). From an operations standpoint, fat clients are maintenance burden that isn't popular. But Java Webstart has evolved, and in the light of that evolution, perhaps it is time to re-evaluate our behavior on defaulting to a web client.

Filed under: Opinion Continue reading