use of com.swiftmq.amqp.v100.client.Connection in project qpid-protonj2 by apache.
the class HelloWorld method main.
public static void main(String[] args) throws Exception {
final String serverHost = System.getProperty("HOST", "localhost");
final int serverPort = Integer.getInteger("PORT", 5672);
final String address = System.getProperty("ADDRESS", "hello-world-example");
final Client client = Client.create();
final ConnectionOptions options = new ConnectionOptions();
options.user(System.getProperty("USER"));
options.password(System.getProperty("PASSWORD"));
try (Connection connection = client.connect(serverHost, serverPort, options);
Receiver receiver = connection.openReceiver(address);
Sender sender = connection.openSender(address)) {
sender.send(Message.create("Hello World"));
Delivery delivery = receiver.receive();
Message<String> received = delivery.message();
System.out.println("Received message with body: " + received.body());
}
}
use of com.swiftmq.amqp.v100.client.Connection in project qpid-protonj2 by apache.
the class StreamReceiverTest method testStreamDeliveryReceivedWhileTransferIsIncomplete.
@Test
public void testStreamDeliveryReceivedWhileTransferIsIncomplete() throws Exception {
final byte[] payload = createEncodedMessage(new AmqpValue<>("Hello World"));
try (ProtonTestServer peer = new ProtonTestServer()) {
peer.expectSASLAnonymousConnect();
peer.expectOpen().respond();
peer.expectBegin().respond();
peer.expectAttach().withRole(Role.RECEIVER.getValue()).respond();
peer.expectFlow();
peer.remoteTransfer().withHandle(0).withDeliveryId(0).withDeliveryTag(new byte[] { 1 }).withMore(true).withMessageFormat(0).withPayload(payload).queue();
peer.start();
URI remoteURI = peer.getServerURI();
LOG.info("Test started, peer listening on: {}", remoteURI);
final Client container = Client.create();
final Connection connection = container.connect(remoteURI.getHost(), remoteURI.getPort());
final StreamReceiver receiver = connection.openStreamReceiver("test-queue");
final StreamDelivery delivery = receiver.receive();
assertNotNull(delivery);
assertFalse(delivery.completed());
peer.waitForScriptToComplete(5, TimeUnit.SECONDS);
peer.expectDisposition().withSettled(true);
peer.remoteTransfer().withHandle(0).withDeliveryId(0).withNullDeliveryTag().withMore(false).withMessageFormat(0).withPayload(payload).now();
Wait.assertTrue("Should eventually be marked as completed", delivery::completed);
peer.expectDetach().respond();
peer.expectEnd().respond();
peer.expectClose().respond();
receiver.closeAsync().get();
connection.closeAsync().get();
peer.waitForScriptToComplete(5, TimeUnit.SECONDS);
}
}
use of com.swiftmq.amqp.v100.client.Connection in project qpid-protonj2 by apache.
the class StreamReceiverTest method doTestStreamReceiverSessionCapacity.
private void doTestStreamReceiverSessionCapacity(int maxFrameSize, int readBufferSize, int expectedSessionWindow) throws Exception {
try (ProtonTestServer peer = new ProtonTestServer()) {
peer.expectSASLAnonymousConnect();
peer.expectOpen().withMaxFrameSize(maxFrameSize).respond();
peer.expectBegin().withIncomingWindow(expectedSessionWindow).respond();
peer.expectAttach().ofReceiver().respond();
peer.expectFlow().withIncomingWindow(expectedSessionWindow);
peer.expectDetach().respond();
peer.expectEnd().respond();
peer.expectClose().respond();
peer.start();
URI remoteURI = peer.getServerURI();
LOG.info("Test started, peer listening on: {}", remoteURI);
Client container = Client.create();
ConnectionOptions connectionOptions = new ConnectionOptions().maxFrameSize(maxFrameSize);
Connection connection = container.connect(remoteURI.getHost(), remoteURI.getPort(), connectionOptions);
StreamReceiverOptions streamOptions = new StreamReceiverOptions().readBufferSize(readBufferSize);
StreamReceiver receiver = connection.openStreamReceiver("test-queue", streamOptions);
receiver.openFuture().get();
receiver.closeAsync().get();
connection.closeAsync().get();
peer.waitForScriptToComplete(5, TimeUnit.SECONDS);
}
}
use of com.swiftmq.amqp.v100.client.Connection in project qpid-protonj2 by apache.
the class StreamReceiverTest method testStreamDeliveryRawInputStreamBlockedReadBytesAborted.
@Test
public void testStreamDeliveryRawInputStreamBlockedReadBytesAborted() throws Exception {
final byte[] payload = createEncodedMessage(new AmqpValue<>("Hello World"));
try (ProtonTestServer peer = new ProtonTestServer()) {
peer.expectSASLAnonymousConnect();
peer.expectOpen().respond();
peer.expectBegin().respond();
peer.expectAttach().withRole(Role.RECEIVER.getValue()).respond();
peer.expectFlow();
peer.remoteTransfer().withHandle(0).withDeliveryId(0).withDeliveryTag(new byte[] { 1 }).withMore(true).withMessageFormat(0).withPayload(payload).queue();
peer.start();
URI remoteURI = peer.getServerURI();
LOG.info("Test started, peer listening on: {}", remoteURI);
final Client container = Client.create();
final Connection connection = container.connect(remoteURI.getHost(), remoteURI.getPort());
final StreamReceiver receiver = connection.openStreamReceiver("test-queue");
final StreamDelivery delivery = receiver.receive();
assertNotNull(delivery);
assertFalse(delivery.completed());
assertFalse(delivery.aborted());
final InputStream stream = delivery.rawInputStream();
assertNotNull(stream);
assertEquals(payload.length, stream.available());
final byte[] deliveryBytes = new byte[payload.length * 2];
peer.remoteTransfer().withHandle(0).withDeliveryId(0).withDeliveryTag(new byte[] { 1 }).withMore(false).withAborted(true).withMessageFormat(0).later(50);
try {
stream.read(deliveryBytes);
fail("Delivery should have been aborted while waiting for more data.");
} catch (IOException ioe) {
assertTrue(ioe.getCause() instanceof ClientDeliveryAbortedException);
}
stream.close();
peer.expectDetach().respond();
peer.expectEnd().respond();
peer.expectClose().respond();
receiver.closeAsync().get();
connection.closeAsync().get();
peer.waitForScriptToComplete(5, TimeUnit.SECONDS);
}
}
use of com.swiftmq.amqp.v100.client.Connection in project qpid-protonj2 by apache.
the class StreamReceiverTest method doTestReceiveFailsWhenLinkRemotelyClose.
private void doTestReceiveFailsWhenLinkRemotelyClose(boolean timed) throws Exception {
try (ProtonTestServer peer = new ProtonTestServer()) {
peer.expectSASLAnonymousConnect();
peer.expectOpen().respond();
peer.expectBegin().respond();
peer.expectAttach().withRole(Role.RECEIVER.getValue()).respond();
peer.expectFlow();
peer.start();
URI remoteURI = peer.getServerURI();
LOG.info("Test started, peer listening on: {}", remoteURI);
Client container = Client.create();
Connection connection = container.connect(remoteURI.getHost(), remoteURI.getPort());
StreamReceiver receiver = connection.openStreamReceiver("test-queue");
peer.waitForScriptToComplete(5, TimeUnit.SECONDS);
peer.expectDetach();
peer.expectEnd().respond();
peer.expectClose().respond();
peer.remoteDetach().later(50);
if (timed) {
assertThrows(ClientLinkRemotelyClosedException.class, () -> receiver.receive(1, TimeUnit.MINUTES));
} else {
assertThrows(ClientLinkRemotelyClosedException.class, () -> receiver.receive());
}
receiver.closeAsync();
connection.closeAsync().get();
peer.waitForScriptToComplete(5, TimeUnit.SECONDS);
}
}
Aggregations