users@glassfish.java.net

Re: EJB3 Remote Referece Thread Safe?

From: Witold Szczerba <pljosh.mail_at_gmail.com>
Date: Wed, 15 Oct 2008 18:11:36 +0200

2008/10/15 <glassfish_at_javadesktop.org>:
> Did you mean that I should do a "lookup" for the bean, in order to get a "fresh" reference any time I have to call a method on that bean? It sounds a bit weird, isn't it?

Not literally. I have created simple utility like this:
utility class Services:

public static <T> T getService(Class<T> serviceClass) {
  // lookups and returns service object
}

So you do not deal with lookup every time you need service.
Now, in (for example) Person form, when someone opens it, it can use:

PersonServiceRemote service = Services.get(PersonServiceRemote.class);

and this form use it. When someone closes that form, and opens again
(or another one) then new stub is looked up. If there are two forms
opened, each one has its own object used to communicate with server.

Of course, now, after 2 years of learning how things should be done, I
would not create static method "get". I would use dependency injection
pattern (either manual or using some library), so my person form would
declare dependency on some service and it would have it injected. That
leads to easy mocking, so you can launch just a form with a stub to
quickly check if it does work without application redeployment.

To be 100% fair, I should note, I actually do not call remote services
beans from the view layer very often. That is a job for lower layer of
application (I use presentation model pattern), but yes - forms
sometimes "talk" to the server by their own.


There is, however something else I did not mention. My Services
utility class wraps looked up objects using CGLIB and I am
intercepting EVERY client-server call in one single place. In this
place, I am checking for EJB specific exceptions and if something goes
wrong I am able to look the bean up again and re-invoke the method in
a way that calling side does not know there was something wrong. So,
when I have, for example, server restart - my client can still use it
without restarting, as it will automatically re-fetch every stub which
was looked up before server shutdown.

If you are interested I can show you exactly how my services class works.

Regards,
Witold Szczerba

>
> Anyway I'll give it a try.
>
> Thanks!
> [Message sent by forum member 'abelmj' (abelmj)]
>
> http://forums.java.net/jive/thread.jspa?messageID=307322
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe_at_glassfish.dev.java.net
> For additional commands, e-mail: users-help_at_glassfish.dev.java.net
>
>