Search in sources :

Example 1 with DeliveryAnnotations

use of org.apache.qpid.protonj2.test.driver.codec.messaging.DeliveryAnnotations 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 DeliveryAnnotations

use of org.apache.qpid.protonj2.test.driver.codec.messaging.DeliveryAnnotations in project qpid-protonj2 by apache.

the class StreamSenderTest method testStreamSenderMessageWithDeliveryAnnotations.

@Test
public void testStreamSenderMessageWithDeliveryAnnotations() throws Exception {
    try (ProtonTestServer peer = new ProtonTestServer()) {
        peer.expectSASLAnonymousConnect();
        peer.expectOpen().respond();
        peer.expectBegin().respond();
        peer.expectAttach().ofSender().respond();
        peer.remoteFlow().withLinkCredit(10).queue();
        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();
        // Populate delivery annotations
        final Map<String, Object> deliveryAnnotations = new HashMap<>();
        deliveryAnnotations.put("da1", 1);
        deliveryAnnotations.put("da2", 2);
        deliveryAnnotations.put("da3", 3);
        StreamSender sender = connection.openStreamSender("test-queue");
        StreamSenderMessage message = sender.beginMessage(deliveryAnnotations);
        final byte[] payload = new byte[] { 0, 1, 2, 3, 4, 5 };
        HeaderMatcher headerMatcher = new HeaderMatcher(true);
        headerMatcher.withDurable(true);
        headerMatcher.withPriority((byte) 1);
        headerMatcher.withTtl(65535);
        headerMatcher.withFirstAcquirer(true);
        headerMatcher.withDeliveryCount(2);
        PropertiesMatcher propertiesMatcher = new PropertiesMatcher(true);
        propertiesMatcher.withMessageId("ID:12345");
        propertiesMatcher.withUserId("user".getBytes(StandardCharsets.UTF_8));
        propertiesMatcher.withTo("the-management");
        propertiesMatcher.withSubject("amqp");
        propertiesMatcher.withReplyTo("the-minions");
        propertiesMatcher.withCorrelationId("abc");
        propertiesMatcher.withContentEncoding("application/json");
        propertiesMatcher.withContentType("gzip");
        propertiesMatcher.withAbsoluteExpiryTime(123);
        propertiesMatcher.withCreationTime(1);
        propertiesMatcher.withGroupId("disgruntled");
        propertiesMatcher.withGroupSequence(8192);
        propertiesMatcher.withReplyToGroupId("/dev/null");
        DeliveryAnnotationsMatcher daMatcher = new DeliveryAnnotationsMatcher(true);
        daMatcher.withEntry("da1", Matchers.equalTo(1));
        daMatcher.withEntry("da2", Matchers.equalTo(2));
        daMatcher.withEntry("da3", Matchers.equalTo(3));
        MessageAnnotationsMatcher maMatcher = new MessageAnnotationsMatcher(true);
        maMatcher.withEntry("ma1", Matchers.equalTo(1));
        maMatcher.withEntry("ma2", Matchers.equalTo(2));
        maMatcher.withEntry("ma3", Matchers.equalTo(3));
        ApplicationPropertiesMatcher apMatcher = new ApplicationPropertiesMatcher(true);
        apMatcher.withEntry("ap1", Matchers.equalTo(1));
        apMatcher.withEntry("ap2", Matchers.equalTo(2));
        apMatcher.withEntry("ap3", Matchers.equalTo(3));
        EncodedDataMatcher bodyMatcher = new EncodedDataMatcher(payload);
        TransferPayloadCompositeMatcher payloadMatcher = new TransferPayloadCompositeMatcher();
        payloadMatcher.setHeadersMatcher(headerMatcher);
        payloadMatcher.setDeliveryAnnotationsMatcher(daMatcher);
        payloadMatcher.setMessageAnnotationsMatcher(maMatcher);
        payloadMatcher.setPropertiesMatcher(propertiesMatcher);
        payloadMatcher.setApplicationPropertiesMatcher(apMatcher);
        payloadMatcher.setMessageContentMatcher(bodyMatcher);
        peer.waitForScriptToComplete(5, TimeUnit.SECONDS);
        peer.expectTransfer().withPayload(payloadMatcher).withMore(false).accept();
        // Populate all Header values
        message.durable(true);
        assertEquals(true, message.durable());
        message.priority((byte) 1);
        assertEquals(1, message.priority());
        message.timeToLive(65535);
        assertEquals(65535, message.timeToLive());
        message.firstAcquirer(true);
        assertTrue(message.firstAcquirer());
        message.deliveryCount(2);
        assertEquals(2, message.deliveryCount());
        // Populate message annotations
        assertFalse(message.hasAnnotations());
        assertFalse(message.hasAnnotation("ma1"));
        message.annotation("ma1", 1);
        assertTrue(message.hasAnnotation("ma1"));
        assertEquals(1, message.annotation("ma1"));
        message.annotation("ma2", 2);
        assertEquals(2, message.annotation("ma2"));
        message.annotation("ma3", 3);
        assertEquals(3, message.annotation("ma3"));
        assertTrue(message.hasAnnotations());
        // Populate all Properties values
        message.messageId("ID:12345");
        assertEquals("ID:12345", message.messageId());
        message.userId("user".getBytes(StandardCharsets.UTF_8));
        assertArrayEquals("user".getBytes(StandardCharsets.UTF_8), message.userId());
        message.to("the-management");
        assertEquals("the-management", message.to());
        message.subject("amqp");
        assertEquals("amqp", message.subject());
        message.replyTo("the-minions");
        assertEquals("the-minions", message.replyTo());
        message.correlationId("abc");
        assertEquals("abc", message.correlationId());
        message.contentEncoding("application/json");
        assertEquals("application/json", message.contentEncoding());
        message.contentType("gzip");
        assertEquals("gzip", message.contentType());
        message.absoluteExpiryTime(123);
        assertEquals(123, message.absoluteExpiryTime());
        message.creationTime(1);
        assertEquals(1, message.creationTime());
        message.groupId("disgruntled");
        assertEquals("disgruntled", message.groupId());
        message.groupSequence(8192);
        assertEquals(8192, message.groupSequence());
        message.replyToGroupId("/dev/null");
        assertEquals("/dev/null", message.replyToGroupId());
        // Populate message application properties
        assertFalse(message.hasProperties());
        assertFalse(message.hasProperty("ma1"));
        message.property("ap1", 1);
        assertEquals(1, message.property("ap1"));
        assertTrue(message.hasProperty("ap1"));
        message.property("ap2", 2);
        assertEquals(2, message.property("ap2"));
        message.property("ap3", 3);
        assertEquals(3, message.property("ap3"));
        assertTrue(message.hasProperties());
        OutputStream stream = message.body();
        stream.write(payload);
        stream.close();
        peer.waitForScriptToComplete(5, TimeUnit.SECONDS);
        peer.expectDetach().respond();
        peer.expectEnd().respond();
        peer.expectClose().respond();
        assertNotNull(message.tracker());
        Wait.assertTrue(() -> message.tracker().settlementFuture().isDone());
        assertTrue(message.tracker().settlementFuture().get().settled());
        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) DeliveryAnnotationsMatcher(org.apache.qpid.protonj2.test.driver.matchers.messaging.DeliveryAnnotationsMatcher) MessageAnnotationsMatcher(org.apache.qpid.protonj2.test.driver.matchers.messaging.MessageAnnotationsMatcher) HashMap(java.util.HashMap) 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) ApplicationPropertiesMatcher(org.apache.qpid.protonj2.test.driver.matchers.messaging.ApplicationPropertiesMatcher) StreamSender(org.apache.qpid.protonj2.client.StreamSender) StreamSenderMessage(org.apache.qpid.protonj2.client.StreamSenderMessage) Client(org.apache.qpid.protonj2.client.Client) PropertiesMatcher(org.apache.qpid.protonj2.test.driver.matchers.messaging.PropertiesMatcher) ApplicationPropertiesMatcher(org.apache.qpid.protonj2.test.driver.matchers.messaging.ApplicationPropertiesMatcher) Test(org.junit.jupiter.api.Test)

