private static void initConnection(Vertx vertx, ProtonConnection connection) { connection.openHandler(res -> { System.out.println("Client connection opened, container-id: " + connection.getRemoteContainer()); connection.open(); }); connection.closeHandler(c -> { System.out.println("Client closing connection, container-id: " + connection.getRemoteContainer()); connection.close(); connection.disconnect(); }); connection.disconnectHandler(c -> { System.out.println("Client socket disconnected, container-id: " + connection.getRemoteContainer()); connection.disconnect(); }); connection.sessionOpenHandler(session -> { session.closeHandler(x -> { session.close(); session.free(); }); session.open(); }); connection.senderOpenHandler(sender -> { initSender(vertx, connection, sender); }); connection.receiverOpenHandler(receiver -> { initReceiver(receiver); }); connection.open(); }
@Override public void start() throws Exception { ProtonClient client = ProtonClient.create(vertx); client.connect("localhost", 5672, res -> { if(!res.succeeded()) { System.out.println("Connect failed: " + res.cause()); connection.open(); ProtonSender sender = connection.createSender(address); sender.open(); if(!sender.sendQueueFull()) { final int msgNum = sent.incrementAndGet(); Message message = message("Hello " + msgNum); sender.send(message, delivery -> { System.out.println(String.format("Message " + msgNum + " was received by the server: remote state=%s", delivery.getRemoteState())); });
@Override public void start() throws Exception { ProtonClient client = ProtonClient.create(vertx); client.connect("localhost", 5672, res -> { if(!res.succeeded()) { System.out.println("Connect failed: " + res.cause()); return; } ProtonConnection connection = res.result(); connection.open(); connection.createReceiver(address).handler((delivery, msg) -> { String content = (String) ((AmqpValue) msg.getBody()).getValue(); System.out.println("Received message with content: " + content); // By default, receivers automatically accept (and settle) the delivery // when the handler returns, if no other disposition has been applied. // To change this and always manage dispositions yourself, use the // setAutoAccept method on the receiver. }).open(); }); } }
private static void initSender(Vertx vertx, ProtonConnection connection, ProtonSender sender) { org.apache.qpid.proton.amqp.messaging.Source remoteSource = (org.apache.qpid.proton.amqp.messaging.Source) sender.getRemoteSource(); if(remoteSource == null) { sender.setTarget(null); sender.setCondition(new ErrorCondition(AmqpError.INVALID_FIELD, "No source terminus specified")); sender.open(); sender.close(); return; sender.setSource(remoteSource); sender.setTarget(sender.getRemoteTarget()); if (connection.isDisconnected()) { vertx.cancelTimer(t); } else { if(!sender.sendQueueFull()) { int msgNum = sent.incrementAndGet(); System.out.println("Sending message " + msgNum + " to client, for address: " + remoteSource.getAddress()); Message m = message("Hello " + msgNum + " from Server!"); sender.send(m, delivery -> { System.out.println("Message " + msgNum + " was received by the client."); }); sender.detachHandler(x -> { vertx.cancelTimer(timer); sender.detach(); sender.free(); });
private static void initReceiver(ProtonReceiver receiver) { org.apache.qpid.proton.amqp.messaging.Target remoteTarget = (org.apache.qpid.proton.amqp.messaging.Target) receiver.getRemoteTarget(); if(remoteTarget == null) { receiver.setTarget(null); receiver.setCondition(new ErrorCondition(AmqpError.INVALID_FIELD, "No target terminus specified")); receiver.open(); receiver.close(); return; receiver.setTarget(remoteTarget); receiver.setSource(receiver.getRemoteSource()); receiver.handler((delivery, msg) -> { String address = remoteTarget.getAddress() ; if (address == null) { receiver.detachHandler(x -> { receiver.detach(); receiver.free(); }); receiver.closeHandler(x -> { receiver.close(); receiver.free(); }); receiver.open();
protected void forwardMessage(final ProtonSender sender, final Message msg, final ProtonDelivery delivery) { sender.send(msg, updatedDelivery -> delivery.disposition(updatedDelivery.getRemoteState(), updatedDelivery.remotelySettled())); }
@Override public void start() throws Exception { ProtonServer server = ProtonServer.create(vertx); // Configure how new connections are handled server.connectHandler((connection) -> { initConnection(vertx, connection); }); server.listen(PORT, (res) -> { if (res.succeeded()) { System.out.println("Listening on port " + res.result().actualPort()); } else { System.out.println("Failed to start listening on port " + PORT + ":"); res.cause().printStackTrace(); } }); }
/** * Open the endpoint, opening the connection */ public void open() { this.connection .sessionOpenHandler(session -> session.open()) .open(); }
@Override protected void handleReceiverOpen(final ProtonConnection con, final ProtonReceiver receiver) { receiver.setCondition(ProtonHelper.condition(AmqpError.NOT_ALLOWED, "cannot write to node")); receiver.close(); }
private ProtonClientOptions createClientOptions() { final ProtonClientOptions options = new ProtonClientOptions(); return options; } }
private void newSession(final ProtonConnection con, final Handler<AsyncResult<ProtonSession>> sessionOpenHandler) { con.createSession().openHandler(sessionOpenHandler).open(); }
/** * {@inheritDoc} */ @Override protected int getActualInsecurePort() { return insecureServer != null ? insecureServer.actualPort() : Constants.PORT_UNCONFIGURED; }
@Override public ProtonSubscriber<Tracker> setSource(Source source) { sender.setSource(source); return this; }
@Override public void stop() { if (server != null) { server.close(); } }
@Override public ProtonSubscriber<Tracker> setTarget(Target target) { sender.setTarget(target); return this; }
/** * Create options from JSON * * @param json the JSON */ public ProtonClientOptions(JsonObject json) { super(json); ProtonClientOptionsConverter.fromJson(json, this); }
public int port() { if (server == null) { return 0; } return server.actualPort(); }