Search in sources :

Example 1 with Client

use of com.alibaba.nacos.naming.core.v2.client.Client 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 2 with Client

use of com.alibaba.nacos.naming.core.v2.client.Client 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 3 with Client

use of com.alibaba.nacos.naming.core.v2.client.Client 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)

Example 4 with Client

use of com.alibaba.nacos.naming.core.v2.client.Client in project qpid-protonj2 by apache.

the class ReconnectReceiver 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", "reconnect-examples");
    final String backupServerHost = System.getProperty("BACKUP_HOST");
    final int backupServerPort = Integer.getInteger("BACKUP_PORT", 5672);
    final Client client = Client.create();
    final ConnectionOptions connectionOpts = new ConnectionOptions();
    connectionOpts.user(System.getProperty("USER"));
    connectionOpts.password(System.getProperty("PASSWORD"));
    connectionOpts.reconnectEnabled(true);
    if (backupServerHost != null) {
        connectionOpts.reconnectOptions().addReconnectLocation(backupServerHost, backupServerPort);
    }
    try (Connection connection = client.connect(serverHost, serverPort, connectionOpts);
        Receiver receiver = connection.openReceiver(address)) {
        for (int receivedCount = 0; receivedCount < MESSAGE_COUNT; ++receivedCount) {
            Delivery delivery = receiver.receive();
            Message<String> message = delivery.message();
            System.out.println(message.body());
        }
    } catch (Exception exp) {
        System.out.println("Caught exception, exiting.");
        exp.printStackTrace(System.out);
        System.exit(1);
    }
}
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)

Example 5 with Client

use of com.alibaba.nacos.naming.core.v2.client.Client in project qpid-protonj2 by apache.

the class ReconnectSender 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", "reconnect-examples");
    final String backupServerHost = System.getProperty("BACKUP_HOST");
    final int backupServerPort = Integer.getInteger("BACKUP_PORT", 5672);
    final Client client = Client.create();
    final ConnectionOptions connectionOpts = new ConnectionOptions();
    connectionOpts.user(System.getProperty("USER"));
    connectionOpts.password(System.getProperty("PASSWORD"));
    connectionOpts.reconnectEnabled(true);
    if (backupServerHost != null) {
        connectionOpts.reconnectOptions().addReconnectLocation(backupServerHost, backupServerPort);
    }
    try (Connection connection = client.connect(serverHost, serverPort, connectionOpts);
        Sender sender = connection.openSender(address)) {
        for (int sentCount = 1; sentCount <= MESSAGE_COUNT; ) {
            try {
                sender.send(Message.create("Hello World: #" + sentCount)).awaitAccepted();
                sentCount++;
            } catch (Exception ex) {
                System.out.println("Caught exception during send, will retry on next connect");
            }
        }
    } catch (Exception exp) {
        System.out.println("Caught exception, exiting.");
        exp.printStackTrace(System.out);
        System.exit(1);
    }
}
Also used : Sender(org.apache.qpid.protonj2.client.Sender) Connection(org.apache.qpid.protonj2.client.Connection) ConnectionOptions(org.apache.qpid.protonj2.client.ConnectionOptions) Client(org.apache.qpid.protonj2.client.Client)

Aggregations

Client (org.apache.qpid.protonj2.client.Client)371 Connection (org.apache.qpid.protonj2.client.Connection)367 URI (java.net.URI)354 ProtonTestServer (org.apache.qpid.protonj2.test.driver.ProtonTestServer)352 Test (org.junit.jupiter.api.Test)252 Session (org.apache.qpid.protonj2.client.Session)166 ConnectionOptions (org.apache.qpid.protonj2.client.ConnectionOptions)113 Sender (org.apache.qpid.protonj2.client.Sender)89 Receiver (org.apache.qpid.protonj2.client.Receiver)79 ExecutionException (java.util.concurrent.ExecutionException)72 StreamReceiver (org.apache.qpid.protonj2.client.StreamReceiver)65 ClientException (org.apache.qpid.protonj2.client.exceptions.ClientException)60 StreamSender (org.apache.qpid.protonj2.client.StreamSender)49 StreamDelivery (org.apache.qpid.protonj2.client.StreamDelivery)46 TransferPayloadCompositeMatcher (org.apache.qpid.protonj2.test.driver.matchers.transport.TransferPayloadCompositeMatcher)44 Tracker (org.apache.qpid.protonj2.client.Tracker)41 StreamSenderMessage (org.apache.qpid.protonj2.client.StreamSenderMessage)38 Client (org.powerbot.bot.rt4.client.Client)36 ReceiverOptions (org.apache.qpid.protonj2.client.ReceiverOptions)34 SenderOptions (org.apache.qpid.protonj2.client.SenderOptions)33