use of org.apache.qpid.protonj2.client.Delivery in project qpid-protonj2 by apache.
the class ClientLocalTransactionContext method disposition.
@Override
public ClientTransactionContext disposition(IncomingDelivery delivery, DeliveryState outcome, boolean settled) {
if (isInTransaction()) {
final DeliveryState txnOutcome;
if (outcome instanceof Accepted) {
txnOutcome = cachedReceiverOutcome != null ? cachedReceiverOutcome : (cachedReceiverOutcome = new TransactionalState().setTxnId(currentTxn.getTxnId()).setOutcome(Accepted.getInstance()));
} else {
txnOutcome = new TransactionalState().setTxnId(currentTxn.getTxnId()).setOutcome((Outcome) outcome);
}
delivery.disposition(txnOutcome, true);
} else {
delivery.disposition(outcome, settled);
}
return this;
}
use of org.apache.qpid.protonj2.client.Delivery in project qpid-protonj2 by apache.
the class ClientReceiver method tryReceive.
@Override
public Delivery tryReceive() throws ClientException {
checkClosedOrFailed();
Delivery delivery = messageQueue.dequeueNoWait();
if (delivery != null) {
if (options.autoAccept()) {
delivery.disposition(org.apache.qpid.protonj2.client.DeliveryState.accepted(), options.autoSettle());
} else {
asyncReplenishCreditIfNeeded();
}
} else {
checkClosedOrFailed();
}
return delivery;
}
use of org.apache.qpid.protonj2.client.Delivery in project qpid-protonj2 by apache.
the class Respond 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", "request-respond-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)) {
ReceiverOptions receiverOptions = new ReceiverOptions();
receiverOptions.sourceOptions().capabilities("queue");
Receiver receiver = connection.openReceiver(address, receiverOptions);
Delivery request = receiver.receive(60, TimeUnit.SECONDS);
if (request != null) {
Message<String> received = request.message();
System.out.println("Received message with body: " + received.body());
String replyAddress = received.replyTo();
if (replyAddress != null) {
Sender sender = connection.openSender(replyAddress);
sender.send(Message.create("Response"));
}
} else {
System.out.println("Failed to read a message during the defined wait interval.");
}
}
}
use of org.apache.qpid.protonj2.client.Delivery 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.Delivery 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();
}
}
Aggregations