Skip to main content

Posts

Showing posts from October, 2014

How to execute a singleton task in a cluster?

Have you come across a situation where you wanted a singleton to execute a task in a cluster of servers (such as running a clean up task every day, sending a report everyday etc)? You know that these type of tasks should be executed by exactly only one of many servers you have. Here you go. Following simple program will let you do that exactly. import java.util.logging.Level; import java.util.logging.Logger; import javax.annotation.PostConstruct; import javax.annotation.PreDestroy; import org.jgroups.Address; import org.jgroups.JChannel; import org.jgroups.ReceiverAdapter; import org.jgroups.View; import org.springframework.stereotype.Component; /** * * @author Pragalathan M */ @Component public class ClusteredSingletonManager { private JChannel channel; private Address ownAddress; private static final Logger logger = Logger.getLogger(ClusteredSingletonManager.class.getName()); @PostConstruct private void init() { try { logger.info(&q