Vishwanath Krishnamurthi's blog

A blog on Java EE, clean code, open source and TDD

Archive for the ‘Java EE’ Category

Where to place the Java EE descriptors ?

with one comment

No intro required – these are some of the standard descriptors used in a JavaEE application. However placing these descriptors in the wrong location is a common mistake.

So here’s a list of some descriptors and the correct location they are expected in – Might save some time searching around the net.

ejb-jar.xml

In a Jar, it would go in /META-INF directory
In a War, it would go in /WEB-INF directory

beans.xml

In a Jar, it would go in /META-INF directory
In a War, it would go in /WEB-INF directory.

But note that just because you have WEB-INF/beans.xml, the JARs in WEB-INF/lib do not become bean-archives.
The JAR would need a META-INF/beans.xml separately

persistence.xml

In a Jar, it would go in /META-INF directory
It would go under WEB-INF/classes/META-INF in a WAR

application.xml

In /META-INF directory of Ear

Advertisements

Written by Vishwanath Krishnamurthi

August 25, 2012 at 11:08 am

Apache TomEE Quickstart – Details on Configurations

leave a comment »

Apache TomEE is now released (beta-1).

What makes it so exciting is that, “develop – deploy -test ” cycle is extremely fast, allowing the developer to experience the “lightweighted-ness” Well, no more talk..

Here’s a quick list on configurations.

So, download TomEE and get on with ‘EE’ the light-way 😉

Written by Vishwanath Krishnamurthi

October 7, 2011 at 11:38 am

Understanding Persistence Context

with 3 comments

Here’s something on Persistence Context !

This understanding below, is mainly based on what I had read ( and re-read ) in this great article by Sahoo

But before you read further, these are the short-forms used in this post.

PC – Persistence Context

BMT – Bean managed transactions

CMT – Container Managed Transactions

SFSB – Stateful session beans

OO – Object Oriented

What is a Persistence Context ? 

Imagine a circle that depicts the OO world. We will call this the PC circle (Persistence Context circle) Imagine another circle that depicts the DB world.

The DB circle depicts only one data-store. There can be multiple entity instances within this PC. For each entity within the PC there is definitely a corresponding row in the DB world. All entities in this PC are managed i.e. any updates (CRUD) done here will be reflected in the DB world.

Well, its a collection of entity instances. And changes to those instances gets reflected in the db rows. Is it anything more ?

Yes. You can think of it as a cache too, since this is an in-memory collection. Once an object is “attached” it is a part of this in-memory-collection.

The term, ‘Persistence Context’ looks pretty abstract. Something more concrete ?

Any operation on the collection of entity instances (PC) is managed by an EntityManager. One entity manager would be associated with a persistence context.. Something like this

And anything in the PC is modified (CRUD) only using an EM.

Fine ! So when is a PC created ?

  • A persistence context is created as soon as the entity manager is created, in the case of Application managed persistence.
  • In the case of container managed persistence the PC is created when the EM is invoked.

 I want to access this collection of entity instances in another component (bean / servlet.. ) How can I do that ?

There are two options.

Option A:  Pass around the EM instance associated with the PC

Option B:  Let the container propagate the PC. ( And associate it with the entity manager of the component that was propagated to )

Option B being much cleaner.

This is called as Persistence Context Propagation. But there is a catch here. It doesn’t do it every time. When using BMT it thinks that you know what you’re doing and doesn’t propagate the PC. PC propagation comes up only with CMT. We know that a transaction can be propagated. REQUIRED, SUPPORTS allow transaction propagation. A PC is propagated along with such a transaction propagation.  But note that there’s no propagation to a bean that already has a PC associated with it !

Written by Vishwanath Krishnamurthi

August 21, 2011 at 7:21 pm

EJB 3.1 Cookbook by Richard Reese – Review

with one comment

Recently, I received a free copy of EJB 3.1 Cookbook, from PacktPub for review.

But I must say that it did not meet my expectations in a lot of ways.

Some had to do with

  • Bad practices given as recipes
  • Lengthy sections on concepts not related to EJB
  • EJB 3.1 Specific features not being highlighted enough.

To explain,

EJB 3.1 Specific features not being highlighted enough:


If you have been following some popular blogs on JavaEE, you’d have already been exposed to some EJB 3.1 features. I fall into that category. And my natural expectation was that, this book would explain those concepts in much more detail. But much to my disappointment, it was the other way around.

 In ‘Packaging the EJB’  Chapter :

I expected to learn about how packaging EJBs was made simpler in EJB 3.1 compared to EJB 3. The most important one being that, it’s no longer required in EJB 3.1 to make an .ear file, and EJBs could now be packaged within WAR.

There’s only a two line description of this, something that a reader could easily miss.The consolidated table in page 348 misses to specify this too.

Instead there was a lengthy explanation on how to archive using the ‘jar’ command in Java, something that I’ve never seen developers doing.

 The Embedded Container:

EJB 3.1 makes testing EJBs easy with the embedded container.  And a team could benefit a lot writing unit tests with this newly introduced API. But there was only a small section on ‘Accessing EJB 3.1 embedded container’.

Wish more examples / more focus on embedded container were present.

Bad practices given as recipes:

EJBs were accessed directly from JSP using scriptlets! Using scriptlets in JSP is considered as a bad practice, something that was done a decade back.

Hundreds of threads in Javaranch explain users of it, and you’d find JSP moderators (like Bear Bibeault ) explaining that often.

Lengthy sections on concepts not related to EJB:

Sections ‘Exception Handling with EJB ‘ and ‘Using Logging with EJB ‘ had nothing to do with EJB after all and merely talked about good practices on exception handling and logging rather than talking about EJB specific exceptions.

I don’t feel the need for a general lecture on “exception handling / logging ” in a EJB 3.1 specific book and if at all, present, I don’t see why it should be in a chapter rather than appendix.

The same about the sections ‘Efficient mainpulation of Strings‘ and ‘How to support Currency

 The good part:

The good thing you’d find about the book is that it is pretty easy to read through, with the sections of the code being discussed, presented in bold.

The language was kept simple, with screenshots and tables when required.

To summarize,

This is a book, that is easy to read. Had it only concentrated only on ‘EJB 3.1’ specific features, in 100 odd pages, it would’ve been a pretty good.

If you wanted a book to get a quick rough picture, in a week’s time, then I’d recommend this. If you had more time, and wanted a book to help you develop a firm understanding of the concepts, I wound not recommend this one.

2/5 Stars

Written by Vishwanath Krishnamurthi

August 8, 2011 at 6:35 pm

Posted in Java EE

What’s cool about using OpenEJB for testing EJBs ?

with 2 comments

You have two forms of OpenEJB servers.

  1. Embedded
  2. Standalone

The embedded server comes in very handy when you want to test your EJBs.

Yes. Testing your EJBs become as simple as including the openejb-core dependency and writing 3 lines of setUp code in your test.

private InitialContext initialContext;

   protected void setUp() throws Exception {
       Properties properties = new Properties();
       properties.setProperty(Context.INITIAL_CONTEXT_FACTORY, "org.apache.openejb.client.LocalInitialContextFactory");

       initialContext = new InitialContext(properties);
   }

And the tests run in milliseconds 🙂

Links from the docs:

  • See here for an example bean and a test.
  • And if you wanted to dig in and make some more changes / configurations this doc might help

META:

Used http://zwibbler.com/ to scribble the diagram. Love this one 🙂

Written by Vishwanath Krishnamurthi

June 23, 2011 at 8:11 pm

Posted in Java EE

Tagged with , , , ,