Java Code Examples for io.vertx.rxjava.core.Vertx

The following examples show how to use io.vertx.rxjava.core.Vertx. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: redpipe   Source File: TestResourceRxJava1.java    License: Apache License 2.0 6 votes vote down vote up
@Path("inject")
@GET
public String inject(@Context Vertx vertx,
		@Context RoutingContext routingContext,
		@Context HttpServerRequest request,
		@Context HttpServerResponse response,
		@Context AuthProvider authProvider,
		@Context User user,
		@Context Session session) {
	if(vertx == null
			|| routingContext == null
			|| request == null
			|| response == null
			|| session == null)
		throw new WebApplicationException(Status.INTERNAL_SERVER_ERROR);
	return "ok";
}
 
Example 2
Source Project: vertx-rx   Source File: CoreRxifiedApiTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testObservablePeriodic() throws Exception {
  Vertx vertx = new Vertx(this.vertx);
  Observable<Long> stream = vertx.periodicStream(1).toObservable();
  stream.subscribe(new Subscriber<Long>() {
    @Override
    public void onNext(Long aLong) {
      unsubscribe();
      testComplete();
    }
    @Override
    public void onCompleted() {
      fail();
    }
    @Override
    public void onError(Throwable e) {
      fail(e.getMessage());
    }
  });
  await();
}
 
Example 3
Source Project: vertx-rx   Source File: CoreApiTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testScheduledTimer() {
  vertx.runOnContext(v -> {
    long startTime = System.currentTimeMillis();
    Context initCtx = Vertx.currentContext();
    Observable.timer(100, 100, TimeUnit.MILLISECONDS, io.vertx.rxjava.core.RxHelper.scheduler(vertx)).take(10).subscribe(new Observer<Long>() {
      public void onNext(Long value) {
        assertEquals(initCtx.getDelegate(), Vertx.currentContext().getDelegate());
      }

      public void onError(Throwable e) {
        fail("unexpected failure");
      }

      public void onCompleted() {
        long timeTaken = System.currentTimeMillis() - startTime;
        assertTrue("Was expecting to have time taken | " + timeTaken + " -  1000 | < 200", Math.abs(timeTaken - 1000) < 1000);
        testComplete();
      }
    });
  });
  await();
}
 
Example 4
Source Project: vertx-rx   Source File: VertxParameterProvider.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public ParameterClosingConsumer<Vertx> parameterClosingConsumer() {
  return vertx -> {
    try {
      if (!vertx.rxClose().toCompletable().await(DEFAULT_TIMEOUT_DURATION, DEFAULT_TIMEOUT_UNIT)) {
        throw new TimeoutException("Closing the Vertx context timed out");
      }
    } catch (Throwable err) {
      if (err instanceof RuntimeException) {
        throw new VertxException(err.getCause());
      } else if (err instanceof Exception) {
        throw err;
      } else {
        throw new VertxException(err);
      }
    }
  };
}
 
Example 5
Source Project: Learn-Java-12-Programming   Source File: ReactiveSystemDemo.java    License: MIT License 5 votes vote down vote up
public static void main(String... args) {
    String address = "One";
    Vertx vertx = Vertx.vertx();
    RxHelper.deployVerticle(vertx, new MessageRcvVert("1", address));
    RxHelper.deployVerticle(vertx, new MessageRcvVert("2", address));
    RxHelper.deployVerticle(vertx, new MessageRcvVert("3", "Two"));
    RxHelper.deployVerticle(vertx, new HttpServerVert(8082));
}
 
Example 6
public CartEventDataSourceImpl(io.vertx.core.Vertx vertx, JsonObject json) {
  this.client = JDBCClient.createNonShared(Vertx.newInstance(vertx), json);
  // TODO: Should not init the table here.
  client.rxGetConnection()
    .flatMap(connection ->
      connection.rxExecute(INIT_STATEMENT)
        .doAfterTerminate(connection::close)
    )
    .subscribe();
}
 
Example 7
Source Project: vertx-rx   Source File: RxifiedExamples.java    License: Apache License 2.0 5 votes vote down vote up
public void toObservable(Vertx vertx) {
  FileSystem fs = vertx.fileSystem();
  fs.open("/data.txt", new OpenOptions(), result -> {
    AsyncFile file = result.result();
    Observable<Buffer> observable = file.toObservable();
    observable.forEach(data -> System.out.println("Read data: " + data.toString("UTF-8")));
  });
}
 
Example 8
Source Project: vertx-rx   Source File: RxifiedExamples.java    License: Apache License 2.0 5 votes vote down vote up
public void deployVerticle(Vertx vertx, Verticle verticle) {
  Observable<String> deployment = RxHelper.deployVerticle(vertx, verticle);

  deployment.subscribe(id -> {
    // Deployed
  }, err -> {
    // Could not deploy
  });
}
 
Example 9
Source Project: vertx-rx   Source File: RxifiedExamples.java    License: Apache License 2.0 5 votes vote down vote up
public void eventBusMessages(Vertx vertx) {
  EventBus eb = vertx.eventBus();
  MessageConsumer<String> consumer = eb.<String>consumer("the-address");
  Observable<Message<String>> observable = consumer.toObservable();
  Subscription sub = observable.subscribe(msg -> {
    // Got message
  });

  // Unregisters the stream after 10 seconds
  vertx.setTimer(10000, id -> {
    sub.unsubscribe();
  });
}
 
Example 10
Source Project: vertx-rx   Source File: RxifiedExamples.java    License: Apache License 2.0 5 votes vote down vote up
public void eventBusMapReduce(Vertx vertx) {
  Observable<Double> observable = vertx.eventBus().
      <Double>consumer("heat-sensor").
      bodyStream().
      toObservable();

  observable.
      buffer(1, TimeUnit.SECONDS).
      map(samples -> samples.
          stream().
          collect(Collectors.averagingDouble(d -> d))).
      subscribe(heat -> {
        vertx.eventBus().send("news-feed", "Current heat is " + heat);
      });
}
 
Example 11
Source Project: vertx-rx   Source File: RxifiedExamples.java    License: Apache License 2.0 5 votes vote down vote up
public void websocketClient(Vertx vertx) {
  HttpClient client = vertx.createHttpClient(new HttpClientOptions());
  client.rxWebSocket(8080, "localhost", "/the_uri").subscribe(
      ws -> {
        // Use the websocket
      },
      error -> {
        // Could not connect
      }
  );
}
 
Example 12
Source Project: vertx-rx   Source File: RxifiedExamples.java    License: Apache License 2.0 5 votes vote down vote up
public void httpClientRequest(Vertx vertx) {
  HttpClient client = vertx.createHttpClient(new HttpClientOptions());
  Single<HttpClientResponse> request = client.rxGet(8080, "localhost", "/the_uri");
  request.subscribe(
      response -> {
        // Process the response
      },
      error -> {
        // Could not connect
      }
  );
}
 
Example 13
Source Project: vertx-rx   Source File: RxifiedExamples.java    License: Apache License 2.0 5 votes vote down vote up
public void timer(Vertx vertx) {
  vertx.timerStream(1000).
      toObservable().
      subscribe(
          id -> {
            System.out.println("Callback after 1 second");
          }
      );
}
 
Example 14
Source Project: vertx-rx   Source File: RxifiedExamples.java    License: Apache License 2.0 5 votes vote down vote up
public void periodic(Vertx vertx) {
  vertx.periodicStream(1000).
      toObservable().
      subscribe(
          id -> {
            System.out.println("Callback every second");
          }
      );
}
 
