Example usage for io.vertx.core Future succeededFuture

List of usage examples for io.vertx.core Future succeededFuture

Introduction

In this page you can find the example usage for io.vertx.core Future succeededFuture.

Prototype

static <T> Future<T> succeededFuture() 

Source Link

Document

Create a succeeded future with a null result

Usage

From source file:org.etourdot.vertx.marklogic.impl.MarkLogicManagementImpl.java

License:Open Source License

private void deleteNameObject(NameViewOptions nameOptions, Handler<AsyncResult<Void>> resultHandler,
        String baseUrl) {//from  ww  w .  j a va 2s .  c  o  m
    MarkLogicRequest marklogicRequest = restService.newMarklogicRequest();
    String serverHttpString = getUrlWithInstance(baseUrl, nameOptions);
    marklogicRequest.delete(serverHttpString);
    marklogicRequest.execute(response -> {
        if (HttpResponseStatus.NO_CONTENT.code() == response.statusCode()) {
            response.contentHandler(buffer -> resultHandler.handle(Future.succeededFuture()));
        } else {
            resultHandler.handle(Future.failedFuture(response.statusMessage()));
        }
    });
}

From source file:org.etourdot.vertx.marklogic.impl.MarkLogicManagementImpl.java

License:Open Source License

private void createNameObject(IdNameDesc idName, Handler<AsyncResult<Void>> resultHandler, String baseUrl) {
    MarkLogicRequest marklogicRequest = restService.newMarklogicRequest();
    marklogicRequest.post(baseUrl).withBody(idName.toJson()).execute(response -> {
        if (HttpResponseStatus.CREATED.code() == response.statusCode()) {
            resultHandler.handle(Future.succeededFuture());
        } else {/*  w ww. j  a va2 s .c om*/
            response.contentHandler(
                    buffer -> resultHandler.handle(Future.failedFuture(buffer.toJsonObject().encode())));
        }
    });
}

From source file:org.etourdot.vertx.marklogic.impl.MarkLogicSearchImpl.java

License:Open Source License

void delete(DeleteOptions deleteOptions, Handler<AsyncResult<JsonObject>> resultHandler) {
    requireNonNull(deleteOptions, "deleteOptions cannot be null");
    requireNonNull(resultHandler, "resultHandler cannot be null");

    MarkLogicRequest request = restService.newMarklogicRequest();
    request.delete(SEARCH_URL);//  w  w w .j  ava  2 s  . c  o m
    if (deleteOptions.hasDirectory()) {
        request.addParam(DIRECTORY, deleteOptions.getDirectory());
    }
    if (deleteOptions.hasCollection()) {
        request.addParam(COLLECTION, deleteOptions.getCollection());
    }
    if (deleteOptions.hasTxId()) {
        request.addParam(TXID, deleteOptions.getTxId());
    }
    request.execute(response -> {
        if (HttpResponseStatus.NO_CONTENT.code() == response.statusCode()) {
            resultHandler.handle(Future.succeededFuture());
        } else {
            resultHandler.handle(Future.failedFuture(response.statusMessage()));
        }
    });
}

From source file:org.sfs.io.AsyncFileWriterImpl.java

License:Apache License

private void doWrite(ByteBuffer buff, long position, int toWrite, Handler<AsyncResult<Void>> handler) {
    if (toWrite <= 0) {
        handler.handle(Future.succeededFuture());
    } else {//from w w  w  .ja  va  2s.  c o  m
        incrementWritesOutstanding(toWrite);
        writeInternal(buff, position, handler);
    }
}

From source file:org.sfs.io.AsyncFileWriterImpl.java

License:Apache License

private void writeInternal(ByteBuffer buff, long position, Handler<AsyncResult<Void>> handler) {
    try {//from   w w w .  ja  va2 s  .  c om
        ch.write(buff, position, null, new java.nio.channels.CompletionHandler<Integer, Object>() {

            public void completed(Integer bytesWritten, Object attachment) {

                long pos = position;

                if (buff.hasRemaining()) {
                    // partial write
                    pos += bytesWritten;
                    // resubmit
                    writeInternal(buff, pos, handler);
                } else {
                    // It's been fully written
                    context.runOnContext((v) -> {
                        decrementWritesOutstanding(buff.limit());
                        handler.handle(Future.succeededFuture());
                    });
                }
            }

            public void failed(Throwable exc, Object attachment) {
                removeWritesOutstandingCounter();
                if (exc instanceof Exception) {
                    context.runOnContext((v) -> handler.handle(Future.succeededFuture()));
                } else {
                    log.error("Error occurred", exc);
                }
            }
        });
    } catch (RuntimeException e) {
        removeWritesOutstandingCounter();
        throw e;
    }
}

