[jboss-user] [EJB3] - Singleton in clustered JBoss AS 6 Final

Dan Osterrath do-not-reply at jboss.com
Tue Jan 25 10:00:04 EST 2011

Dan Osterrath [http://community.jboss.org/people/DanOsterrath] created the discussion

"Singleton in clustered JBoss AS 6 Final"

To view the discussion, visit: http://community.jboss.org/message/583084#583084

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;
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

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.jboss.org/pipermail/jboss-user/attachments/20110125/42e90742/attachment.html 

More information about the jboss-user mailing list