Search in sources :

Example 1 with Tracker

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

the class ClientSender method sendMessage.

private Tracker sendMessage(AdvancedMessage<?> message, Map<String, Object> deliveryAnnotations, boolean waitForCredit) throws ClientException {
    final ClientFuture<Tracker> operation = session.getFutureFactory().createFuture();
    final ProtonBuffer buffer = message.encode(deliveryAnnotations);
    executor.execute(() -> {
        if (notClosedOrFailed(operation)) {
            try {
                final ClientOutgoingEnvelope envelope = new ClientOutgoingEnvelope(this, message.messageFormat(), buffer, operation);
                if (protonSender.isSendable() && protonSender.current() == null) {
                    session.getTransactionContext().send(envelope, null, protonSender.getSenderSettleMode() == SenderSettleMode.SETTLED);
                } else if (waitForCredit) {
                    addToTailOfBlockedQueue(envelope);
                } else {
                    operation.complete(null);
                }
            } catch (Exception error) {
                operation.failed(ClientExceptionSupport.createNonFatalOrPassthrough(error));
            }
        }
    });
    return session.request(this, operation);
}
Also used : ProtonBuffer(org.apache.qpid.protonj2.buffer.ProtonBuffer) Tracker(org.apache.qpid.protonj2.client.Tracker) ClientResourceRemotelyClosedException(org.apache.qpid.protonj2.client.exceptions.ClientResourceRemotelyClosedException) ClientException(org.apache.qpid.protonj2.client.exceptions.ClientException) ClientConnectionRemotelyClosedException(org.apache.qpid.protonj2.client.exceptions.ClientConnectionRemotelyClosedException) ClientSendTimedOutException(org.apache.qpid.protonj2.client.exceptions.ClientSendTimedOutException)

Example 2 with Tracker

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

the class StreamSenderTest method testAutoFlushDuringWriteThatExceedConfiguredBufferLimit.

