ClusterForwardProvider 接口 demo 插件。
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

74 lines
2.2 KiB

package com.fr.plugin.cluster.req.demo;
import com.fr.cluster.lock.ClusterLock;
import com.fr.log.FineLoggerFactory;
import com.fr.store.StateHubManager;
import com.fr.store.StateHubService;
import com.fr.web.core.ClusterShareInfo;
import com.fr.web.core.cluster.ClusterStatusHelper;
public class TaskInfoManager {
private static final String KEY = "ReqDemoTaskInfoPool";
private TaskInfoManager() {
}
/**
* 向队列中添加一个列表
*
* @param id 任务id
* @throws Exception 异常
*/
public static void addTask(String id) throws Exception {
ClusterLock lock = getLock();
try {
lock.lock();
StateHubService stateHubService = StateHubManager.applyForService(KEY);
stateHubService.put(id, new ClusterShareInfo(id, ClusterStatusHelper.getCurrentNodeId()));
} finally {
lock.unlock();
}
}
public static void removeTask(String id) throws Exception {
ClusterLock lock = getLock();
try {
lock.lock();
StateHubService stateHubService = StateHubManager.applyForService(KEY);
stateHubService.delete(id);
} finally {
lock.unlock();
}
}
public static String getTargetNodeId(String id) {
ClusterLock lock = getLock();
try {
lock.lock();
StateHubService stateHubService = StateHubManager.applyForService(KEY);
ClusterShareInfo info = stateHubService.get(id);
if (info != null) {
return info.getNodeID();
} else {
FineLoggerFactory.getLogger().debug("process cluster share info not found");
return null;
}
} catch (Exception e) {
FineLoggerFactory.getLogger().debug("occur exception while finding process cluster share info", e);
return null;
} finally {
lock.unlock();
}
}
/**
* 获取锁(集群时就是集群锁,非集群时返回的是单机的)
*
* @return 集群锁
*/
private static ClusterLock getLock() {
return ClusterLockManager.getLock();
}
}