use of io.vertx.proton.ProtonServer in project vertx-proton by vert-x3.
the class ProtonSubscriberIntTest method testCreateAnonymousRelaySubscriber.
@Test(timeout = 20000)
public void testCreateAnonymousRelaySubscriber(TestContext context) throws Exception {
server.close();
final Async serverLinkOpenAsync = context.async();
final Async serverReceivedMessageAsync = context.async();
final Async serverLinkCloseAsync = context.async();
final String address1 = "testCreateAnonymousRelaySubscriber1";
final String address2 = "testCreateAnonymousRelaySubscriber2";
final AtomicInteger msgCounter = new AtomicInteger(0);
final AtomicInteger acceptedMsgCounter = new AtomicInteger(0);
ProtonServer protonServer = null;
try {
protonServer = createServer((serverConnection) -> {
serverConnection.openHandler(result -> {
serverConnection.open();
});
serverConnection.sessionOpenHandler(session -> session.open());
serverConnection.receiverOpenHandler(serverReceiver -> {
LOG.trace("Server receiver opened");
context.assertFalse(serverLinkOpenAsync.isCompleted(), "should only be one link opened");
context.assertNull(serverReceiver.getRemoteTarget().getAddress(), "link target should have null address for anonymous relay");
serverReceiver.handler((delivery, msg) -> {
int count = msgCounter.incrementAndGet();
if (LOG.isTraceEnabled()) {
LOG.trace("Server got msg: " + getMessageBody(context, msg));
}
switch(count) {
case 1:
{
validateMessage(context, 1, "1", msg);
context.assertEquals(address1, msg.getAddress(), "Unexpected message1 'to' address");
break;
}
case 2:
{
validateMessage(context, 2, "2", msg);
context.assertEquals(address2, msg.getAddress(), "Unexpected message2 'to' address");
// Complete the test
serverReceivedMessageAsync.complete();
break;
}
}
});
serverReceiver.closeHandler(x -> {
serverReceiver.close();
serverLinkCloseAsync.complete();
});
// Set the local terminus details [naively]
serverReceiver.setTarget(serverReceiver.getRemoteTarget().copy());
serverReceiver.open();
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 subscriber without an address, to send to the anonymous relay
ProtonSubscriber<Tracker> subscriber = ProtonStreams.createTrackerProducer(connection, null);
Tracker envelope1 = Tracker.create(message(address1, "1"), env1 -> {
context.assertTrue(env1.isAccepted(), "msg1 should be accepted");
context.assertTrue(env1.isRemotelySettled(), "msg1 should be remotely settled");
context.assertTrue(acceptedMsgCounter.compareAndSet(0, 1), "unexpected acceptedMsgCounter:" + acceptedMsgCounter);
});
Tracker envelope2 = Tracker.create(message(address2, "2"), env2 -> {
context.assertTrue(env2.isAccepted(), "msg1 should be accepted");
context.assertTrue(env2.isRemotelySettled(), "msg1 should be remotely settled");
context.assertTrue(acceptedMsgCounter.compareAndSet(1, 2), "unexpected acceptedMsgCounter:" + acceptedMsgCounter);
});
Publisher<Tracker> producer = Flowable.just(envelope1, envelope2);
producer.subscribe(subscriber);
});
serverLinkOpenAsync.awaitSuccess();
serverReceivedMessageAsync.awaitSuccess();
serverLinkCloseAsync.awaitSuccess();
} finally {
if (protonServer != null) {
protonServer.close();
}
}
}
use of io.vertx.proton.ProtonServer in project vertx-proton by vert-x3.
the class ProtonSubscriberIntTest method testCreateUsingCustomTarget.
@Test(timeout = 20000)
public void testCreateUsingCustomTarget(TestContext context) throws Exception {
server.close();
final Async serverLinkOpenAsync = context.async();
final Async serverReceivedMessageAsync = context.async();
final Async serverLinkCloseAsync = context.async();
final String address = "testCreateUsingCustomTarget";
ProtonServer protonServer = null;
try {
protonServer = createServer((serverConnection) -> {
serverConnection.openHandler(result -> {
serverConnection.open();
});
serverConnection.sessionOpenHandler(session -> session.open());
serverConnection.receiverOpenHandler(serverReceiver -> {
serverReceiver.handler((delivery, msg) -> {
// We got the message that was sent, complete the test
if (LOG.isTraceEnabled()) {
LOG.trace("Server got msg: " + getMessageBody(context, msg));
}
validateMessage(context, 1, "1", msg);
serverReceivedMessageAsync.complete();
});
// Verify the remote terminus details used were as expected
context.assertNotNull(serverReceiver.getRemoteTarget(), "target should not be null");
org.apache.qpid.proton.amqp.messaging.Target remoteTarget = (org.apache.qpid.proton.amqp.messaging.Target) serverReceiver.getRemoteTarget();
context.assertEquals(address, remoteTarget.getAddress(), "unexpected target address");
Symbol[] capabilities = remoteTarget.getCapabilities();
context.assertTrue(Arrays.equals(new Symbol[] { Symbol.valueOf("custom") }, capabilities), "Unexpected capabilities: " + Arrays.toString(capabilities));
// Set the local terminus details
serverReceiver.setTarget(remoteTarget.copy());
serverReceiver.closeHandler(x -> {
serverReceiver.close();
serverLinkCloseAsync.complete();
});
LOG.trace("Server receiver opened");
serverReceiver.open();
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 subscriber with custom target configured to be dynamic
ProtonSubscriber<Tracker> subscriber = ProtonStreams.createTrackerProducer(connection, address);
org.apache.qpid.proton.amqp.messaging.Target target = (org.apache.qpid.proton.amqp.messaging.Target) subscriber.getTarget();
target.setCapabilities(new Symbol[] { Symbol.valueOf("custom") });
Tracker envelope = Tracker.create(message("1"));
Publisher<Tracker> publisher = Flowable.just(envelope);
publisher.subscribe(subscriber);
});
serverLinkOpenAsync.awaitSuccess();
serverReceivedMessageAsync.awaitSuccess();
serverLinkCloseAsync.awaitSuccess();
} finally {
if (protonServer != null) {
protonServer.close();
}
}
}
use of io.vertx.proton.ProtonServer in project vertx-proton by vert-x3.
the class ProtonSubscriberIntTest method testConfigureProducerLinkName.
@Test(timeout = 20000)
public void testConfigureProducerLinkName(TestContext context) throws Exception {
server.close();
final Async serverLinkOpenAsync = context.async();
final Async serverReceivedMessageAsync = context.async();
final Async serverLinkCloseAsync = context.async();
final String linkName = "testConfigureProducerLinkName";
ProtonServer protonServer = null;
try {
protonServer = createServer((serverConnection) -> {
serverConnection.openHandler(result -> {
serverConnection.open();
});
serverConnection.sessionOpenHandler(session -> session.open());
serverConnection.receiverOpenHandler(serverReceiver -> {
LOG.trace("Server receiver opened");
// Verify the link details used were as expected
context.assertEquals(linkName, serverReceiver.getName(), "unexpected link name");
serverReceiver.handler((delivery, msg) -> {
// We got the message that was sent, complete the test
if (LOG.isTraceEnabled()) {
LOG.trace("Server got msg: " + getMessageBody(context, msg));
}
validateMessage(context, 1, "1", msg);
serverReceivedMessageAsync.complete();
});
serverReceiver.closeHandler(x -> {
serverReceiver.close();
serverLinkCloseAsync.complete();
});
// Set the local terminus details [naively]
serverReceiver.setTarget(serverReceiver.getRemoteTarget().copy());
serverReceiver.open();
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 subscriber with given link name
ProtonSubscriberOptions options = new ProtonSubscriberOptions().setLinkName(linkName);
ProtonSubscriber<Tracker> subscriber = ProtonStreams.createTrackerProducer(connection, "myAddress", options);
Tracker envelope = Tracker.create(message("1"));
Publisher<Tracker> producer = Flowable.just(envelope);
producer.subscribe(subscriber);
});
serverLinkOpenAsync.awaitSuccess();
serverReceivedMessageAsync.awaitSuccess();
serverLinkCloseAsync.awaitSuccess();
} finally {
if (protonServer != null) {
protonServer.close();
}
}
}
use of io.vertx.proton.ProtonServer in project vertx-proton by vert-x3.
the class ProtonSubscriberIntTest method createServer.
private ProtonServer createServer(Handler<ProtonConnection> serverConnHandler) throws InterruptedException, ExecutionException {
ProtonServer server = ProtonServer.create(vertx);
server.connectHandler(serverConnHandler);
FutureHandler<ProtonServer, AsyncResult<ProtonServer>> handler = FutureHandler.asyncResult();
server.listen(0, handler);
handler.get();
return server;
}
use of io.vertx.proton.ProtonServer in project vertx-proton by vert-x3.
the class HelloWorldServer method main.
public static void main(String[] args) {
// Create the Vert.x instance
Vertx vertx = Vertx.vertx();
// Create the Vert.x AMQP client instance
ProtonServer server = ProtonServer.create(vertx).connectHandler((connection) -> {
helloProcessConnection(vertx, connection);
}).listen(5672, (res) -> {
if (res.succeeded()) {
System.out.println("Listening on: " + res.result().actualPort());
} else {
res.cause().printStackTrace();
}
});
// Just stop main() from exiting
try {
System.in.read();
} catch (Exception ignore) {
}
}
Aggregations