Java Code Examples for io.nats.client.Connection#publish()

The following examples show how to use io.nats.client.Connection#publish() . 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: PubBenchmark.java    From nats.java with Apache License 2.0 6 votes vote down vote up
public void execute(Options connectOptions) throws InterruptedException {
    byte[] payload = createPayload();
    String subject = getSubject();

    try {
        Connection nc = Nats.connect(connectOptions);
        try {
            this.startTiming();
            for(int i = 0; i < this.getMessageCount(); i++) {
                nc.publish(subject, payload);
            }
            try {nc.flush(Duration.ofSeconds(5));}catch(Exception e){}
            this.endTiming();
        } finally {
            nc.close();
        }
    } catch (IOException ex) {
        this.setException(ex);
    }
}
 
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: NatsMessageTests.java    From nats.java with Apache License 2.0 5 votes vote down vote up
@Test(expected=IllegalArgumentException.class)
public void testCustomMaxControlLine() throws Exception {
    byte[] body = new byte[10];
    String subject = "subject";
    String replyTo = "reply";
    int maxControlLine = 1024;

    while (subject.length() <= maxControlLine) {
        subject = subject + subject;
    }

    try (NatsTestServer ts = new NatsTestServer()) {
        Options options = new Options.Builder().
                    server(ts.getURI()).
                    maxReconnects(0).
                    maxControlLine(maxControlLine).
                    build();
        Connection nc = Nats.connect(options);
        try {
            assertTrue("Connected Status", Connection.Status.CONNECTED == nc.getStatus());
            nc.publish(subject, replyTo, body);
            assertFalse(true);
        } finally {
            nc.close();
            assertTrue("Closed Status", Connection.Status.CLOSED == nc.getStatus());
        }
    }
}
 
Example 4
Source File: ReconnectTests.java    From nats.java with Apache License 2.0 5 votes vote down vote up
@Test(expected=IllegalStateException.class)
public void testOverflowReconnectBuffer() throws Exception {
    Connection nc = null;
    TestHandler handler = new TestHandler();
    
    try {
        try (NatsTestServer ts = new NatsTestServer()) {
            Options options = new Options.Builder().
                                    server(ts.getURI()).
                                    maxReconnects(-1).
                                    connectionListener(handler).
                                    reconnectBufferSize(4*512).
                                    reconnectWait(Duration.ofSeconds(480)).
                                    build();
            nc = Nats.connect(options);
            assertTrue("Connected Status", Connection.Status.CONNECTED == nc.getStatus());
            handler.prepForStatusChange(Events.DISCONNECTED);
        }

        flushAndWait(nc, handler);
        checkReconnectingStatus(nc);

        for (int i=0;i<20;i++) {
            nc.publish("test", new byte[512]);// Should blow up by the 5th message
        }

        assertFalse(true);
    } finally {
        if (nc != null) {
            nc.close();
        }
    }
}
 
Example 5
Source File: ErrorListenerTests.java    From nats.java with Apache License 2.0 5 votes vote down vote up
@Test
public void testDiscardedMessageServerClosed() throws Exception {
    int maxMessages = 10;
    TestHandler handler = new TestHandler();
    try (NatsTestServer ts = new NatsTestServer(false)) {
        Options options = new Options.Builder().
                server(ts.getURI()).
                maxMessagesInOutgoingQueue(maxMessages).
                discardMessagesWhenOutgoingQueueFull().
                pingInterval(Duration.ofSeconds(100)). // make this long so we don't ping during test
                connectionListener(handler).
                errorListener(handler).
                build();
        Connection nc = Nats.connect(options);
        assertTrue("Connected Status", Connection.Status.CONNECTED == nc.getStatus());
        try {
            nc.flush(Duration.ofSeconds(1)); // Get the sub to the server

            handler.prepForStatusChange(Events.DISCONNECTED);
            ts.close();
            handler.waitForStatusChange(2, TimeUnit.SECONDS); // make sure the connection is down

            for (int i = 0; i < maxMessages + 1; i++) {
                nc.publish("subject" + i, ("message" + i).getBytes());
            }
        } finally {
            nc.close();
            assertTrue("Closed Status", Connection.Status.CLOSED == nc.getStatus());
        }
    }

    List<Message> discardedMessages = handler.getDiscardedMessages();
    assertTrue("At least one message discarded", discardedMessages.size() >= 1);
    assertTrue("Message subject", discardedMessages.get(0).getSubject().startsWith("subject"));
    assertTrue("Message data", new String(discardedMessages.get(0).getData()).startsWith("message"));
}
 
Example 6
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();
    }
}