use of org.apache.ignite.IgniteServices in project ignite by apache.
the class ServicesExample method main.
/**
* Executes example.
*
* @param args Command line arguments, none required.
* @throws Exception If example execution failed.
*/
public static void main(String[] args) throws Exception {
// Mark this node as client node.
Ignition.setClientMode(true);
try (Ignite ignite = Ignition.start("examples/config/example-ignite.xml")) {
if (!ExamplesUtils.hasServerNodes(ignite))
return;
// Deploy services only on server nodes.
IgniteServices svcs = ignite.services(ignite.cluster().forServers());
try {
// Deploy cluster singleton.
svcs.deployClusterSingleton("myClusterSingletonService", new SimpleMapServiceImpl());
// Deploy node singleton.
svcs.deployNodeSingleton("myNodeSingletonService", new SimpleMapServiceImpl());
// Deploy 2 instances, regardless of number nodes.
svcs.deployMultiple("myMultiService", new SimpleMapServiceImpl(), 2, /*total number*/
0);
// Example for using a service proxy
// to access a remotely deployed service.
serviceProxyExample(ignite);
// Example for auto-injecting service proxy
// into remote closure execution.
serviceInjectionExample(ignite);
} finally {
// Undeploy all services.
ignite.services().cancelAll();
}
}
}
use of org.apache.ignite.IgniteServices in project ignite by apache.
the class ClientServiceInvokeRequest method process.
/**
* {@inheritDoc}
*/
@Override
public ClientResponse process(ClientConnectionContext ctx) {
if (F.isEmpty(name))
throw new IgniteException("Service name can't be empty");
if (F.isEmpty(methodName))
throw new IgniteException("Method name can't be empty");
ServiceDescriptor desc = findServiceDescriptor(ctx, name);
Class<?> svcCls = desc.serviceClass();
ClusterGroupAdapter grp = ctx.kernalContext().cluster().get();
grp = (ClusterGroupAdapter) (nodeIds.isEmpty() ? grp.forServers() : grp.forNodeIds(nodeIds));
IgniteServices services = grp.services();
try {
Object res;
if (PlatformService.class.isAssignableFrom(svcCls)) {
// Never deserialize platform service arguments and result: may contain platform-only types.
PlatformService proxy = ((IgniteServicesImpl) services).serviceProxy(name, PlatformService.class, false, timeout, true);
res = proxy.invokeMethod(methodName, keepBinary(), false, args, callAttrs);
} else {
// Deserialize Java service arguments when not in keepBinary mode.
if (!keepBinary() && args.length > 0) {
for (int i = 0; i < args.length; i++) {
if (paramTypeIds != null)
// Skip parameter typeId, we already read it in constructor.
reader.readInt();
args[i] = reader.readObject();
}
}
GridServiceProxy<?> proxy = new GridServiceProxy<>(grp, name, Service.class, false, timeout, ctx.kernalContext(), null, true);
Method method = resolveMethod(ctx, svcCls);
if (!BinaryArray.useBinaryArrays())
PlatformServices.convertArrayArgs(args, method);
res = proxy.invokeMethod(method, args, callAttrs);
}
return new ClientObjectResponse(requestId(), res);
} catch (PlatformNativeException e) {
ctx.kernalContext().log(getClass()).error("Failed to invoke platform service", e);
throw new IgniteException("Failed to invoke platform service, see server logs for details");
} catch (Throwable e) {
throw new IgniteException(e);
}
}
use of org.apache.ignite.IgniteServices in project ignite by apache.
the class IgniteClientReconnectServicesTest method testReconnectInDeployingNew.
/**
* @throws Exception If failed.
*/
@SuppressWarnings("ThrowableResultOfMethodCallIgnored")
@Test
public void testReconnectInDeployingNew() throws Exception {
IgniteEx client = grid(serverCount());
assertTrue(client.cluster().localNode().isClient());
final IgniteServices services = client.services();
Ignite srv = ignite(0);
final IgniteInternalFuture<Object> fut = GridTestUtils.runAsync(new Callable<Object>() {
@Override
public Object call() throws Exception {
try {
services.deployClusterSingleton("testReconnectInDeploying", new TestServiceImpl());
} catch (IgniteClientDisconnectedException e) {
checkAndWait(e);
return true;
}
return false;
}
});
reconnectClientNode(client, srv, () -> {
// Check that client waiting operation.
GridTestUtils.assertThrows(log, () -> fut.get(200), IgniteFutureTimeoutCheckedException.class, null);
try {
assertNotDone(fut);
} catch (Exception e) {
fail("Unexpected exception has been thrown, err=" + e.getMessage());
}
});
assertTrue((Boolean) fut.get(2, TimeUnit.SECONDS));
}
use of org.apache.ignite.IgniteServices in project ignite by apache.
the class IgniteServiceDynamicCachesSelfTest method testDeployCalledAfterCacheStart.
/**
* @throws Exception If failed.
*/
@Test
public void testDeployCalledAfterCacheStart() throws Exception {
String cacheName = "cache";
CacheConfiguration ccfg = new CacheConfiguration(cacheName);
ccfg.setBackups(1);
Ignite ig = ignite(0);
ig.createCache(ccfg);
try {
final IgniteServices svcs = ig.services();
final String svcName = "myService";
svcs.deployKeyAffinitySingleton(svcName, new TestService(), cacheName, primaryKey(ig.cache(cacheName)));
boolean res = GridTestUtils.waitForCondition(new PA() {
@Override
public boolean apply() {
return svcs.service(svcName) != null;
}
}, 10 * 1000);
assertTrue("Service was not deployed", res);
ig.destroyCache(cacheName);
res = GridTestUtils.waitForCondition(new PA() {
@Override
public boolean apply() {
return svcs.service(svcName) == null;
}
}, 10 * 1000);
assertTrue("Service was not undeployed", res);
} finally {
ig.services().cancelAll();
ig.destroyCache(cacheName);
}
}
use of org.apache.ignite.IgniteServices in project ignite by apache.
the class CacheLateAffinityAssignmentTest method testServiceReassign.
/**
* @throws Exception If failed.
*/
@Test
public void testServiceReassign() throws Exception {
skipCheckOrder = true;
Ignite ignite0 = startServer(0, 1);
IgniteServices svcs = ignite0.services();
for (int i = 0; i < 10; i++) svcs.deployKeyAffinitySingleton("service-" + i, new TestServiceImpl(i), CACHE_NAME1, i);
startServer(1, 2);
startServer(2, 3);
Map<String, List<List<ClusterNode>>> assignments = checkAffinity(3, topVer(3, 1), true);
checkServicesDeploy(ignite(0), assignments.get(CACHE_NAME1));
stopGrid(0);
boolean primaryChanged = calculateAffinity(4, false, assignments);
assignments = checkAffinity(2, topVer(4, 0), !primaryChanged);
if (primaryChanged)
checkAffinity(2, topVer(4, 1), true);
checkServicesDeploy(ignite(1), assignments.get(CACHE_NAME1));
}
Aggregations