Search in sources :

Example 16 with Session

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

the class TransactionsTest method testSendMessageInsideOfTransaction.

@Test
public void testSendMessageInsideOfTransaction() throws Exception {
    final byte[] txnId = new byte[] { 0, 1, 2, 3 };
    try (ProtonTestServer peer = new ProtonTestServer()) {
        peer.expectSASLAnonymousConnect();
        peer.expectOpen().respond();
        peer.expectBegin().respond();
        peer.expectAttach().ofSender().respond();
        peer.remoteFlow().withLinkCredit(1).queue();
        peer.expectCoordinatorAttach().respond();
        peer.remoteFlow().withLinkCredit(2).queue();
        peer.expectDeclare().accept(txnId);
        peer.expectTransfer().withHandle(0).withNonNullPayload().withState().transactional().withTxnId(txnId).and().respond().withState().transactional().withTxnId(txnId).withAccepted().and().withSettled(true);
        peer.expectDischarge().withFail(false).withTxnId(txnId).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();
        Sender sender = session.openSender("address").openFuture().get();
        session.beginTransaction();
        final Tracker tracker = sender.send(Message.create("test-message"));
        assertNotNull(tracker);
        assertNotNull(tracker.settlementFuture().get());
        assertEquals(tracker.remoteState().getType(), DeliveryState.Type.TRANSACTIONAL, "Delivery inside transaction should have Transactional state");
        assertNotNull(tracker.state());
        assertEquals(tracker.state().getType(), DeliveryState.Type.TRANSACTIONAL, "Delivery inside transaction should have Transactional state: " + tracker.state().getType());
        Wait.assertTrue("Delivery in transaction should be locally settled after response", () -> tracker.settled());
        session.commitTransaction();
        session.closeAsync();
        connection.closeAsync().get(10, TimeUnit.SECONDS);
        peer.waitForScriptToComplete(5, TimeUnit.SECONDS);
    }
}
Also used : Sender(org.apache.qpid.protonj2.client.Sender) StreamSender(org.apache.qpid.protonj2.client.StreamSender) Tracker(org.apache.qpid.protonj2.client.Tracker) 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)

Example 17 with Session

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

the class TransactionsTest method testReceiveMessageInsideOfTransaction.

@Test
public void testReceiveMessageInsideOfTransaction() throws Exception {
    final byte[] txnId = new byte[] { 0, 1, 2, 3 };
    try (ProtonTestServer peer = new ProtonTestServer()) {
        peer.expectSASLAnonymousConnect();
        peer.expectOpen().respond();
        peer.expectBegin().respond();
        peer.expectAttach().ofReceiver().respond();
        peer.expectFlow();
        peer.start();
        final URI remoteURI = peer.getServerURI();
        final byte[] payload = createEncodedMessage(new AmqpValue<>("Hello World"));
        LOG.info("Test started, peer listening on: {}", remoteURI);
        Client container = Client.create();
        Connection connection = container.connect(remoteURI.getHost(), remoteURI.getPort());
        Session session = connection.openSession();
        Receiver receiver = session.openReceiver("test-queue").openFuture().get();
        peer.expectCoordinatorAttach().respond();
        peer.remoteFlow().withLinkCredit(2).queue();
        peer.expectDeclare().accept(txnId);
        peer.remoteTransfer().withHandle(0).withDeliveryId(0).withDeliveryTag(new byte[] { 1 }).withMore(false).withMessageFormat(0).withPayload(payload).queue();
        peer.expectDisposition().withSettled(true).withState().transactional().withTxnId(txnId).withAccepted();
        peer.expectDischarge().withFail(false).withTxnId(txnId).accept();
        peer.expectDetach().respond();
        peer.expectClose().respond();
        session.beginTransaction();
        Delivery delivery = receiver.receive(100, TimeUnit.MILLISECONDS);
        assertNotNull(delivery);
        Message<?> received = delivery.message();
        assertNotNull(received);
        assertTrue(received.body() instanceof String);
        String value = (String) received.body();
        assertEquals("Hello World", value);
        session.commitTransaction();
        receiver.closeAsync();
        connection.closeAsync().get();
        peer.waitForScriptToComplete(5, TimeUnit.SECONDS);
    }
}
Also used : ProtonTestServer(org.apache.qpid.protonj2.test.driver.ProtonTestServer) Connection(org.apache.qpid.protonj2.client.Connection) Receiver(org.apache.qpid.protonj2.client.Receiver) Delivery(org.apache.qpid.protonj2.client.Delivery) 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 18 with Session

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

