Search in sources :

Example 1 with StreamDelivery

use of org.apache.qpid.protonj2.client.StreamDelivery in project qpid-protonj2 by apache.

the class StreamingFileReceiver method main.

public static void main(String[] args) throws Exception {
    if (args.length == 0) {
        System.out.println("Example requires a valid directory where the incoming file should be written");
        System.exit(1);
    }
    final File outputPath = new File(args[0]);
    if (!outputPath.isDirectory() || !outputPath.canWrite()) {
        System.out.println("Example requires a valid / writable directory to transfer to");
        System.exit(1);
    }
    final String fileNameKey = "filename";
    final String serverHost = System.getProperty("HOST", "localhost");
    final int serverPort = Integer.getInteger("PORT", 5672);
    final String address = System.getProperty("ADDRESS", "file-transfer");
    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);
        StreamReceiver receiver = connection.openStreamReceiver(address)) {
        StreamDelivery delivery = receiver.receive();
        StreamReceiverMessage message = delivery.message();
        // The remote should have told us the filename of the original file it sent.
        String filename = (String) message.property(fileNameKey);
        if (filename == null || filename.isBlank()) {
            System.out.println("Remote did not include the source filename in the incoming message");
            System.exit(1);
        } else {
            System.out.println("Starting receive of incoming file named: " + filename);
        }
        try (FileOutputStream outputStream = new FileOutputStream(new File(outputPath, filename))) {
            message.body().transferTo(outputStream);
        }
        System.out.println("Received file written to: " + new File(outputPath, filename));
    }
}
Also used : StreamDelivery(org.apache.qpid.protonj2.client.StreamDelivery) StreamReceiver(org.apache.qpid.protonj2.client.StreamReceiver) StreamReceiverMessage(org.apache.qpid.protonj2.client.StreamReceiverMessage) FileOutputStream(java.io.FileOutputStream) Connection(org.apache.qpid.protonj2.client.Connection) ConnectionOptions(org.apache.qpid.protonj2.client.ConnectionOptions) Client(org.apache.qpid.protonj2.client.Client) File(java.io.File)

Example 2 with StreamDelivery

use of org.apache.qpid.protonj2.client.StreamDelivery 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);
    }
}
Also used : StreamDelivery(org.apache.qpid.protonj2.client.StreamDelivery) ProtonTestServer(org.apache.qpid.protonj2.test.driver.ProtonTestServer) StreamReceiver(org.apache.qpid.protonj2.client.StreamReceiver) Connection(org.apache.qpid.protonj2.client.Connection) Client(org.apache.qpid.protonj2.client.Client) URI(java.net.URI) Test(org.junit.jupiter.api.Test)

Example 3 with StreamDelivery

use of org.apache.qpid.protonj2.client.StreamDelivery 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);
    }
}
Also used : StreamDelivery(org.apache.qpid.protonj2.client.StreamDelivery) ProtonTestServer(org.apache.qpid.protonj2.test.driver.ProtonTestServer) ClientDeliveryAbortedException(org.apache.qpid.protonj2.client.exceptions.ClientDeliveryAbortedException) StreamReceiver(org.apache.qpid.protonj2.client.StreamReceiver) InputStream(java.io.InputStream) Connection(org.apache.qpid.protonj2.client.Connection) IOException(java.io.IOException) Client(org.apache.qpid.protonj2.client.Client) URI(java.net.URI) Test(org.junit.jupiter.api.Test)

Example 4 with StreamDelivery

use of org.apache.qpid.protonj2.client.StreamDelivery in project qpid-protonj2 by apache.

the class StreamReceiverTest method testReadBytesFromBodyInputStreamWithinTransactedSession.