From source file:org.wisdom.framework.vertx.Server.java

License:Apache License

private void bind(int p, Handler<AsyncResult<Void>> completion) {
    // Get port number.
    final int thePort = pickAPort(port);
    HttpServerOptions options = new HttpServerOptions();
    if (ssl) {/*from  www .j a v a  2 s .c  o m*/
        options.setSsl(true);
        options.setTrustStoreOptions(SSLServerContext.getTrustStoreOption(accessor));
        options.setKeyStoreOptions(SSLServerContext.getKeyStoreOption(accessor));
        if (authentication) {
            options.setClientAuth(ClientAuth.REQUIRED);
        }
    }

    if (hasCompressionEnabled()) {
        options.setCompressionSupported(true);
    }

    if (configuration.getIntegerWithDefault("vertx.acceptBacklog", -1) != -1) {
        options.setAcceptBacklog(configuration.getInteger("vertx.acceptBacklog"));
    }
    if (configuration.getIntegerWithDefault("vertx.maxWebSocketFrameSize", -1) != -1) {
        options.setMaxWebsocketFrameSize(configuration.getInteger("vertx.maxWebSocketFrameSize"));
    }
    if (configuration.getStringArray("wisdom.websocket.subprotocols").length > 0) {
        options.setWebsocketSubProtocols(configuration.get("wisdom.websocket.subprotocols"));
    }
    if (configuration.getStringArray("vertx.websocket-subprotocols").length > 0) {
        options.setWebsocketSubProtocols(configuration.get("vertx.websocket-subprotocols"));
    }
    if (configuration.getIntegerWithDefault("vertx.receiveBufferSize", -1) != -1) {
        options.setReceiveBufferSize(configuration.getInteger("vertx.receiveBufferSize"));
    }
    if (configuration.getIntegerWithDefault("vertx.sendBufferSize", -1) != -1) {
        options.setSendBufferSize(configuration.getInteger("vertx.sendBufferSize"));
    }

    http = vertx.createHttpServer(options).requestHandler(new HttpHandler(vertx, accessor, this))
            .websocketHandler(new WebSocketHandler(accessor, this));

    http.listen(thePort, host, event -> {
        if (event.succeeded()) {
            logger.info("Wisdom is going to serve HTTP requests on port {}.", thePort);
            port = thePort;
            completion.handle(Future.succeededFuture());
        } else if (port == 0) {
            logger.debug("Cannot bind on port {} (port already used probably)", thePort, event.cause());
            bind(0, completion);
        } else {
            logger.error("Cannot bind on port {} (port already used probably)", thePort, event.cause());
            completion.handle(Future.failedFuture("Cannot bind on port " + thePort));
        }
    });
}

From source file:se.liquidbytes.jel.system.device.DeviceManager.java

License:Apache License

/**
 * Update the value of an existing result using specified id and value.
 *
 * @param deviceId id of existing result.
 * @param value value to set.//from  w  w  w  . j  ava  2  s  .c  om
 * @param resultHandler
 */
public void updateDeviceValue(String deviceId, String value, Handler<AsyncResult<Void>> resultHandler) {
    JsonObject device = allDevices.get(deviceId);

    if (device == null) {
        resultHandler
                .handle(Future.failedFuture(String.format("No device with that id(%s) exists.", deviceId)));
    } else {
        String adapterId = device.getString("adapterId");
        DeliveryOptions options = new DeliveryOptions();
        options.addHeader("action", "updateDeviceValue");
        DeployedAdapter adapter = JelService.adapterManager().getAdapter(adapterId);

        if (adapter == null) {
            resultHandler.handle(
                    Future.failedFuture(String.format("Adapter with id %s does not exist.", adapterId)));
        } else {
            JsonObject data = new JsonObject().put("hwId", device.getString("hwId")).put("value", value);

            logger.info("Setting new value('{}') on device with id: {} and hwId: {}.", value,
                    device.getString("deviceId"), device.getString("hwId"));

            JelService.vertx().eventBus().send(String.format("%s.%s@%s:%d", AdapterEvents.EVENTBUS_ADAPTERS,
                    adapter.config().getType(), adapter.config().getAddress(), adapter.config().getPort()),
                    data, options);

            resultHandler.handle(Future.succeededFuture());
        }
    }
}