Java Code Examples for io.nats.client.Options#DEFAULT_URL

The following examples show how to use io.nats.client.Options#DEFAULT_URL . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: NatsReq.java    From nats.java with Apache License 2.0 5 votes vote down vote up
public static void main(String args[]) {
    String subject;
    String message;
    String server;

    if (args.length == 3) {
        server = args[0];
        subject = args[1];
        message = args[2];
    } else if (args.length == 2) {
        server = Options.DEFAULT_URL;
        subject = args[0];
        message = args[1];
    } else {
        usage();
        return;
    }

    try {
        Connection nc = Nats.connect(ExampleUtils.createExampleOptions(server, false));
        Future<Message> replyFuture = nc.request(subject, message.getBytes(StandardCharsets.UTF_8));
        Message reply = replyFuture.get(5, TimeUnit.SECONDS);

        System.out.println();
        System.out.printf("Received reply \"%s\" on subject \"%s\"\n", 
                                new String(reply.getData(), StandardCharsets.UTF_8), 
                                reply.getSubject());
        System.out.println();

        nc.close();

    } catch (Exception exp) {
        exp.printStackTrace();
    }
}
 
Example 2
Source File: NatsPub.java    From nats.java with Apache License 2.0 5 votes vote down vote up
public static void main(String args[]) {
    String subject;
    String message;
    String server;

    if (args.length == 3) {
        server = args[0];
        subject = args[1];
        message = args[2];
    } else if (args.length == 2) {
        server = Options.DEFAULT_URL;
        subject = args[0];
        message = args[1];
    } else {
        usage();
        return;
    }

    try {
        Connection nc = Nats.connect(ExampleUtils.createExampleOptions(server, false));

        System.out.println();
        System.out.printf("Sending %s on %s, server is %s\n", message, subject, server);
        System.out.println();
        nc.publish(subject, message.getBytes(StandardCharsets.UTF_8));
        nc.flush(Duration.ofSeconds(5));
        nc.close();

    } catch (Exception exp) {
        exp.printStackTrace();
    }
}
 
Example 3
Source File: StanExit.java    From nats.java with Apache License 2.0 5 votes vote down vote up
public static void main(String args[]) {
    String server = "help";

    if (args.length == 1) {
        server = args[0];
    } else if (args.length == 0) {
        server = Options.DEFAULT_URL;
    }

    if (server.equals("help")) {
        usage();
        return;
    }

    try {
        
        Options options = new Options.Builder().server(server).noReconnect().build();
        Connection nc = Nats.connect(options);
        Future<Message> replyFuture = nc.request("stan.exit", null);
        Message reply = replyFuture.get();

        System.out.printf("I asked stan to exit without confirmation, he replied \"%s\"\n", 
                                new String(reply.getData(), StandardCharsets.UTF_8));

        replyFuture = nc.request("stan.exit", "confirm".getBytes(StandardCharsets.UTF_8));
        reply = replyFuture.get();

        System.out.printf("I asked stan to exit with confirmation, he replied \"%s\"\n", 
                                new String(reply.getData(), StandardCharsets.UTF_8));

        nc.close();

    } catch (Exception exp) {
        exp.printStackTrace();
    }
}
 
Example 4
Source File: StanRandom.java    From nats.java with Apache License 2.0 5 votes vote down vote up
public static void main(String args[]) {
    String server = "help";

    if (args.length == 1) {
        server = args[0];
    } else if (args.length == 0) {
        server = Options.DEFAULT_URL;
    }

    if (server.equals("help")) {
        usage();
        return;
    }

    try {
        
        Options options = new Options.Builder().server(server).noReconnect().build();
        Connection nc = Nats.connect(options);
        Future<Message> replyFuture = nc.request("stan.random", null);
        Message reply = replyFuture.get();

        System.out.printf("The next stan-random number is %s\n", 
                                new String(reply.getData(), StandardCharsets.UTF_8));

        nc.close();

    } catch (Exception exp) {
        exp.printStackTrace();
    }
}
 
