Skip to content

Instantly share code, notes, and snippets.

@tobias
Created March 19, 2015 17:23
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 tobias/70c1725255f2fcd8e97b to your computer and use it in GitHub Desktop.
Save tobias/70c1725255f2fcd8e97b to your computer and use it in GitHub Desktop.
import java.lang.reflect.Method;
public class ClusterUtils {
public static boolean inCluster() {
return channelFactory() != null;
}
public static JChannel lockableChannel(String id) throws Exception {
Object factory = channelFactory();
Class channelInterface;
try {
channelInterface = Class.forName("org.jboss.as.clustering.jgroups.ChannelFactory");
} catch (ClassNotFoundException e) {
try {
channelInterface = Class.forName("org.wildfly.clustering.jgroups.ChannelFactory");
} catch (ClassNotFoundException e2) {
throw new RuntimeException("ffs", e2);
}
}
if (channelInterface == null) {
return null;
}
Method createChannel = channelInterface.getDeclaredMethod("createChannel", String.class);
JChannel chan = (JChannel)createChannel.invoke(factory, id);
//TODO: check the stack and see if it already contains a lock proto
// and we should doc that as the preferred way, since you can configure the number of backups?
CENTRAL_LOCK l = new CENTRAL_LOCK();
l.setNumberOfBackups(1);
chan.getProtocolStack().addProtocol(l);
l.init();
return chan;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment