• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Metaprogramming tricks in Java

 
Filip Croes
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I'm using a proprietary product Java API (java 1.4) that has the notion of a "session" that you receive from a "sessionPool".

They state that you should only get a session when needed and release it as quickly as possible, always in the scope of a method. So a typical method always looks as follows:



This is alot of boilerplate code that I would like to avoid in the future.

How could I achieve an automatic finally release of my session when I get one from the sessionPool?

In groovy I think I would create something like .withSession { session -> ... }, but I have to stick with Java in this case.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49793
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to JavaRanch

I can't think of an alternative in Java 1.4; there might be something like what you want added to Java 7, but that's not out yet.
 
Filip Croes
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks

Java 5 would be a possibility, but 6 or 7 is not supported and will not run in their JVM.

I was first thinking of Aspects, but I don't know the possibilities or limitations of this technology applied to this example.
[ November 28, 2008: Message edited by: Filip Croes ]
 
Garrett Rowe
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How about something like:





That eliminates some of the boilerplate, but now add the line noise of having to code up a class or anonymous inner class that implements the interface. But Java is a verbose language, you just have to learn to love it. :roll:
[ November 28, 2008: Message edited by: Garrett Rowe ]
 
Rob Spoor
Sheriff
Pie
Posts: 20609
63
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is one of those few features of .NET that would come in handy.

.NET has an interface called IDisposable. Anything that implements that interface can be used in a "using" block:

This will call session.dispose() at the end of the using block.

If only Java had something similar...
[ November 29, 2008: Message edited by: Rob Prime ]
 
Gamini Sirisena
Ranch Hand
Posts: 378
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could do something like this with AspectJ.

Note that the signature of the method "example" is changed to accept a Session object.



 
Filip Croes
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the many suggestions!

Is there a way around the session method parameter in the AspectJ solution?

If I have a method without session and change my implementation so that suddenly I do need a session, then I would have to change my interface because of the additional session parameter. Furthermore, the calling class now needs to make the call to getSession(), which adds overhead somewhere else. In most cases, the SessionPool is injected by Spring configuration.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic