Search in sources :

Example 1 with Delivery

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;
}
Also used : DeliveryState(org.apache.qpid.protonj2.types.transport.DeliveryState) Outcome(org.apache.qpid.protonj2.types.messaging.Outcome) Accepted(org.apache.qpid.protonj2.types.messaging.Accepted) TransactionalState(org.apache.qpid.protonj2.types.transactions.TransactionalState)

Example 2 with Delivery

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;
}
Also used : Delivery(org.apache.qpid.protonj2.client.Delivery) IncomingDelivery(org.apache.qpid.protonj2.engine.IncomingDelivery)

Example 3 with 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.");
        }
    }
}
Also used : Sender(org.apache.qpid.protonj2.client.Sender) Connection(org.apache.qpid.protonj2.client.Connection) ReceiverOptions(org.apache.qpid.protonj2.client.ReceiverOptions) Receiver(org.apache.qpid.protonj2.client.Receiver) ConnectionOptions(org.apache.qpid.protonj2.client.ConnectionOptions) Delivery(org.apache.qpid.protonj2.client.Delivery) Client(org.apache.qpid.protonj2.client.Client)

Example 4 with Delivery

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));
    }
}
Also used : StreamDelivery(org.apache.qpid.protonj2.client.StreamDelivery) StreamReceiver(org.apache.qpid.protonj2.client.StreamReceiver) StreamReceiverMessage(org.apache.qpid.protonj2.client.StreamReceiverMessage) FileOutputStream(java.io.FileOutputStream) Connection(org.apache.qpid.protonj2.client.Connection) ConnectionOptions(org.apache.qpid.protonj2.client.ConnectionOptions) Client(org.apache.qpid.protonj2.client.Client) File(java.io.File)

Example 5 with Delivery

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();
    }
}
Also used : Connection(org.apache.qpid.protonj2.client.Connection) Receiver(org.apache.qpid.protonj2.client.Receiver) ConnectionOptions(org.apache.qpid.protonj2.client.ConnectionOptions) Delivery(org.apache.qpid.protonj2.client.Delivery) Client(org.apache.qpid.protonj2.client.Client) Session(org.apache.qpid.protonj2.client.Session)

Aggregations

Test (org.junit.jupiter.api.Test)112 Client (org.apache.qpid.protonj2.client.Client)77 Connection (org.apache.qpid.protonj2.client.Connection)77 Connection (org.apache.qpid.protonj2.engine.Connection)70 Engine (org.apache.qpid.protonj2.engine.Engine)70 Session (org.apache.qpid.protonj2.engine.Session)70 ProtonTestConnector (org.apache.qpid.protonj2.test.driver.ProtonTestConnector)70 URI (java.net.URI)68 ProtonTestServer (org.apache.qpid.protonj2.test.driver.ProtonTestServer)67 StreamDelivery (org.apache.qpid.protonj2.client.StreamDelivery)47 StreamReceiver (org.apache.qpid.protonj2.client.StreamReceiver)46 OutgoingDelivery (org.apache.qpid.protonj2.engine.OutgoingDelivery)40 Sender (org.apache.qpid.protonj2.engine.Sender)39 Receiver (org.apache.qpid.protonj2.engine.Receiver)31 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)30 AtomicReference (java.util.concurrent.atomic.AtomicReference)30 InputStream (java.io.InputStream)28 ProtonBuffer (org.apache.qpid.protonj2.buffer.ProtonBuffer)28 StreamReceiverMessage (org.apache.qpid.protonj2.client.StreamReceiverMessage)28 IncomingDelivery (org.apache.qpid.protonj2.engine.IncomingDelivery)25