📄 clustermanager.java
字号:
* Starts the cluster service if clustering is enabled. The process of starting clustering
* will recreate caches as distributed caches.<p>
*
* Before starting a cluster the
* {@link XMPPServer#setRemoteSessionLocator(org.jivesoftware.openfire.session.RemoteSessionLocator)} and
* {@link org.jivesoftware.openfire.RoutingTable#setRemotePacketRouter(org.jivesoftware.openfire.RemotePacketRouter)}
* need to be properly configured.
*/
public static synchronized void startup() {
if (isClusteringStarted()) {
return;
}
// See if clustering should be enabled.
if (isClusteringEnabled()) {
if (XMPPServer.getInstance().getRemoteSessionLocator() == null) {
throw new IllegalStateException("No RemoteSessionLocator was found.");
}
if (XMPPServer.getInstance().getRoutingTable().getRemotePacketRouter() == null) {
throw new IllegalStateException("No RemotePacketRouter was found.");
}
// Start up the cluster and reset caches
CacheFactory.startClustering();
}
}
/**
* Shuts down the clustering service. This method should be called when the Jive
* system is shutting down, and must not be called otherwise. Failing to call
* this method may temporarily impact cluster performance, as the system will
* have to do extra work to recover from a non-clean shutdown.
* If clustering is not enabled, this method will do nothing.
*/
public static synchronized void shutdown() {
// Reset packet router to use to deliver packets to remote cluster nodes
XMPPServer.getInstance().getRoutingTable().setRemotePacketRouter(null);
if (isClusteringStarted()) {
Log.debug("ClusterManager: Shutting down clustered cache service.");
CacheFactory.stopClustering();
}
// Reset the session locator to use
XMPPServer.getInstance().setRemoteSessionLocator(null);
}
/**
* Sets true if clustering support is enabled. An attempt to start or join
* an existing cluster will be attempted in the service was enabled. On the
* other hand, if disabled then this JVM will leave or stop the cluster.
*
* @param enabled if clustering support is enabled.
*/
public static void setClusteringEnabled(boolean enabled) {
if (enabled) {
// Check that clustering is not already enabled and we are already in a cluster
if (isClusteringEnabled() && isClusteringStarted()) {
return;
}
}
else {
// Check that clustering is disabled
if (!isClusteringEnabled()) {
return;
}
}
JiveGlobals.setXMLProperty(CLUSTER_PROPERTY_NAME, Boolean.toString(enabled));
if (!enabled) {
shutdown();
}
else {
// Reload Jive properties. This will ensure that this nodes copy of the
// properties starts correct.
JiveProperties.getInstance().init();
startup();
}
}
/**
* Returns true if clustering support is enabled. This does not mean
* that clustering has started or that clustering will be able to start.
*
* @return true if clustering support is enabled.
*/
public static boolean isClusteringEnabled() {
return JiveGlobals.getXMLProperty(CLUSTER_PROPERTY_NAME, false);
}
/**
* Returns true if clustering is installed and can be used by this JVM
* to join a cluster. A false value could mean that either clustering
* support is not available or the license does not allow to have more
* than 1 cluster node.
*
* @return true if clustering is installed and can be used by
* this JVM to join a cluster.
*/
public static boolean isClusteringAvailable() {
return CacheFactory.isClusteringAvailable();
}
/**
* Returns true is clustering is currently being started. Once the cluster
* is started or failed to be started this value will be false.
*
* @return true is clustering is currently being started.
*/
public static boolean isClusteringStarting() {
return CacheFactory.isClusteringStarting();
}
/**
* Returns true if this JVM is part of a cluster. The cluster may have many nodes
* or this JVM could be the only node.
*
* @return true if this JVM is part of a cluster.
*/
public static boolean isClusteringStarted() {
return CacheFactory.isClusteringStarted();
}
/**
* Returns true if this member is the senior member in the cluster. If clustering
* is not enabled, this method will also return true. This test is useful for
* tasks that should only be run on a single member in a cluster.
*
* @return true if this cluster member is the senior or if clustering is not enabled.
*/
public static boolean isSeniorClusterMember() {
return CacheFactory.isSeniorClusterMember();
}
/**
* Returns basic information about the current members of the cluster or an empty
* collection if not running in a cluster.
*
* @return information about the current members of the cluster or an empty
* collection if not running in a cluster.
*/
public static Collection<ClusterNodeInfo> getNodesInfo() {
return CacheFactory.getClusterNodesInfo();
}
/**
* Returns the maximum number of cluster members allowed. Both values 0 and 1 mean that clustering
* is not available. However, a value of 1 means that it's a license problem rather than not having
* the ability to do clustering as defined with value 0.
*
* @return the maximum number of cluster members allowed or 0 or 1 if clustering is not allowed.
*/
public static int getMaxClusterNodes() {
return CacheFactory.getMaxClusterNodes();
}
/**
* Returns the id of the node that is the senior cluster member. When not in a cluster the returned
* node id will be the {@link XMPPServer#getNodeID()}.
*
* @return the id of the node that is the senior cluster member.
*/
public static NodeID getSeniorClusterMember() {
byte[] clusterMemberID = CacheFactory.getSeniorClusterMemberID();
if (clusterMemberID == null) {
return XMPPServer.getInstance().getNodeID();
}
return NodeID.getInstance(clusterMemberID);
}
/**
* Returns true if the specified node ID belongs to a known cluster node
* of this cluster.
*
* @param nodeID the ID of the node to verify.
* @return true if the specified node ID belongs to a known cluster node
* of this cluster.
*/
public static boolean isClusterMember(byte[] nodeID) {
for (ClusterNodeInfo nodeInfo : getNodesInfo()) {
if (nodeInfo.getNodeID().equals(nodeID)) {
return true;
}
}
return false;
}
private static class Event {
private EventType type;
private byte[] nodeID;
private boolean processed;
public Event(EventType type, byte[] oldNodeID) {
this.type = type;
this.nodeID = oldNodeID;
}
public EventType getType() {
return type;
}
public byte[] getNodeID() {
return nodeID;
}
public boolean isProcessed() {
return processed;
}
public void setProcessed(boolean processed) {
this.processed = processed;
}
public String toString() {
return super.toString() + " type: " + type;
}
}
/**
* Represents valid event types.
*/
private enum EventType {
/**
* This JVM joined a cluster.
*/
joined_cluster,
/**
* This JVM is no longer part of the cluster.
*/
left_cluster,
/**
* This JVM is now the senior cluster member.
*/
marked_senior_cluster_member
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -