Java Code Examples for io.vertx.proton.ProtonConnection#createReceiver()

The following examples show how to use io.vertx.proton.ProtonConnection#createReceiver() . 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: AuthenticationServerClient.java    From hono with Eclipse Public License 2.0 5 votes vote down vote up
private static Future<ProtonReceiver> openReceiver(final ProtonConnection openConnection, final ProtonMessageHandler messageHandler) {

        final Promise<ProtonReceiver> result = Promise.promise();
        final ProtonReceiver recv = openConnection.createReceiver(AuthenticationConstants.ENDPOINT_NAME_AUTHENTICATION);
        recv.openHandler(result);
        recv.handler(messageHandler);
        recv.open();
        return result.future();
    }
 
Example 2
Source File: Receiver.java    From enmasse with Apache License 2.0 4 votes vote down vote up
private void connectionOpened(ProtonConnection conn, String linkName, Source source) {
    receiver = conn.createReceiver(source.getAddress(), new ProtonLinkOptions().setLinkName(linkName));
    receiver.setSource(source);
    receiver.setPrefetch(0);
    receiver.setAutoAccept(false);
    receiver.handler((protonDelivery, message) -> {
        log.info("Got message, count is {}", messageCount.get());
        messages.add(message);
        messageCount.incrementAndGet();
        deliveryHandler.handle(protonDelivery);
        if (done.test(message)) {
            resultPromise.complete(messages);
            conn.close();
        } else {
            receiver.flow(1);
        }
    });
    receiver.openHandler(result -> {
        if (result.succeeded()) {
            log.info("Receiver link '{}' opened, granting credits", source.getAddress());
            receiver.flow(1);
            connectPromise.complete(null);
        } else {
            handleError(conn, receiver.getRemoteCondition());
        }
    });

    receiver.closeHandler(closed -> {
        if (receiver.getRemoteCondition() != null && LinkError.REDIRECT.equals(receiver.getRemoteCondition().getCondition())) {
            String relocated = (String) receiver.getRemoteCondition().getInfo().get("address");
            log.info("Receiver link redirected to '{}'", relocated);
            Source newSource = linkOptions.getSource();
            newSource.setAddress(relocated);
            String newLinkName = linkOptions.getLinkName().orElse(UUID.randomUUID().toString());

            vertx.runOnContext(id -> connectionOpened(conn, newLinkName, newSource));
        } else {
            handleError(conn, receiver.getRemoteCondition());
        }
        receiver.close();
    });
    receiver.open();
}