@Override public <T> Future<T> submit(Callable<T> task) { return this.executor.submit(task); }
private void doExecute(boolean onKey, boolean onMember, String[] args) { // executeOnKey <echo-string> <key> try { IExecutorService executorService = hazelcast.getExecutorService("default"); Echo callable = new Echo(args[1]); Future<String> future; if (onKey) { String key = args[2]; future = executorService.submitToKeyOwner(callable, key); } else if (onMember) { int memberIndex = Integer.parseInt(args[2]); List<Member> members = new LinkedList<Member>(hazelcast.getCluster().getMembers()); if (memberIndex >= members.size()) { throw new IndexOutOfBoundsException("Member index: " + memberIndex + " must be smaller than " + members .size()); } Member member = members.get(memberIndex); future = executorService.submitToMember(callable, member); } else { future = executorService.submit(callable); } println("Result: " + future.get()); } catch (InterruptedException e) { currentThread().interrupt(); e.printStackTrace(); } catch (ExecutionException e) { e.printStackTrace(); } }
@Override public Future<?> submit(Runnable task) { return this.executor.submit(task); }
@Override public <T> Future<T> submit(Runnable task, T result) { return this.executor.submit(task, result); } }
public static void main(String[] args) throws Exception { HazelcastInstance hz = Hazelcast.newHazelcastInstance(); IExecutorService executor = hz.getExecutorService("executor"); Future<Integer> future = executor.submit(new ComputationHeavyTask(), MemberSelectors.LITE_MEMBER_SELECTOR); System.out.println("Result: " + future.get()); } }
private DocumentAccessor runSimpleDocumentTask(Callable<DocumentAccessor> task, Properties props) throws BagriException { //checkDocumentProperties(props); Future<DocumentAccessor> future = execService.submit(task); try { DocumentAccessor result = future.get(); logger.trace("storeDocument.exit; returning: {}", result); return (DocumentAccessor) result; } catch (InterruptedException | ExecutionException ex) { // the document could be processed anyway.. logger.error("runSimpleDocumentTask.error", ex); throw new BagriException(ex, ecDocument); } }
public static void main(String[] args) { HazelcastInstance hz = Hazelcast.newHazelcastInstance(); IExecutorService executor = hz.getExecutorService("executor"); ExecutionCallback<Long> executionCallback = new ExecutionCallback<Long>() { public void onFailure(Throwable t) { t.printStackTrace(); } public void onResponse(Long response) { System.out.println("Result: " + response); } }; executor.submit(new FibonacciCallable(10), executionCallback); System.out.println("Fibonacci task submitted"); } }
@Override public boolean authenticate(String username, String password) { logger.trace("authenticate.enter; got username: {}", username); UserAuthenticator auth = new UserAuthenticator(username, password); Future<Boolean> future = execService.submit(auth); try { Boolean result = future.get(); logger.trace("authenticate.exit; returning: {}", result); return result; } catch (InterruptedException | ExecutionException ex) { logger.error("authenticate; error getting result", ex); //throw new XDMException(ex, XDMException.ecDocument); } return false; }
public static void main(String[] args) throws Exception { int n = 10; if (args.length != 0) { n = Integer.parseInt(args[0]); } HazelcastInstance hz = Hazelcast.newHazelcastInstance(); IExecutorService executor = hz.getExecutorService("executor"); Future<Long> future = executor.submit(new FibonacciCallable(n)); try { long result = future.get(10, TimeUnit.SECONDS); System.out.println("Result: " + result); } catch (TimeoutException e) { System.err.println("A timeout occurred!"); future.cancel(true); } Hazelcast.shutdownAll(); } }
public class HazelcastTest { @Test public void test() { HazelcastInstance instance = Hazelcast.newHazelcastInstance(); IExecutorService exec = instance.getExecutorService("exec"); for (int i = 0; i < 100; i++) { exec.submit(new MyRunnable()); } } } public class MyRunnable implements Runnable, Serializable { @Override public void run() { long threadId = Thread.currentThread().getId(); System.err.println("threadId: " + threadId); } }
@Override public Collection<String> getCollections() throws BagriException { logger.trace("getCollections.enter; "); Collection<String> result = null; CollectionsProvider task = new CollectionsProvider(repo.getClientId()); Future<Collection<String>> future = execService.submit(task); try { result = future.get(); logger.trace("getCollections.exit; returning: {}", result); return result; } catch (InterruptedException | ExecutionException ex) { logger.error("getCollections; error getting result", ex); throw new BagriException(ex, ecDocument); } }
public static void main(String[] args) throws ExecutionException, InterruptedException { ApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class); HazelcastInstance hazelcastInstance = context.getBean(HazelcastInstance.class); Future<String> f = hazelcastInstance.getExecutorService("test").submit(new SomeTask()); System.out.println(f.get()); HazelcastClient.shutdownAll(); Hazelcast.shutdownAll(); } }
@TimeStep public void timeStep(ThreadState state) throws Exception { int index = state.randomInt(executors.length); IExecutorService executorService = executors[index]; state.futureList.clear(); for (int i = 0; i < submitCount; i++) { Future future = executorService.submit(new Task(testContext.getTestId())); state.futureList.add(future); state.iteration++; } for (Future future : state.futureList) { future.get(); } }
public static void main(String[] args) throws Exception { Config config = new Config(); config.getMemberAttributeConfig().setBooleanAttribute("EAST", true); HazelcastInstance node = Hazelcast.newHazelcastInstance(config); IExecutorService executorService = node.getExecutorService(ForceLocalMemberToBeSafe.class.getName()); Future<Boolean> result = executorService.submit(new MemberSafe(), new MemberSelector() { @Override public boolean select(Member member) { Boolean east = member.getBooleanAttribute("EAST"); return Boolean.TRUE.equals(east); } }); System.out.printf("# Is forcing member to be safe is successful\t: %s\n", result.get()); }
public static void main(String[] args) throws Exception { ApplicationContext context = new GenericXmlApplicationContext("applicationContext.xml"); HazelcastInstance hazelcastInstance = (HazelcastInstance) context.getBean("instance"); Future<String> future = hazelcastInstance.getExecutorService("test").submit(new SomeTask()); System.out.println(future.get()); HazelcastClient.shutdownAll(); Hazelcast.shutdownAll(); } }
@ManagedOperation(description="Register Schemas") @ManagedOperationParameters({ @ManagedOperationParameter(name = "dataFormat", description = "DataFormat responsible for corresponding Schema"), @ManagedOperationParameter(name = "schemaCatalog", description = "A full path to the directory containing Schema files to register")}) public int registerModels(String dataFormat, String modelCatalog) { logger.trace("registerModels.enter;"); long stamp = System.currentTimeMillis(); ModelRegistrator task = new ModelRegistrator(dataFormat, modelCatalog, false); Future<Integer> result = execService.submit(task); int cnt = 0; try { cnt = result.get(); } catch (InterruptedException | ExecutionException ex) { logger.error("", ex); } stamp = System.currentTimeMillis() - stamp; logger.trace("registerModels.exit; returning: {}; timeTaken: {}", cnt, stamp); return cnt; }
@ManagedOperation(description="Register Model") @ManagedOperationParameters({ @ManagedOperationParameter(name = "dataFormat", description = "DataFormat responsible for corresponding Schema"), @ManagedOperationParameter(name = "modelFile", description = "A full path to Schema file to register")}) public int registerModel(String dataFormat, String modelFile) { logger.trace("registerModel.enter;"); long stamp = System.currentTimeMillis(); ModelRegistrator task = new ModelRegistrator(dataFormat, modelFile, true); Future<Integer> result = execService.submit(task); int cnt = 0; try { cnt = result.get(); } catch (InterruptedException | ExecutionException ex) { logger.error("", ex); } stamp = System.currentTimeMillis() - stamp; logger.trace("registerModel.exit; returning: {}; timeTaken: {}", cnt, stamp); return cnt; }
@Override public void run() { long iteration = 0; List<Future> futureList = new LinkedList<Future>(); while (!testContext.isStopped()) { int index = random.nextInt(executors.length); IExecutorService executorService = executors[index]; futureList.clear(); for (int k = 0; k < submitCount; k++) { Future future = executorService.submit(new Task(testContext.getTestId())); futureList.add(future); iteration++; } for (Future future : futureList) { try { future.get(); } catch (InterruptedException e) { throw new RuntimeException(e); } catch (ExecutionException e) { throw new RuntimeException(e); } } if (iteration % 10000 == 0) { log.info(Thread.currentThread().getName() + " At iteration: " + iteration); } } expectedExecutedCounter.addAndGet(iteration); } }
public static void main(String[] args) { // Start the Embedded Hazelcast Cluster Member. HazelcastInstance hz1 = Hazelcast.newHazelcastInstance(); // Start a Second Embedded Hazelcast Cluster Member HazelcastInstance hz2 = Hazelcast.newHazelcastInstance(); // Get the Distributed Executor Service IExecutorService ex = hz1.getExecutorService("my-distributed-executor"); // Submit the MessagePrinter Runnable to a random Hazelcast Cluster Member ex.submit(new MessagePrinter("message to any node")); // Get the first Hazelcast Cluster Member Member firstMember = hz1.getCluster().getMembers().iterator().next(); // Submit the MessagePrinter Runnable to the first Hazelcast Cluster Member ex.executeOnMember(new MessagePrinter("message to very first member of the cluster"), firstMember); // Submit the MessagePrinter Runnable to all Hazelcast Cluster Members ex.executeOnAllMembers(new MessagePrinter("message to all members in the cluster")); // Submit the MessagePrinter Runnable to the Hazelcast Cluster Member owning the key called "key" ex.executeOnKeyOwner(new MessagePrinter("message to the member that owns the following key"), "key"); // Shutdown this Hazelcast Cluster Member hz1.shutdown(); // Shutdown this Hazelcast Cluster Member hz2.shutdown(); }
public static void main(String[] args) { // Enable Code Deployment from this Client classpath to the Cluster Members classpath // User Code Deployment needs to be enabled on the Cluster Members as well. ClientConfig config = new ClientConfig(); ClientUserCodeDeploymentConfig userCodeDeploymentConfig = config.getUserCodeDeploymentConfig(); userCodeDeploymentConfig.setEnabled(true); userCodeDeploymentConfig.addClass(ExecutorServiceSample.MessagePrinter.class); // Start the Hazelcast Client and connect to an already running Hazelcast Cluster on 127.0.0.1 HazelcastInstance hz = HazelcastClient.newHazelcastClient(config); // Get the Distributed Executor Service IExecutorService ex = hz.getExecutorService("my-distributed-executor"); // Submit the MessagePrinter Runnable to a random Hazelcast Cluster Member ex.submit(new MessagePrinter("message to any node")); // Get the first Hazelcast Cluster Member Member firstMember = hz.getCluster().getMembers().iterator().next(); // Submit the MessagePrinter Runnable to the first Hazelcast Cluster Member ex.executeOnMember(new MessagePrinter("message to very first member of the cluster"), firstMember); // Submit the MessagePrinter Runnable to all Hazelcast Cluster Members ex.executeOnAllMembers(new MessagePrinter("message to all members in the cluster")); // Submit the MessagePrinter Runnable to the Hazelcast Cluster Member owning the key called "key" ex.executeOnKeyOwner(new MessagePrinter("message to the member that owns the following key"), "key"); // Shutdown this Hazelcast Client hz.shutdown(); } }