/** * Send internal message to hazelcast ring * * @param ring Hazelcast RingBuffer * @param message Internal Message */ protected void sendMessage(Ringbuffer<InternalMessage> ring, InternalMessage message) { ring.addAsync(message, OverflowPolicy.OVERWRITE).andThen(new ExecutionCallback<Long>() { @Override public void onResponse(Long response) { if (response > 0) { logger.debug("Communicator succeed: Successful add message {} to ring buffer {}", message.getMessageType(), ring.getName()); } else { logger.debug("Communicator failed: Failed to add message {} to ring buffer {}: no space", message.getMessageType(), ring.getName()); } } @Override public void onFailure(Throwable t) { logger.warn("Communicator failed: Failed to add message {} to ring buffer {}: ", message.getMessageType(), ring.getName(), t); } }); }
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 void accept(Command command, CompletableFuture<CommandResult> commandResultCompletableFuture) { executorService.submitToKeyOwner( RemoteCommand.processing(command), command.getAggregateId().getId(), new ExecutionCallback<CommandResult>() { @Override public void onResponse(CommandResult commandResult) { commandResultCompletableFuture.complete(commandResult); } @Override public void onFailure(Throwable throwable) { commandResultCompletableFuture.completeExceptionally(throwable); } }); }
@Test(timeout = DEFAULT_TIMEOUT, expected = IllegalArgumentException.class) @SuppressWarnings("unchecked") public void testAwait_withExceptionInFuture() { when(map.putAsync(anyInt(), anyString())).thenReturn(future); doAnswer(new Answer() { @Override public Object answer(InvocationOnMock invocation) throws Throwable { Object[] arguments = invocation.getArguments(); ExecutionCallback<String> callback = (ExecutionCallback<String>) arguments[0]; Exception exception = new IllegalArgumentException("expected exception"); callback.onFailure(exception); return null; } }).when(future).andThen(any(ExecutionCallback.class)); streamer.pushEntry(1, "value"); streamer.await(); }
@Test(timeout = DEFAULT_TIMEOUT, expected = IllegalArgumentException.class) @SuppressWarnings("unchecked") public void testAwait_withExceptionInFuture() { when(cache.putAsync(anyInt(), anyString())).thenReturn(future); doAnswer(new Answer() { @Override public Object answer(InvocationOnMock invocation) throws Throwable { Object[] arguments = invocation.getArguments(); ExecutionCallback<String> callback = (ExecutionCallback<String>) arguments[0]; Exception exception = new IllegalArgumentException("expected exception"); callback.onFailure(exception); return null; } }).when(future).andThen(any(ExecutionCallback.class)); streamer.pushEntry(1, "value"); streamer.await(); }
/** * {@inheritDoc} * * @deprecated not implemented yet * @throws UnsupportedOperationException not implemented yet */ @Deprecated @SuppressWarnings("rawtypes") @Override public void submitToKey(K key, EntryProcessor entryProcessor, ExecutionCallback callback) { throw new UnsupportedOperationException(); }
@Override public void andThen(ExecutionCallback<Void> callback) { future.andThen(new ExecutionCallback<T>() { @Override public void onResponse(T response) { callback.onResponse(null); } @Override public void onFailure(Throwable t) { callback.onFailure(t); } }); }
@Override public void andThen(ExecutionCallback<Void> callback, Executor executor) { future.andThen(new ExecutionCallback<T>() { @Override public void onResponse(T response) { callback.onResponse(null); } @Override public void onFailure(Throwable t) { callback.onFailure(t); } }, executor); }
private void invokeStartExecution() { jobStatus.set(RUNNING); logger.fine("Executing " + jobIdString()); long executionId = this.executionId; AtomicBoolean cancellation = new AtomicBoolean(); ExecutionCallback<Object> callback = new ExecutionCallback<Object>() { @Override public void onResponse(Object response) { } @Override public void onFailure(Throwable t) { if (cancellation.compareAndSet(false, true)) { cancelExecute(jobId, executionId); } } }; cancellationFuture.whenComplete(withTryCatch(logger, (r, e) -> { if (e instanceof CancellationException) { callback.onFailure(e); } })); Function<ExecutionPlan, Operation> operationCtor = plan -> new StartExecutionOperation(jobId, executionId); invoke(operationCtor, this::onExecuteStepCompleted, callback); if (isSnapshottingEnabled()) { coordinationService.scheduleSnapshot(jobId, executionId); } }
/** * This method will generate an {@link ExecutionCallback} which * allows to asynchronously get notified when the execution is completed, * either successfully or with error by calling {@code onResponse} on success * and {@code onError} on error respectively. * * @param onResponse function to call when execution is completed successfully * @param onError function to call when execution is completed with error * @param <T> type of the response * @return {@link ExecutionCallback} */ public static <T> ExecutionCallback<T> callbackOf(Consumer<T> onResponse, Consumer<Throwable> onError) { return new ExecutionCallback<T>() { @Override public void onResponse(T o) { onResponse.accept(o); } @Override public void onFailure(Throwable throwable) { onError.accept(throwable); } }; }
private <T extends Serializable> CompletableFuture<CommandResult<T>> invokeAsync(K key, JCacheEntryProcessor<K, T> entryProcessor) { CompletableFuture<CommandResult<T>> future = new CompletableFuture<>(); cache.submitToKey(key, adoptEntryProcessor(entryProcessor), new ExecutionCallback() { @Override public void onResponse(Object response) { future.complete((CommandResult<T>) response); } @Override public void onFailure(Throwable t) { future.completeExceptionally(t); } }); return future; }
private static ExecutionCallback<Map<String, Long>> buildCallback() { return new ExecutionCallback<Map<String, Long>>() { @Override public void onResponse(Map<String, Long> stringLongMap) { System.out.println("Calculation finished! :)"); } @Override public void onFailure(Throwable throwable) { throwable.printStackTrace(); } }; }
@Test(timeout = DEFAULT_TIMEOUT) @SuppressWarnings("unchecked") public void testAwait() { when(map.putAsync(anyInt(), anyString())).thenReturn(future); doAnswer(new Answer() { @Override public Object answer(InvocationOnMock invocation) throws Throwable { Object[] arguments = invocation.getArguments(); ExecutionCallback<String> callback = (ExecutionCallback<String>) arguments[0]; callback.onResponse("value"); return null; } }).when(future).andThen(any(ExecutionCallback.class)); Thread thread = new Thread() { @Override public void run() { for (int i = 0; i < 5000; i++) { streamer.pushEntry(i, "value"); } } }; thread.start(); streamer.await(); joinThread(thread); }
@Test(timeout = DEFAULT_TIMEOUT) @SuppressWarnings("unchecked") public void testAwait() { when(cache.putAsync(anyInt(), anyString())).thenReturn(future); doAnswer(new Answer() { @Override public Object answer(InvocationOnMock invocation) throws Throwable { Object[] arguments = invocation.getArguments(); ExecutionCallback<String> callback = (ExecutionCallback<String>) arguments[0]; callback.onResponse("value"); return null; } }).when(future).andThen(any(ExecutionCallback.class)); Thread thread = new Thread() { @Override public void run() { for (int i = 0; i < 5000; i++) { streamer.pushEntry(i, "value"); } } }; thread.start(); streamer.await(); joinThread(thread); }
public static void main(String[] args) throws Exception { // Prepare Hazelcast cluster HazelcastInstance hazelcastInstance = buildCluster(3); try { // Read data fillMapWithData(hazelcastInstance); JobTracker tracker = hazelcastInstance.getJobTracker(TRACKER_NAME); IMap<String, String> map = hazelcastInstance.getMap(MAP_NAME); KeyValueSource<String, String> source = KeyValueSource.fromMap(map); Job<String, String> job = tracker.newJob(source); final JobCompletableFuture<List<Map.Entry<String, Integer>>> future = job .mapper(new TokenizerMapper()) // Activate Combiner to add combining phase! // .combiner(new WordcountCombinerFactory()) .reducer(new WordcountReducerFactory()) // .submit(); // add collator for sorting and top10 .submit(new WordcountCollator()); future.andThen(new ExecutionCallback<List<Map.Entry<String, Integer>>>() { @Override public void onResponse(List<Map.Entry<String, Integer>> response) { System.out.println(ToStringPrettyfier.toString(response)); } @Override public void onFailure(Throwable t) { } }); //System.out.println(ToStringPrettyfier.toString(future.get())); } finally { // Shutdown cluster //Hazelcast.shutdownAll(); } }
@Override public void submitToKey(K key, EntryProcessor entryProcessor, ExecutionCallback callback) { map.submitToKey(key, entryProcessor, callback); }
@Override public void andThen(ExecutionCallback<Object> executionCallback) { executionCallback.onResponse(null); }
@Override public void andThen(ExecutionCallback<Object> executionCallback, Executor executor) { throw new UnsupportedOperationException("not implemented"); }
@Override public void submitToKey(Object key, EntryProcessor entryProcessor, ExecutionCallback callback) { }
ExecutionCallback<V> getExecutionCallback();