Skip to content

Instantly share code, notes, and snippets.

@rmjacobs
Last active December 25, 2015 07:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rmjacobs/6941244 to your computer and use it in GitHub Desktop.
Save rmjacobs/6941244 to your computer and use it in GitHub Desktop.
import com.day.cq.jcrclustersupport.ClusterAware;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Properties;
import org.apache.felix.scr.annotations.Property;
import org.apache.felix.scr.annotations.Service;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@Component(immediate = true, metatype = true, label = "Periodic Job", description = "Does something on a schedule")
@Service
@Properties({
@Property(name = "service.vendor", value = "CSG"),
@Property(name = "service.description", value = "Does stuff periodically"),
@Property(name = "scheduler.expression", value = "0 * * * * ?")
})
public class MyClusterAwareService implements Runnable, ClusterAware {
private boolean isMaster;
Logger log = LoggerFactory.getLogger(this.getClass());
@Override
public void bindRepository(String repositoryId, String clusterId, boolean isMaster) {
log.info("A new repository has been bound:" + repositoryId + "; clusterid:" + clusterId);
this.isMaster = isMaster;
}
@Override
public void unbindRepository() {
//do something useful here...
}
public void run() {
if (isMaster) {
log.info("I am the master and will do my task.");
//Do your task here.
} else {
log.info("I am the slave and will not do anything.");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment