@Test public void testSubmitToKeyOwnerCallable() throws Exception { final int k = simpleTestNodeCount; TestHazelcastInstanceFactory factory = createHazelcastInstanceFactory(k); final HazelcastInstance[] instances = factory.newInstances(new Config()); final AtomicInteger count = new AtomicInteger(0); final CountDownLatch latch = new CountDownLatch(k / 2); final ExecutionCallback callback = new ExecutionCallback() { public void onResponse(Object response) { if ((Boolean) response) count.incrementAndGet(); latch.countDown(); } public void onFailure(Throwable t) {} }; for (int i = 0; i < k; i++) { final HazelcastInstance instance = instances[i]; final IExecutorService service = instance.getExecutorService("testSubmitToKeyOwnerCallable"); final String script = "hazelcast.getCluster().getLocalMember().equals(member)"; final HashMap map = new HashMap(); final Member localMember = instance.getCluster().getLocalMember(); map.put("member", localMember); int key = 0; while (!localMember.equals(instance.getPartitionService().getPartition(++key).getOwner())) ; if (i % 2 == 0) { final Future f = service.submitToKeyOwner(new ScriptCallable(script, map), key); assertTrue((Boolean) f.get(5, TimeUnit.SECONDS)); } else { service.submitToKeyOwner(new ScriptCallable(script, map), key, callback); } } assertTrue(latch.await(30, TimeUnit.SECONDS)); assertEquals(k / 2, count.get()); }
@Test public void testSubmitToMemberRunnable() throws InterruptedException { final int k = simpleTestNodeCount; TestHazelcastInstanceFactory factory = createHazelcastInstanceFactory(k); final HazelcastInstance[] instances = factory.newInstances(new Config()); final AtomicInteger count = new AtomicInteger(0); final CountDownLatch latch = new CountDownLatch(k); final ExecutionCallback callback = new ExecutionCallback() { public void onResponse(Object response) { if (response == null) { count.incrementAndGet(); } latch.countDown(); } public void onFailure(Throwable t) {} }; for (int i = 0; i < k; i++) { final HazelcastInstance instance = instances[i]; final IExecutorService service = instance.getExecutorService("testSubmitToMemberRunnable"); final String script = "if(!hazelcast.getCluster().getLocalMember().equals(member)) " + "hazelcast.getAtomicLong('testSubmitToMemberRunnable').incrementAndGet();"; final HashMap map = new HashMap(); map.put("member", instance.getCluster().getLocalMember()); service.submitToMember( new ScriptRunnable(script, map), instance.getCluster().getLocalMember(), callback); } latch.await(10, TimeUnit.SECONDS); assertEquals(0, instances[0].getAtomicLong("testSubmitToMemberRunnable").get()); assertEquals(k, count.get()); }
@Test public void hazelcastInstanceAwareAndLocal() throws Exception { final Config config = new Config(); config.addExecutorConfig(new ExecutorConfig("test", 1)); final HazelcastInstance instance = createHazelcastInstance(config); IExecutorService executor = instance.getExecutorService("test"); HazelcastInstanceAwareRunnable task = new HazelcastInstanceAwareRunnable(); executor.submit(task).get(); assertTrue("The setHazelcastInstance should have been called", task.initializeCalled); }
@Test public void testManagedContextAndLocal() throws Exception { final Config config = new Config(); config.addExecutorConfig(new ExecutorConfig("test", 1)); config.setManagedContext( new ManagedContext() { @Override public Object initialize(Object obj) { if (obj instanceof RunnableWithManagedContext) { RunnableWithManagedContext task = (RunnableWithManagedContext) obj; task.initializeCalled = true; } return obj; } }); final HazelcastInstance instance = createHazelcastInstance(config); IExecutorService executor = instance.getExecutorService("test"); RunnableWithManagedContext task = new RunnableWithManagedContext(); executor.submit(task).get(); assertTrue( "The task should have been initialized by the ManagedContext", task.initializeCalled); }
private IExecutorService createSingleNodeExecutorService(String name, int poolSize) { final Config config = new Config(); config.addExecutorConfig(new ExecutorConfig(name, poolSize)); final HazelcastInstance instance = createHazelcastInstance(config); return instance.getExecutorService(name); }
public String call() throws Exception { Future future = instance.getExecutorService("NestedExecutorTask").submit(new BasicTestTask()); return (String) future.get(); }