Search in sources :

Example 1 with StreamSenderOptions

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

the class ClientSenderBuilder method streamSender.

public ClientStreamSender streamSender(String address, StreamSenderOptions senderOptions) throws ClientException {
    final StreamSenderOptions options = senderOptions != null ? senderOptions : getDefaultStreamSenderOptions();
    final String senderId = nextSenderId();
    final Sender protonSender = createSender(session.getProtonSession(), address, options, senderId);
    return new ClientStreamSender(session, options, senderId, protonSender);
}
Also used : Sender(org.apache.qpid.protonj2.engine.Sender) StreamSenderOptions(org.apache.qpid.protonj2.client.StreamSenderOptions)

Example 2 with StreamSenderOptions

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

the class ClientConnection method openStreamSender.

@Override
public StreamSender openStreamSender(String address, StreamSenderOptions senderOptions) throws ClientException {
    checkClosedOrFailed();
    Objects.requireNonNull(address, "Cannot create a sender with a null address");
    final ClientFuture<StreamSender> createRequest = getFutureFactory().createFuture();
    executor.execute(() -> {
        try {
            int sessionCapacity = StreamSenderOptions.DEFAULT_PENDING_WRITES_BUFFER_SIZE;
            if (senderOptions != null) {
                sessionCapacity = senderOptions.pendingWritesBufferSize();
            }
            // 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.outgoingCapacity(sessionCapacity)).open();
            createRequest.complete(session.internalOpenStreamSender(address, senderOptions));
        } catch (Throwable error) {
            createRequest.failed(ClientExceptionSupport.createNonFatalOrPassthrough(error));
        }
    });
    return request(this, createRequest);
}
Also used : SessionOptions(org.apache.qpid.protonj2.client.SessionOptions) StreamSender(org.apache.qpid.protonj2.client.StreamSender)

Example 3 with StreamSenderOptions

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

the class StreamSenderTest method testSendCustomMessageWithMultipleAmqpValueSections.

