Search in sources :

Example 1 with StreamReceiver

use of org.apache.qpid.protonj2.client.StreamReceiver 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 StreamReceiver

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

the class ClientConnection method openStreamReceiver.

@Override
public StreamReceiver openStreamReceiver(String address, StreamReceiverOptions receiverOptions) throws ClientException {
    checkClosedOrFailed();
    final ClientFuture<StreamReceiver> createRequest = getFutureFactory().createFuture();
    executor.execute(() -> {
        try {
            int sessionCapacity = StreamReceiverOptions.DEFAULT_READ_BUFFER_SIZE;
            if (receiverOptions != null) {
                sessionCapacity = receiverOptions.readBufferSize() / 2;
            }
            // Session capacity cannot be smaller than one frame size so we adjust to the lower bound
            sessionCapacity = (int) Math.max(sessionCapacity, protonConnection.getMaxFrameSize());
            checkClosedOrFailed();
            SessionOptions sessionOptions = new SessionOptions(sessionBuilder.getDefaultSessionOptions());
            ClientStreamSession session = (ClientStreamSession) sessionBuilder.streamSession(sessionOptions.incomingCapacity(sessionCapacity)).open();
            createRequest.complete(session.internalOpenStreamReceiver(address, receiverOptions));
        } catch (Throwable error) {
            createRequest.failed(ClientExceptionSupport.createNonFatalOrPassthrough(error));
        }
    });
    return request(this, createRequest);
}
Also used : StreamReceiver(org.apache.qpid.protonj2.client.StreamReceiver) SessionOptions(org.apache.qpid.protonj2.client.SessionOptions)

Example 3 with StreamReceiver

use of org.apache.qpid.protonj2.client.StreamReceiver 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 4 with StreamReceiver

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

Example 5 with StreamReceiver

use of org.apache.qpid.protonj2.client.StreamReceiver 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)

Aggregations

StreamReceiver (org.apache.qpid.protonj2.client.StreamReceiver)68 Client (org.apache.qpid.protonj2.client.Client)65 Connection (org.apache.qpid.protonj2.client.Connection)65 URI (java.net.URI)63 ProtonTestServer (org.apache.qpid.protonj2.test.driver.ProtonTestServer)63 StreamDelivery (org.apache.qpid.protonj2.client.StreamDelivery)46 Test (org.junit.jupiter.api.Test)46 StreamReceiverOptions (org.apache.qpid.protonj2.client.StreamReceiverOptions)29 StreamReceiverMessage (org.apache.qpid.protonj2.client.StreamReceiverMessage)28 InputStream (java.io.InputStream)26 Data (org.apache.qpid.protonj2.types.messaging.Data)20 ConnectionOptions (org.apache.qpid.protonj2.client.ConnectionOptions)15 ExecutionException (java.util.concurrent.ExecutionException)9 ClientException (org.apache.qpid.protonj2.client.exceptions.ClientException)9 HashMap (java.util.HashMap)6 Header (org.apache.qpid.protonj2.types.messaging.Header)6 Random (java.util.Random)5 ClientOperationTimedOutException (org.apache.qpid.protonj2.client.exceptions.ClientOperationTimedOutException)5 IOException (java.io.IOException)4 ClientIllegalStateException (org.apache.qpid.protonj2.client.exceptions.ClientIllegalStateException)4