JBoss Community

Singleton in clustered JBoss AS 6 Final

created by Dan Osterrath in EJB3 - View the full discussion

Hello to everyone,

 

I have a singleton session bean that handles some nightly database cleanup and import. This is done via @Singleton and @Schedule annotation. Unfortunately when deploying in a cluster this bean is executed on all nodes.

 

package session;
 
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Date;
 
import javax.ejb.Schedule;
import javax.ejb.Singleton;
 
import org.jboss.ejb3.annotation.Clustered;
 
@Singleton
@Clustered
public class MyScheduler {
    @Schedule(second = "*/20", minute = "*", hour = "*", persistent = false)
    public void printTime() {
        System.out.println(getHostName() + ": " + new Date());
    }
 
    private String getHostName() {
        try {
            InetAddress addr = InetAddress.getLocalHost();
            return addr.getHostName();
        } catch (UnknownHostException e) {
        }
        return "";
    }
}

This example prints the hostname and the current time on all nodes.

 

That behaviour seems to fit the EJB 3.1 requirements but is quite useless for me (and probably others). So how can I implement an high available singleton in a cluster? If there is no way how can I block the other nodes singleton instances so that only one gets executed?

Reply to this message by going to Community

Start a new discussion in EJB3 at Community