Example 15
Source Project: vertx-rx   Source File: RxifiedExamples.java    License: Apache License 2.0 5 votes vote down vote up
public void periodicUnsubscribe(Vertx vertx) {
  vertx.periodicStream(1000).
      toObservable().
      subscribe(new Subscriber<Long>() {
        public void onNext(Long aLong) {
          // Callback
          unsubscribe();
        }
        public void onError(Throwable e) {}
        public void onCompleted() {}
      });
}
 
Example 16
Source Project: vertx-rx   Source File: VerticleTest.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public void setUp() throws Exception {
  super.setUp();
  vertx = new Vertx(super.vertx);
  startedCount = 0;
  stoppedCount = 0;
}
 
Example 17
Source Project: vertx-rx   Source File: CoreApiTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testScheduledBuffer() {
  vertx.runOnContext(v -> {
    long startTime = System.currentTimeMillis();
    Context initCtx = Vertx.currentContext();
    Observable
        .timer(10, 10, TimeUnit.MILLISECONDS, io.vertx.rxjava.core.RxHelper.scheduler(vertx))
        .buffer(100, TimeUnit.MILLISECONDS, io.vertx.rxjava.core.RxHelper.scheduler(vertx))
        .take(10)
        .subscribe(new Observer<List<Long>>() {
          private int eventCount = 0;

          public void onNext(List<Long> value) {
            eventCount++;
            assertEquals(initCtx.getDelegate(), Vertx.currentContext().getDelegate());
          }

          public void onError(Throwable e) {
            fail("unexpected failure");
          }

          public void onCompleted() {
            long timeTaken = System.currentTimeMillis() - startTime;
            assertEquals(10, eventCount);
            assertTrue("Was expecting to have time taken | " + timeTaken + " -  1000 | < 200", Math.abs(timeTaken - 1000) < 1000);
            testComplete();
          }
        });
  });
  await();
}
 
Example 18
Source Project: vertx-rx   Source File: CoreApiTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testTimeMap() {
  vertx.runOnContext(v -> {
    Context initCtx = Vertx.currentContext();
    EventBus eb = vertx.eventBus();
    ReadStream<String> consumer = eb.<String>localConsumer("the-address").bodyStream();
    Observer<String> observer = new Subscriber<String>() {
      boolean first = true;
      @Override
      public void onNext(String s) {
        if (first) {
          first = false;
          assertEquals(initCtx.getDelegate(), Vertx.currentContext().getDelegate());
          assertEquals("msg1msg2msg3", s);
          testComplete();
        }
      }
      @Override
      public void onError(Throwable e) {
        fail(e.getMessage());
      }
      @Override
      public void onCompleted() {
      }
    };
    Observable<String> observable = consumer.toObservable();
    observable.
        buffer(500, TimeUnit.MILLISECONDS, io.vertx.rxjava.core.RxHelper.scheduler(vertx)).
        map(samples -> samples.stream().reduce("", (a, b) -> a + b)).
        subscribe(observer);
    eb.send("the-address", "msg1");
    eb.send("the-address", "msg2");
    eb.send("the-address", "msg3");
  });
  await();
}
 
Example 19
Source Project: vertx-rx   Source File: RxJava1Test.java    License: Apache License 2.0 5 votes vote down vote up
@Test
@DisplayName("Check the injection of a /io.vertx.rxjava.core.Vertx/ instance")
void check_injection(Vertx vertx, VertxTestContext testContext) {
  testContext.verify(() -> {
    assertThat(vertx).isNotNull();
    testContext.completeNow();
  });
}
 
Example 20
Source Project: vertx-rx   Source File: RxJava1Test.java    License: Apache License 2.0 5 votes vote down vote up
@Test
@DisplayName("Check the deployment and interaction of a Rx1 verticle")
void check_deployment_and_message_send(Vertx vertx, VertxTestContext testContext) {
  RxHelper
    .deployVerticle(vertx, new RxVerticle())
    .toSingle()
    .flatMap(id -> vertx.eventBus().rxRequest("check", "Ok?"))
    .subscribe(
      message -> testContext.verify(() -> {
        assertThat(message.body()).isEqualTo("Check!");
        testContext.completeNow();
      }),
      testContext::failNow);
}
 
