2

I have a few Servlets which call remote EJB session to manage user's requests. At a first time I created a new manager in each method this way:

ManagerAdminRemote managerAdmin;
Context jndiContext = new javax.naming.InitialContext();
Object ref = jndiContext.lookup("ManagerAdmin/remote");
managerAdmin = (ManagerAdminRemote) PortableRemoteObject.narrow(ref, ManagerAdminRemote.class);

To avoid this repetition, I implemented the init method like this:

public void init(ServletConfig config) throws ServletException {
    super.init(config);
    try {
        Context jndiContext = new javax.naming.InitialContext();
        Object ref = jndiContext.lookup("ManagerAdmin/remote");
        managerAdmin = (ManagerAdminRemote) PortableRemoteObject.narrow(ref, ManagerAdminRemote.class);
    } catch (NamingException e) {
        e.printStackTrace();
    }
}

With managerAdmin declared as class attribute.
Session Bean ManagerAdmin is stateless.

I'm using JBoss 5 and I saw that init() is called the first time the Servlet is called. But I also noticed that all the users share the same objects declared as class attribute. So this way, for instance, different users will share the same managerAdmin.

For now I didn't encounter any problem, but I'm asking: could this sharing bring problems of any kind? Delays? Or, since managers are stateless, is it fine?

Thanks in advance.

Bhesh Gurung
  • 48,464
  • 20
  • 87
  • 139
Simon
  • 4,072
  • 5
  • 27
  • 49
  • 1
    http://stackoverflow.com/q/4498234/738746 – Bhesh Gurung Dec 28 '11 at 19:53
  • 1
    As for your lookup code, please note that `PortableRemoteObject.narrow` is an EJB 2 method and is completely unnecessary in EJB 3. Just cast the result of the lookup directly to the `ManagerAdminRemote` type. – Arjan Tijms Dec 28 '11 at 21:45

1 Answers1

1

You should be fine using stateless services as long as the references to those services are instance variables. Alternatively you could create a getManagerAdmin() method that handles the lookup for you and thus you wouldn't have to repeat the lookup code in each method.

If you can use Java EE 6 and thus EJB 3.1 you might want to let the container inject the services into the servlet. Note that this only works for local lookups in the same class loading context (normally the same application but it might be the same JVM if the applications are not isolated by the web/application server).

Arjan Tijms
  • 36,666
  • 12
  • 105
  • 134
Thomas
  • 80,843
  • 12
  • 111
  • 143
  • 1
    Pardon me, multiple servlet instances? The OP's servlet doesn't seem to implement the (deprecated) `SingleThreadModel`. – BalusC Dec 28 '11 at 19:19
  • @BalusC I'm no expert with servlets but if you have multiple concurrent requests multiple servlet instances would be used, wouldn't they? – Thomas Dec 28 '11 at 19:22
  • 1
    @Thomas: read this: [How do servlets work?](http://stackoverflow.com/questions/3106452/how-do-servlets-work-instantiation-session-variables-and-multithreading/3106909#3106909) – BalusC Dec 28 '11 at 19:24
  • @BalusC thanks for the link, this makes things clearer. If I understood that correctly, there _might_ be multiple instances or just one, which depends on the web server architecture. However, since you actually have a proxy for the service using it as an instance variable should still be safe since the ejb container should handle the pooling, right? – Thomas Dec 28 '11 at 19:30
  • You must lookup remote EJBs. No injection. – Bhesh Gurung Dec 28 '11 at 19:43
  • @gurung yes, but you don't have to use the remote interface if you're in the same application or class loading context. In that case a local lookup would work as well and thus injection would be possible (with EJB 3.1 and the `@WebServlet` annotation). – Thomas Dec 28 '11 at 20:48
  • Thanks guys, things are getting clearer. So, there will be one servlet with one manager used by many user, and this will not give any problem, right? Even if different users need the same manager, there will not be delays? – Simon Dec 28 '11 at 20:53
  • @Simon AFAIK no, since the ejb container will serve multiple ejb instances even if they are accessed using the same looked up proxy. – Thomas Dec 28 '11 at 20:56