0
votes

I have multiple live charts. Currently I have multiple links one for each chart. When a user clicks a link, the corresponding chart is loaded and every 5 seconds an ajax call is made to invoke the action method in the viewscoped managedbean. When the user clicks another link, previous setinterval is cleared and the same process is repeated for another chart. For all these charts (links), their corresponding action methods are in the same viewscoped managedbean which is backed by one EJB, with one service method corresponding to each action method.

So far so good.

The next step is to have a link to display all those live charts simultaneously on the view. Now, I know that there are going to be multiple asynchronous ajax calls. I believe that the servlet (FacesServlet) handles all these requests in a separate thread. But what happens with the managedbean and the ejb. Do I need to create separate managedbeans (if the MB is RequestScoped then does JSF create a separate instance for multiple asynchronous ajax requests) and separate ejbs? I am kind of confused right now and need some helpful advice. I want them to be processed concurrently.

Thank you.

Update:

First of all thanks to bogdan.mustiata.

Now, since I am not getting any more answers I will try to make little bit clearer.

What I am really trying to understand is:

When I have a ViewScoped (or a SessionScoped) managed-bean and a Stateless EJB -

  1. How is a request handled on the server side?
  2. How are multiple aynchromous Ajax requests handled on the server side?

Following is my understanding -

  1. Request is sent to the server.
  2. FacesServlet starts a thread per request.
      Within that thread -
    1. Managd-bean action method is executed.
    2. An instance of EJB is provided by the container and corresponding method in the EJB is executed.

So that way multiple Ajax requests are handled simultaneously.

Is that how it is?

1

1 Answers

2
votes

If your EJBs are @Stateless it doesn't matter, you're fine. If they are @Session you are out of luck since last time I checked (in EJB 3.0) you were not allowed to call multiple stateful EJBs methods simultaneously. There was a framework out there named Seam, that was able to serialize calls to the stateful EJB though.

You can mark the methods from the stateful EJB as synchronized but that's not recommended. Since it is the same instance it will probably work, but hardcore EJB evangelists might not be happy.

Update:

I read the specification for EJB 3.1 and the text at 4.3.14 states:

The container serializes calls to each stateful and stateless session bean instance. Most containers will support many instances of a session bean executing concurrently; however, each instance sees only a serialized sequence of method calls. Therefore, a stateful or stateless session bean does not have to be coded as reentrant.

To make things more fascinating it goes on saying:

By default, clients are allowed to make concurrent calls to a stateful session object and the container is required to serialize such concurrent requests. Note that the container never permits multi-threaded access to the actual stateful session bean instance.

Since I don't know of any way of calling a service concurrently but without using different threads since the actual calls are blocking, I can only assume they refer to the proxy on which you do your calls.

So if you really want to call the code in parallel the short answer is you can't with stateful EJBs, but it is out of the box for stateless EJBs since you get a new instance for each call.

Update: You never get an instance to an EJB, you always get a proxy that will call the EJB. In case of a stateless EJB, your proxy will eventually instantiate a new stateless EJB for each method call of your own, and the EJB code will each time run on a new instance.