@Suspendable @Override public void start() throws Exception { // Wait for a single event - this just demonstrates that you can wait for single events System.out.println("Waiting for single event"); long tid = awaitEvent(h -> vertx.setTimer(1000, h)); System.out.println("Single event has fired"); }
@Override @Suspendable public void start() throws Exception {
@Suspendable @Override public void start() throws Exception { EventBus eb = vertx.eventBus(); eb.consumer(ADDRESS).handler(msg -> { System.out.println("Waiting"); // reply after one second vertx.setTimer(1000, tid -> msg.reply("wibble")); }); // If you want to do sync stuff in an async handler it must be transformed to a fiber handler vertx.createHttpServer().requestHandler(fiberHandler(req -> { // Send a message to address and wait for a reply Message<String> reply = awaitResult(h -> eb.send(ADDRESS, "blah", h)); System.out.println("Got reply: " + reply.body()); req.response().end("blah"); })).listen(8080, "localhost"); }
@Suspendable @Override public void start() throws Exception { EventBus eb = vertx.eventBus(); eb.consumer(ADDRESS).handler(msg -> msg.reply("pong")); // This runs on an event loop but the event loop is at no time blocked! for (int i = 0; i < 10; i++) { System.out.println("Thread is " + Thread.currentThread()); Message<String> reply = awaitResult(h -> eb.send(ADDRESS, "ping", h)); System.out.println("got reply: " + reply.body()); // Like Thread.sleep but doesn't block the OS thread Strand.sleep(1000); } }
@Override @Suspendable public void start() throws Exception { JsonObject config = new JsonObject().put("url", "jdbc:hsqldb:mem:test?shutdown=true") .put("driver_class", "org.hsqldb.jdbcDriver"); JDBCClient jdbc = JDBCClient.createShared(vertx, config); // Get a connection try (SQLConnection conn = awaitResult(jdbc::getConnection)) { // Create a table Void v = awaitResult(h -> conn.execute("CREATE TABLE test(col VARCHAR(20))", h)); // Insert some stuff for (int i = 0; i < 10; i++) { int ii = i; UpdateResult res = awaitResult(h -> conn.update("INSERT INTO test (col) VALUES ('val" + ii + "')", h)); System.out.println("Rows updated: " + res.getUpdated()); } // Select the results ResultSet res = awaitResult(h -> conn.query("SELECT * FROM test", h)); System.out.println("Selected " + res.getNumRows() + " results"); res.getResults().forEach(System.out::println); } }
@Suspendable @Override public void start() throws Exception { EventBus eb = vertx.eventBus(); // Create a couple of consumers on different addresses // The adaptor allows handler to be used as a Channel HandlerReceiverAdaptor<Message<String>> adaptor1 = streamAdaptor(); eb.<String>consumer(ADDRESS1).handler(adaptor1); HandlerReceiverAdaptor<Message<String>> adaptor2 = streamAdaptor(); eb.<String>consumer(ADDRESS2).handler(adaptor2); // Set up a periodic timer to send messages to these addresses vertx.setPeriodic(500, tid -> { eb.send(ADDRESS1, "wibble"); eb.send(ADDRESS2, "flibble"); }); // This runs on an event loop but the event loop is at no time blocked! for (int i = 0; i < 10; i++) { System.out.println("Thread is " + Thread.currentThread()); Message<String> received1 = adaptor1.receive(); System.out.println("got message: " + received1.body()); Message<String> received2 = adaptor2.receive(); System.out.println("got message: " + received2.body()); } }
@Suspendable @Override public void start() throws Exception { EventBus eb = vertx.eventBus(); // Create a couple of consumers on different addresses // The adaptor allows handler to be used as a Channel HandlerReceiverAdaptor<Message<String>> adaptor1 = streamAdaptor(); eb.<String>consumer(ADDRESS1).handler(adaptor1); HandlerReceiverAdaptor<Message<String>> adaptor2 = streamAdaptor(); eb.<String>consumer(ADDRESS2).handler(adaptor2); // Set up a periodic timer to send messages to these addresses vertx.setPeriodic(500, tid -> { eb.send(ADDRESS1, "wibble"); eb.send(ADDRESS2, "flibble"); }); ReceivePort<Message<String>> channel1 = adaptor1.receivePort(); ReceivePort<Message<String>> channel2 = adaptor2.receivePort(); // Combine them into a single channel // Not sure how to avoid this ugly cast with Quasar Mix<Message<String>> mix = (Mix<Message<String>>)Channels.mix(channel1, channel2); // Take the first ten for (int i = 0; i < 10; i++) { Message<String> msg = mix.receive(); System.out.println("got message: " + msg.body()); } System.out.println("done"); }
/** * @throws NotYetConnectedException If this channel is not yet connected */ @Override @Suspendable public abstract int read(ByteBuffer dst) throws IOException;
/** * @throws NotYetConnectedException If this channel is not yet connected */ @Override @Suspendable public abstract int write(final ByteBuffer src) throws IOException;
@Override @Suspendable public final void join() throws ExecutionException, InterruptedException { get(); }
@Override @Suspendable public int write(final ByteBuffer src) throws IOException { try { return write(src, 0L, TimeUnit.MILLISECONDS); } catch (SuspendExecution e) { throw new AssertionError(); } }
@Override @Suspendable public final long write(ByteBuffer[] srcs) throws IOException { return write(srcs, 0, srcs.length); }
@Override @Suspendable public String getSchemaTerm() throws SQLException { return JDBCFiberAsync.exec(executor, new CheckedCallable<String, SQLException>() { @Override public String call() throws SQLException { return dbMeta.getSchemaTerm(); } }); }
@Override @Suspendable public boolean supportsCatalogsInDataManipulation() throws SQLException { return JDBCFiberAsync.exec(executor, new CheckedCallable<Boolean, SQLException>() { @Override public Boolean call() throws SQLException { return dbMeta.supportsCatalogsInDataManipulation(); } }); }
@Override @Suspendable public int getMaxColumnsInOrderBy() throws SQLException { return JDBCFiberAsync.exec(executor, new CheckedCallable<Integer, SQLException>() { @Override public Integer call() throws SQLException { return dbMeta.getMaxColumnsInOrderBy(); } }); }
@Override @Suspendable public boolean supportsTransactions() throws SQLException { return JDBCFiberAsync.exec(executor, new CheckedCallable<Boolean, SQLException>() { @Override public Boolean call() throws SQLException { return dbMeta.supportsTransactions(); } }); }
@Override @Suspendable public boolean othersDeletesAreVisible(final int type) throws SQLException { return JDBCFiberAsync.exec(executor, new CheckedCallable<Boolean, SQLException>() { @Override public Boolean call() throws SQLException { return dbMeta.othersDeletesAreVisible(type); } }); }
@Override @Suspendable public final V get() throws ExecutionException, InterruptedException { try { return future().get(); } catch (RuntimeExecutionException t) { throw new ExecutionException(t.getCause()); } }
@Override @Suspendable public FiberResultSet getTables(final String catalog, final String schemaPattern, final String tableNamePattern, final String[] types) throws SQLException { final ResultSet result = JDBCFiberAsync.exec(executor, new CheckedCallable<ResultSet, SQLException>() { @Override public ResultSet call() throws SQLException { return dbMeta.getTables(catalog, schemaPattern, tableNamePattern, types); } }); return new FiberResultSet(result, executor); }
@Override @Suspendable public FiberResultSet getTypeInfo() throws SQLException { final ResultSet result = JDBCFiberAsync.exec(executor, new CheckedCallable<ResultSet, SQLException>() { @Override public ResultSet call() throws SQLException { return dbMeta.getTypeInfo(); } }); return new FiberResultSet(result, executor); }