use of org.apache.qpid.protonj2.client.StreamReceiverOptions in project qpid-protonj2 by apache.
the class ClientReceiverBuilder method getDefaultStreamReceiverOptions.
/*
* Stream Receiver options used when none specified by the caller creating a new receiver.
*/
private StreamReceiverOptions getDefaultStreamReceiverOptions() {
StreamReceiverOptions receiverOptions = defaultStreamReceiverOptions;
if (receiverOptions == null) {
synchronized (this) {
receiverOptions = defaultStreamReceiverOptions;
if (receiverOptions == null) {
receiverOptions = new StreamReceiverOptions();
receiverOptions.openTimeout(sessionOptions.openTimeout());
receiverOptions.closeTimeout(sessionOptions.closeTimeout());
receiverOptions.requestTimeout(sessionOptions.requestTimeout());
receiverOptions.drainTimeout(sessionOptions.drainTimeout());
}
defaultStreamReceiverOptions = receiverOptions;
}
}
return receiverOptions;
}
use of org.apache.qpid.protonj2.client.StreamReceiverOptions 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);
}
use of org.apache.qpid.protonj2.client.StreamReceiverOptions in project qpid-protonj2 by apache.
the class StreamReceiverTest method doTestStreamReceiverSessionCapacity.
private void doTestStreamReceiverSessionCapacity(int maxFrameSize, int readBufferSize, int expectedSessionWindow) throws Exception {
try (ProtonTestServer peer = new ProtonTestServer()) {
peer.expectSASLAnonymousConnect();
peer.expectOpen().withMaxFrameSize(maxFrameSize).respond();
peer.expectBegin().withIncomingWindow(expectedSessionWindow).respond();
peer.expectAttach().ofReceiver().respond();
peer.expectFlow().withIncomingWindow(expectedSessionWindow);
peer.expectDetach().respond();
peer.expectEnd().respond();
peer.expectClose().respond();
peer.start();
URI remoteURI = peer.getServerURI();
LOG.info("Test started, peer listening on: {}", remoteURI);
Client container = Client.create();
ConnectionOptions connectionOptions = new ConnectionOptions().maxFrameSize(maxFrameSize);
Connection connection = container.connect(remoteURI.getHost(), remoteURI.getPort(), connectionOptions);
StreamReceiverOptions streamOptions = new StreamReceiverOptions().readBufferSize(readBufferSize);
StreamReceiver receiver = connection.openStreamReceiver("test-queue", streamOptions);
receiver.openFuture().get();
receiver.closeAsync().get();
connection.closeAsync().get();
peer.waitForScriptToComplete(5, TimeUnit.SECONDS);
}
}
use of org.apache.qpid.protonj2.client.StreamReceiverOptions in project qpid-protonj2 by apache.
the class StreamReceiverTest method testStreamReadOpensSessionWindowForAdditionalInputAndGrantsCreditOnClose.
@Test
public void testStreamReadOpensSessionWindowForAdditionalInputAndGrantsCreditOnClose() throws Exception {
final byte[] body1 = new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
final byte[] body2 = new byte[] { 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0 };
final byte[] payload1 = createEncodedMessage(new Data(body1));
final byte[] payload2 = createEncodedMessage(new Data(body2));
try (ProtonTestServer peer = new ProtonTestServer()) {
peer.expectSASLAnonymousConnect();
peer.expectOpen().withMaxFrameSize(1000).respond();
peer.expectBegin().withIncomingWindow(1).respond();
peer.expectAttach().ofReceiver().respond();
peer.expectFlow().withIncomingWindow(1).withLinkCredit(1);
peer.remoteTransfer().withHandle(0).withDeliveryId(0).withDeliveryTag(new byte[] { 1 }).withMore(true).withMessageFormat(0).withPayload(payload1).queue();
peer.start();
URI remoteURI = peer.getServerURI();
LOG.info("Test started, peer listening on: {}", remoteURI);
Client container = Client.create();
ConnectionOptions connectionOptions = new ConnectionOptions().maxFrameSize(1000);
Connection connection = container.connect(remoteURI.getHost(), remoteURI.getPort(), connectionOptions);
StreamReceiverOptions streamOptions = new StreamReceiverOptions().readBufferSize(2000).creditWindow(1);
StreamReceiver receiver = connection.openStreamReceiver("test-queue", streamOptions);
StreamDelivery delivery = receiver.receive();
assertNotNull(delivery);
StreamReceiverMessage message = delivery.message();
assertNotNull(message);
// Creating the input stream instance should read the first chunk of data from the incoming
// delivery which should result in a new credit being available to expand the session window.
// An additional transfer should be placed into the delivery buffer but not yet read since
// the user hasn't read anything. Since we are in auto settle the completed transfer should
// trigger settlement and also open the credit window but the session window should not be
// expanded since we haven't read the data yet.
peer.waitForScriptToComplete(5, TimeUnit.SECONDS);
peer.expectFlow().withDeliveryCount(0).withIncomingWindow(1).withLinkCredit(1);
peer.remoteTransfer().withHandle(0).withDeliveryId(0).withMore(false).withMessageFormat(0).withPayload(payload2).queue();
peer.expectDisposition().withSettled(true).withState().accepted();
peer.expectFlow().withDeliveryCount(1).withIncomingWindow(0).withLinkCredit(1);
InputStream bodyStream = message.body();
assertNotNull(bodyStream);
// Once the read of all data completes the session window should be opened
peer.waitForScriptToComplete(5, TimeUnit.SECONDS);
peer.expectFlow().withDeliveryCount(1).withIncomingWindow(1).withLinkCredit(1);
byte[] combinedPayloads = new byte[body1.length + body2.length];
bodyStream.read(combinedPayloads);
peer.waitForScriptToComplete(5, TimeUnit.SECONDS);
// No frames should be triggered by closing the stream since we already auto settled
// and updated the session window on the remote.
assertTrue(Arrays.equals(body1, 0, body1.length, combinedPayloads, 0, body1.length));
assertTrue(Arrays.equals(body2, 0, body2.length, combinedPayloads, body1.length, body1.length + body2.length));
bodyStream.close();
peer.waitForScriptToComplete(5, TimeUnit.SECONDS);
peer.expectDetach().respond();
peer.expectEnd().respond();
peer.expectClose().respond();
receiver.openFuture().get();
receiver.closeAsync().get();
connection.closeAsync().get();
peer.waitForScriptToComplete(5, TimeUnit.SECONDS);
}
}
use of org.apache.qpid.protonj2.client.StreamReceiverOptions in project qpid-protonj2 by apache.
the class StreamReceiverTest method tryReadReceiverTarget.
private void tryReadReceiverTarget(boolean attachResponse) throws Exception {
try (ProtonTestServer peer = new ProtonTestServer()) {
peer.expectSASLAnonymousConnect();
peer.expectOpen().respond();
peer.expectBegin().respond();
peer.expectAttach().withRole(Role.RECEIVER.getValue());
peer.expectFlow();
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());
StreamReceiverOptions options = new StreamReceiverOptions().openTimeout(150, TimeUnit.MILLISECONDS);
StreamReceiver receiver = connection.openStreamReceiver("test-receiver", options);
peer.waitForScriptToComplete(5, TimeUnit.SECONDS);
if (attachResponse) {
peer.expectDetach().respond();
peer.expectEnd().respond();
peer.respondToLastAttach().later(10);
} else {
peer.expectDetach();
peer.expectEnd();
}
if (attachResponse) {
assertNotNull(receiver.target(), "Remote should have responded with a Target value");
} else {
try {
receiver.target();
fail("Should failed to get remote source due to no attach response");
} catch (ClientException ex) {
LOG.debug("Caught expected exception from blocking call", ex);
}
}
try {
receiver.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 detach sent");
}
peer.expectClose().respond();
connection.closeAsync().get();
peer.waitForScriptToComplete(5, TimeUnit.SECONDS);
}
}
Aggregations