Wednesday 16 June 2010

Singleton EJBs . . . good news

JBoss is releasing the milestone 3 of JBoss 6. One of the good news is that it seems to be possible to annotate an EJB as a singleton. That mean, no more unnecessary queries all over the application or parameters where has non sense. Neither Stateless Session Beans nor Stateful Session Beans meet this need.

Now you only need the @Singleton class annotation to make an EJB singleton.

With this you guarantee that there will be only one instance of that EJB in the entire application. This is very useful to manage application shared data.

But this brings some concurrency problems. Now we need to look after the access of that shared data.

Suppose the following example:


import javax.ejb.Lock;
import javax.ejb.LockType;
import javax.ejb.Remote;
import javax.ejb.Stateful;

@Stateful
@Remote(UsersCounter.class)
public class UsersCounterBean {
 int maximun = 2000;
 int users;

 @Lock(LockType.WRITE)
 public boolean canLogin() {
     if (users < maximun) {
         users++;
         return true;
     }else{
         return false;
     }
 }

 @Lock(LockType.WRITE)
 public void doLogout() {
  users--;
 }

 @Lock(LockType.READ)
 public int getUsersCount() {
  return users;
 }

}


For some weird reason you want to control the maximum number of users, and obviously you decide to use this approach(:D).

How to deal with synchronization?
Supposing that two clients invokes the same method. The container ensure us that there will be only on thread at a time executing each method.

For those who have ever use the synchronization on J2SE, this is exactly like the same as if we marked a method as synchronized.

This is quiet good but it's also a bottleneck but you have a workaround on this.

J2EE allows us to fine tune this by using @Lock annotation. That way we can make some methods to have a read lock allowing multiple threads accessing it but with the consign of not modifying class attributes.

So, we can add @Lock(LockType.WRITE) to those methods that modify the users attribute and @Lock(LockType.READ) to the other.

This will be get much more performance to the use of the users count.

I hope you can use this soon and please correct me if you find mistakes(also English please)

:D

No comments:

Post a Comment