public static void main(String[] args) { // 同時実行 CompletableFuture.allOf( CompletableFuture.runAsync(() -> executeJob("chunk-sample")), CompletableFuture.runAsync(() -> executeJob("chunk-sample")), CompletableFuture.runAsync(() -> executeJob("chunk-sample")), CompletableFuture.runAsync(() -> executeJob("chunk-sample"))) .join(); executeJob("cleanup-job"); System.exit(0); }
/** * Connects to an RS world. * * @param world the world, as a string. For RS3, use a numeric string. For oldschool, use * "oldschool[world]". * @return A future for the connection task. */ public CompletableFuture<Void> connect(String world) { if (connectionTask != null && !connectionTask.isDone()) { connectionTask.cancel(true); } disconnect(); String host; int port; if (world.startsWith("oldschool")) { host = world + HOST_SUFFIX; port = OLDSCHOOL_PORT; } else { host = "world" + world + HOST_SUFFIX; port = RS3_PORT; } connectionTask = new CompletableFuture<>(); CompletableFuture.runAsync( () -> { try { synchronized (this) { socket = new Socket(host, port); } connectionTask.complete(null); } catch (IOException e) { connectionTask.completeExceptionally(e); } }); return connectionTask; }
@Test @SuppressWarnings("unchecked") public void modificationDuringTransactionCausesAbort() throws Exception { Map<String, String> testMap = getRuntime().getObjectsView().open(CorfuRuntime.getStreamID("A"), SMRMap.class); assertThat(testMap.put("a", "z")); getRuntime().getObjectsView().TXBegin(); assertThat(testMap.put("a", "a")).isEqualTo("z"); assertThat(testMap.put("a", "b")).isEqualTo("a"); assertThat(testMap.get("a")).isEqualTo("b"); CompletableFuture cf = CompletableFuture.runAsync( () -> { Map<String, String> testMap2 = getRuntime() .getObjectsView() .open( UUID.nameUUIDFromBytes("A".getBytes()), SMRMap.class, null, EnumSet.of(ObjectOpenOptions.NO_CACHE), SerializerType.JSON); testMap2.put("a", "f"); }); cf.join(); assertThatThrownBy(() -> getRuntime().getObjectsView().TXEnd()) .isInstanceOf(TransactionAbortedException.class); }
@Override protected void append(final ILoggingEvent evt) { try { final URL url = new URL(API_URL); final StringWriter w = new StringWriter(); w.append("token=").append(token).append("&"); if (channel != null) { w.append("channel=").append(URLEncoder.encode(channel, "UTF-8")).append("&"); } if (iconEmoji != null) { w.append("icon_emoji=") .append(URLEncoder.encode(String.format(":%s:", iconEmoji), "UTF-8")) .append("&"); } if (userName != null) { w.append("username="******"UTF-8")).append("&"); } if (layout != null) { w.append("text=").append(URLEncoder.encode(layout.doLayout(evt), "UTF-8")); } else { w.append("text=").append(URLEncoder.encode(defaultLayout.doLayout(evt), "UTF-8")); } final byte[] bytes = w.toString().getBytes("UTF-8"); CompletableFuture.runAsync( () -> { HttpURLConnection conn = null; try { conn = (HttpURLConnection) url.openConnection(); conn.setDoOutput(true); conn.setRequestMethod("POST"); conn.setFixedLengthStreamingMode(bytes.length); conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); try (OutputStream os = conn.getOutputStream()) { os.write(bytes); os.flush(); } } catch (IOException e) { e.printStackTrace(); addError("Error to post log to Slack.com (" + channel + "): " + evt, e); } finally { if (conn != null) { conn.disconnect(); } } }); } catch (Exception ex) { ex.printStackTrace(); addError("Error to post log to Slack.com (" + channel + "): " + evt, ex); } }
@Override public CompletableFuture<Void> close() { return CompletableFuture.runAsync( () -> { if (keepAliveFuture != null) { keepAliveFuture.cancel(); } if (retryFuture != null) { retryFuture.cancel(); } onClose(); }, context.executor()); }
@Override @SuppressWarnings("unchecked") public CompletableFuture<Void> dispatchAsync(String dispatchId, Runnable task) { try { return (CompletableFuture<Void>) cachedDispatchQueues.compute( dispatchId, (k, queue) -> { CompletableFuture<Void> voidCompletableFuture = (queue == null) ? CompletableFuture.runAsync(task) : ((CompletableFuture<Void>) queue).thenRunAsync(task); return voidCompletableFuture; }); } catch (Throwable t) { log.warn( "Exception thrown when calling dispatchAngGetFuture for dispatchId[{}]", dispatchId, t); throw t; } }
public static void addServer(String serverName) { CompletableFuture.runAsync( () -> { Connection c = null; PreparedStatement ps = null; try { c = XCom.getInstance().getConnection(); ps = c.prepareStatement("insert into servers values(?);"); ps.setString(1, serverName); ps.execute(); } catch (Exception e) { e.printStackTrace(); } finally { try { ps.close(); } catch (Exception e) { e.printStackTrace(); } } }); }
public static void main(String[] args) throws ExecutionException, InterruptedException { Set<String> set = new HashSet<>(Arrays.asList("1", "2", "3")); CompletableFuture<Void> future = CompletableFuture.runAsync( () -> { HazelcastInstance hz1 = Hazelcast.newHazelcastInstance(); IMap<String, Integer> map = hz1.getMap("somemap"); EntryUpdatedListener<String, String> stringStringEntryUpdatedListener = event -> System.out.println("Entry Updated:" + event); map.addLocalEntryListener(stringStringEntryUpdatedListener); set.forEach(k -> map.put(k, Integer.valueOf(k))); set.forEach(k -> map.put(k, Integer.valueOf(k) + 10)); }) .thenRun( () -> { HazelcastInstance hz2 = Hazelcast.newHazelcastInstance(); IMap<String, Integer> map = hz2.getMap("somemap"); Set<String> localKeySet = map.localKeySet(); set.removeAll(localKeySet); System.out.println("out keys:" + set); map.executeOnKeys( set, new AbstractEntryProcessor<String, Integer>() { @Override public Object process(Map.Entry<String, Integer> entry) { return entry.setValue(2); } }); // Object executeOnKey( K key, EntryProcessor entryProcessor ); // void submitToKey( K key, EntryProcessor entryProcessor, // ExecutionCallback callback ); // Map<K, Object> executeOnEntries( EntryProcessor entryProcessor ); // Map<K, Object> executeOnEntries( EntryProcessor entryProcessor, // Predicate predicate ); }); future.get(); }
public CompletableFuture<Duration> doClick() { long t0 = System.nanoTime(); CompletableFuture<Duration> task = new CompletableFuture<>(); CompletableFuture.runAsync( () -> { synchronized (this) { try { if (isConnected()) { socket.getOutputStream().write(SOCKET_SEND_BYTE); socket.getInputStream().read(); } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); task.completeExceptionally(e); } } long t1 = System.nanoTime(); task.complete(Duration.ofNanos(t1 - t0)); }); return task; }