添加定制缩放侦听器

可使用 WebSphere® Application Server Developer Tools for Eclipse 创建定制缩放侦听器捆绑软件。系统会向定制缩放侦听器通知所有暂挂缩放操作。然后此侦听器可允许或拒绝该缩放操作。它还可决定处理该操作,从而提供缩放行为的全面定制。

开始之前

确保 Liberty 安装具有 scalingController-1.0 功能部件。有关缩放控制器的信息,请参阅为 Liberty 集合体设置自动缩放缩放控制器

关于此任务

必须实现侦听器接口并将实现类注册至服务注册表。缩放侦听器可允许或拒绝及处理缩放操作。

每个 Liberty SPI 的 Java API 文档作为单独压缩文件在 ${wlp.install.dir}/dev 目录的某个 API 文档子目录下提供。

过程

  1. 单击文件 > 新建 > 其他,然后展开 OSGi
  2. 单击 OSGi 捆绑软件项目,然后单击下一步。“新建 OSGi 捆绑软件项目”窗口将打开。
  3. 输入 ScalingSPISampleBundle 作为项目名称。在目标运行时列表中,选择 WebSphere Application Server Liberty。如果运行时不存在,请单击新建运行时以创建 WebSphere Application Server Liberty 运行时。
  4. 取消选中将捆绑软件添加至应用程序单选按钮。
  5. 单击下一步两次并转至“OSGi 捆绑软件”页面。
  6. 在“OSGi 捆绑软件”页面上,选中生成激活程序,用于控制捆绑软件生命周期的 Java 类。将激活程序名称保留为 scalingspisamplebundle.Activator 并单击完成
  7. 单击文件 > 新建 > 其他,然后展开 OSGi
  8. 单击 Liberty 功能部件项目,然后单击下一步。“Liberty 功能部件项目”窗口将打开。
  9. 指定 ScalingSPISampleFeature 作为项目名称。
  10. 在目标运行时列表中,选择 WebSphere Application Server Liberty,然后单击下一步OSGi 捆绑软件选择页面”将打开。
  11. 在“OSGi 捆绑软件选择页面”上,选择 ScalingSPISampleBundle 1.0.0 作为包含的捆绑软件,然后单击完成
  12. 单击窗口 > 首选项 > 插件开发 > 目标平台,然后选择带 SPI 的 WebSphere Application Server Liberty
  13. 单击应用,然后单击确定
  14. 展开 ScalingSPISampleBundle > BundleContent > META-INF,然后使用插件清单编辑器打开 MANIFEST.MF 文件。
  15. 选择依赖项选项卡,并将 com.ibm.wsspi.scaling.action.consumercom.ibm.wsspi.scaling.action.controller 包添加至“已导入包”窗格中。您可能需要按 Ctrl+S 以保存更改。
  16. 在 ScalingSPISampleBundle 项目中,添加名为 ScalingSPISamplePlugin 的实现类。此新类实现 com.ibm.wsspi.scaling.action.consumer.ScalingActionPlugin 接口。
    package scalingspisamplebundle;
    
    import com.ibm.wsspi.scaling.action.consumer.ScalingActionPlugin;
    import com.ibm.wsspi.scaling.action.controller.ScalingActionContext;
    import com.ibm.wsspi.scaling.action.controller.ScalingActionContext.ActionType;
    import com.ibm.wsspi.scaling.action.controller.ScalingActionContext.ActionDecision;;
    
    /**
     * This a sample Liberty scaling SPI plugin that acts as a template for developing a custom
     * SPI plugin. In this plugin, no actual work is done and the return value to the Liberty Scaling
     * controller is always DEFAULT_ACTION.
     * 
     */
    public class ScalingSPISamplePlugin implements ScalingActionPlugin {
    
        /** {@inheritDoc} */
        @Override
        public ActionDecision actionRequired(ScalingActionContext action) {
    
            ActionDecision returnType = ActionDecision.DEFAULT_ACTION;
    
            if (action.getActionType() == ActionType.START_SERVER) {
                returnType = startServer(action);
            }
            else if (action.getActionType() == ActionType.CREATE_SERVER) {
                returnType = createServer(action);
            }
            else if (action.getActionType() == ActionType.STOP_SERVER) {
                returnType = stopServer(action);
            }
    
            return returnType;
    
        }
    
        private ActionDecision startServer(ScalingActionContext action) {
            // perform some action to start a server
    
            return ActionDecision.DEFAULT_ACTION;
        }
    
        private ActionDecision createServer(ScalingActionContext action) {
            // perform some action to create a server
    
            return ActionDecision.DEFAULT_ACTION;
        }
    
        private ActionDecision stopServer(ScalingActionContext action) {
            // perform some action to stop a server
    
            return ActionDecision.DEFAULT_ACTION;
        }
    }
  17. ScalingSPISampleBundle 项目中,打开 Activator 类并编辑 start(BundleContext) 方法以添加用于注册新侦听器服务的代码。
    public void start(BundleContext context) throws Exception {
        final Hashtable<String, Object> properties = new Hashtable<String, Object>();
        ScalingSPISamplePlugin scalingSPISamplePlugin = new ScalingSPISamplePlugin();
        context.registerService(ScalingActionPlugin.class, scalingSPISamplePlugin, properties);
    }

    如果系统提示导入 HashMap,请选择导入 java.util.HashMap

  18. 右键单击 ScalingSPISampleFeature 项目,然后单击安装功能部件以将该功能部件安装至 Liberty 运行时。

    安装功能部件菜单选项位于 Java EE 透视图的“企业资源管理器”视图中。

  19. 编辑 server.xml 文件以启用 ScalingSPISampleFeature。
    ...
    <featureManager>
      ...
      <feature>scalingController-1.0</feature>
      <feature>usr:ScalingSPISampleFeature</feature>
    </featureManager>...

用于指示主题类型的图标 任务主题

文件名:twlp_wve_add_scaling_listener.html