@Test
void testAutoFlushDuringWriteThatExceedConfiguredBufferLimit() throws Exception {
    try (ProtonTestServer peer = new ProtonTestServer()) {
        peer.expectSASLAnonymousConnect();
        peer.expectOpen().respond();
        peer.expectBegin().respond();
        peer.expectAttach().ofSender().respond();
        peer.remoteFlow().withLinkCredit(1).queue();
        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());
        StreamSender sender = connection.openStreamSender("test-queue", new StreamSenderOptions().writeBufferSize(256));
        StreamSenderMessage tracker = sender.beginMessage();
        final byte[] payload = new byte[1024];
        Arrays.fill(payload, 0, 256, (byte) 1);
        Arrays.fill(payload, 256, 512, (byte) 2);
        Arrays.fill(payload, 512, 768, (byte) 3);
        Arrays.fill(payload, 768, 1024, (byte) 4);
        final byte[] payload1 = new byte[256];
        Arrays.fill(payload1, (byte) 1);
        final byte[] payload2 = new byte[256];
        Arrays.fill(payload2, (byte) 2);
        final byte[] payload3 = new byte[256];
        Arrays.fill(payload3, (byte) 3);
        final byte[] payload4 = new byte[256];
        Arrays.fill(payload4, (byte) 4);
        // Populate all Header values
        Header header = new Header();
        header.setDurable(true);
        header.setPriority((byte) 1);
        header.setTimeToLive(65535);
        header.setFirstAcquirer(true);
        header.setDeliveryCount(2);
        tracker.header(header);
        OutputStreamOptions options = new OutputStreamOptions();
        OutputStream stream = tracker.body(options);
        HeaderMatcher headerMatcher = new HeaderMatcher(true);
        headerMatcher.withDurable(true);
        headerMatcher.withPriority((byte) 1);
        headerMatcher.withTtl(65535);
        headerMatcher.withFirstAcquirer(true);
        headerMatcher.withDeliveryCount(2);
        EncodedDataMatcher dataMatcher1 = new EncodedDataMatcher(payload1);
        TransferPayloadCompositeMatcher payloadMatcher1 = new TransferPayloadCompositeMatcher();
        payloadMatcher1.setHeadersMatcher(headerMatcher);
        payloadMatcher1.setMessageContentMatcher(dataMatcher1);
        EncodedDataMatcher dataMatcher2 = new EncodedDataMatcher(payload2);
        TransferPayloadCompositeMatcher payloadMatcher2 = new TransferPayloadCompositeMatcher();
        payloadMatcher2.setMessageContentMatcher(dataMatcher2);
        EncodedDataMatcher dataMatcher3 = new EncodedDataMatcher(payload3);
        TransferPayloadCompositeMatcher payloadMatcher3 = new TransferPayloadCompositeMatcher();
        payloadMatcher3.setMessageContentMatcher(dataMatcher3);
        EncodedDataMatcher dataMatcher4 = new EncodedDataMatcher(payload4);
        TransferPayloadCompositeMatcher payloadMatcher4 = new TransferPayloadCompositeMatcher();
        payloadMatcher4.setMessageContentMatcher(dataMatcher4);
        peer.waitForScriptToComplete(5, TimeUnit.SECONDS);
        peer.expectTransfer().withPayload(payloadMatcher1).withMore(true);
        peer.expectTransfer().withPayload(payloadMatcher2).withMore(true);
        peer.expectTransfer().withPayload(payloadMatcher3).withMore(true);
        peer.expectTransfer().withPayload(payloadMatcher4).withMore(true);
        // Stream won't output until some body bytes are written.
        stream.write(payload);
        peer.waitForScriptToComplete(5, TimeUnit.SECONDS);
        peer.expectTransfer().withNullPayload().withMore(false).accept();
        peer.expectDetach().respond();
        peer.expectEnd().respond();
        peer.expectClose().respond();
        stream.close();
        sender.close();
        connection.close();
        peer.waitForScriptToComplete(5, TimeUnit.SECONDS);
    }
}
Also used : HeaderMatcher(org.apache.qpid.protonj2.test.driver.matchers.messaging.HeaderMatcher) ProtonTestServer(org.apache.qpid.protonj2.test.driver.ProtonTestServer) ByteArrayOutputStream(java.io.ByteArrayOutputStream) OutputStream(java.io.OutputStream) Connection(org.apache.qpid.protonj2.client.Connection) EncodedDataMatcher(org.apache.qpid.protonj2.test.driver.matchers.types.EncodedDataMatcher) TransferPayloadCompositeMatcher(org.apache.qpid.protonj2.test.driver.matchers.transport.TransferPayloadCompositeMatcher) URI(java.net.URI) StreamSenderOptions(org.apache.qpid.protonj2.client.StreamSenderOptions) Header(org.apache.qpid.protonj2.types.messaging.Header) OutputStreamOptions(org.apache.qpid.protonj2.client.OutputStreamOptions) StreamSender(org.apache.qpid.protonj2.client.StreamSender) StreamSenderMessage(org.apache.qpid.protonj2.client.StreamSenderMessage) Client(org.apache.qpid.protonj2.client.Client) Test(org.junit.jupiter.api.Test)

Example 3 with Tracker

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

the class StreamSenderTest method testStreamMessageWaitingOnCreditWritesWhileCompleteSendWaitsInQueue.

