Monday, January 29, 2007

And now I'm moving towards full fledged JEE development

Yesterday I moved my project into the realm of JEE with EJB and all. Coming from a web only application I stumbled in too many problems I didn't want to think about too much. Most of them had to do with transaction management, lousy design (on my part), and getting used to developing serverside code in NB55 (I only used MobPack for the last 12 months or so) didn't help either.
I downloaded Spring this weekend and thought it would solve all of my problems, just like a friend of mine promised, but the lack of a tutorial that would fit my needs and the lack of NB supporting it the way I'm used to NB supporting something made it a road I should have traveled.

So let me tell you something about what I am developing that is so different from all that I've seen. And probably it isn't.
I'm working on a server that is accessible by J2ME clients, they are nothing more than thin clients it turns out. It's all for a game we're developing and the world the game takes place in is ever expanding. So I have a server that serves the requests from the clients through NB's generated servlet interface. I blogged about that a while ago, a feature that is really helpful, but there are some improvements thinkable. So I went the servlet way.
The ever expanding universe I thought of doing using a thread started in a servlet. Something I shouldn't do, but still can get away with in Sun AS9 and the thread is running nicely doing its job the way I intended… almost. That is where the problems started. Every time I tried to do something with the database, I got an exception upon the commit of the transaction. I tried everything I could think of this weekend. Instantiating new EntityManagers, UserTransactions, additional PersistenceUnits etc, but I couldn't get rid of the exception. Everything I was doing with the database from the servlet code itself worked like a charm. Ergo I thought about the thread opening an URL and have the servlet behind that URL do the work the thread was supposed to do, but I dismissed that idea straight away. I knew I was sinking in the swamp enterprise development can be, but I didn't want to sink that deep. I knew also that I had a Scheduler in stock that would periodically poll the database to see if there were any jobs scheduled and than execute those jobs. Another thread in the system, that I saw doomed to fail, just like my UniverseExpander. I know I shouldn't use threads in a server application anyway.
So Spring to the rescue, but as it turns out, a quick google on "Spring, tutorial, JPA" got me with either tutorials that assumed extensive knowledge of Spring (which I don't posses), or with tutorials that incorporated Spring MVC. Problem here is that I don't have a problem with the database handling from the web end of the application, it is with the 'JSE' part (the threads) of the application. And the JSE type of database handling didn't work for me.

After a weekend of strugling with the code, the problems etc I decided to spend Sunday night on a proper design. Forget about technologies and just make a proper design, the way I do when working on a client's project. I realized that EJB would help me with the transaction management and EJB3 allows me to work with POJOs all the way, which means that I can keep it fairly lightweight, something I need. Although the in-memory model will be huge… relatively, everything happening in the system will be based on single transactions, something that EJBs can handle nicely. For now that is.

I was a bit intimidated with the JEE stuff NB throws at you. You get 3 projects, not a lot of explanation of what is going on in what project and on top of that, it generates about everything I want to do. So I right- and left-click my way around NB and generate SessionBeans, Entities, WebServices and all without really having to do something. So this is what everybody is raving about when talking about NB and JEE… just as cool as J2ME support.

While designing I found that I need some kind of event system in the server as well, pointing me into the direction of JMS.

While googling I stumbled upon Quartz and decided to use it for my job scheduling instead of developing it by myself. It provides exactly what I want and more.

I will post my proceedings in this blog and hope it will be of help to people. This blog entry is more about what I was facing than about how I was solving… so maybe Spring and JPA and just web tier are coming back… for now it is all about full fledged JEE.


1 comment:

Craig MacKay said...

I have a basic spring support module for netbeans that I made. You might want to check it out. The main purpose of the module is to help people get up and running quickly with spring and netbeans. you can download it the project page is at

Also as for communicating with spring beans from a MIDP client, you might want to look at the HessianServiceExporter in Spring. Hope this helps,