@Override public void start() throws Exception { EventBus eb = vertx.eventBus(); eb.consumer("news-feed"). toObservable(). subscribe(message -> System.out.println("Received news: " + message.body())); System.out.println("Ready!"); } }
@Override public void start() throws Exception { Router router = Router.router(vertx); router.route("/news-feed/*").handler(SockJSHandler.create(vertx).socketHandler(sockJSSocket -> { // Consumer the event bus address as an Observable Observable<String> msg = vertx.eventBus().<String>consumer("news-feed") .bodyStream() .toObservable(); // Send the event to the client Subscription subscription = msg.subscribe(sockJSSocket::write); // Unsubscribe when the socket closes sockJSSocket.endHandler(v -> { subscription.unsubscribe(); }); })); // Serve the static resources router.route().handler(StaticHandler.create()); vertx.createHttpServer().requestHandler(router).listen(8080); // Publish a message to the address "news-feed" every second vertx.setPeriodic(1000, t -> vertx.eventBus().publish("news-feed", "news from the server!")); } }
@Override public void start() throws Exception { HttpClient client = vertx.createHttpClient(); HttpClientRequest req = client.request(HttpMethod.GET, 8080, "localhost", "/"); req.toObservable(). flatMap(HttpClientResponse::toObservable). // Unmarshall the response to the Data object via Jackon lift(io.vertx.rxjava.core.RxHelper.unmarshaller(Data.class)). subscribe( data -> { System.out.println("Got response " + data.message); }); // End request req.end(); } }
@Override public void start() throws Exception { EventBus eb = vertx.eventBus(); vertx.setPeriodic(1000, v -> {
/** * Like {@link io.reactiverse.rxjava.pgclient.PgClient#pool} with options build from <code>connectionUri</code>. * @param vertx * @param connectionUri * @return */ public static io.reactiverse.rxjava.pgclient.PgPool pool(io.vertx.rxjava.core.Vertx vertx, String connectionUri) { io.reactiverse.rxjava.pgclient.PgPool ret = io.reactiverse.rxjava.pgclient.PgPool.newInstance(io.reactiverse.pgclient.PgClient.pool(vertx.getDelegate(), connectionUri)); return ret; }
/** * Creates a non clustered instance using default options. * @return the instance */ public static io.vertx.rxjava.core.Vertx vertx() { io.vertx.rxjava.core.Vertx ret = io.vertx.rxjava.core.Vertx.newInstance(io.vertx.core.Vertx.vertx()); return ret; }
@Test public void testHttpClientFlatMapUnmarshallPojo() { HttpServer server = vertx.createHttpServer(new HttpServerOptions().setPort(8080)); server.requestStream().handler(req -> { req.response().setChunked(true).end("{\"foo\":\"bar\"}"); }); server.listen(ar -> { HttpClient client = vertx.createHttpClient(new HttpClientOptions()); Single<HttpClientResponse> req = client.rxGetNow(8080, "localhost", "/the_uri"); ArrayList<SimplePojo> objects = new ArrayList<>(); req.flatMapObservable(HttpClientResponse::toObservable). lift(io.vertx.rxjava.core.RxHelper.unmarshaller(SimplePojo.class)). forEach( objects::add, err -> fail(), () -> { server.close(); assertEquals(Arrays.asList(new SimplePojo("bar")), objects); testComplete(); }); }); await(); }
public static Vertx newInstance(io.vertx.core.Vertx arg) { return arg != null ? new Vertx(arg) : null; } }
/** * Starts a MockServer verticle with a StoreEndpoint to test against * @param context the test context */ @BeforeClass public static void setupServer(TestContext context) { Async async = context.async(); vertx = new Vertx(rule.vertx()); vertxCore = vertx.getDelegate(); setConfig(vertx.getOrCreateContext().config()); setupMockEndpoint().subscribe(x -> async.complete()); }
@Override public void start() throws Exception { HttpServer server = vertx.createHttpServer(); server.requestStream().toObservable().subscribe(req -> { req.response().putHeader("content-type", "application/json").end(new JsonObject().put("time", System.currentTimeMillis()).toString()); }); server.listen(8080); } }
private Observable<DeployApplicationRequest> registerApplication(DeployApplicationRequest deployApplicationRequest) { io.vertx.rxjava.core.Vertx rxVertx = new io.vertx.rxjava.core.Vertx(vertx); return rxVertx.fileSystem() .rxExists(config.getRunDir() + deployApplicationRequest.getModuleId()) .toObservable() .flatMap(exists -> { if (!exists) { return rxVertx.fileSystem().rxCreateFile(config.getRunDir() + deployApplicationRequest.getModuleId()) .toObservable() .flatMap(x -> just(deployApplicationRequest)); } else { return just(deployApplicationRequest); } }); }
@ConditionalOnMissingBean(FileSystem.class) @Bean public FileSystem fileSystem(Vertx vertx) { return vertx.fileSystem(); }
/** * Like {@link io.vertx.rxjava.cassandra.CassandraClient#createShared}, but with default client options and datasource. * @param vertx * @return */ public static io.vertx.rxjava.cassandra.CassandraClient createShared(io.vertx.rxjava.core.Vertx vertx) { io.vertx.rxjava.cassandra.CassandraClient ret = io.vertx.rxjava.cassandra.CassandraClient.newInstance(io.vertx.cassandra.CassandraClient.createShared(vertx.getDelegate())); return ret; }
/** * Creates a non clustered instance using the specified options * @param options the options to use * @return the instance */ public static io.vertx.rxjava.core.Vertx vertx(VertxOptions options) { io.vertx.rxjava.core.Vertx ret = io.vertx.rxjava.core.Vertx.newInstance(io.vertx.core.Vertx.vertx(options)); return ret; }
@Test public void testResponseBodyAsAsJsonMapped() throws Exception { JsonObject expected = new JsonObject().put("cheese", "Goat Cheese").put("wine", "Condrieu"); HttpServer server = vertx.createHttpServer(new HttpServerOptions().setPort(8080)); server.requestStream().handler(req -> req.response().end(expected.encode())); try { server.listen(ar -> { client = WebClient.wrap(vertx.createHttpClient(new HttpClientOptions())); Single<HttpResponse<WineAndCheese>> single = client .get(8080, "localhost", "/the_uri") .as(BodyCodec.json(WineAndCheese.class)) .rxSend(); single.subscribe(resp -> { assertEquals(200, resp.statusCode()); assertEquals(new WineAndCheese().setCheese("Goat Cheese").setWine("Condrieu"), resp.body()); testComplete(); }, this::fail); }); await(); } finally { server.close(); } }
public static Vertx newInstance(io.vertx.core.Vertx arg) { return arg != null ? new Vertx(arg) : null; } }
@Override public void start() throws Exception { HttpServer server = vertx.createHttpServer(); server.requestStream().toObservable().subscribe(req -> { req.response().putHeader("content-type", "application/json").end(new JsonObject().put("time", System.currentTimeMillis()).toString()); }); server.listen(8080); } }
public Observable<Boolean> cleanup() { List<String> runningApplications = new ProcessUtils(config).listModules(); FileSystem fs = new io.vertx.rxjava.core.Vertx(vertx).fileSystem(); return fs.rxReadDir(config.getRunDir()) .toObservable() .flatMapIterable(x -> x) .flatMap(s -> just(Pattern.compile("/").splitAsStream(s).reduce((a, b) -> b).orElse(""))) .filter(s -> !s.isEmpty() && !runningApplications.contains(s)) .flatMap(file -> fs.rxDelete(config.getRunDir() + file).toObservable()) .toList() .flatMap(x -> just(Boolean.TRUE).doOnError(t -> LOG.error("error"))) .onErrorReturn(x -> { LOG.error("Error during cleanup of run files {}", x.getMessage()); return Boolean.FALSE; }); }
private void removeBasePath(ModuleRequest request, Path basePath) { if (!basePath.getParent().toFile().exists() || !basePath.getParent().toFile().canWrite()) { LOG.warn("[{} - {}]: Unable to extract artifact {} -> {} not exist or not writable.", request.getLogName(), request.getId(), request.getModuleId(), basePath.getParent()); LOG.warn("[{} - {}]: Unable to extract artifact {} to basePath -> {}.", request.getLogName(), request.getId(), request.getModuleId(), basePath.getParent().toFile()); } if (basePath.toFile().exists()) { LOG.info("[{} - {}]: Removing base path -> {}.", request.getLogName(), request.getId(), basePath.toAbsolutePath()); vertx.fileSystem().deleteRecursiveBlocking(basePath.toString(), true); } }
@Override public void start() throws Exception { EventBus eb = vertx.eventBus(); eb.consumer(ADDRESS) .toObservable() .subscribe(message -> { System.out.println("Received " + message.body()); message.reply("PONG"); }); // Send a message every second vertx.setPeriodic(1000, v -> { eb.rxSend(ADDRESS, "PING") .subscribe(reply -> { System.out.println("Received reply " + reply.body()); }); }); } }