Search in sources :

Example 1 with SessionOptions

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

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

the class ClientConnection method openStreamReceiver.

@Override
public StreamReceiver openStreamReceiver(String address, StreamReceiverOptions receiverOptions) throws ClientException {
    checkClosedOrFailed();
    final ClientFuture<StreamReceiver> createRequest = getFutureFactory().createFuture();
    executor.execute(() -> {
        try {
            int sessionCapacity = StreamReceiverOptions.DEFAULT_READ_BUFFER_SIZE;
            if (receiverOptions != null) {
                sessionCapacity = receiverOptions.readBufferSize() / 2;
            }
            // 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.incomingCapacity(sessionCapacity)).open();
            createRequest.complete(session.internalOpenStreamReceiver(address, receiverOptions));
        } catch (Throwable error) {
            createRequest.failed(ClientExceptionSupport.createNonFatalOrPassthrough(error));
        }
    });
    return request(this, createRequest);
}
Also used : StreamReceiver(org.apache.qpid.protonj2.client.StreamReceiver) SessionOptions(org.apache.qpid.protonj2.client.SessionOptions)

Example 3 with SessionOptions

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

the class SessionTest method doTestSessionCloseTimeoutWhenNoRemoteEndArrives.

private void doTestSessionCloseTimeoutWhenNoRemoteEndArrives(boolean timeout) throws Exception {
    try (ProtonTestServer peer = new ProtonTestServer()) {
        peer.expectSASLAnonymousConnect();
        peer.expectOpen().respond();
        peer.expectBegin().respond();
        peer.expectEnd();
        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());
        SessionOptions options = new SessionOptions();
        options.closeTimeout(75);
        Session session = connection.openSession(options).openFuture().get();
        try {
            if (timeout) {
                session.closeAsync().get(10, TimeUnit.SECONDS);
            } else {
                session.closeAsync().get();
            }
            fail("Close should throw an error if the Session end doesn't arrive in time");
        } catch (Throwable error) {
            LOG.info("Session close failed with error: ", error);
        }
        connection.closeAsync().get();
        peer.waitForScriptToComplete(5, TimeUnit.SECONDS);
    }
}
Also used : ProtonTestServer(org.apache.qpid.protonj2.test.driver.ProtonTestServer) SessionOptions(org.apache.qpid.protonj2.client.SessionOptions) 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)

Example 4 with SessionOptions

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

the class SessionTest method tryReadSessionRemoteProperties.

private void tryReadSessionRemoteProperties(boolean beginResponse) throws Exception {
    try (ProtonTestServer peer = new ProtonTestServer()) {
        peer.expectSASLAnonymousConnect();
        peer.expectOpen().respond();
        peer.expectBegin();
        peer.start();
        URI remoteURI = peer.getServerURI();
        LOG.info("Connect test started, peer listening on: {}", remoteURI);
        Client container = Client.create();
        Connection connection = container.connect(remoteURI.getHost(), remoteURI.getPort());
        SessionOptions options = new SessionOptions().openTimeout(120, TimeUnit.MILLISECONDS);
        Session session = connection.openSession(options);
        peer.waitForScriptToComplete(5, TimeUnit.SECONDS);
        Map<String, Object> expectedProperties = new HashMap<>();
        expectedProperties.put("TEST", "test-property");
        if (beginResponse) {
            peer.expectEnd().respond();
            peer.respondToLastBegin().withProperties(expectedProperties).later(10);
        } else {
            peer.expectEnd();
        }
        if (beginResponse) {
            assertNotNull(session.properties(), "Remote should have responded with a remote properties value");
            assertEquals(expectedProperties, session.properties());
        } else {
            try {
                session.properties();
                fail("Should failed to get remote state due to no begin response");
            } catch (ClientException ex) {
                LOG.debug("Caught expected exception from blocking call", ex);
            }
        }
        try {
            session.closeAsync().get();
        } catch (ExecutionException ex) {
            LOG.debug("Caught unexpected exception from close call", ex);
            fail("Should not fail to close when connection not closed and end was sent");
        }
        peer.expectClose().respond();
        connection.closeAsync().get();
        peer.waitForScriptToComplete(5, TimeUnit.SECONDS);
    }
}
Also used : ProtonTestServer(org.apache.qpid.protonj2.test.driver.ProtonTestServer) HashMap(java.util.HashMap) SessionOptions(org.apache.qpid.protonj2.client.SessionOptions) Connection(org.apache.qpid.protonj2.client.Connection) ClientException(org.apache.qpid.protonj2.client.exceptions.ClientException) Client(org.apache.qpid.protonj2.client.Client) ExecutionException(java.util.concurrent.ExecutionException) URI(java.net.URI) Session(org.apache.qpid.protonj2.client.Session)

