use of io.vertx.proton.ProtonConnection in project vertx-proton by vert-x3.
the class MessageSubscriberWhiteboxVerificationTckTest method createSubscriber.
@Override
public Subscriber<Message> createSubscriber(WhiteboxSubscriberProbe<Message> probe) {
int actualPort = server.actualPort();
ProtonClient client = ProtonClient.create(vertx);
AtomicReference<Subscriber<Message>> ref = new AtomicReference<>();
CountDownLatch latch = new CountDownLatch(1);
client.connect("localhost", actualPort, result -> {
if (result.succeeded()) {
ProtonConnection conn = result.result();
conn.open();
// Modified stream impl, overriding methods to add test probe instrumentation
ProtonSubscriberImpl subscriber = new ProtonSubscriberImpl("myAddress", (ProtonConnectionImpl) conn);
ProtonSubscriber<Message> stream = new ProtonSubscriberWrapperImpl(subscriber) {
@Override
public void onSubscribe(final Subscription s) {
super.onSubscribe(s);
probe.registerOnSubscribe(new SubscriberPuppet() {
@Override
public void triggerRequest(long n) {
s.request(n);
}
@Override
public void signalCancel() {
s.cancel();
}
});
}
@Override
public void onNext(Message value) {
probe.registerOnNext(value);
super.onNext(value);
}
@Override
public void onError(Throwable t) {
probe.registerOnError(t);
super.onError(t);
}
@Override
public void onComplete() {
probe.registerOnComplete();
super.onComplete();
}
};
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 subscriber", e);
}
return ref.get();
}
use of io.vertx.proton.ProtonConnection in project vertx-proton by vert-x3.
the class HelloWorld method helloWorldSendAndConsumeExample.
private static void helloWorldSendAndConsumeExample(ProtonConnection connection) {
connection.open();
// Receive messages from queue "foo" (using an ActiveMQ style address as example).
String address = "queue://foo";
connection.createReceiver(address).handler((delivery, msg) -> {
Section body = msg.getBody();
if (body instanceof AmqpValue) {
String content = (String) ((AmqpValue) body).getValue();
System.out.println("Received message with content: " + content);
}
// By default, the receiver automatically accepts (and settles) 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();
// Create an anonymous (no address) sender, have the message carry its destination
ProtonSender sender = connection.createSender(null);
// Create a message to send, have it carry its destination for use with the anonymous sender
Message message = message(address, "Hello World from client");
// Can optionally add an openHandler or sendQueueDrainHandler
// to await remote sender open completing or credit to send being
// granted. But here we will just buffer the send immediately.
sender.open();
System.out.println("Sending message to server");
sender.send(message, delivery -> {
System.out.println(String.format("The message was received by the server: remote state=%s, remotely settled=%s", delivery.getRemoteState(), delivery.remotelySettled()));
});
}
use of io.vertx.proton.ProtonConnection in project vertx-proton by vert-x3.
the class HelloWorld method main.
public static void main(String[] args) {
// Create the Vert.x instance
Vertx vertx = Vertx.vertx();
// Create the Vert.x AMQP client instance
ProtonClient client = ProtonClient.create(vertx);
// Connect, then use the event loop thread to process the connection
client.connect("localhost", 5672, res -> {
if (res.succeeded()) {
System.out.println("We're connected");
ProtonConnection connection = res.result();
helloWorldSendAndConsumeExample(connection);
} else {
res.cause().printStackTrace();
}
});
// Just stop main() from exiting
try {
System.in.read();
} catch (Exception ignore) {
}
}
use of io.vertx.proton.ProtonConnection in project vertx-proton by vert-x3.
the class ProtonServerImplTest method testCustomAuthenticatorSuceedsAuthentication.
@Test(timeout = 20000)
public void testCustomAuthenticatorSuceedsAuthentication(TestContext context) {
Async connectedAsync = context.async();
Async authenticatedAsync = context.async();
ProtonServer.create(vertx).saslAuthenticatorFactory(new TestPlainAuthenticatorFactory()).connectHandler(protonConnection -> {
// Verify the expected auth detail was recorded in the connection attachments, just using a String here.
String authValue = protonConnection.attachments().get(AUTH_KEY, String.class);
context.assertEquals(AUTH_VALUE, authValue);
authenticatedAsync.complete();
}).listen(server -> ProtonClient.create(vertx).connect("localhost", server.result().actualPort(), GOOD_USER, PASSWD, protonConnectionAsyncResult -> {
context.assertTrue(protonConnectionAsyncResult.succeeded());
protonConnectionAsyncResult.result().disconnect();
connectedAsync.complete();
}));
authenticatedAsync.awaitSuccess();
connectedAsync.awaitSuccess();
}
use of io.vertx.proton.ProtonConnection in project vertx-proton by vert-x3.
the class TrackerSubscriberWhiteboxVerificationTckTest method createSubscriber.
@Override
public Subscriber<Tracker> createSubscriber(WhiteboxSubscriberProbe<Tracker> probe) {
int actualPort = server.actualPort();
ProtonClient client = ProtonClient.create(vertx);
AtomicReference<Subscriber<Tracker>> ref = new AtomicReference<>();
CountDownLatch latch = new CountDownLatch(1);
client.connect("localhost", actualPort, result -> {
if (result.succeeded()) {
ProtonConnection conn = result.result();
conn.open();
// Modified stream impl, overriding methods to add test probe instrumentation
ProtonSubscriber<Tracker> stream = new ProtonSubscriberImpl("myAddress", (ProtonConnectionImpl) conn) {
@Override
public void onSubscribe(final Subscription s) {
super.onSubscribe(s);
probe.registerOnSubscribe(new SubscriberPuppet() {
@Override
public void triggerRequest(long n) {
s.request(n);
}
@Override
public void signalCancel() {
s.cancel();
}
});
}
@Override
public void onNext(Tracker value) {
probe.registerOnNext(value);
super.onNext(value);
}
@Override
public void onError(Throwable t) {
probe.registerOnError(t);
super.onError(t);
}
@Override
public void onComplete() {
probe.registerOnComplete();
super.onComplete();
}
};
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 subscriber", e);
}
return ref.get();
}
Aggregations