新增自訂調整接聽器
您可以利用 WebSphere® Application Server Developer Tools for Eclipse 來建立自訂調整接聽器軟體組。自訂調整接聽器會獲知所有擱置的調整動作。接著,接聽器可以允許或拒絕該調整動作。它也可以決定處理動作,因而提供完全自訂的調整行為。
開始之前
請確定 Liberty 安裝架構具有 scalingController-1.0 特性。如需調整控制器的相關資訊,請參閱設定 Liberty 群體的自動調整和調整控制器。
關於這項作業
您必須實作接聽器介面,並將實作類別登錄至服務登錄。調整接聽器可允許(或拒絕)和處理調整動作。
每一個 Liberty SPI 的 Java API 說明文件都以個別的壓縮檔提供(其位於 ${wlp.install.dir}/dev 目錄下的其中一個 API 說明文件子目錄中)。
程序
- 按一下檔案 > 新建 > 其他,然後展開 OSGi。
- 按一下 OSGi 軟體組專案,然後按下一步。這時會開啟「新建 OSGi 軟體組專案」視窗。
- 輸入 ScalingSPISampleBundle 作為專案名稱。在目標執行時期清單中,選取 WebSphere Application Server Liberty。如果沒有任何執行時期,請按一下新建執行時期,建立一個 WebSphere Application Server Liberty 執行時期。
- 清除將軟體組新增至應用程式圓鈕。
- 按兩次下一步,並移至「OSGi 軟體組」頁面。
- 在「OSGi 軟體組」頁面上,勾選產生啟動器,這是一個用來控制軟體組生命週期的 Java 類別。保留啟動器名稱 scalingspisamplebundle.Activator,並按一下完成。
- 按一下檔案 > 新建 > 其他,然後展開 OSGi。
- 按一下 Liberty 特性專案,然後按下一步。這時會開啟「Liberty 特性專案」視窗。
- 指定 ScalingSPISampleFeature 作為專案名稱。
- 在目標執行時期清單中,選取 WebSphere Application Server Liberty,然後按下一步。 即會開啟「OSGi 軟體組選擇頁面」。
- 在「OSGi 軟體組選擇頁面」中,選取 ScalingSPISampleBundle 1.0.0 作為包含的軟體組,並按一下完成。
- 按一下視窗 > 喜好設定 > 外掛程式開發 > 目標平台,並選取具有 SPI 的 WebSphere Application Server Liberty。
- 按一下套用,然後按一下確定。
- 展開 ScalingSPISampleBundle > BundleContent > META-INF,並使用外掛程式資訊清單編輯器來開啟 MANIFEST.MF 檔。
- 選取相依關係標籤,並將 com.ibm.wsspi.scaling.action.consumer 和 com.ibm.wsspi.scaling.action.controller 套件新增至「匯入的套件」窗格。您可能需要按 Ctrl+S,儲存變更。
- 在 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; } }
- 在 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。
- 用滑鼠右鍵按一下 ScalingSPISampleFeature 專案,並按一下安裝特性,將特性安裝至 Liberty 執行時期。
安裝特性功能表選項是在 Java EE 視景的「企業瀏覽器」視圖中。
- 編輯 server.xml 檔,以啟用 ScalingSPISampleFeature。
... <featureManager> ... <feature>scalingController-1.0</feature> <feature>usr:ScalingSPISampleFeature</feature> </featureManager> ...
上層主題: Liberty SPI 公用程式

檔名:twlp_wve_add_scaling_listener.html