@Override public boolean apply() { MapService<Integer, String> svc = grid(idx) .services() .serviceProxy(name, MapService.class, false); ref.set(svc); return svc instanceof Service; } }, 2000);
/** * @param node Node. * @return Service proxy. */ private static MyService serviceProxy(Ignite node) { return node.services().serviceProxy("DummyService", MyService.class, true); }
/** * @throws Exception If failed. */ private CounterService proxy(Ignite g) throws Exception { return g.services().serviceProxy(SERVICE_NAME, CounterService.class, false); }
/** {@inheritDoc} */ @Override public void inject(GridResourceMethod mtd, Object target, Class<?> depCls, GridDeployment dep) throws IgniteCheckedException { ServiceResource ann = (ServiceResource)mtd.getAnnotation(); Class svcItf = ann.proxyInterface(); Object svc; if (svcItf == Void.class) svc = ignite.services().service(ann.serviceName()); else svc = ignite.services().serviceProxy(ann.serviceName(), svcItf, ann.proxySticky()); Class<?>[] types = mtd.getMethod().getParameterTypes(); if (types.length != 1) throw new IgniteCheckedException("Setter does not have single parameter of required type [type=" + svc.getClass().getName() + ", setter=" + mtd + ']'); if (svc != null) GridResourceUtils.inject(mtd.getMethod(), target, svc); } }
/** * @throws Exception If failed. */ @Test public void test() throws Exception { captureErr(); new Thread(this::startServer).start(); Ignite client = startGrid(getConfiguration("client").setClientMode(true)); IgniteServices services = client.services(); SimpleService srvc = services.serviceProxy("service", SimpleService.class, false); Thread.sleep(1000); srvc.isWorking(); assertFalse(getErr().contains("Cache is not configured:")); }
/** {@inheritDoc} */ @Override public void inject(GridResourceField field, Object target, Class<?> depCls, GridDeployment dep) throws IgniteCheckedException { ServiceResource ann = (ServiceResource)field.getAnnotation(); Class svcItf = ann.proxyInterface(); Object svc; if (svcItf == Void.class) svc = ignite.services().service(ann.serviceName()); else svc = ignite.services().serviceProxy(ann.serviceName(), svcItf, ann.proxySticky()); if (svc != null) GridResourceUtils.inject(field.getField(), target, svc); }
/** {@inheritDoc} */ @Override public PlatformTarget processInStreamOutObject(int type, BinaryRawReaderEx reader) throws IgniteCheckedException { switch (type) { case OP_SERVICE_PROXY: { String name = reader.readString(); boolean sticky = reader.readBoolean(); ServiceDescriptor d = findDescriptor(name); if (d == null) throw new IgniteException("Failed to find deployed service: " + name); Object proxy = PlatformService.class.isAssignableFrom(d.serviceClass()) ? services.serviceProxy(name, PlatformService.class, sticky) : new GridServiceProxy<>(services.clusterGroup(), name, Service.class, sticky, 0, platformCtx.kernalContext()); return new ServiceProxyHolder(proxy, d.serviceClass(), platformContext()); } } return super.processInStreamOutObject(type, reader); }
/** * @throws Exception If failed. */ @Test public void testExecution() throws Exception { startGrid(0); // Server. Ignition.setClientMode(true); Ignite ignite = startGrid(); // Client. ignite.services().deployClusterSingleton("my-service", new MyServiceImpl()); MyService svc = ignite.services().serviceProxy("my-service", MyService.class, false); svc.hello(); }
/** * @throws Exception If failed. */ @Test public void testClusterSingletonProxy() throws Exception { String name = "testClusterSingletonProxy"; Ignite ignite = randomGrid(); ignite.services().deployClusterSingleton(name, new CounterServiceImpl()); CounterService svc = ignite.services().serviceProxy(name, CounterService.class, true); for (int i = 0; i < 10; i++) svc.increment(); assertEquals(10, svc.get()); }
/** * @throws Exception If failed. */ @Test public void testNodeSingletonProxy() throws Exception { String name = "testNodeSingletonProxy"; Ignite ignite = randomGrid(); ignite.services().deployNodeSingleton(name, new CounterServiceImpl()); CounterService svc = ignite.services().serviceProxy(name, CounterService.class, false); for (int i = 0; i < 10; i++) svc.increment(); assertEquals(10, svc.get()); assertEquals(10, svc.localIncrements()); assertEquals(10, ignite.services(ignite.cluster().forLocal()). serviceProxy(name, CounterService.class, false).localIncrements()); // Make sure that remote proxies were not called. for (ClusterNode n : ignite.cluster().forRemotes().nodes()) { CounterService rmtSvc = ignite.services(ignite.cluster().forNode(n)).serviceProxy(name, CounterService.class, false); assertEquals(0, rmtSvc.localIncrements()); } }
/** * Unwraps error message from InvocationTargetException. * * @throws Exception If failed. */ @SuppressWarnings("ThrowableNotThrown") @Test public void testException() throws Exception { String name = "errorService"; Ignite ignite = grid(0); ignite.services(ignite.cluster().forRemotes()).deployNodeSingleton(name, new ErrorServiceImpl()); final ErrorService svc = ignite.services().serviceProxy(name, ErrorService.class, false); GridTestUtils.assertThrows(log, new Callable<Object>() { @Override public Object call() throws Exception { svc.go(); return null; } }, ErrorServiceException.class, "Test exception"); }
/** * @throws Exception If failed. */ @Test public void testRemoteNotStickProxyInvocation() throws Exception { final String name = "testRemoteNotStickProxyInvocation"; final Ignite ignite = grid(0); ignite.services().deployNodeSingleton(name, new MapServiceImpl<String, Integer>()); // Get remote proxy. MapService<Integer, String> svc = ignite.services(ignite.cluster().forRemotes()). serviceProxy(name, MapService.class, false); // Make sure service is a local instance. assertFalse(svc instanceof Service); for (int i = 0; i < nodeCount(); i++) svc.put(i, Integer.toString(i)); int size = 0; for (ClusterNode n : ignite.cluster().forRemotes().nodes()) { MapService<Integer, String> map = ignite.services(ignite.cluster().forNode(n)). serviceProxy(name, MapService.class, false); // Make sure service is a local instance. assertFalse(map instanceof Service); size += map.size(); } assertEquals(nodeCount(), size); }
/** * @throws Exception If failed. */ @Test public void testClientReconnect() throws Exception { startGrid("server"); Ignite client = startGrid("client"); client.services().deployClusterSingleton("my-service", new MyServiceImpl()); MyService proxy = client.services().serviceProxy("my-service", MyService.class, false); assertEquals(42, proxy.hello()); final CountDownLatch latch = new CountDownLatch(1); client.events().localListen(new IgnitePredicate<Event>() { @Override public boolean apply(Event event) { latch.countDown(); return true; } }, EventType.EVT_CLIENT_NODE_RECONNECTED); stopGrid("server"); startGrid("server"); assertTrue(latch.await(10, TimeUnit.SECONDS)); client.services().deployClusterSingleton("my-service", new MyServiceImpl()); assertEquals(42, proxy.hello()); }
/** * @throws Exception If failed. */ @Test public void testPackagePrivateService() throws Exception { try { Ignite server = startGrid("server"); server.services().deployClusterSingleton("my-service", MyServiceFactory.create()); Ignition.setClientMode(true); Ignite client = startGrid("client"); MyService svc = client.services().serviceProxy("my-service", MyService.class, true); assertEquals(42, svc.hello()); } finally { stopAllGrids(); } } }
/** * @throws Exception If failed. */ @Test public void testClientReconnectLongServiceInit() throws Exception { startGrid("server"); Ignite client = startGrid("client"); client.services().deployClusterSingleton("my-service", new MyLongInitServiceImpl()); MyService proxy = client.services().serviceProxy("my-service", MyService.class, false); assertEquals(9001, proxy.hello()); final CountDownLatch latch = new CountDownLatch(1); client.events().localListen(new IgnitePredicate<Event>() { @Override public boolean apply(Event event) { latch.countDown(); return true; } }, EventType.EVT_CLIENT_NODE_RECONNECTED); stopGrid("server"); startGrid("server"); assertTrue(latch.await(10, TimeUnit.SECONDS)); client.services().deployClusterSingleton("my-service", new MyLongInitServiceImpl()); assertEquals(9001, proxy.hello()); }
/** * @throws Exception If failed. */ @Test public void testMultiNodeProxy() throws Exception { Ignite ignite = randomGrid(); int extras = 3; startExtraNodes(extras); String name = "testMultiNodeProxy"; ignite.services().deployNodeSingleton(name, new CounterServiceImpl()); CounterService svc = ignite.services().serviceProxy(name, CounterService.class, false); for (int i = 0; i < extras; i++) { svc.increment(); stopGrid(nodeCount() + i); } assertEquals(extras, svc.get()); }
/** * @throws Exception If failed. */ @Test public void testNodeSingletonRemoteStickyProxy() throws Exception { String name = "testNodeSingletonRemoteStickyProxy"; Ignite ignite = randomGrid(); // Deploy only on remote nodes. ignite.services(ignite.cluster().forRemotes()).deployNodeSingleton(name, new CounterServiceImpl()); // Get local proxy. CounterService svc = ignite.services().serviceProxy(name, CounterService.class, true); for (int i = 0; i < 10; i++) svc.increment(); assertEquals(10, svc.get()); int total = 0; for (ClusterNode n : ignite.cluster().forRemotes().nodes()) { CounterService rmtSvc = ignite.services(ignite.cluster().forNode(n)).serviceProxy(name, CounterService.class, false); int cnt = rmtSvc.localIncrements(); assertTrue("Invalid local increments: " + cnt, cnt == 10 || cnt == 0); total += rmtSvc.localIncrements(); } assertEquals(10, total); }
/** * Test OOME in service method invocation. */ @Test public void testServiceInvokeOomError() throws Exception { IgniteEx ignite0 = startGrid(0); IgniteEx ignite1 = startGrid(1); IgniteCache<Integer, Integer> cache1 = ignite1.getOrCreateCache(DEFAULT_CACHE_NAME); awaitPartitionMapExchange(); Integer key = primaryKey(cache1); ignite0.services().deployKeyAffinitySingleton("fail-invoke-service", new FailServiceImpl(false), DEFAULT_CACHE_NAME, key); FailService svc = ignite0.services().serviceProxy("fail-invoke-service", FailService.class, false); try { svc.fail(); } catch (Throwable ignore) { // Expected. } assertFailureState(ignite0, ignite1); }
/** * @throws Exception If failed. */ @Test public void testReconnect() throws Exception { Ignite client = grid(serverCount()); assertTrue(client.cluster().localNode().isClient()); IgniteServices services = client.services(); services.deployClusterSingleton("testReconnect", new TestServiceImpl()); TestService srvc = services.serviceProxy("testReconnect", TestService.class, false); assertNotNull(srvc); long topVer = grid(0).cluster().topologyVersion(); assertEquals((Object)topVer, srvc.test()); Ignite srv = ignite(0); reconnectClientNode(client, srv, null); CountDownLatch latch = new CountDownLatch(1); DummyService.exeLatch("testReconnect2", latch); services.deployClusterSingleton("testReconnect2", new DummyService()); assertTrue(latch.await(5000, TimeUnit.MILLISECONDS)); assertEquals((Object)(topVer + 2), srvc.test()); }
/** * @throws Exception If failed. */ @Test public void testNodeSingletonNotDeployedProxy() throws Exception { String name = "testNodeSingletonNotDeployedProxy"; IgniteEx ignite = randomGrid(); try { // Deploy only on remote nodes. ignite.services(ignite.cluster().forRemotes()).deployNodeSingleton(name, new CounterServiceImpl()); assertFalse("Should not reach here in this mode, because exception should be thrown.", ignite.context().service() instanceof IgniteServiceProcessor); info("Deployed service: " + name); // Get local proxy. CounterService svc = ignite.services().serviceProxy(name, CounterService.class, false); svc.increment(); fail("Should never reach here."); } catch (IgniteException e) { info("Got expected exception: " + e.getMessage()); } } }