public interface Service extends Serializable
IgniteServices facade or directly from grid configuration at startup.
execute(ServiceContext) method on that service. It is up to the user
to control whenever the service should exit from the execute method.
For example, user may choose to implement service as follows:
public class MyIgniteService implements Service {
...
// Example of ignite resource injection. All resources are optional.
// You should inject resources only as needed.
@IgniteInstanceResource
private Ignite ignite;
...
@Override public void cancel(ServiceContext ctx) {
// No-op.
}
@Override public void execute(ServiceContext ctx) {
// Loop until service is cancelled.
while (!ctx.isCancelled()) {
// Do something.
...
}
}
}
Consecutively, this service can be deployed as follows:
...
GridServices svcs = grid.services();
svcs.deployClusterSingleton("mySingleton", new MyGridService());
Or from grid configuration on startup:
IgniteConfiguration gridCfg = new IgniteConfiguration();
GridServiceConfiguration svcCfg = new GridServiceConfiguration();
// Configuration for cluster-singleton service.
svcCfg.setName("mySingleton");
svcCfg.setMaxPerNodeCount(1);
svcCfg.setTotalCount(1);
svcCfg.setService(new MyGridService());
gridCfg.setServiceConfiguration(svcCfg);
...
Ignition.start(gridCfg);
cancel methods on IgniteServices API.
Whenever a deployed service is cancelled, Ignite will automatically call
cancel(ServiceContext) method on that service.
Note that Ignite cannot guarantee that the service exits from execute(ServiceContext)
method whenever cancel(ServiceContext) is called. It is up to the user to
make sure that the service code properly reacts to cancellations.
| Modifier and Type | Method and Description |
|---|---|
void |
cancel(ServiceContext ctx)
Cancels this service.
|
void |
execute(ServiceContext ctx)
Starts execution of this service.
|
void |
init(ServiceContext ctx)
Pre-initializes service before execution.
|
void cancel(ServiceContext ctx)
cancel methods on IgniteServices API are called.
Note that Ignite cannot guarantee that the service exits from execute(ServiceContext)
method whenever cancel(GridServiceContext) method is called. It is up to the user to
make sure that the service code properly reacts to cancellations.
ctx - Service execution context.void init(ServiceContext ctx) throws Exception
execute(ServiceContext) is called).ctx - Service execution context.Exception - If service initialization failed.void execute(ServiceContext ctx) throws Exception
execute
method and can be cancelled (or undeployed) only by calling any of the cancel methods on
IgniteServices API. Also note that service is not required to exit from execute method until
cancel(ServiceContext) method was called.ctx - Service execution context.Exception - If service execution failed. Not that service will still remain deployed, until
IgniteServices.cancel(String) method will be called.
Follow @ApacheIgnite
Ignite Fabric : ver. 1.5.11 Release Date : April 8 2016