@Test
public void testSendCustomMessageWithMultipleAmqpValueSections() throws Exception {
    try (ProtonTestServer peer = new ProtonTestServer()) {
        peer.expectSASLAnonymousConnect();
        peer.expectOpen().respond();
        peer.expectBegin().respond();
        // Hidden session for stream sender
        peer.expectBegin().respond();
        peer.expectAttach().ofSender().respond();
        peer.remoteFlow().withLinkCredit(10).queue();
        // Open a receiver to ensure sender link has processed
        peer.expectAttach().respond();
        // the inbound flow frame we sent previously before send.
        peer.expectFlow();
        peer.start();
        URI remoteURI = peer.getServerURI();
        LOG.info("Sender test started, peer listening on: {}", remoteURI);
        Client container = Client.create();
        Connection connection = container.connect(remoteURI.getHost(), remoteURI.getPort()).openFuture().get();
        Session session = connection.openSession().openFuture().get();
        StreamSenderOptions options = new StreamSenderOptions();
        options.deliveryMode(DeliveryMode.AT_MOST_ONCE);
        options.writeBufferSize(Integer.MAX_VALUE);
        StreamSender sender = connection.openStreamSender("test-qos", options);
        // Create a custom message format send context and ensure that no early buffer writes take place
        StreamSenderMessage message = sender.beginMessage();
        assertEquals(sender, message.sender());
        assertNull(message.tracker());
        assertEquals(Header.DEFAULT_PRIORITY, message.priority());
        assertEquals(Header.DEFAULT_DELIVERY_COUNT, message.deliveryCount());
        assertEquals(Header.DEFAULT_FIRST_ACQUIRER, message.firstAcquirer());
        assertEquals(Header.DEFAULT_TIME_TO_LIVE, message.timeToLive());
        assertEquals(Header.DEFAULT_DURABILITY, message.durable());
        message.messageFormat(17);
        // Gates send on remote flow having been sent and received
        session.openReceiver("dummy").openFuture().get();
        HeaderMatcher headerMatcher = new HeaderMatcher(true);
        headerMatcher.withDurable(true);
        headerMatcher.withPriority((byte) 1);
        headerMatcher.withTtl(65535);
        headerMatcher.withFirstAcquirer(true);
        headerMatcher.withDeliveryCount(2);
        // Note: This is a specification violation but could be used by other message formats
        // and we don't attempt to enforce at the Send Context what users write
        EncodedAmqpValueMatcher bodyMatcher1 = new EncodedAmqpValueMatcher("one", true);
        EncodedAmqpValueMatcher bodyMatcher2 = new EncodedAmqpValueMatcher("two", true);
        EncodedAmqpValueMatcher bodyMatcher3 = new EncodedAmqpValueMatcher("three", false);
        TransferPayloadCompositeMatcher payloadMatcher = new TransferPayloadCompositeMatcher();
        payloadMatcher.setHeadersMatcher(headerMatcher);
        payloadMatcher.addMessageContentMatcher(bodyMatcher1);
        payloadMatcher.addMessageContentMatcher(bodyMatcher2);
        payloadMatcher.addMessageContentMatcher(bodyMatcher3);
        peer.waitForScriptToComplete(5, TimeUnit.SECONDS);
        peer.expectTransfer().withMore(false).withMessageFormat(17).withPayload(payloadMatcher).accept();
        peer.expectDetach().respond();
        peer.expectEnd().respond();
        peer.expectClose().respond();
        // Populate all Header values
        Header header = new Header();
        header.setDurable(true);
        header.setPriority((byte) 1);
        header.setTimeToLive(65535);
        header.setFirstAcquirer(true);
        header.setDeliveryCount(2);
        message.header(header);
        message.addBodySection(new AmqpValue<>("one"));
        message.addBodySection(new AmqpValue<>("two"));
        message.addBodySection(new AmqpValue<>("three"));
        message.complete();
        assertNotNull(message.tracker());
        assertEquals(17, message.messageFormat());
        Wait.assertTrue(() -> message.tracker().settlementFuture().isDone());
        assertTrue(message.tracker().settlementFuture().get().settled());
        assertThrows(ClientIllegalStateException.class, () -> message.addBodySection(new AmqpValue<>("three")));
        assertThrows(ClientIllegalStateException.class, () -> message.body());
        assertThrows(ClientIllegalStateException.class, () -> message.rawOutputStream());
        assertThrows(ClientIllegalStateException.class, () -> message.abort());
        sender.closeAsync().get(10, TimeUnit.SECONDS);
        connection.closeAsync().get(10, TimeUnit.SECONDS);
        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) Connection(org.apache.qpid.protonj2.client.Connection) EncodedAmqpValueMatcher(org.apache.qpid.protonj2.test.driver.matchers.types.EncodedAmqpValueMatcher) TransferPayloadCompositeMatcher(org.apache.qpid.protonj2.test.driver.matchers.transport.TransferPayloadCompositeMatcher) URI(java.net.URI) AmqpValue(org.apache.qpid.protonj2.types.messaging.AmqpValue) StreamSenderOptions(org.apache.qpid.protonj2.client.StreamSenderOptions) Header(org.apache.qpid.protonj2.types.messaging.Header) StreamSender(org.apache.qpid.protonj2.client.StreamSender) StreamSenderMessage(org.apache.qpid.protonj2.client.StreamSenderMessage) Client(org.apache.qpid.protonj2.client.Client) Session(org.apache.qpid.protonj2.client.Session) Test(org.junit.jupiter.api.Test)

Example 4 with StreamSenderOptions

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

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

Aggregations

StreamSenderOptions (org.apache.qpid.protonj2.client.StreamSenderOptions)14 URI (java.net.URI)12 Client (org.apache.qpid.protonj2.client.Client)12 Connection (org.apache.qpid.protonj2.client.Connection)12 ProtonTestServer (org.apache.qpid.protonj2.test.driver.ProtonTestServer)12 StreamSender (org.apache.qpid.protonj2.client.StreamSender)11 Test (org.junit.jupiter.api.Test)11 StreamSenderMessage (org.apache.qpid.protonj2.client.StreamSenderMessage)9 TransferPayloadCompositeMatcher (org.apache.qpid.protonj2.test.driver.matchers.transport.TransferPayloadCompositeMatcher)7 OutputStream (java.io.OutputStream)6 ByteArrayOutputStream (java.io.ByteArrayOutputStream)5 OutputStreamOptions (org.apache.qpid.protonj2.client.OutputStreamOptions)5 HeaderMatcher (org.apache.qpid.protonj2.test.driver.matchers.messaging.HeaderMatcher)5 EncodedDataMatcher (org.apache.qpid.protonj2.test.driver.matchers.types.EncodedDataMatcher)5 Header (org.apache.qpid.protonj2.types.messaging.Header)5 IOException (java.io.IOException)3 Session (org.apache.qpid.protonj2.client.Session)3 EncodedAmqpValueMatcher (org.apache.qpid.protonj2.test.driver.matchers.types.EncodedAmqpValueMatcher)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ArrayList (java.util.ArrayList)1