io.vertx.servicediscovery.types.MessageSource Java Examples

The following examples show how to use io.vertx.servicediscovery.types.MessageSource. 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 File: MessageSourceExamples.java    From vertx-service-discovery with Apache License 2.0 6 votes vote down vote up
public void example1(ServiceDiscovery discovery) {
  Record record = MessageSource.createRecord(
      "some-message-source-service", // The service name
      "some-address" // The event bus address
  );

  discovery.publish(record, ar -> {
    // ...
  });

  record = MessageSource.createRecord(
      "some-other-message-source-service", // The service name
      "some-address", // The event bus address
      "examples.MyData" // The payload type
  );
}
 
Example #2
Source File: RawOrderDispatcher.java    From vertx-blueprint-microservice with Apache License 2.0 6 votes vote down vote up
@Override
public void start(Future<Void> future) throws Exception {
  super.start();
  MessageSource.<JsonObject>getConsumer(discovery,
    new JsonObject().put("name", "shopping-order-message-source"),
    ar -> {
      if (ar.succeeded()) {
        MessageConsumer<JsonObject> orderConsumer = ar.result();
        orderConsumer.handler(message -> {
          Order wrappedOrder = wrapRawOrder(message.body());
          dispatchOrder(wrappedOrder, message);
        });
        future.complete();
      } else {
        future.fail(ar.cause());
      }
    });
}
 
Example #3
Source File: AuditVerticleTest.java    From vertx-microservices-workshop with Apache License 2.0 6 votes vote down vote up
@Before
public void setUp(TestContext tc) {
  Async async = tc.async();
  vertx = Vertx.vertx();
  Record record = MessageSource.createRecord("portfolio-events", "portfolio", JsonObject
      .class);
  ServiceDiscovery.create(vertx, new ServiceDiscoveryOptions()
      .setBackendConfiguration(new JsonObject().put("backend-name", DefaultServiceDiscoveryBackend.class.getName())))
      .publish(record,
          r -> {
            if (r.failed()) {
              r.cause().printStackTrace();
              tc.fail(r.cause());
            }
            vertx.deployVerticle(AuditVerticle.class.getName(), new DeploymentOptions().setConfig(CONFIGURATION), tc.asyncAssertSuccess(s -> async.complete()));
          });
}
 
Example #4
Source File: CallbackTraderVerticle.java    From vertx-kubernetes-workshop with Apache License 2.0 5 votes vote down vote up
private Future<MessageConsumer<JsonObject>> getMarketSource(ServiceDiscovery discovery) {
    Future<MessageConsumer<JsonObject>> future = Future.future();
    MessageSource.getConsumer(discovery,
        rec -> rec.getName().equalsIgnoreCase("market-data"),
        future);
    return future;
}
 
Example #5
Source File: CallbackTraderVerticle.java    From vertx-kubernetes-workshop with Apache License 2.0 5 votes vote down vote up
private Future<MessageConsumer<JsonObject>> getMarketSource(ServiceDiscovery discovery) {
    Future<MessageConsumer<JsonObject>> future = Future.future();
    MessageSource.getConsumer(discovery,
        rec -> rec.getName().equalsIgnoreCase("market-data"),
        future);
    return future;
}
 
Example #6
Source File: AuditVerticle.java    From microtrader with MIT License 5 votes vote down vote up
private Future<MessageConsumer<JsonObject>> retrieveThePortfolioMessageSource() {
    Future<MessageConsumer<JsonObject>> future = Future.future();
    MessageSource.getConsumer(discovery,
            new JsonObject().put("name", "portfolio-events"),
            future.completer()
    );
    return future;
}
 
Example #7
Source File: MessageSourceExamples.java    From vertx-service-discovery with Apache License 2.0 5 votes vote down vote up
public void example3(ServiceDiscovery discovery) {
  MessageSource.<JsonObject>getConsumer(discovery, new JsonObject().put("name", "some-message-source-service"), ar -> {
    if (ar.succeeded()) {
      MessageConsumer<JsonObject> consumer = ar.result();

      // Attach a message handler on it
      consumer.handler(message -> {
        // message handler
        JsonObject payload = message.body();
      });
      // ...
    }
  });
}
 
