use of org.reactivestreams.Subscriber in project vertx-proton by vert-x3.
the class TrackerSubscriberWhiteboxVerificationTckTest method createServer.
private TestServer createServer() throws Exception {
return new TestServer(vertx, (connection) -> {
connection.openHandler(res -> {
LOG.trace("Client connected: " + connection.getRemoteContainer());
connection.open();
}).closeHandler(c -> {
LOG.trace("Client closing amqp connection: " + connection.getRemoteContainer());
connection.close();
connection.disconnect();
}).disconnectHandler(c -> {
LOG.trace("Client socket disconnected: " + connection.getRemoteContainer());
connection.disconnect();
}).sessionOpenHandler(session -> session.open());
connection.receiverOpenHandler(receiver -> {
if (!server.getDetachLink()) {
LOG.trace("Receiving from client to: " + receiver.getRemoteTarget().getAddress());
// This is rather naive, for example use only, proper
receiver.setTarget(receiver.getRemoteTarget());
// servers should ensure that they advertise their own
// Target settings that actually reflect what is in place.
// The request may have also been for a dynamic address.
receiver.handler((delivery, msg) -> {
String address = msg.getAddress();
if (address == null) {
address = receiver.getRemoteTarget().getAddress();
}
Section body = msg.getBody();
String content = "unknown";
if (body instanceof AmqpValue) {
content = (String) ((AmqpValue) body).getValue();
}
LOG.trace("message to:" + address + ", body: " + content);
});
receiver.closeHandler(s -> {
s.result().close();
});
}
receiver.open();
if (server.getDetachLink()) {
receiver.setCondition(new ErrorCondition(Symbol.getSymbol("Failed Subscriber Requested"), ""));
receiver.close();
}
});
});
}
use of org.reactivestreams.Subscriber in project vertx-proton by vert-x3.
the class Sender method main.
public static void main(String[] args) throws Exception {
try {
Vertx vertx = Vertx.vertx();
ProtonClient client = ProtonClient.create(vertx);
// Connect, then use the event loop thread to process the connection
client.connect("localhost", 5672, res -> {
Context ctx = Vertx.currentContext();
if (res.succeeded()) {
System.out.println("We're connected");
ProtonConnection connection = res.result();
connection.open();
Subscriber<Message> producerSubscriber = ProtonStreams.createProducer(connection, "queue");
Publisher<Message> publisher = Flowable.range(1, COUNT).map(i -> {
Message m = Proton.message();
m.setBody(new AmqpValue("Hello " + i));
return m;
}).doFinally(() -> {
ctx.runOnContext(x -> {
System.out.println("Publisher finished, closing connection.");
connection.closeHandler(y -> {
System.out.println("Connection closed.");
connection.disconnect();
vertx.close();
}).close();
});
});
publisher.subscribe(producerSubscriber);
} else {
System.out.println("Failed to connect, exiting: " + res.cause());
System.exit(1);
}
});
} catch (Exception exp) {
System.out.println("Caught exception, exiting.");
exp.printStackTrace(System.out);
System.exit(1);
}
}
Aggregations