Search in sources :

Example 1 with Session

use of com.swiftmq.amqp.v100.client.Session in project qpid-protonj2 by apache.

the class ClientLocalTransactionContext method handleTransactionDischargeFailed.

private void handleTransactionDischargeFailed(Transaction<TransactionController> transaction) {
    ClientFuture<Session> future = transaction.getAttachments().get(DISCHARGE_FUTURE_NAME);
    LOG.trace("Discharge of transaction:{} failed", transaction);
    ClientException cause = ClientExceptionSupport.convertToNonFatalException(transaction.getCondition());
    future.failed(new ClientTransactionRolledBackException(cause.getMessage(), cause));
}
Also used : ClientTransactionRolledBackException(org.apache.qpid.protonj2.client.exceptions.ClientTransactionRolledBackException) ClientException(org.apache.qpid.protonj2.client.exceptions.ClientException) Session(org.apache.qpid.protonj2.client.Session)

Example 2 with Session

use of com.swiftmq.amqp.v100.client.Session in project qpid-protonj2 by apache.

the class TransactedReceiver method main.

public static void main(String[] args) throws Exception {
    final String serverHost = System.getProperty("HOST", "localhost");
    final int serverPort = Integer.getInteger("PORT", 5672);
    final String address = System.getProperty("ADDRESS", "transaction-example");
    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)) {
        Session session = connection.openSession();
        Receiver receiver = session.openReceiver(address);
        session.beginTransaction();
        Delivery delivery = receiver.receive();
        Message<String> message = delivery.message();
        System.out.println("Received message with body: " + message.body());
        session.commitTransaction();
    }
}
Also used : Connection(org.apache.qpid.protonj2.client.Connection) Receiver(org.apache.qpid.protonj2.client.Receiver) ConnectionOptions(org.apache.qpid.protonj2.client.ConnectionOptions) Delivery(org.apache.qpid.protonj2.client.Delivery) Client(org.apache.qpid.protonj2.client.Client) Session(org.apache.qpid.protonj2.client.Session)

Example 3 with Session

use of com.swiftmq.amqp.v100.client.Session 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 Session

use of com.swiftmq.amqp.v100.client.Session in project qpid-protonj2 by apache.

the class TransactionsTest method testExceptionOnCommitWhenCoordinatorRejectsDischarge.

@Test
public void testExceptionOnCommitWhenCoordinatorRejectsDischarge() throws Exception {
    final String errorMessage = "Transaction aborted due to timeout";
    final byte[] txnId1 = new byte[] { 0, 1, 2, 3 };
    final byte[] txnId2 = new byte[] { 1, 1, 2, 3 };
    try (ProtonTestServer peer = new ProtonTestServer()) {
        peer.expectSASLAnonymousConnect();
        peer.expectOpen().respond();
        peer.expectBegin().respond();
        peer.expectCoordinatorAttach().respond();
        peer.remoteFlow().withLinkCredit(4).queue();
        peer.expectDeclare().accept(txnId1);
        peer.expectDischarge().withFail(false).withTxnId(txnId1).reject(TransactionErrors.TRANSACTION_TIMEOUT.toString(), "Transaction aborted due to timeout");
        peer.expectDeclare().accept(txnId2);
        peer.expectDischarge().withFail(true).withTxnId(txnId2).accept();
        peer.expectEnd().respond();
        peer.expectClose().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());
        Session session = connection.openSession().openFuture().get();
        session.beginTransaction();
        try {
            session.commitTransaction();
            fail("Commit should have failed after link closed.");
        } catch (ClientTransactionRolledBackException expected) {
            // Expect this to time out.
            String message = expected.getMessage();
            assertTrue(message.contains(errorMessage));
        }
        session.beginTransaction();
        session.rollbackTransaction();
        session.closeAsync();
        connection.closeAsync().get();
        peer.waitForScriptToComplete(5, TimeUnit.SECONDS);
    }
}
Also used : ProtonTestServer(org.apache.qpid.protonj2.test.driver.ProtonTestServer) ClientTransactionRolledBackException(org.apache.qpid.protonj2.client.exceptions.ClientTransactionRolledBackException) Connection(org.apache.qpid.protonj2.client.Connection) Client(org.apache.qpid.protonj2.client.Client) URI(java.net.URI) Session(org.apache.qpid.protonj2.client.Session) Test(org.junit.jupiter.api.Test)

Example 5 with Session

use of com.swiftmq.amqp.v100.client.Session in project qpid-protonj2 by apache.

the class TransactionsTest method testBeginTransactionAndClose.

/**
 * Create a transaction and then close the Session which result in the remote rolling back
 * the transaction by default so the client doesn't manually roll it back itself.
 *
 * @throws Exception
 */
@Test
public void testBeginTransactionAndClose() throws Exception {
    try (ProtonTestServer peer = new ProtonTestServer()) {
        peer.expectSASLAnonymousConnect();
        peer.expectOpen().respond();
        peer.expectBegin().respond();
        peer.expectCoordinatorAttach().respond();
        peer.remoteFlow().withLinkCredit(2).queue();
        peer.expectDeclare().accept();
        peer.expectEnd().respond();
        peer.expectClose().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());
        Session session = connection.openSession().openFuture().get();
        session.beginTransaction();
        session.closeAsync();
        connection.closeAsync().get(10, TimeUnit.SECONDS);
        peer.waitForScriptToComplete(5, TimeUnit.SECONDS);
    }
}
Also used : ProtonTestServer(org.apache.qpid.protonj2.test.driver.ProtonTestServer) Connection(org.apache.qpid.protonj2.client.Connection) Client(org.apache.qpid.protonj2.client.Client) URI(java.net.URI) Session(org.apache.qpid.protonj2.client.Session) Test(org.junit.jupiter.api.Test)

Aggregations

Session (org.apache.qpid.protonj2.client.Session)167 Client (org.apache.qpid.protonj2.client.Client)165 Connection (org.apache.qpid.protonj2.client.Connection)165 URI (java.net.URI)163 ProtonTestServer (org.apache.qpid.protonj2.test.driver.ProtonTestServer)163 Test (org.junit.jupiter.api.Test)109 Sender (org.apache.qpid.protonj2.client.Sender)66 Receiver (org.apache.qpid.protonj2.client.Receiver)64 ExecutionException (java.util.concurrent.ExecutionException)35 ConnectionOptions (org.apache.qpid.protonj2.client.ConnectionOptions)35 Tracker (org.apache.qpid.protonj2.client.Tracker)32 ClientException (org.apache.qpid.protonj2.client.exceptions.ClientException)29 SenderOptions (org.apache.qpid.protonj2.client.SenderOptions)28 ReceiverOptions (org.apache.qpid.protonj2.client.ReceiverOptions)25 TransferPayloadCompositeMatcher (org.apache.qpid.protonj2.test.driver.matchers.transport.TransferPayloadCompositeMatcher)20 ClientConnectionRemotelyClosedException (org.apache.qpid.protonj2.client.exceptions.ClientConnectionRemotelyClosedException)18 HashMap (java.util.HashMap)17 ClientIllegalStateException (org.apache.qpid.protonj2.client.exceptions.ClientIllegalStateException)16 EncodedAmqpValueMatcher (org.apache.qpid.protonj2.test.driver.matchers.types.EncodedAmqpValueMatcher)14 Delivery (org.apache.qpid.protonj2.client.Delivery)13