use of org.apache.qpid.protonj2.client.StreamReceiver in project qpid-protonj2 by apache.
the class StreamingFileReceiver method main.
public static void main(String[] args) throws Exception {
if (args.length == 0) {
System.out.println("Example requires a valid directory where the incoming file should be written");
System.exit(1);
}
final File outputPath = new File(args[0]);
if (!outputPath.isDirectory() || !outputPath.canWrite()) {
System.out.println("Example requires a valid / writable directory to transfer to");
System.exit(1);
}
final String fileNameKey = "filename";
final String serverHost = System.getProperty("HOST", "localhost");
final int serverPort = Integer.getInteger("PORT", 5672);
final String address = System.getProperty("ADDRESS", "file-transfer");
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);
StreamReceiver receiver = connection.openStreamReceiver(address)) {
StreamDelivery delivery = receiver.receive();
StreamReceiverMessage message = delivery.message();
// The remote should have told us the filename of the original file it sent.
String filename = (String) message.property(fileNameKey);
if (filename == null || filename.isBlank()) {
System.out.println("Remote did not include the source filename in the incoming message");
System.exit(1);
} else {
System.out.println("Starting receive of incoming file named: " + filename);
}
try (FileOutputStream outputStream = new FileOutputStream(new File(outputPath, filename))) {
message.body().transferTo(outputStream);
}
System.out.println("Received file written to: " + new File(outputPath, filename));
}
}
use of org.apache.qpid.protonj2.client.StreamReceiver 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.StreamReceiver in project qpid-protonj2 by apache.
the class StreamReceiverTest method testStreamDeliveryReceivedWhileTransferIsIncomplete.
@Test
public void testStreamDeliveryReceivedWhileTransferIsIncomplete() throws Exception {
final byte[] payload = createEncodedMessage(new AmqpValue<>("Hello World"));
try (ProtonTestServer peer = new ProtonTestServer()) {
peer.expectSASLAnonymousConnect();
peer.expectOpen().respond();
peer.expectBegin().respond();
peer.expectAttach().withRole(Role.RECEIVER.getValue()).respond();
peer.expectFlow();
peer.remoteTransfer().withHandle(0).withDeliveryId(0).withDeliveryTag(new byte[] { 1 }).withMore(true).withMessageFormat(0).withPayload(payload).queue();
peer.start();
URI remoteURI = peer.getServerURI();
LOG.info("Test started, peer listening on: {}", remoteURI);
final Client container = Client.create();
final Connection connection = container.connect(remoteURI.getHost(), remoteURI.getPort());
final StreamReceiver receiver = connection.openStreamReceiver("test-queue");
final StreamDelivery delivery = receiver.receive();
assertNotNull(delivery);
assertFalse(delivery.completed());
peer.waitForScriptToComplete(5, TimeUnit.SECONDS);
peer.expectDisposition().withSettled(true);
peer.remoteTransfer().withHandle(0).withDeliveryId(0).withNullDeliveryTag().withMore(false).withMessageFormat(0).withPayload(payload).now();
Wait.assertTrue("Should eventually be marked as completed", delivery::completed);
peer.expectDetach().respond();
peer.expectEnd().respond();
peer.expectClose().respond();
receiver.closeAsync().get();
connection.closeAsync().get();
peer.waitForScriptToComplete(5, TimeUnit.SECONDS);
}
}
use of org.apache.qpid.protonj2.client.StreamReceiver 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.StreamReceiver in project qpid-protonj2 by apache.
the class StreamReceiverTest method testStreamDeliveryRawInputStreamBlockedReadBytesAborted.
@Test
public void testStreamDeliveryRawInputStreamBlockedReadBytesAborted() throws Exception {
final byte[] payload = createEncodedMessage(new AmqpValue<>("Hello World"));
try (ProtonTestServer peer = new ProtonTestServer()) {
peer.expectSASLAnonymousConnect();
peer.expectOpen().respond();
peer.expectBegin().respond();
peer.expectAttach().withRole(Role.RECEIVER.getValue()).respond();
peer.expectFlow();
peer.remoteTransfer().withHandle(0).withDeliveryId(0).withDeliveryTag(new byte[] { 1 }).withMore(true).withMessageFormat(0).withPayload(payload).queue();
peer.start();
URI remoteURI = peer.getServerURI();
LOG.info("Test started, peer listening on: {}", remoteURI);
final Client container = Client.create();
final Connection connection = container.connect(remoteURI.getHost(), remoteURI.getPort());
final StreamReceiver receiver = connection.openStreamReceiver("test-queue");
final StreamDelivery delivery = receiver.receive();
assertNotNull(delivery);
assertFalse(delivery.completed());
assertFalse(delivery.aborted());
final InputStream stream = delivery.rawInputStream();
assertNotNull(stream);
assertEquals(payload.length, stream.available());
final byte[] deliveryBytes = new byte[payload.length * 2];
peer.remoteTransfer().withHandle(0).withDeliveryId(0).withDeliveryTag(new byte[] { 1 }).withMore(false).withAborted(true).withMessageFormat(0).later(50);
try {
stream.read(deliveryBytes);
fail("Delivery should have been aborted while waiting for more data.");
} catch (IOException ioe) {
assertTrue(ioe.getCause() instanceof ClientDeliveryAbortedException);
}
stream.close();
peer.expectDetach().respond();
peer.expectEnd().respond();
peer.expectClose().respond();
receiver.closeAsync().get();
connection.closeAsync().get();
peer.waitForScriptToComplete(5, TimeUnit.SECONDS);
}
}
Aggregations