Adding custom scaling listeners

You can create a custom scaling listener bundle by using WebSphere® Application Server Developer Tools for Eclipse. A custom scaling listener is notified of all pending scaling actions. The listener can then permit or deny the scaling action. It can also decide to handle the action, thus provide a full customization of the scaling behavior.

Before you begin

Ensure your Liberty installation has the scalingController-1.0 feature. For information about scaling controllers, see Setting up auto scaling for Liberty collectives and Scaling Controller.

About this task

You must implement the listener interface and register the implementation class into the service registry. Your scaling listener can permit or deny and handle scaling actions.

The Java API documentation for each Liberty SPI is available in a separate compressed file in one of the API documentation subdirectories of the ${wlp.install.dir}/dev directory.

Procedure

  1. Click File > New > Other and then expand OSGi.
  2. Click OSGi Bundle Project and click Next. The New OSGi Bundle Project window opens.
  3. Enter ScalingSPISampleBundle as the Project name. In the Target runtime list, select WebSphere Application Server Liberty. If no runtime exists, click New Runtime to create a WebSphere Application Server Liberty runtime.
  4. Clear the Add bundle to application radio button.
  5. Click Next twice and go to the OSGi Bundle page.
  6. On the OSGi Bundle page, check Generate an activator, a Java class that controls the life cycle of the bundle. Leave the Activator name as scalingspisamplebundle.Activator and click Finish.
  7. Click File > New > Other and then expand OSGi.
  8. Click Liberty Feature Project and then click Next. The Liberty Feature Project window opens.
  9. Specify ScalingSPISampleFeature as the Project name.
  10. In the Target runtime list, select WebSphere Application Server Liberty and click Next. The OSGi Bundles Selection Page opens.
  11. On OSGi Bundles Selection Page, select ScalingSPISampleBundle 1.0.0 as the Contained Bundles and click Finish.
  12. Click Window > Preferences > Plug-in Development > Target Platform and select WebSphere Application Server Liberty with SPI.
  13. Click Apply and click OK.
  14. Expand ScalingSPISampleBundle > BundleContent > META-INF and open the MANIFEST.MF file by using the Plug-in Manifest Editor.
  15. Select the Dependencies tab and add the com.ibm.wsspi.scaling.action.consumer and com.ibm.wsspi.scaling.action.controller packages to the Imported Packages pane. You might need to press Ctrl+S to save the changes.
  16. In the ScalingSPISampleBundle project, add an implementation class called ScalingSPISamplePlugin. This new class implements the com.ibm.wsspi.scaling.action.consumer.ScalingActionPlugin interface.
    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. In the ScalingSPISampleBundle project, open the Activator class and edit the start(BundleContext) method to add code that registers the new listener service.
    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);
    }

    If prompted to import a HashMap, select to import java.util.HashMap.

  18. Right-click the ScalingSPISampleFeature project, and click Install Feature to install the feature to Liberty runtime.

    The Install Feature menu option is in the Enterprise Explorer view of the Java EE perspective.

  19. Edit the server.xml file to enable the ScalingSPISampleFeature.
    ...
    <featureManager>
      ...
      <feature>scalingController-1.0</feature>
      <feature>usr:ScalingSPISampleFeature</feature>
    </featureManager>
    ...

Icon that indicates the type of topic Task topic

File name: twlp_wve_add_scaling_listener.html