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.");
}
}
}
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));
}
}
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();
}
}
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);
}
}
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);
}
}
Aggregations