Example 3 with DeliveryAnnotations

use of org.apache.qpid.protonj2.test.driver.codec.messaging.DeliveryAnnotations in project qpid-protonj2 by apache.

the class MessageSendTest method testSendMessageWithDeliveryAnnotationsPopulated.

@Test
public void testSendMessageWithDeliveryAnnotationsPopulated() throws Exception {
    try (ProtonTestServer peer = new ProtonTestServer()) {
        peer.expectSASLAnonymousConnect();
        peer.expectOpen().respond();
        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();
        SenderOptions options = new SenderOptions().deliveryMode(DeliveryMode.AT_MOST_ONCE);
        Sender sender = session.openSender("test-qos", options);
        // Gates send on remote flow having been sent and received
        session.openReceiver("dummy").openFuture().get();
        DeliveryAnnotationsMatcher daMatcher = new DeliveryAnnotationsMatcher(true);
        daMatcher.withEntry("one", Matchers.equalTo(1));
        daMatcher.withEntry("two", Matchers.equalTo(2));
        daMatcher.withEntry("three", Matchers.equalTo(3));
        EncodedAmqpValueMatcher bodyMatcher = new EncodedAmqpValueMatcher("Hello World");
        TransferPayloadCompositeMatcher payloadMatcher = new TransferPayloadCompositeMatcher();
        payloadMatcher.setDeliveryAnnotationsMatcher(daMatcher);
        payloadMatcher.setMessageContentMatcher(bodyMatcher);
        peer.waitForScriptToComplete(5, TimeUnit.SECONDS);
        peer.expectTransfer().withPayload(payloadMatcher).accept();
        peer.expectDetach().respond();
        peer.expectClose().respond();
        final Message<String> message = Message.create("Hello World");
        // Populate delivery annotations
        Map<String, Object> deliveryAnnotations = new HashMap<>();
        deliveryAnnotations.put("one", 1);
        deliveryAnnotations.put("two", 2);
        deliveryAnnotations.put("three", 3);
        final Tracker tracker = sender.send(message, deliveryAnnotations);
        assertNotNull(tracker);
        assertNotNull(tracker.settlementFuture().isDone());
        assertNotNull(tracker.settlementFuture().get().settled());
        sender.closeAsync().get(10, TimeUnit.SECONDS);
        connection.closeAsync().get(10, TimeUnit.SECONDS);
        peer.waitForScriptToComplete(5, TimeUnit.SECONDS);
    }
}
Also used : Tracker(org.apache.qpid.protonj2.client.Tracker) ProtonTestServer(org.apache.qpid.protonj2.test.driver.ProtonTestServer) SenderOptions(org.apache.qpid.protonj2.client.SenderOptions) DeliveryAnnotationsMatcher(org.apache.qpid.protonj2.test.driver.matchers.messaging.DeliveryAnnotationsMatcher) HashMap(java.util.HashMap) 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) Sender(org.apache.qpid.protonj2.client.Sender) Client(org.apache.qpid.protonj2.client.Client) Session(org.apache.qpid.protonj2.client.Session) Test(org.junit.jupiter.api.Test)

Example 4 with DeliveryAnnotations

use of org.apache.qpid.protonj2.test.driver.codec.messaging.DeliveryAnnotations in project qpid-protonj2 by apache.

the class ClientStreamSender method beginMessage.

@Override
public ClientStreamSenderMessage beginMessage(Map<String, Object> deliveryAnnotations) throws ClientException {
    checkClosedOrFailed();
    final ClientFuture<ClientStreamSenderMessage> request = session.getFutureFactory().createFuture();
    final DeliveryAnnotations annotations;
    if (deliveryAnnotations != null) {
        annotations = new DeliveryAnnotations(StringUtils.toSymbolKeyedMap(deliveryAnnotations));
    } else {
        annotations = null;
    }
    executor.execute(() -> {
        if (protonSender.current() != null) {
            request.failed(new ClientIllegalStateException("Cannot initiate a new streaming send until the previous one is complete"));
        } else {
            // Grab the next delivery and hold for stream writes, no other sends
            // can occur while we hold the delivery.
            final OutgoingDelivery streamDelivery = protonSender.next();
            final ClientStreamTracker streamTracker = createTracker(streamDelivery);
            streamDelivery.setLinkedResource(streamTracker);
            request.complete(new ClientStreamSenderMessage(this, streamTracker, annotations));
        }
    });
    return session.request(this, request);
}
Also used : DeliveryAnnotations(org.apache.qpid.protonj2.types.messaging.DeliveryAnnotations) ClientIllegalStateException(org.apache.qpid.protonj2.client.exceptions.ClientIllegalStateException) OutgoingDelivery(org.apache.qpid.protonj2.engine.OutgoingDelivery)