@Test
void testStreamMessageWaitingOnCreditWritesWhileCompleteSendWaitsInQueue() throws Exception {
    try (ProtonTestServer peer = new ProtonTestServer()) {
        peer.expectSASLAnonymousConnect();
        peer.expectOpen().respond();
        peer.expectBegin().respond();
        peer.expectAttach().ofSender().respond();
        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());
        StreamSender sender = connection.openStreamSender("test-queue");
        StreamSenderMessage tracker = sender.beginMessage();
        OutputStream stream = tracker.body();
        final byte[] payload1 = new byte[256];
        Arrays.fill(payload1, (byte) 1);
        final byte[] payload2 = new byte[256];
        Arrays.fill(payload2, (byte) 2);
        final byte[] payload3 = new byte[256];
        Arrays.fill(payload3, (byte) 3);
        EncodedDataMatcher dataMatcher1 = new EncodedDataMatcher(payload1);
        TransferPayloadCompositeMatcher payloadMatcher1 = new TransferPayloadCompositeMatcher();
        payloadMatcher1.setMessageContentMatcher(dataMatcher1);
        EncodedDataMatcher dataMatcher2 = new EncodedDataMatcher(payload2);
        TransferPayloadCompositeMatcher payloadMatcher2 = new TransferPayloadCompositeMatcher();
        payloadMatcher2.setMessageContentMatcher(dataMatcher2);
        EncodedDataMatcher dataMatcher3 = new EncodedDataMatcher(payload3);
        TransferPayloadCompositeMatcher payloadMatcher3 = new TransferPayloadCompositeMatcher();
        payloadMatcher3.setMessageContentMatcher(dataMatcher3);
        final AtomicBoolean sendFailed = new AtomicBoolean();
        final CountDownLatch streamSend1Complete = new CountDownLatch(1);
        // Stream won't output until some body bytes are written.
        ForkJoinPool.commonPool().execute(() -> {
            try {
                stream.write(payload1);
                stream.flush();
            } catch (IOException e) {
                LOG.info("send failed with error: ", e);
                sendFailed.set(true);
            } finally {
                streamSend1Complete.countDown();
            }
        });
        peer.waitForScriptToComplete(5, TimeUnit.SECONDS);
        peer.expectTransfer().withPayload(payloadMatcher1).withMore(true);
        // Now trigger the next send by granting credit for payload 1
        peer.remoteFlow().withIncomingWindow(1).withNextIncomingId(1).withLinkCredit(10).now();
        assertTrue(streamSend1Complete.await(5, TimeUnit.SECONDS), "Stream sender completed first send");
        assertFalse(sendFailed.get());
        final CountDownLatch sendStarted = new CountDownLatch(1);
        final CountDownLatch sendCompleted = new CountDownLatch(1);
        ForkJoinPool.commonPool().execute(() -> {
            try {
                LOG.info("Test send 1 is preparing to fire:");
                sendStarted.countDown();
                sender.send(Message.create(payload3));
            } catch (Exception e) {
                LOG.info("Test send 1 failed with error: ", e);
                sendFailed.set(true);
            } finally {
                sendCompleted.countDown();
            }
        });
        assertTrue(sendStarted.await(10, TimeUnit.SECONDS));
        peer.waitForScriptToComplete(5, TimeUnit.SECONDS);
        peer.expectTransfer().withPayload(payloadMatcher2).withMore(true);
        // Queue a flow that will allow send by granting credit for payload 3 via sender.send
        peer.remoteFlow().withIncomingWindow(1).withNextIncomingId(3).withLinkCredit(10).queue();
        // Now trigger the next send by granting credit for payload 2
        peer.remoteFlow().withIncomingWindow(1).withNextIncomingId(2).withLinkCredit(10).now();
        stream.write(payload2);
        stream.flush();
        peer.waitForScriptToComplete(5, TimeUnit.SECONDS);
        peer.expectTransfer().withNullPayload().withMore(false).accept();
        peer.remoteFlow().withIncomingWindow(1).withNextIncomingId(4).withLinkCredit(10).queue();
        peer.expectTransfer().withPayload(payloadMatcher3).withMore(false);
        peer.expectDetach().respond();
        peer.expectEnd().respond();
        peer.expectClose().respond();
        stream.close();
        assertTrue(sendCompleted.await(100, TimeUnit.SECONDS));
        assertFalse(sendFailed.get());
        sender.closeAsync().get();
        connection.closeAsync().get();
        peer.waitForScriptToComplete(5, TimeUnit.SECONDS);
    }
}
Also used : ProtonTestServer(org.apache.qpid.protonj2.test.driver.ProtonTestServer) ByteArrayOutputStream(java.io.ByteArrayOutputStream) OutputStream(java.io.OutputStream) Connection(org.apache.qpid.protonj2.client.Connection) EncodedDataMatcher(org.apache.qpid.protonj2.test.driver.matchers.types.EncodedDataMatcher) IOException(java.io.IOException) TransferPayloadCompositeMatcher(org.apache.qpid.protonj2.test.driver.matchers.transport.TransferPayloadCompositeMatcher) CountDownLatch(java.util.concurrent.CountDownLatch) URI(java.net.URI) ClientUnsupportedOperationException(org.apache.qpid.protonj2.client.exceptions.ClientUnsupportedOperationException) ClientException(org.apache.qpid.protonj2.client.exceptions.ClientException) ClientIllegalStateException(org.apache.qpid.protonj2.client.exceptions.ClientIllegalStateException) IOException(java.io.IOException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) StreamSender(org.apache.qpid.protonj2.client.StreamSender) StreamSenderMessage(org.apache.qpid.protonj2.client.StreamSenderMessage) Client(org.apache.qpid.protonj2.client.Client) Test(org.junit.jupiter.api.Test)

Example 4 with Tracker

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