the class ReconnectTest method testConnectionNotifiesReconnectionLifecycleEvents.

@Test
public void testConnectionNotifiesReconnectionLifecycleEvents() throws Exception {
    try (ProtonTestServer firstPeer = new ProtonTestServer();
        ProtonTestServer finalPeer = new ProtonTestServer()) {
        firstPeer.expectSASLAnonymousConnect();
        firstPeer.expectOpen().withContainerId(any(String.class)).respond();
        firstPeer.dropAfterLastHandler(5);
        firstPeer.start();
        finalPeer.expectSASLAnonymousConnect();
        finalPeer.expectOpen().withContainerId(any(String.class)).respond();
        finalPeer.start();
        final URI primaryURI = firstPeer.getServerURI();
        final URI backupURI = finalPeer.getServerURI();
        final CountDownLatch connected = new CountDownLatch(1);
        final CountDownLatch disconnected = new CountDownLatch(1);
        final CountDownLatch reconnected = new CountDownLatch(1);
        final CountDownLatch failed = new CountDownLatch(1);
        ConnectionOptions options = new ConnectionOptions();
        options.reconnectOptions().reconnectEnabled(true);
        options.reconnectOptions().maxReconnectAttempts(5);
        options.reconnectOptions().reconnectDelay(10);
        options.reconnectOptions().useReconnectBackOff(false);
        options.reconnectOptions().addReconnectLocation(backupURI.getHost(), backupURI.getPort());
        options.connectedHandler((connection, context) -> {
            connected.countDown();
        });
        options.interruptedHandler((connection, context) -> {
            disconnected.countDown();
        });
        options.reconnectedHandler((connection, context) -> {
            reconnected.countDown();
        });
        options.disconnectedHandler((connection, context) -> {
            failed.countDown();
        });
        Client container = Client.create();
        Connection connection = container.connect(primaryURI.getHost(), primaryURI.getPort(), options);
        firstPeer.waitForScriptToComplete();
        connection.openFuture().get();
        finalPeer.waitForScriptToComplete();
        finalPeer.expectBegin().respond();
        finalPeer.expectEnd().respond();
        finalPeer.dropAfterLastHandler(10);
        Session session = connection.openSession().openFuture().get();
        session.close();
        finalPeer.waitForScriptToComplete();
        assertTrue(connected.await(5, TimeUnit.SECONDS));
        assertTrue(disconnected.await(5, TimeUnit.SECONDS));
        assertTrue(reconnected.await(5, TimeUnit.SECONDS));
        assertTrue(failed.await(5, TimeUnit.SECONDS));
        connection.close();
        finalPeer.waitForScriptToComplete();
    }
}
Also used : ProtonTestServer(org.apache.qpid.protonj2.test.driver.ProtonTestServer) Connection(org.apache.qpid.protonj2.client.Connection) ConnectionOptions(org.apache.qpid.protonj2.client.ConnectionOptions) CountDownLatch(java.util.concurrent.CountDownLatch) 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 19 with Session

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

the class SenderTest method testRemotelyCloseSenderLinkWithRedirect.

