use of io.vertx.proton.ProtonClient in project vertx-proton by vert-x3.
the class VertxProtonExamples method example1.
public void example1(Vertx vertx) {
ProtonClient client = ProtonClient.create(vertx);
// Connect, then use the event loop thread to process things thereafter
client.connect("hostname", 5672, "username", "password", connectResult -> {
if (connectResult.succeeded()) {
connectResult.result().setContainer("my-container/client-id").openHandler(openResult -> {
if (openResult.succeeded()) {
ProtonConnection conn = openResult.result();
// Create senders, receivers etc..
}
}).open();
}
});
}
use of io.vertx.proton.ProtonClient 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);
}
}
use of io.vertx.proton.ProtonClient in project vertx-proton by vert-x3.
the class MessagePublisherVerificationTckTest method createPublisher.
@Override
public Publisher<Message> createPublisher(long elements) {
int actualPort = server.actualPort();
ProtonClient client = ProtonClient.create(vertx);
AtomicReference<Publisher<Message>> ref = new AtomicReference<>();
CountDownLatch latch = new CountDownLatch(1);
client.connect("localhost", actualPort, result -> {
if (result.succeeded()) {
ProtonConnection conn = result.result();
conn.open();
ProtonPublisher<Message> stream = ProtonStreams.createConsumer(conn, testName);
((ProtonPublisherWrapperImpl) stream).setEmitOnConnectionEnd(false);
ref.set(stream);
} else {
LOG.error("Connection failed");
}
latch.countDown();
});
try {
LOG.trace("Awaiting connection");
boolean res = latch.await(2, TimeUnit.SECONDS);
LOG.trace("Client connected: " + res);
} catch (InterruptedException e) {
throw new RuntimeException("Interrupted while creating publisher", e);
}
return ref.get();
}
use of io.vertx.proton.ProtonClient in project vertx-proton by vert-x3.
the class ProtonPublisherIntTest method doSubscriptionConfigTestImpl.
private void doSubscriptionConfigTestImpl(TestContext context, boolean durable, String linkName, boolean shared, boolean global) throws InterruptedException, ExecutionException {
server.close();
final Async clientLinkOpenAsync = context.async();
final Async serverLinkOpenAsync = context.async();
final Async clientLinkCloseAsync = context.async();
ProtonServer protonServer = null;
try {
protonServer = createServer((serverConnection) -> {
serverConnection.openHandler(result -> {
serverConnection.open();
});
serverConnection.sessionOpenHandler(session -> session.open());
serverConnection.senderOpenHandler(serverSender -> {
serverSender.closeHandler(res -> {
context.assertFalse(durable, "unexpected link close for durable sub");
serverSender.close();
});
serverSender.detachHandler(res -> {
context.assertTrue(durable, "unexpected link detach for non-durable sub");
serverSender.detach();
});
LOG.trace("Server sender opened");
serverSender.open();
// Verify the link details used were as expected
context.assertEquals(linkName, serverSender.getName(), "unexpected link name");
context.assertNotNull(serverSender.getRemoteSource(), "source should not be null");
org.apache.qpid.proton.amqp.messaging.Source source = (org.apache.qpid.proton.amqp.messaging.Source) serverSender.getRemoteSource();
if (durable) {
context.assertEquals(TerminusExpiryPolicy.NEVER, source.getExpiryPolicy(), "unexpected expiry");
context.assertEquals(TerminusDurability.UNSETTLED_STATE, source.getDurable(), "unexpected durability");
}
Symbol[] capabilities = source.getCapabilities();
if (shared && global) {
context.assertTrue(Arrays.equals(new Symbol[] { Symbol.valueOf("shared"), Symbol.valueOf("global") }, capabilities), "Unexpected capabilities: " + Arrays.toString(capabilities));
} else if (shared) {
context.assertTrue(Arrays.equals(new Symbol[] { Symbol.valueOf("shared") }, capabilities), "Unexpected capabilities: " + Arrays.toString(capabilities));
}
serverLinkOpenAsync.complete();
});
});
// ===== Client Handling =====
ProtonClient client = ProtonClient.create(vertx);
client.connect("localhost", protonServer.actualPort(), res -> {
context.assertTrue(res.succeeded());
ProtonConnection connection = res.result();
connection.open();
// Create publisher with given link name
ProtonPublisherOptions options = new ProtonPublisherOptions().setLinkName(linkName);
if (durable) {
options.setDurable(true);
}
if (shared) {
options.setShared(true);
}
if (global) {
options.setGlobal(true);
}
ProtonPublisher<Delivery> publisher = ProtonStreams.createDeliveryConsumer(connection, "myAddress", options);
publisher.subscribe(new Subscriber<Delivery>() {
Subscription sub = null;
@Override
public void onSubscribe(Subscription s) {
clientLinkOpenAsync.complete();
sub = s;
s.request(1);
sub.cancel();
}
@Override
public void onNext(Delivery e) {
}
@Override
public void onError(Throwable t) {
if (!clientLinkCloseAsync.isCompleted()) {
context.fail("onError called");
}
}
@Override
public void onComplete() {
clientLinkCloseAsync.complete();
}
});
});
serverLinkOpenAsync.awaitSuccess();
clientLinkOpenAsync.awaitSuccess();
clientLinkCloseAsync.awaitSuccess();
} finally {
if (protonServer != null) {
protonServer.close();
}
}
}
use of io.vertx.proton.ProtonClient in project vertx-proton by vert-x3.
the class ProtonPublisherIntTest method testMaxOutstandingCredit1.
@Test(timeout = 20000)
public void testMaxOutstandingCredit1(TestContext context) throws Exception {
server.close();
final int maxOutstanding = 1000;
final int amount1 = 15;
final int amount2 = 1100;
final int totalRequests = amount1 + amount2;
final Async receivedCreditAsync = context.async();
final Async verifiedCreditAsync = context.async();
final Async receivedCreditAsync2 = context.async();
final Async receivedMessages = context.async();
List<Integer> credits = new ArrayList<>();
AtomicInteger counter = new AtomicInteger();
ProtonServer protonServer = null;
try {
protonServer = createServer((serverConnection) -> {
serverConnection.openHandler(result -> {
serverConnection.open();
});
serverConnection.sessionOpenHandler(session -> session.open());
serverConnection.senderOpenHandler(serverSender -> {
LOG.trace("Server sender opened");
serverSender.open();
AtomicInteger msgNum = new AtomicInteger();
serverSender.sendQueueDrainHandler(s -> {
int credit = serverSender.getCredit();
LOG.trace("Server credit: " + credit);
credits.add(credit);
if (credit > maxOutstanding) {
context.fail("Received unexpected amount of credit: " + credit);
} else if (credit == maxOutstanding) {
LOG.trace("Server reached max outstanding: " + credit);
receivedCreditAsync.complete();
verifiedCreditAsync.await();
while (!serverSender.sendQueueFull()) {
serverSender.send(message(String.valueOf(msgNum.incrementAndGet())));
}
return;
} else if (receivedCreditAsync.isCompleted()) {
LOG.trace("Server received topup credit, post-max-outstanding: " + credit);
receivedCreditAsync2.complete();
while (!serverSender.sendQueueFull()) {
serverSender.send(message(String.valueOf(msgNum.incrementAndGet())));
}
}
});
});
});
// ===== Client Handling =====
ProtonClient client = ProtonClient.create(vertx);
client.connect("localhost", protonServer.actualPort(), res -> {
context.assertTrue(res.succeeded());
ProtonConnection connection = res.result();
connection.open();
ProtonPublisher<Delivery> publisher = ProtonStreams.createDeliveryConsumer(connection, "myAddress");
publisher.subscribe(new Subscriber<Delivery>() {
Subscription sub = null;
@Override
public void onSubscribe(Subscription s) {
LOG.trace("Flowing initial credit");
sub = s;
sub.request(amount1);
vertx.setTimer(250, x -> {
LOG.trace("Granting additional credit");
sub.request(amount2);
});
}
@Override
public void onNext(Delivery d) {
int count = counter.incrementAndGet();
validateMessage(context, count, String.valueOf(count), d.message());
if (count >= totalRequests) {
receivedMessages.complete();
}
}
@Override
public void onError(Throwable t) {
if (!receivedMessages.isCompleted()) {
context.fail("onError called");
}
}
@Override
public void onComplete() {
context.fail("onComplete called");
}
});
});
receivedCreditAsync.awaitSuccess();
// Verify the requests were all received as expected
context.assertEquals(credits.size(), 2, "Unexpected credits count");
context.assertEquals(credits.get(0), amount1, "Unexpected credit 1");
context.assertEquals(credits.get(1), maxOutstanding, "Unexpected credit 2");
verifiedCreditAsync.complete();
receivedCreditAsync2.awaitSuccess();
// Verify the requests were all received as expected
context.assertEquals(credits.size(), 3, "Unexpected credits count");
context.assertEquals(credits.get(2), amount1 + amount2 - maxOutstanding, "Unexpected credit 3");
receivedMessages.awaitSuccess();
} finally {
if (protonServer != null) {
protonServer.close();
}
}
}
Aggregations