public interface IgniteServices extends IgniteAsyncSupport
Instance of IgniteServices
which spans all cluster nodes can be obtained from Ignite as follows:
Ignite ignite = Ignition.ignite(); IgniteServices svcs = ignite.services();You can also obtain an instance of the services facade over a specific cluster group:
// Cluster group over remote nodes (excluding the local node). ClusterGroup remoteNodes = ignite.cluster().forRemotes(); // Services instance spanning all remote cluster nodes. IgniteServices svcs = ignite.services(remoteNodes);
With distributed services you can do the following:
deploy(...)
methods,
you can also automatically deploy services on startup by specifying them in IgniteConfiguration
like so:
IgniteConfiguration gridCfg = new IgniteConfiguration(); GridServiceConfiguration svcCfg1 = new GridServiceConfiguration(); // Cluster-wide singleton configuration. svcCfg1.setName("myClusterSingletonService"); svcCfg1.setMaxPerNodeCount(1); svcCfg1.setTotalCount(1); svcCfg1.setService(new MyClusterSingletonService()); GridServiceConfiguration svcCfg2 = new GridServiceConfiguration(); // Per-node singleton configuration. svcCfg2.setName("myNodeSingletonService"); svcCfg2.setMaxPerNodeCount(1); svcCfg2.setService(new MyNodeSingletonService()); gridCfg.setServiceConfiguration(svcCfg1, svcCfg2); ... Ignition.start(gridCfg);
// Simple service implementation. public class MyGridService implements GridService { ... // Example of ignite resource injection. All resources are optional. // You should inject resources only as needed. @IgniteInstanceResource private Grid grid; ... @Override public void cancel(GridServiceContext ctx) { // No-op. } @Override public void execute(GridServiceContext ctx) { // Loop until service is cancelled. while (!ctx.isCancelled()) { // Do something. ... } } } ... GridServices svcs = grid.services(); svcs.deployClusterSingleton("mySingleton", new MyGridService());
Modifier and Type | Method and Description |
---|---|
void |
cancel(String name)
Cancels service deployment.
|
void |
cancelAll()
Cancels all deployed services.
|
ClusterGroup |
clusterGroup()
Gets the cluster group to which this
GridServices instance belongs. |
void |
deploy(ServiceConfiguration cfg)
Deploys multiple instances of the service on the grid according to provided
configuration.
|
void |
deployClusterSingleton(String name,
Service svc)
Deploys a cluster-wide singleton service.
|
void |
deployKeyAffinitySingleton(String name,
Service svc,
String cacheName,
Object affKey)
Deploys one instance of this service on the primary node for a given affinity key.
|
void |
deployMultiple(String name,
Service svc,
int totalCnt,
int maxPerNodeCnt)
Deploys multiple instances of the service on the grid.
|
void |
deployNodeSingleton(String name,
Service svc)
Deploys a per-node singleton service.
|
<T> T |
service(String name)
Gets deployed service with specified name.
|
Collection<ServiceDescriptor> |
serviceDescriptors()
Gets metadata about all deployed services.
|
<T> T |
serviceProxy(String name,
Class<? super T> svcItf,
boolean sticky)
Gets a remote handle on the service.
|
<T> Collection<T> |
services(String name)
Gets all deployed services with specified name.
|
IgniteServices |
withAsync()
Gets instance of this component with asynchronous mode enabled.
|
future, isAsync
ClusterGroup clusterGroup()
GridServices
instance belongs.GridServices
instance belongs.@IgniteAsyncSupported void deployClusterSingleton(String name, Service svc) throws IgniteException
Note that in case of topology changes, due to network delays, there may be a temporary situation when a singleton service instance will be active on more than one node (e.g. crash detection delay).
This method is analogous to calling
deployMultiple(name, svc, 1, 1)
method.
name
- Service name.svc
- Service instance.IgniteException
- If failed to deploy service.@IgniteAsyncSupported void deployNodeSingleton(String name, Service svc) throws IgniteException
This method is analogous to calling
deployMultiple(name, svc, 0, 1)
method.
name
- Service name.svc
- Service instance.IgniteException
- If failed to deploy service.@IgniteAsyncSupported void deployKeyAffinitySingleton(String name, Service svc, @Nullable String cacheName, Object affKey) throws IgniteException
Note that in case of topology changes, due to network delays, there may be a temporary situation when a service instance will be active on more than one node (e.g. crash detection delay).
This method is analogous to the invocation of deploy(org.apache.ignite.services.ServiceConfiguration)
method
as follows:
GridServiceConfiguration cfg = new GridServiceConfiguration(); cfg.setName(name); cfg.setService(svc); cfg.setCacheName(cacheName); cfg.setAffinityKey(affKey); cfg.setTotalCount(1); cfg.setMaxPerNodeCount(1); grid.services().deploy(cfg);
name
- Service name.svc
- Service instance.cacheName
- Name of the cache on which affinity for key should be calculated, null
for
default cache.affKey
- Affinity cache key.IgniteException
- If failed to deploy service.@IgniteAsyncSupported void deployMultiple(String name, Service svc, int totalCnt, int maxPerNodeCnt) throws IgniteException
'totalCnt'
parameter making sure that
there are no more than 'maxPerNodeCnt'
service instances running
on each node. Whenever topology changes, Ignite will automatically rebalance
the deployed services within cluster to make sure that each node will end up with
about equal number of deployed instances whenever possible.
Note that at least one of 'totalCnt'
or 'maxPerNodeCnt'
parameters must have
value greater than 0
.
This method is analogous to the invocation of deploy(org.apache.ignite.services.ServiceConfiguration)
method
as follows:
GridServiceConfiguration cfg = new GridServiceConfiguration(); cfg.setName(name); cfg.setService(svc); cfg.setTotalCount(totalCnt); cfg.setMaxPerNodeCount(maxPerNodeCnt); grid.services().deploy(cfg);
name
- Service name.svc
- Service instance.totalCnt
- Maximum number of deployed services in the grid, 0
for unlimited.maxPerNodeCnt
- Maximum number of deployed services on each node, 0
for unlimited.IgniteException
- If failed to deploy service.@IgniteAsyncSupported void deploy(ServiceConfiguration cfg) throws IgniteException
cfg.getTotalCount()
parameter
making sure that there are no more than cfg.getMaxPerNodeCount()
service instances running on each node. Whenever topology changes, Ignite will automatically rebalance
the deployed services within cluster to make sure that each node will end up with
about equal number of deployed instances whenever possible.
If cfg.getAffinityKey()
is not null
, then Ignite
will deploy the service on the primary node for given affinity key. The affinity will be calculated
on the cache with cfg.getCacheName()
name.
If cfg.getNodeFilter()
is not null
, then
Ignite will deploy service on all grid nodes for which the provided filter evaluates to true
.
The node filter will be checked in addition to the underlying cluster group filter, or the
whole grid, if the underlying cluster group includes all the cluster nodes.
Note that at least one of 'totalCnt'
or 'maxPerNodeCnt'
parameters must have
value greater than 0
.
Here is an example of creating service deployment configuration:
GridServiceConfiguration cfg = new GridServiceConfiguration(); cfg.setName(name); cfg.setService(svc); cfg.setTotalCount(0); // Unlimited. cfg.setMaxPerNodeCount(2); // Deploy 2 instances of service on each node. grid.services().deploy(cfg);
cfg
- Service configuration.IgniteException
- If failed to deploy service.@IgniteAsyncSupported void cancel(String name) throws IgniteException
Service.cancel(org.apache.ignite.services.ServiceContext)
method will be called on it.
Note that Ignite cannot guarantee that the service exits from Service.execute(org.apache.ignite.services.ServiceContext)
method whenever Service.cancel(org.apache.ignite.services.ServiceContext)
is called. It is up to the user to
make sure that the service code properly reacts to cancellations.
Supports asynchronous execution (see IgniteAsyncSupport
).
name
- Name of service to cancel.IgniteException
- If failed to cancel service.@IgniteAsyncSupported void cancelAll() throws IgniteException
Note that depending on user logic, it may still take extra time for a service to finish execution, even after it was cancelled.
Supports asynchronous execution (see IgniteAsyncSupport
).
IgniteException
- If failed to cancel services.Collection<ServiceDescriptor> serviceDescriptors()
<T> T service(String name)
T
- Service typename
- Service name.<T> Collection<T> services(String name)
T
- Service type.name
- Service name.<T> T serviceProxy(String name, Class<? super T> svcItf, boolean sticky) throws IgniteException
name
- Service name.svcItf
- Interface for the service.sticky
- Whether or not Ignite should always contact the same remote
service or try to load-balance between services.IgniteException
- If failed to create service proxy.IgniteServices withAsync()
withAsync
in interface IgniteAsyncSupport
Follow @ApacheIgnite
Ignite Fabric : ver. 1.2.0-incubating Release Date : June 16 2015