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.
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
- Click and then expand OSGi.
- Click OSGi Bundle Project and click Next. The
New OSGi Bundle Project window opens.
- 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.
- Clear the Add bundle to application radio button.
- Click Next twice and go to the OSGi
Bundle page.
- 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.
- Click and then expand OSGi.
- Click Liberty Feature Project and then
click Next. The Liberty Feature Project window
opens.
- Specify ScalingSPISampleFeature as the Project name.
- In the Target runtime list, select WebSphere Application Server Liberty
and click Next. The OSGi Bundles Selection Page opens.
- On OSGi Bundles Selection Page, select ScalingSPISampleBundle
1.0.0 as the Contained Bundles and
click Finish.
- Click and select WebSphere Application Server Liberty with SPI.
- Click Apply and click OK.
- Expand and open the MANIFEST.MF file by using the Plug-in
Manifest Editor.
- 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.
- 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;
}
}
- 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.
- 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.
- Edit the server.xml file to enable the ScalingSPISampleFeature.
...
<featureManager>
...
<feature>scalingController-1.0</feature>
<feature>usr:ScalingSPISampleFeature</feature>
</featureManager>
...