Example 5
Source File: StanTime.java    From nats.java with Apache License 2.0 5 votes vote down vote up
public static void main(String args[]) {
    String server = "help";

    if (args.length == 1) {
        server = args[0];
    } else if (args.length == 0) {
        server = Options.DEFAULT_URL;
    }

    if (server.equals("help")) {
        usage();
        return;
    }

    try {
        
        Options options = new Options.Builder().server(server).noReconnect().build();
        Connection nc = Nats.connect(options);
        Future<Message> replyFuture = nc.request("stan.time", null);
        Message reply = replyFuture.get();

        System.out.printf("The time where stan is, is \"%s\"\n", 
                                new String(reply.getData(), StandardCharsets.UTF_8));

        nc.close();

    } catch (Exception exp) {
        exp.printStackTrace();
    }
}
 
Example 6
Source File: NatsSub.java    From nats.java with Apache License 2.0 4 votes vote down vote up
public static void main(String args[]) {
    String subject;
    int msgCount;
    String server;

    if (args.length == 3) {
        server = args[0];
        subject = args[1];
        msgCount = Integer.parseInt(args[2]);
    } else if (args.length == 2) {
        server = Options.DEFAULT_URL;
        subject = args[0];
        msgCount = Integer.parseInt(args[1]);
    } else {
        usage();
        return;
    }

    try {
        System.out.println();
        System.out.printf("Trying to connect to %s, and listen to %s for %d messages.\n", server, subject, msgCount);
        System.out.println();

        Connection nc = Nats.connect(ExampleUtils.createExampleOptions(server, true));
        Subscription sub = nc.subscribe(subject);
        nc.flush(Duration.ofSeconds(5));

        for(int i=0;i<msgCount;i++) {
            Message msg = sub.nextMessage(Duration.ofHours(1));

            System.out.printf("Received message \"%s\" on subject \"%s\"\n", 
                                    new String(msg.getData(), StandardCharsets.UTF_8), 
                                    msg.getSubject());
        }

        nc.close();
        
    } catch (Exception exp) {
        exp.printStackTrace();
    }
}
 
Example 7
Source File: NatsReply.java    From nats.java with Apache License 2.0 4 votes vote down vote up
public static void main(String args[]) {
    String subject;
    int msgCount;
    String server;

    if (args.length == 3) {
        server = args[0];
        subject = args[1];
        msgCount = Integer.parseInt(args[2]);
    } else if (args.length == 2) {
        server = Options.DEFAULT_URL;
        subject = args[0];
        msgCount = Integer.parseInt(args[1]);
    } else {
        usage();
        return;
    }

    try {
        Connection nc = Nats.connect(ExampleUtils.createExampleOptions(server, true));
        CountDownLatch latch = new CountDownLatch(msgCount); // dispatcher runs callback in another thread
        
        System.out.println();
        Dispatcher d = nc.createDispatcher((msg) -> {
            System.out.printf("Received message \"%s\" on subject \"%s\", replying to %s\n", 
                                    new String(msg.getData(), StandardCharsets.UTF_8), 
                                    msg.getSubject(), msg.getReplyTo());
            nc.publish(msg.getReplyTo(), msg.getData());
            latch.countDown();
        });
        d.subscribe(subject);

        nc.flush(Duration.ofSeconds(5));

        latch.await();

        nc.closeDispatcher(d); // This isn't required, closing the connection will do it
        nc.close();
        
    } catch (Exception exp) {
        exp.printStackTrace();
    }
}
 
Example 8
Source File: NatsPubMod.java    From nats.java with Apache License 2.0 4 votes vote down vote up
public static void main(String args[]) {
    String subject;
    String message;
    String server;

    if (args.length == 3) {
        server = args[0];
        subject = args[1];
        message = args[2];
    } else if (args.length == 2) {
        server = Options.DEFAULT_URL;
        subject = args[0];
        message = args[1];
    } else {
        usage();
        return;
    }

    try {
        Connection nc = Nats.connect(ExampleUtils.createExampleOptions(server, true));

        System.out.println();
        System.out.printf("Sending %s on %s, server is %s\n", message, subject, server);
        System.out.println();


        for (int i = 0; i < 100000; i++) {
            try
            {
                Thread.sleep(500);
                nc.publish(subject, message.getBytes(StandardCharsets.UTF_8));
                nc.flush(Duration.ofSeconds(5));
            }
            catch (Exception e)
            {
                System.out.println("Exception: " + e.getMessage());
            }
        }


        nc.close();

    } catch (Exception exp) {
        exp.printStackTrace();
    }
}
 