Example #8
Source File: MessageSourceExamples.java    From vertx-service-discovery with Apache License 2.0 5 votes vote down vote up
public void example1(ServiceDiscovery discovery) {
  Record record1 = MessageSource.createRecord(
      "some-message-source-service", // The service name
      "some-address", // The event bus address
      JsonObject.class // The message payload type
  );

  Record record2 = MessageSource.createRecord(
      "some-other-message-source-service", // The service name
      "some-address", // The event bus address
      JsonObject.class, // The message payload type
      new JsonObject().put("some-metadata", "some value")
  );
}
 
Example #9
Source File: JavaCompulsiveTraderVerticle.java    From vertx-microservices-workshop with Apache License 2.0 5 votes vote down vote up
@Override
public void start(Future<Void> future) {
  super.start();

  //----
  // Initialize the trader
  String company = TraderUtils.pickACompany();
  int numberOfShares = TraderUtils.pickANumber();
  System.out.println("Java compulsive trader configured for company " + company + " and shares: " + numberOfShares);

  // We need to retrieve two services, create two futures object that will get the services
  Future<MessageConsumer<JsonObject>> marketFuture = Future.future();
  Future<PortfolioService> portfolioFuture = Future.future();
  // Retrieve the services, use the "special" completed to assign the future
  MessageSource.getConsumer(discovery, new JsonObject().put("name", "market-data"), marketFuture);
  EventBusService.getProxy(discovery, PortfolioService.class, portfolioFuture);

  // When done (both services retrieved), execute the handler
  CompositeFuture.all(marketFuture, portfolioFuture).setHandler(ar -> {
    if (ar.failed()) {
      future.fail("One of the required service cannot " +
          "be retrieved: " + ar.cause());
    } else {
      // Our services:
      PortfolioService portfolio = portfolioFuture.result();
      MessageConsumer<JsonObject> marketConsumer = marketFuture.result();

      // Listen the market...
      marketConsumer.handler(message -> {
        JsonObject quote = message.body();
        TraderUtils.dumbTradingLogic(company, numberOfShares, portfolio, quote);
      });

      future.complete();
    }
  });
  // ----
}
 
Example #10
Source File: MicroserviceVerticle.java    From microtrader with MIT License 4 votes vote down vote up
public void publishMessageSource(String name, String address, Class contentClass, Handler<AsyncResult<Void>>
        completionHandler) {
    Record record = MessageSource.createRecord(name, address, contentClass);
    publish(record, completionHandler);
}
 
Example #11
Source File: MicroserviceVerticle.java    From microtrader with MIT License 4 votes vote down vote up
public void publishMessageSource(String name, String address, Handler<AsyncResult<Void>>
        completionHandler) {
    Record record = MessageSource.createRecord(name, address);
    publish(record, completionHandler);
}
 
Example #12
Source File: BaseMicroserviceVerticle.java    From vertx-blueprint-microservice with Apache License 2.0 4 votes vote down vote up
protected Future<Void> publishMessageSource(String name, String address) {
  Record record = MessageSource.createRecord(name, address);
  return publish(record);
}
 
Example #13
Source File: BaseMicroserviceRxVerticle.java    From vertx-blueprint-microservice with Apache License 2.0 4 votes vote down vote up
protected Single<Void> publishMessageSource(String name, String address) {
  Record record = MessageSource.createRecord(name, address);
  return publish(record);
}
 
Example #14
Source File: MicroServiceVerticle.java    From vertx-microservices-workshop with Apache License 2.0 4 votes vote down vote up
public void publishMessageSource(String name, String address, Class<?> contentClass, Handler<AsyncResult<Void>>
    completionHandler) {
  Record record = MessageSource.createRecord(name, address, contentClass);
  publish(record, completionHandler);
}
 
Example #15
Source File: MicroServiceVerticle.java    From vertx-microservices-workshop with Apache License 2.0 4 votes vote down vote up
public void publishMessageSource(String name, String address, Handler<AsyncResult<Void>>
    completionHandler) {
  Record record = MessageSource.createRecord(name, address);
  publish(record, completionHandler);
}