static ProcessorService createProxy(Vertx vertx, String address) { return new ServiceProxyBuilder(vertx) .setAddress(address) .build(ProcessorService.class); // Alternatively, you can create the proxy directly using: // return new ProcessorServiceVertxEBProxy(vertx, address); // The name of the class to instantiate is the service interface + `VertxEBProxy`. // This class is generated during the compilation }
public static void dealWithFailure(Throwable t) { if (t instanceof ServiceException) { ServiceException exc = (ServiceException) t; if (exc.failureCode() == BAD_NAME_ERROR) { System.out.println("Failed to process the document: The name in the document is bad. " + "The name provided is: " + exc.getDebugInfo().getString("name")); } else if (exc.failureCode() == NO_NAME_ERROR) { System.out.println("Failed to process the document: No name was found"); } } else { System.out.println("Unexpected error: " + t); } } }
@Override public void start() throws Exception { // Use Factory method or just with constructor, either is OK someDatabaseService = SomeDatabaseService.create(); // Register your service to the address. new ServiceBinder(vertx.getDelegate()) .setAddress("proxy.address") .register(SomeDatabaseService.class, someDatabaseService); } }
public static <T> MessageConsumer<JsonObject> registerLocalService(Class<T> clazz, Vertx vertx, T service, String address, boolean topLevel, long timeoutSeconds) { return new ServiceBinder(vertx) .setAddress(address) .setTopLevel(topLevel) .setTimeoutSeconds(timeoutSeconds) .registerLocal(clazz, service); }
@Override public void stop() { serviceBinder.unregister(consumer); } }
/** * Create a proxy to a service that is deployed somewhere on the event bus * * @param vertx the Vert.x instance * @param address the address the service is listening on on the event bus * @return the service */ static MongoService createEventBusProxy(Vertx vertx, String address) { return ProxyHelper.createProxy(MongoService.class, vertx, address); }
@Override public void process(JsonObject document, Handler<AsyncResult<JsonObject>> resultHandler) { System.out.println("Processing..."); JsonObject result = document.copy(); if (!document.containsKey("name")) { resultHandler.handle(ServiceException.fail(NO_NAME_ERROR, "No name in the document")); } else if (document.getString("name").isEmpty() || document.getString("name").equalsIgnoreCase("bad")) { resultHandler.handle(ServiceException.fail(BAD_NAME_ERROR, "Bad name in the document: " + document.getString("name"), new JsonObject().put("name", document.getString("name")))); } else { result.put("approved", true); resultHandler.handle(Future.succeededFuture(result)); } }
/** * Register the local proxy handle on the event bus. * The registration will not be propagated to other nodes in the cluster. * * @param eventBus the event bus * @param address the proxy address */ public MessageConsumer<JsonObject> registerLocal(EventBus eventBus, String address) { return registerLocal(eventBus, address, null); }
/** * Register the proxy handle on the event bus. * * @param eventBus the event bus * @param address the proxy address */ public MessageConsumer<JsonObject> register(EventBus eventBus, String address) { return register(eventBus, address, null); }
/** * Start transaction service */ private void startTransactionService() { serviceBinder = new ServiceBinder(vertx); TransactionPersistence persistence = TransactionPersistence.create(); // Create an instance of TransactionManagerService and mount to event bus TransactionsManagerService transactionsManagerService = TransactionsManagerService.create(persistence); consumer = serviceBinder .setAddress("transactions_manager.myapp") .register(TransactionsManagerService.class, transactionsManagerService); }
@Override public void stop() { serviceBinder.unregister(consumer); } }
/** * Factory method for creating a {@link JobService} service proxy. * This is useful for doing RPCs. * * @param vertx Vertx instance * @param address event bus address of RPC * @return the new {@link JobService} service proxy */ static JobService createProxy(Vertx vertx, String address) { return ProxyHelper.createProxy(JobService.class, vertx, address); }
public static <T> T createProxy(Class<T> clazz, Vertx vertx, String address) { return new ServiceProxyBuilder(vertx) .setAddress(address) .build(clazz); }
@Override public void start() throws Exception { // Create the client object MyService service = new MyServiceImpl(); // Register the handler new ServiceBinder(vertx) .setAddress("proxy.example") .register(MyService.class, service); Router router = Router.router(vertx); BridgeOptions options = new BridgeOptions().addInboundPermitted(new PermittedOptions().setAddress("proxy.example")); router.route("/eventbus/*").handler(SockJSHandler.create(vertx).bridge(options)); // Serve the static resources router.route().handler(StaticHandler.create()); vertx.createHttpServer().requestHandler(router).listen(8080); } }
@Override public void stop() throws Exception { serviceBinder.unregister(consumer); } }
@Override public void start() throws Exception { // Create the client object ProcessorService service = new ProcessorServiceImpl(); // Register the handler new ServiceBinder(vertx) .setAddress("vertx.processor") .register(ProcessorService.class, service); Router router = Router.router(vertx); // Allow events for the designated addresses in/out of the event bus bridge BridgeOptions opts = new BridgeOptions() .addInboundPermitted(new PermittedOptions().setAddress("vertx.processor")) .addOutboundPermitted(new PermittedOptions().setAddress("vertx.processor")); // Create the event bus bridge and add it to the router. SockJSHandler ebHandler = SockJSHandler.create(vertx).bridge(opts); router.route("/eventbus/*").handler(ebHandler); router.route().handler(StaticHandler.create()); // vertx.createHttpServer().requestHandler(router).listen(8080); }
@Override public void stop() throws Exception { serviceBinder.unregister(consumer); } }
@Override public void stop() throws Exception { serviceBinder.unregister(consumer); } }
@Override public void stop() { serviceBinder.unregister(consumer); } }
@Override public void stop() throws Exception { serviceBinder.unregister(consumer); } }