the class StreamSenderTest method doTestStreamMessageSendFromByteArrayInputStream.

private void doTestStreamMessageSendFromByteArrayInputStream(boolean setBodySize) throws Exception {
    final Random random = new Random(System.nanoTime());
    final byte[] array = new byte[4096];
    final ByteArrayInputStream bytesIn = new ByteArrayInputStream(array);
    // Populate the array with something other than zeros.
    random.nextBytes(array);
    EncodedCompositingDataSectionMatcher matcher = new EncodedCompositingDataSectionMatcher(array);
    try (ProtonTestServer peer = new ProtonTestServer()) {
        peer.expectSASLAnonymousConnect();
        peer.expectOpen().respond();
        peer.expectBegin().respond();
        peer.expectAttach().ofSender().respond();
        peer.remoteFlow().withLinkCredit(100).queue();
        for (int i = 0; i < (array.length / 1023); ++i) {
            peer.expectTransfer().withDeliveryId(0).withMore(true).withPayload(matcher);
        }
        // A small number of trailing bytes will be transmitted in the final frame.
        peer.expectTransfer().withDeliveryId(0).withMore(false).withPayload(matcher);
        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());
        StreamSenderOptions options = new StreamSenderOptions().writeBufferSize(1023);
        StreamSender sender = connection.openStreamSender("test-queue", options);
        StreamSenderMessage tracker = sender.beginMessage();
        final OutputStream stream;
        if (setBodySize) {
            stream = tracker.body(new OutputStreamOptions().bodyLength(array.length));
        } else {
            stream = tracker.body();
        }
        try {
            bytesIn.transferTo(stream);
        } finally {
            // Ensure any trailing bytes get written and transfer marked as done.
            stream.close();
        }
        peer.waitForScriptToComplete();
        peer.expectDetach().respond();
        peer.expectEnd().respond();
        peer.expectClose().respond();
        sender.close();
        connection.close();
        peer.waitForScriptToComplete(5, TimeUnit.SECONDS);
    }
}
Also used : ProtonTestServer(org.apache.qpid.protonj2.test.driver.ProtonTestServer) ByteArrayOutputStream(java.io.ByteArrayOutputStream) OutputStream(java.io.OutputStream) Connection(org.apache.qpid.protonj2.client.Connection) URI(java.net.URI) StreamSenderOptions(org.apache.qpid.protonj2.client.StreamSenderOptions) Random(java.util.Random) OutputStreamOptions(org.apache.qpid.protonj2.client.OutputStreamOptions) ByteArrayInputStream(java.io.ByteArrayInputStream) EncodedCompositingDataSectionMatcher(org.apache.qpid.protonj2.test.driver.matchers.types.EncodedCompositingDataSectionMatcher) StreamSender(org.apache.qpid.protonj2.client.StreamSender) StreamSenderMessage(org.apache.qpid.protonj2.client.StreamSenderMessage) Client(org.apache.qpid.protonj2.client.Client)

Example 5 with Tracker

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

the class StreamSenderTest method testFlushAfterSecondWriteDoesNotEncodeAMQPHeaderFromConfiguration.