@Test
public void testReadBytesFromBodyInputStreamWithinTransactedSession() throws Exception {
    final byte[] body = new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
    final byte[] payload = createEncodedMessage(new Data(body));
    final byte[] txnId = new byte[] { 0, 1, 2, 3 };
    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(false).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");
        peer.waitForScriptToComplete(5, TimeUnit.SECONDS);
        peer.expectCoordinatorAttach().respond();
        peer.remoteFlow().withLinkCredit(2).queue();
        peer.expectDeclare().accept(txnId);
        peer.expectDisposition().withSettled(true).withState().transactional().withTxnId(txnId).withAccepted();
        peer.expectDischarge().withFail(false).withTxnId(txnId).accept();
        receiver.session().beginTransaction();
        final StreamDelivery delivery = receiver.receive();
        assertNotNull(delivery);
        assertTrue(delivery.completed());
        assertFalse(delivery.aborted());
        StreamReceiverMessage message = delivery.message();
        assertNotNull(message);
        InputStream bodyStream = message.body();
        assertNotNull(bodyStream);
        assertNull(message.header());
        assertNull(message.annotations());
        assertNull(message.properties());
        assertNull(delivery.annotations());
        final byte[] receivedBody = new byte[body.length];
        for (int i = 0; i < body.length; ++i) {
            receivedBody[i] = (byte) bodyStream.read();
        }
        assertArrayEquals(body, receivedBody);
        assertEquals(-1, bodyStream.read());
        receiver.session().commitTransaction();
        peer.waitForScriptToComplete(5, TimeUnit.SECONDS);
        peer.expectDetach().respond();
        peer.expectEnd().respond();
        peer.expectClose().respond();
        receiver.closeAsync().get();
        connection.closeAsync().get();
        peer.waitForScriptToComplete(5, TimeUnit.SECONDS);
    }
}
Also used : StreamDelivery(org.apache.qpid.protonj2.client.StreamDelivery) ProtonTestServer(org.apache.qpid.protonj2.test.driver.ProtonTestServer) StreamReceiver(org.apache.qpid.protonj2.client.StreamReceiver) StreamReceiverMessage(org.apache.qpid.protonj2.client.StreamReceiverMessage) InputStream(java.io.InputStream) Connection(org.apache.qpid.protonj2.client.Connection) Data(org.apache.qpid.protonj2.types.messaging.Data) Client(org.apache.qpid.protonj2.client.Client) URI(java.net.URI) Test(org.junit.jupiter.api.Test)

Example 5 with StreamDelivery

use of org.apache.qpid.protonj2.client.StreamDelivery in project qpid-protonj2 by apache.

the class StreamReceiverTest method testStreamReadOpensSessionWindowForAdditionalInputAndGrantsCreditOnClose.

