6.3. 集群服务的公共 API
JBoss EAP 7 引入了一种经过优化的公共集群 API,供应用程序使用。新服务设计为轻量、可注入且无外部依赖项。
org.wildfly.clustering.group.Group
组服务提供了一种机制,可用于查看 JGroups 频道的集群拓扑,并在拓扑更改时获得通知。
@Resource(lookup = "java:jboss/clustering/group/channel-name") private Group channelGroup;
org.wildfly.clustering.dispatcher.CommandDispatcher
CommandDispatcherFactory
服务提供创建分配程序的机制,用于在群集的节点上执行命令。生成的CommandDispatcher
是命令特征模拟以前的 JBoss EAP 版本中基于事件的GroupRpcDispatcher
。@Resource(lookup = "java:jboss/clustering/dispatcher/channel-name") private CommandDispatcherFactory factory; public void foo() { String context = "Hello world!"; // Exclude node1 and node3 from the executeOnCluster try (CommandDispatcher<String> dispatcher = this.factory.createCommandDispatcher(context)) { dispatcher.executeOnGroup(new StdOutCommand(), node1, node3); } } public static class StdOutCommand implements Command<Void, String> { @Override public Void execute(String context) { System.out.println(context); return null; } }