Package org.apache.ignite.services
Interface Service
-
- All Superinterfaces:
Serializable
public interface Service extends Serializable
An instance of grid-managed service. Grid-managed services may be deployed fromIgniteServices
facade or directly from grid configuration at startup.Deployment
Whenever service is deployed, Ignite will automatically calculate how many instances of this service should be deployed on each node within the cluster. Whenever service is deployed on a cluster node, Ignite will callexecute()
method on that service. It is up to the user to control whenever the service should exit from theexecute
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; @ServiceContextResource private ServiceContext ctx; ... @Override public void cancel() { // No-op. } @Override public void execute() { // Loop until service is cancelled. while (!ctx.isCancelled()) { // Do something. ... } } }
Consecutively, this service can be deployed as follows:... IgniteServices svcs = ignite.services(); svcs.deployClusterSingleton("mySingleton", new MyIgniteService());
Or from grid configuration on startup:IgniteConfiguration gridCfg = new IgniteConfiguration(); IgniteServiceConfiguration svcCfg = new IgniteServiceConfiguration(); // Configuration for cluster-singleton service. svcCfg.setName("mySingleton"); svcCfg.setMaxPerNodeCount(1); svcCfg.setTotalCount(1); svcCfg.setService(new MyIgniteService()); gridCfg.setServiceConfiguration(svcCfg); ... Ignition.start(gridCfg);
Cancellation
Services can be cancelled by calling any of thecancel
methods onIgniteServices
API. Whenever a deployed service is cancelled, Ignite will automatically callcancel()
method on that service.Note that Ignite cannot guarantee that the service exits from
execute()
method whenevercancel()
is called. It is up to the user to make sure that the service code properly reacts to cancellations.
-
-
Method Summary
All Methods Instance Methods Default Methods Deprecated Methods Modifier and Type Method Description default void
cancel()
Cancels this service.default void
cancel(ServiceContext ctx)
Deprecated.Usecancel()
instead.default void
execute()
Starts execution of this service.default void
execute(ServiceContext ctx)
Deprecated.Useexecute()
instead.default void
init()
Pre-initializes service before execution.default void
init(ServiceContext ctx)
Deprecated.Useinit()
instead.
-
-
-
Method Detail
-
cancel
default void cancel()
Cancels this service. Ignite will automatically call this method whenever any of thecancel
methods onIgniteServices
API are called.Note that Ignite cannot guarantee that the service exits from
execute()
method whenevercancel()
method is called. It is up to the user to make sure that the service code properly reacts to cancellations.- See Also:
ServiceContextResource
-
init
default void init() throws Exception
Pre-initializes service before execution. This method is guaranteed to be called before service deployment is complete (this guarantees that this method will be called before methodexecute()
is called).- Throws:
Exception
- If service initialization failed.- See Also:
ServiceContextResource
-
execute
default void execute() throws Exception
Starts execution of this service. This method is automatically invoked whenever an instance of the service is deployed on a grid node. Note that service is considered deployed even after it exits theexecute
method and can be cancelled (or undeployed) only by calling any of thecancel
methods onIgniteServices
API. Also note that service is not required to exit fromexecute
method untilcancel()
method was called.- Throws:
Exception
- If service execution failed. Not that service will still remain deployed, untilIgniteServices.cancel(String)
method will be called.- See Also:
ServiceContextResource
-
cancel
@Deprecated default void cancel(ServiceContext ctx)
Deprecated.Usecancel()
instead.Cancels this service. Ignite will automatically call this method whenever any of thecancel
methods onIgniteServices
API are called.Note that Ignite cannot guarantee that the service exits from
execute(ServiceContext)
method whenevercancel(ServiceContext)
method is called. It is up to the user to make sure that the service code properly reacts to cancellations.- Parameters:
ctx
- Service execution context.
-
init
@Deprecated default void init(ServiceContext ctx) throws Exception
Deprecated.Useinit()
instead.Pre-initializes service before execution. This method is guaranteed to be called before service deployment is complete (this guarantees that this method will be called before methodexecute(ServiceContext)
is called).- Parameters:
ctx
- Service execution context.- Throws:
Exception
- If service initialization failed.
-
execute
@Deprecated default void execute(ServiceContext ctx) throws Exception
Deprecated.Useexecute()
instead.Starts execution of this service. This method is automatically invoked whenever an instance of the service is deployed on a grid node. Note that service is considered deployed even after it exits theexecute
method and can be cancelled (or undeployed) only by calling any of thecancel
methods onIgniteServices
API. Also note that service is not required to exit fromexecute
method untilcancel(ServiceContext)
method was called.- Parameters:
ctx
- Service execution context.- Throws:
Exception
- If service execution failed. Not that service will still remain deployed, untilIgniteServices.cancel(String)
method will be called.
-
-