@Test
public void testStreamReadOpensSessionWindowForAdditionalInputAndGrantsCreditOnClose() throws Exception {
    final byte[] body1 = new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
    final byte[] body2 = new byte[] { 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0 };
    final byte[] payload1 = createEncodedMessage(new Data(body1));
    final byte[] payload2 = createEncodedMessage(new Data(body2));
    try (ProtonTestServer peer = new ProtonTestServer()) {
        peer.expectSASLAnonymousConnect();
        peer.expectOpen().withMaxFrameSize(1000).respond();
        peer.expectBegin().withIncomingWindow(1).respond();
        peer.expectAttach().ofReceiver().respond();
        peer.expectFlow().withIncomingWindow(1).withLinkCredit(1);
        peer.remoteTransfer().withHandle(0).withDeliveryId(0).withDeliveryTag(new byte[] { 1 }).withMore(true).withMessageFormat(0).withPayload(payload1).queue();
        peer.start();
        URI remoteURI = peer.getServerURI();
        LOG.info("Test started, peer listening on: {}", remoteURI);
        Client container = Client.create();
        ConnectionOptions connectionOptions = new ConnectionOptions().maxFrameSize(1000);
        Connection connection = container.connect(remoteURI.getHost(), remoteURI.getPort(), connectionOptions);
        StreamReceiverOptions streamOptions = new StreamReceiverOptions().readBufferSize(2000).creditWindow(1);
        StreamReceiver receiver = connection.openStreamReceiver("test-queue", streamOptions);
        StreamDelivery delivery = receiver.receive();
        assertNotNull(delivery);
        StreamReceiverMessage message = delivery.message();
        assertNotNull(message);
        // Creating the input stream instance should read the first chunk of data from the incoming
        // delivery which should result in a new credit being available to expand the session window.
        // An additional transfer should be placed into the delivery buffer but not yet read since
        // the user hasn't read anything. Since we are in auto settle the completed transfer should
        // trigger settlement and also open the credit window but the session window should not be
        // expanded since we haven't read the data yet.
        peer.waitForScriptToComplete(5, TimeUnit.SECONDS);
        peer.expectFlow().withDeliveryCount(0).withIncomingWindow(1).withLinkCredit(1);
        peer.remoteTransfer().withHandle(0).withDeliveryId(0).withMore(false).withMessageFormat(0).withPayload(payload2).queue();
        peer.expectDisposition().withSettled(true).withState().accepted();
        peer.expectFlow().withDeliveryCount(1).withIncomingWindow(0).withLinkCredit(1);
        InputStream bodyStream = message.body();
        assertNotNull(bodyStream);
        // Once the read of all data completes the session window should be opened
        peer.waitForScriptToComplete(5, TimeUnit.SECONDS);
        peer.expectFlow().withDeliveryCount(1).withIncomingWindow(1).withLinkCredit(1);
        byte[] combinedPayloads = new byte[body1.length + body2.length];
        bodyStream.read(combinedPayloads);
        peer.waitForScriptToComplete(5, TimeUnit.SECONDS);
        // No frames should be triggered by closing the stream since we already auto settled
        // and updated the session window on the remote.
        assertTrue(Arrays.equals(body1, 0, body1.length, combinedPayloads, 0, body1.length));
        assertTrue(Arrays.equals(body2, 0, body2.length, combinedPayloads, body1.length, body1.length + body2.length));
        bodyStream.close();
        peer.waitForScriptToComplete(5, TimeUnit.SECONDS);
        peer.expectDetach().respond();
        peer.expectEnd().respond();
        peer.expectClose().respond();
        receiver.openFuture().get();
        receiver.closeAsync().get();
        connection.closeAsync().get();
        peer.waitForScriptToComplete(5, TimeUnit.SECONDS);
    }
}
Also used : StreamDelivery(org.apache.qpid.protonj2.client.StreamDelivery) ProtonTestServer(org.apache.qpid.protonj2.test.driver.ProtonTestServer) StreamReceiver(org.apache.qpid.protonj2.client.StreamReceiver) StreamReceiverOptions(org.apache.qpid.protonj2.client.StreamReceiverOptions) StreamReceiverMessage(org.apache.qpid.protonj2.client.StreamReceiverMessage) InputStream(java.io.InputStream) Connection(org.apache.qpid.protonj2.client.Connection) Data(org.apache.qpid.protonj2.types.messaging.Data) ConnectionOptions(org.apache.qpid.protonj2.client.ConnectionOptions) Client(org.apache.qpid.protonj2.client.Client) URI(java.net.URI) Test(org.junit.jupiter.api.Test)

Aggregations

StreamDelivery (org.apache.qpid.protonj2.client.StreamDelivery)47 Client (org.apache.qpid.protonj2.client.Client)46 Connection (org.apache.qpid.protonj2.client.Connection)46 StreamReceiver (org.apache.qpid.protonj2.client.StreamReceiver)46 URI (java.net.URI)44 ProtonTestServer (org.apache.qpid.protonj2.test.driver.ProtonTestServer)44 Test (org.junit.jupiter.api.Test)39 StreamReceiverMessage (org.apache.qpid.protonj2.client.StreamReceiverMessage)28 InputStream (java.io.InputStream)26 Data (org.apache.qpid.protonj2.types.messaging.Data)20 StreamReceiverOptions (org.apache.qpid.protonj2.client.StreamReceiverOptions)16 ConnectionOptions (org.apache.qpid.protonj2.client.ConnectionOptions)13 Header (org.apache.qpid.protonj2.types.messaging.Header)6 Random (java.util.Random)5 IOException (java.io.IOException)4 HashMap (java.util.HashMap)4 ApplicationProperties (org.apache.qpid.protonj2.types.messaging.ApplicationProperties)4 CountDownLatch (java.util.concurrent.CountDownLatch)2 ClientDeliveryAbortedException (org.apache.qpid.protonj2.client.exceptions.ClientDeliveryAbortedException)2 ClientException (org.apache.qpid.protonj2.client.exceptions.ClientException)2