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

My other big passion!!

Lutherie is my other passion. I started to be interested in lutherie up to 4 years ago. I was learning to play classical guitar
and I start to feel the need of a better guitar. Because I couldn't buy a concert guitar I started to read guitarmaking material. Soon I found myself, building my first ukelele with guitar dreams(jaja just a joke). My second try was a better approach. Nevertheless, I never gave up and I try once again. The third one was the good one. Now I'm playing with this one. The emotion when I listen this this guitar was indescribable. In that moment I knew that this going to be the hobby for the rest of my life.

Ok guys!! Soon I will write more about that. Meanwhile, I will show you a picture of my workshop!!

Kick off!!

First of all, I want to explain the name of the blogg. The first time I comment that I'm planning to move to Australia, one friend told me that he has an Australian friend nicknamed "alfadingo". I made a little research and find out that the Dingo is an Australian traditional wild dog. And I start to call myself javadingo, because Java is almost my way of life. Then I found, over the Internet, this funny abbreviation of overseas that really likes me so I made that weired mix of none senses my blogg name. Yes. I'm a little crazy!!

The reason why I'm writing in English is because I need practice to take the IELTS which it's needed to migrate Australia. This is a good way of mix all my intentions.

See you soon!!