With WebSphere® eXtreme Scale, you can use a programmatic API for managing the life cycle of embedded servers and containers. You can programmatically configure the server with any of the options that you can also configure with the command line options or file-based server properties. You can configure the embedded server to be a container server, a catalog service, or both.
You can run many administration tasks with the Administration API. One common use of the API is as an internal server for storing Web application state. The Web server can start an embedded WebSphere eXtreme Scale server, report the container server to the catalog service, and the server is then added as a member of a larger distributed grid. This usage can provide scalability and high availability to an otherwise volatile data store.
You can programmatically control the complete life cycle of an embedded eXtreme Scale server. The examples are as generic as possible and only show direct code examples for the outlined steps.ServerProperties props = ServerFactory.getServerProperties();
props.setCatalogServiceBootstrap("host:port"); // required to connect to specific catalog service
props.setServerName("ServerOne"); // name server
props.setTraceSpecification("com.ibm.ws.objectgrid=all=enabled"); // Sets trace spec
For more information about the CatalogServerProperties interface, see CatalogServerProperties interface.
CatalogServerProperties catalogProps = ServerFactory.getCatalogProperties();
catalogProps.setCatalogServer(true); // false by default, it is required to set as a catalog service
catalogProps.setQuorum(true); // enables / disables quorum
For more information about the Server interface, see Server interface. For more information abut the ServerFactory class, see ServerFactory class.
The following example shows how to create the Server instance: Server server = ServerFactory.getInstance();
Reviewing
the previous example, the ServerFactory class provides
a static method that returns a Server instance.
The ServerFactory class is intended to be the only
interface for obtaining a Server instance. Therefore,
the class ensures that the instance is a singleton, or one instance
for each JVM or isolated classloader. The getInstance method
initializes the Server instance. You must configure
all the server properties before you initialize the instance. The Server class
is responsible for creating new Container instances.
You can use both the ServerFactory and Server classes for managing
the life cycle of the embedded Server instance.For more information about the Container interface, see Container interface.
DeploymentPolicy policy = DeploymentPolicyFactory.createDeploymentPolicy(new
URL("file://urltodeployment.xml"),
new URL("file://urltoobjectgrid.xml"));
Container container = server.createContainer(policy);
You can remove and clean up a container server by using the running the teardown method on the obtained Container instance. Running the teardown method on a container properly cleans up the container and removes the container from the embedded server.
container.teardown();
ServerFactory.stopServer(); // Uses the factory to kill the Server singleton
// or
server.stopServer(); // Uses the Server instance directly
server.waitFor(); // Returns when the server has properly completed its shutdown procedures
Full
code example: import java.net.MalformedURLException;
import java.net.URL;
import com.ibm.websphere.objectgrid.ObjectGridException;
import com.ibm.websphere.objectgrid.deployment.DeploymentPolicy;
import com.ibm.websphere.objectgrid.deployment.DeploymentPolicyFactory;
import com.ibm.websphere.objectgrid.server.Container;
import com.ibm.websphere.objectgrid.server.Server;
import com.ibm.websphere.objectgrid.server.ServerFactory;
import com.ibm.websphere.objectgrid.server.ServerProperties;
public class ServerFactoryTest {
public static void main(String[] args) {
try {
ServerProperties props = ServerFactory.getServerProperties();
props.setCatalogServiceBootstrap("catalogservice-hostname:catalogservice-port");
props.setServerName("ServerOne"); // name server
props.setTraceSpecification("com.ibm.ws.objectgrid=all=enabled"); // TraceSpec
/*
* In most cases, the server will serve as a container server only
* and will connect to an external catalog service. This is a more
* highly available way of doing things. The commented code excerpt
* below will enable this Server to be a catalog service.
*
*
* CatalogServerProperties catalogProps =
* ServerFactory.getCatalogProperties();
* catalogProps.setCatalogServer(true); // enable catalog service
* catalogProps.setQuorum(true); // enable quorum
*/
Server server = ServerFactory.getInstance();
DeploymentPolicy policy = DeploymentPolicyFactory.createDeploymentPolicy
(new URL("url to deployment xml"), new URL("url to objectgrid xml file"));
Container container = server.createContainer(policy);
/*
* Shard will now be placed on this container if the deployment requirements are met.
* This encompasses embedded server and container creation.
*
* The lines below will simply demonstrate calling the cleanup methods
*/
container.teardown();
server.stopServer();
int success = server.waitFor();
} catch (ObjectGridException e) {
// Container failed to initialize
} catch (MalformedURLException e2) {
// invalid url to xml file(s)
}
}
}