Reputation: 4501
I'm having trouble figuring out how to do an EJB3-compliant serial processing in a stateful bean. That is, have only one instance that can be accessed by a single thread only at a given moment. I can add synchronized
to a method, but those are non-compliant additions, e.g.:
Reading this:
only one thread should be accessing it anyway, so puzzled.
I read this:
where it says:
An extension offered by JBoss EJB 3.0 is the notion of a @org.jboss.annotation.ejb.Service annotated bean. They are singleton beans and are not pooled, so only one instance of the bean exists in the server. They can have both @Remote and @Local interfaces so they can be accessed by java clients. When different clients look up the interfaces for @Service beans, all clients will work on the same instance of the bean on the server.
However, it does not say whether multiple threads can access it simultaneously. That is, if I have 10 clients which got a remote proxy to this @Service instance, they can call one method 10 times. Each of them will still operate on the same bean instance, but the method will be called multiple times. That's a testing result, I could not find anything specifically saying that in the API / docs.
This precludes this from being used in some scenarios such as e.g. producer / consumer ones where I need to process things in order and basically single threaded. Am I missing something? How to implement serial processing without resorting to other things (e.g. using DB locking or MDX or such)?
Btw, the code is simple / usual - here's the gist:
public interface MyService {
public void callMe();
}
@Service
@Remote(MyService.class)
public class MyServiceImpl implements MyService {
private Logger logger = Logger.getLogger(MyService.class);
public void callMe() {
logger.info("called entry");
try {
Thread.sleep(1000); // simulate work
} catch(InterruptedException e) {
}
logger.info("called exit");
}
}
Hope I did not make typos, but you get the idea. Cannot go to EJB 3.1 to try @Singleton, using ancient JBoss 4.2.1, no I cannot upgrade :(. When I run the above, I get several statements for entry, then they all wait a second, then they all exit. So if I were to process some state (and I read that @Service is a stateful bean), then all would get mangled up.
I thought of using @PoolClass(value=org.jboss.ejb3.StrictMaxPool.class, maxSize=1), but that would not help anything, as it's a single instance anyway, just accessed multiple times. Do you have any clues or directions?
And of course - adding synchronized
to callMe method actually solves the issue, so not sure to think about all this. Server misconfiguration?
Upvotes: 0
Views: 478
Reputation:
How to implement serial processing without resorting to other things (e.g. using DB locking or MDX or such)?
You could use JMS with "Point-to-Point Messaging Style", although you cannot be sure the message was delivered to consumer (only to the queue).
Upvotes: 1