Example 5 with SessionOptions

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

the class SessionTest method tryReadSessionRemoteOfferedCapabilities.

private void tryReadSessionRemoteOfferedCapabilities(boolean beginResponse) throws Exception {
    try (ProtonTestServer peer = new ProtonTestServer()) {
        peer.expectSASLAnonymousConnect();
        peer.expectOpen().respond();
        peer.expectBegin();
        peer.start();
        URI remoteURI = peer.getServerURI();
        LOG.info("Connect test started, peer listening on: {}", remoteURI);
        Client container = Client.create();
        Connection connection = container.connect(remoteURI.getHost(), remoteURI.getPort());
        connection.openFuture().get();
        SessionOptions options = new SessionOptions().openTimeout(125);
        Session session = connection.openSession(options);
        peer.waitForScriptToComplete(5, TimeUnit.SECONDS);
        if (beginResponse) {
            peer.expectEnd().respond();
            peer.respondToLastBegin().withOfferedCapabilities("transactions").later(10);
        } else {
            peer.expectEnd();
        }
        if (beginResponse) {
            assertNotNull(session.offeredCapabilities(), "Remote should have responded with a remote offered Capabilities value");
            assertEquals(1, session.offeredCapabilities().length);
            assertEquals("transactions", session.offeredCapabilities()[0]);
        } else {
            try {
                session.offeredCapabilities();
                fail("Should failed to get remote state due to no begin response");
            } catch (ClientException ex) {
                LOG.debug("Caught expected exception from blocking call", ex);
            }
        }
        try {
            session.closeAsync().get();
        } catch (ExecutionException ex) {
            LOG.debug("Caught unexpected exception from close call", ex);
            fail("Should not fail to close when connection not closed and end was sent");
        }
        peer.expectClose().respond();
        connection.closeAsync().get();
        peer.waitForScriptToComplete(5, TimeUnit.SECONDS);
    }
}
Also used : ProtonTestServer(org.apache.qpid.protonj2.test.driver.ProtonTestServer) SessionOptions(org.apache.qpid.protonj2.client.SessionOptions) Connection(org.apache.qpid.protonj2.client.Connection) ClientException(org.apache.qpid.protonj2.client.exceptions.ClientException) Client(org.apache.qpid.protonj2.client.Client) ExecutionException(java.util.concurrent.ExecutionException) URI(java.net.URI) Session(org.apache.qpid.protonj2.client.Session)

Aggregations

SessionOptions (org.apache.qpid.protonj2.client.SessionOptions)15 URI (java.net.URI)10 Client (org.apache.qpid.protonj2.client.Client)10 Connection (org.apache.qpid.protonj2.client.Connection)10 ProtonTestServer (org.apache.qpid.protonj2.test.driver.ProtonTestServer)10 Session (org.apache.qpid.protonj2.client.Session)9 ExecutionException (java.util.concurrent.ExecutionException)6 ConnectionOptions (org.apache.qpid.protonj2.client.ConnectionOptions)3 ClientException (org.apache.qpid.protonj2.client.exceptions.ClientException)3 Test (org.junit.jupiter.api.Test)3 Receiver (org.apache.qpid.protonj2.client.Receiver)2 Session (org.apache.qpid.protonj2.engine.Session)2 InputStream (java.io.InputStream)1 HashMap (java.util.HashMap)1 Delivery (org.apache.qpid.protonj2.client.Delivery)1 StreamReceiver (org.apache.qpid.protonj2.client.StreamReceiver)1 StreamSender (org.apache.qpid.protonj2.client.StreamSender)1 ClientOperationTimedOutException (org.apache.qpid.protonj2.client.exceptions.ClientOperationTimedOutException)1