@Test
public void testRemotelyCloseSenderLinkWithRedirect() throws Exception {
    final String redirectVhost = "vhost";
    final String redirectNetworkHost = "localhost";
    final String redirectAddress = "redirect-queue";
    final int redirectPort = 5677;
    final String redirectScheme = "wss";
    final String redirectPath = "/websockets";
    // Tell the test peer to close the connection when executing its last handler
    final Map<String, Object> errorInfo = new HashMap<>();
    errorInfo.put(ClientConstants.OPEN_HOSTNAME.toString(), redirectVhost);
    errorInfo.put(ClientConstants.NETWORK_HOST.toString(), redirectNetworkHost);
    errorInfo.put(ClientConstants.PORT.toString(), redirectPort);
    errorInfo.put(ClientConstants.SCHEME.toString(), redirectScheme);
    errorInfo.put(ClientConstants.PATH.toString(), redirectPath);
    errorInfo.put(ClientConstants.ADDRESS.toString(), redirectAddress);
    try (ProtonTestServer peer = new ProtonTestServer()) {
        peer.expectSASLAnonymousConnect();
        peer.expectOpen().respond();
        peer.expectBegin().respond();
        peer.expectAttach().ofSender().respond().withNullTarget();
        peer.remoteDetach().withClosed(true).withErrorCondition(LinkError.REDIRECT.toString(), "Not accepting links here", errorInfo).queue();
        peer.expectDetach();
        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();
        Sender sender = session.openSender("test-queue");
        try {
            sender.openFuture().get();
            fail("Should not be able to create sender since the remote is redirecting.");
        } catch (Exception ex) {
            LOG.debug("Received expected exception from sender open: {}", ex.getMessage());
            Throwable cause = ex.getCause();
            assertTrue(cause instanceof ClientLinkRedirectedException);
            ClientLinkRedirectedException linkRedirect = (ClientLinkRedirectedException) ex.getCause();
            assertEquals(redirectVhost, linkRedirect.getHostname());
            assertEquals(redirectNetworkHost, linkRedirect.getNetworkHost());
            assertEquals(redirectPort, linkRedirect.getPort());
            assertEquals(redirectScheme, linkRedirect.getScheme());
            assertEquals(redirectPath, linkRedirect.getPath());
            assertEquals(redirectAddress, linkRedirect.getAddress());
            URI redirect = linkRedirect.getRedirectionURI();
            assertEquals(redirectNetworkHost, redirect.getHost());
            assertEquals(redirectPort, redirect.getPort());
            assertEquals(redirectScheme, redirect.getScheme());
            assertEquals(redirectPath, redirect.getPath());
        }
        connection.close();
        peer.waitForScriptToComplete(5, TimeUnit.SECONDS);
    }
}
Also used : ProtonTestServer(org.apache.qpid.protonj2.test.driver.ProtonTestServer) HashMap(java.util.HashMap) Connection(org.apache.qpid.protonj2.client.Connection) URI(java.net.URI) ClientDeliveryStateException(org.apache.qpid.protonj2.client.exceptions.ClientDeliveryStateException) ClientResourceRemotelyClosedException(org.apache.qpid.protonj2.client.exceptions.ClientResourceRemotelyClosedException) ClientLinkRemotelyClosedException(org.apache.qpid.protonj2.client.exceptions.ClientLinkRemotelyClosedException) ClientOperationTimedOutException(org.apache.qpid.protonj2.client.exceptions.ClientOperationTimedOutException) ClientUnsupportedOperationException(org.apache.qpid.protonj2.client.exceptions.ClientUnsupportedOperationException) ClientException(org.apache.qpid.protonj2.client.exceptions.ClientException) ClientConnectionRemotelyClosedException(org.apache.qpid.protonj2.client.exceptions.ClientConnectionRemotelyClosedException) ClientSendTimedOutException(org.apache.qpid.protonj2.client.exceptions.ClientSendTimedOutException) ClientLinkRedirectedException(org.apache.qpid.protonj2.client.exceptions.ClientLinkRedirectedException) ExecutionException(java.util.concurrent.ExecutionException) Sender(org.apache.qpid.protonj2.client.Sender) ClientLinkRedirectedException(org.apache.qpid.protonj2.client.exceptions.ClientLinkRedirectedException) Client(org.apache.qpid.protonj2.client.Client) Session(org.apache.qpid.protonj2.client.Session) Test(org.junit.jupiter.api.Test)

Example 20 with Session

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

the class SenderTest method doTestCloseOrDetachWithErrorCondition.

public void doTestCloseOrDetachWithErrorCondition(boolean close) throws Exception {
    final String condition = "amqp:link:detach-forced";
    final String description = "something bad happened.";
    try (ProtonTestServer peer = new ProtonTestServer()) {
        peer.expectSASLAnonymousConnect();
        peer.expectOpen().respond();
        peer.expectBegin().respond();
        peer.expectAttach().ofSender().respond();
        peer.expectDetach().withClosed(close).withError(condition, description).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();
        Sender sender = session.openSender("test-sender");
        sender.openFuture().get();
        if (close) {
            sender.closeAsync(ErrorCondition.create(condition, description, null));
        } else {
            sender.detachAsync(ErrorCondition.create(condition, description, null));
        }
        connection.closeAsync().get();
        peer.waitForScriptToComplete(5, TimeUnit.SECONDS);
    }
}
Also used : Sender(org.apache.qpid.protonj2.client.Sender) 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)

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