Example 5 with DeliveryAnnotations

use of org.apache.qpid.protonj2.test.driver.codec.messaging.DeliveryAnnotations in project qpid-protonj2 by apache.

the class DeliveryAnnotationsTypeDecoder method readArrayElements.

@Override
public DeliveryAnnotations[] readArrayElements(ProtonBuffer buffer, DecoderState state, int count) throws DecodeException {
    final TypeDecoder<?> decoder = state.getDecoder().readNextTypeDecoder(buffer, state);
    final DeliveryAnnotations[] result = new DeliveryAnnotations[count];
    if (decoder instanceof NullTypeDecoder) {
        for (int i = 0; i < count; ++i) {
            decoder.readValue(buffer, state);
            result[i] = new DeliveryAnnotations(null);
        }
        return result;
    }
    for (int i = 0; i < count; ++i) {
        result[i] = new DeliveryAnnotations(readMap(buffer, state, checkIsExpectedTypeAndCast(MapTypeDecoder.class, decoder)));
    }
    return result;
}
Also used : NullTypeDecoder(org.apache.qpid.protonj2.codec.decoders.primitives.NullTypeDecoder) DeliveryAnnotations(org.apache.qpid.protonj2.types.messaging.DeliveryAnnotations) MapTypeDecoder(org.apache.qpid.protonj2.codec.decoders.primitives.MapTypeDecoder)

Aggregations

DeliveryAnnotations (org.apache.qpid.protonj2.types.messaging.DeliveryAnnotations)11 ProtonBuffer (org.apache.qpid.protonj2.buffer.ProtonBuffer)10 HashMap (java.util.HashMap)9 InputStream (java.io.InputStream)7 ProtonBufferInputStream (org.apache.qpid.protonj2.buffer.ProtonBufferInputStream)7 URI (java.net.URI)6 Client (org.apache.qpid.protonj2.client.Client)6 Connection (org.apache.qpid.protonj2.client.Connection)6 ProtonTestServer (org.apache.qpid.protonj2.test.driver.ProtonTestServer)6 DeliveryAnnotationsMatcher (org.apache.qpid.protonj2.test.driver.matchers.messaging.DeliveryAnnotationsMatcher)6 TransferPayloadCompositeMatcher (org.apache.qpid.protonj2.test.driver.matchers.transport.TransferPayloadCompositeMatcher)6 Tracker (org.apache.qpid.protonj2.client.Tracker)5 EncodedAmqpValueMatcher (org.apache.qpid.protonj2.test.driver.matchers.types.EncodedAmqpValueMatcher)5 Symbol (org.apache.qpid.protonj2.types.Symbol)5 Sender (org.apache.qpid.protonj2.client.Sender)4 Session (org.apache.qpid.protonj2.client.Session)4 SenderOptions (org.apache.qpid.protonj2.client.SenderOptions)3 ApplicationPropertiesMatcher (org.apache.qpid.protonj2.test.driver.matchers.messaging.ApplicationPropertiesMatcher)3 HeaderMatcher (org.apache.qpid.protonj2.test.driver.matchers.messaging.HeaderMatcher)3 MessageAnnotationsMatcher (org.apache.qpid.protonj2.test.driver.matchers.messaging.MessageAnnotationsMatcher)3