@Test
void testFlushAfterSecondWriteDoesNotEncodeAMQPHeaderFromConfiguration() throws Exception {
    try (ProtonTestServer peer = new ProtonTestServer()) {
        peer.expectSASLAnonymousConnect();
        peer.expectOpen().respond();
        peer.expectBegin().respond();
        peer.expectAttach().ofSender().respond();
        peer.remoteFlow().withLinkCredit(1).queue();
        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());
        StreamSender sender = connection.openStreamSender("test-queue");
        StreamSenderMessage tracker = sender.beginMessage();
        // Populate all Header values
        Header header = new Header();
        header.setDurable(true);
        header.setPriority((byte) 1);
        header.setTimeToLive(65535);
        header.setFirstAcquirer(true);
        header.setDeliveryCount(2);
        tracker.header(header);
        OutputStreamOptions options = new OutputStreamOptions();
        OutputStream stream = tracker.body(options);
        HeaderMatcher headerMatcher = new HeaderMatcher(true);
        headerMatcher.withDurable(true);
        headerMatcher.withPriority((byte) 1);
        headerMatcher.withTtl(65535);
        headerMatcher.withFirstAcquirer(true);
        headerMatcher.withDeliveryCount(2);
        EncodedDataMatcher dataMatcher1 = new EncodedDataMatcher(new byte[] { 0, 1, 2, 3 });
        TransferPayloadCompositeMatcher payloadMatcher1 = new TransferPayloadCompositeMatcher();
        payloadMatcher1.setHeadersMatcher(headerMatcher);
        payloadMatcher1.setMessageContentMatcher(dataMatcher1);
        // Second flush expectation
        EncodedDataMatcher dataMatcher2 = new EncodedDataMatcher(new byte[] { 4, 5, 6, 7 });
        TransferPayloadCompositeMatcher payloadMatcher2 = new TransferPayloadCompositeMatcher();
        payloadMatcher2.setMessageContentMatcher(dataMatcher2);
        peer.waitForScriptToComplete(5, TimeUnit.SECONDS);
        peer.expectTransfer().withPayload(payloadMatcher1).withMore(true);
        peer.expectTransfer().withPayload(payloadMatcher2).withMore(true);
        peer.expectTransfer().withNullPayload().withMore(false).accept();
        peer.expectDetach().respond();
        peer.expectEnd().respond();
        peer.expectClose().respond();
        // Stream won't output until some body bytes are written.
        stream.write(new byte[] { 0, 1, 2, 3 });
        stream.flush();
        // Next write should only be a single Data section
        stream.write(new byte[] { 4, 5, 6, 7 });
        stream.flush();
        // Final Transfer that completes the Delivery
        stream.close();
        sender.closeAsync().get();
        connection.closeAsync().get();
        peer.waitForScriptToComplete(5, TimeUnit.SECONDS);
    }
}
Also used : HeaderMatcher(org.apache.qpid.protonj2.test.driver.matchers.messaging.HeaderMatcher) ProtonTestServer(org.apache.qpid.protonj2.test.driver.ProtonTestServer) Header(org.apache.qpid.protonj2.types.messaging.Header) OutputStreamOptions(org.apache.qpid.protonj2.client.OutputStreamOptions) ByteArrayOutputStream(java.io.ByteArrayOutputStream) OutputStream(java.io.OutputStream) Connection(org.apache.qpid.protonj2.client.Connection) EncodedDataMatcher(org.apache.qpid.protonj2.test.driver.matchers.types.EncodedDataMatcher) StreamSender(org.apache.qpid.protonj2.client.StreamSender) StreamSenderMessage(org.apache.qpid.protonj2.client.StreamSenderMessage) Client(org.apache.qpid.protonj2.client.Client) TransferPayloadCompositeMatcher(org.apache.qpid.protonj2.test.driver.matchers.transport.TransferPayloadCompositeMatcher) URI(java.net.URI) Test(org.junit.jupiter.api.Test)

Aggregations

Client (org.apache.qpid.protonj2.client.Client)56 Connection (org.apache.qpid.protonj2.client.Connection)56 URI (java.net.URI)55 ProtonTestServer (org.apache.qpid.protonj2.test.driver.ProtonTestServer)55 Test (org.junit.jupiter.api.Test)46 Tracker (org.apache.qpid.protonj2.client.Tracker)41 Sender (org.apache.qpid.protonj2.client.Sender)39 Session (org.apache.qpid.protonj2.client.Session)32 TransferPayloadCompositeMatcher (org.apache.qpid.protonj2.test.driver.matchers.transport.TransferPayloadCompositeMatcher)28 SenderOptions (org.apache.qpid.protonj2.client.SenderOptions)20 StreamSender (org.apache.qpid.protonj2.client.StreamSender)20 EncodedAmqpValueMatcher (org.apache.qpid.protonj2.test.driver.matchers.types.EncodedAmqpValueMatcher)13 ByteArrayOutputStream (java.io.ByteArrayOutputStream)12 OutputStream (java.io.OutputStream)12 StreamSenderMessage (org.apache.qpid.protonj2.client.StreamSenderMessage)12 ClientException (org.apache.qpid.protonj2.client.exceptions.ClientException)11 HeaderMatcher (org.apache.qpid.protonj2.test.driver.matchers.messaging.HeaderMatcher)11 EncodedDataMatcher (org.apache.qpid.protonj2.test.driver.matchers.types.EncodedDataMatcher)10 OutputStreamOptions (org.apache.qpid.protonj2.client.OutputStreamOptions)9 CountDownLatch (java.util.concurrent.CountDownLatch)8