Example 9
Source File: NatsDispatch.java    From nats.java with Apache License 2.0 4 votes vote down vote up
public static void main(String args[]) {
    String subject;
    int msgCount;
    String server;

    if (args.length == 3) {
        server = args[0];
        subject = args[1];
        msgCount = Integer.parseInt(args[2]);
    } else if (args.length == 2) {
        server = Options.DEFAULT_URL;
        subject = args[0];
        msgCount = Integer.parseInt(args[1]);
    } else {
        usage();
        return;
    }

    try {
        System.out.println();
        System.out.printf("Trying to connect to %s, and listen to %s for %d messages.\n", server, subject, msgCount);
        System.out.println();

        Connection nc = Nats.connect(ExampleUtils.createExampleOptions(server, true));
        CountDownLatch latch = new CountDownLatch(msgCount); // dispatcher runs callback in another thread
        
        Dispatcher d = nc.createDispatcher((msg) -> {
            System.out.printf("Received message \"%s\" on subject \"%s\"\n", 
                                    new String(msg.getData(), StandardCharsets.UTF_8), 
                                    msg.getSubject());
            latch.countDown();
        });
        d.subscribe(subject);

        nc.flush(Duration.ZERO);

        latch.await();
        nc.close();
        
    } catch (Exception exp) {
        exp.printStackTrace();
    }
}
 
Example 10
Source File: NatsQSub.java    From nats.java with Apache License 2.0 4 votes vote down vote up
public static void main(String args[]) {
    String subject;
    String queue;
    int msgCount;
    String server;

    if (args.length == 4) {
        server = args[0];
        subject = args[1];
        queue = args[2];
        msgCount = Integer.parseInt(args[3]);
    } else if (args.length == 3) {
        server = Options.DEFAULT_URL;
        subject = args[0];
        queue = args[1];
        msgCount = Integer.parseInt(args[2]);
    } else {
        usage();
        return;
    }

    try {
        Connection nc = Nats.connect(ExampleUtils.createExampleOptions(server, true));
        Subscription sub = nc.subscribe(subject, queue);
        nc.flush(Duration.ofSeconds(5));

        System.out.println();
        for(int i=0;i<msgCount;i++) {
            Message msg = sub.nextMessage(Duration.ofHours(1));

            System.out.printf("Received message \"%s\" on subject \"%s\"\n", 
                                    new String(msg.getData(), StandardCharsets.UTF_8), 
                                    msg.getSubject());
        }

        nc.close();
        
    } catch (Exception exp) {
        exp.printStackTrace();
    }
}
 
Example 11
Source File: Stan.java    From nats.java with Apache License 2.0 4 votes vote down vote up
public static void main(String args[]) {
    String server = "help";

    if (args.length == 1) {
        server = args[0];
    } else if (args.length == 0) {
        server = Options.DEFAULT_URL;
    }

    if (server.equals("help")) {
        usage();
        return;
    }

    try {
        SecureRandom random = new SecureRandom();
        CompletableFuture<Void> latch = new CompletableFuture<>();
        Options options = new Options.Builder().server(server).noReconnect().build();
        Connection nc = Nats.connect(options);

        Dispatcher d = nc.createDispatcher((msg) -> {

            String reply = "";
            boolean exit = false;

            switch (msg.getSubject()) {
            case "stan.time":
                reply = LocalTime.now().toString();
                break;
            case "stan.random":
                reply = String.valueOf(random.nextInt());
                break;
            case "stan.exit":
                if ("confirm".equals(new String(msg.getData(), StandardCharsets.UTF_8))) {
                    reply = "exiting";
                    exit = true;
                } else {
                    reply = "you have to confirm the exit";
                }
                break;
            }

            nc.publish(msg.getReplyTo(), reply.getBytes(StandardCharsets.UTF_8));

            if (exit) {
                try {
                    nc.flush(Duration.ZERO);
                    latch.complete(null);
                } catch (TimeoutException e) {
                    e.printStackTrace();
                }
            }
        });
        d.subscribe("stan.time");
        d.subscribe("stan.random");
        d.subscribe("stan.exit");

        nc.flush(Duration.ZERO);

        System.out.println("Stan is listening...");
        latch.get();
        System.out.println("Stan is exiting ...");

        nc.close();

    } catch (Exception exp) {
        exp.printStackTrace();
    }
}