Example 21
public AwsPollingAsStateObservable(io.vertx.core.Vertx vertx, AwsAutoScalingUtil awsAsUtil, LocalDateTime timeout, long pollInterval, AwsState... acceptedStates) {
    this.rxVertx = new Vertx(vertx);
    this.awsAsUtil = awsAsUtil;
    this.timeout = timeout;
    this.pollInterval = pollInterval;
    this.acceptedStates = Arrays.asList(acceptedStates);

}
 
Example 22
public AwsPollingElbStateObservable(io.vertx.core.Vertx vertx, String deployId, AwsElbUtil awsElbUtil, LocalDateTime timeout,long pollInterval, Function<String, Boolean> requestStillActive, AwsState... acceptedStates) {
    this.deployId = deployId;
    this.requestStillActive = requestStillActive;
    this.rxVertx = new Vertx(vertx);
    this.awsElbUtil = awsElbUtil;
    this.timeout = timeout;
    this.acceptedStates = Arrays.asList(acceptedStates);
    this.pollInterval = pollInterval;

}
 
Example 23
Source Project: vertx-kue   Source File: CallbackKue.java    License: Apache License 2.0 4 votes vote down vote up
public static CallbackKue createKue(Vertx vertx, JsonObject config) {
  CallbackKue ret = CallbackKue.newInstance(io.vertx.blueprint.kue.CallbackKue.createKue(vertx.getDelegate(), config));
  return ret;
}
 
Example 24
@Override
public void start() {
  super.start();
  vertx = Vertx.newInstance(super.vertx);
  discovery = ServiceDiscovery.newInstance(super.discovery);
}
 
Example 25
Source Project: vertx-pac4j   Source File: VertxClusteredMapStore.java    License: Apache License 2.0 4 votes vote down vote up
public VertxClusteredMapStore(final io.vertx.core.Vertx vertx) {
    this(vertx, 1);
}
 
Example 26
Source Project: vertx-pac4j   Source File: VertxClusteredMapStore.java    License: Apache License 2.0 4 votes vote down vote up
public VertxClusteredMapStore(final io.vertx.core.Vertx vertx, final int timeoutSeconds) {
    rxVertx = Vertx.newInstance(vertx);
    blockingTimeoutSeconds = timeoutSeconds;
}
 
Example 27
Source Project: vertx-rx   Source File: RxifiedExamples.java    License: Apache License 2.0 4 votes vote down vote up
public void schedulerHook(Vertx vertx) {
  RxJavaSchedulersHook hook = io.vertx.rxjava.core.RxHelper.schedulerHook(vertx);
    RxJavaHooks.setOnIOScheduler(f -> hook.getIOScheduler());
    RxJavaHooks.setOnNewThreadScheduler(f -> hook.getNewThreadScheduler());
    RxJavaHooks.setOnComputationScheduler(f -> hook.getComputationScheduler());
}
 
Example 28
Source Project: vertx-rx   Source File: RxifiedExamples.java    License: Apache License 2.0 4 votes vote down vote up
public void embedded() {
  Vertx vertx = io.vertx.rxjava.core.Vertx.vertx();
}
 
Example 29
Source Project: vertx-rx   Source File: RxifiedExamples.java    License: Apache License 2.0 4 votes vote down vote up
public void eventBusBodies(Vertx vertx) {
  EventBus eb = vertx.eventBus();
  MessageConsumer<String> consumer = eb.<String>consumer("the-address");
  Observable<String> observable = consumer.bodyStream().toObservable();
}
 
Example 30
Source Project: vertx-rx   Source File: WebClientTest.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public void setUp() throws Exception {
  super.setUp();
  vertx = new Vertx(super.vertx);

}