public void publish(String subject, String replyTo, byte[] body) { if (isClosed()) { throw new IllegalStateException("Connection is Closed"); } else if (blockPublishForDrain.get()) { throw new IllegalStateException("Connection is Draining"); // Ok to publish while waiting on subs } if (subject == null || subject.length() == 0) { throw new IllegalArgumentException("Subject is required in publish"); } if (replyTo != null && replyTo.length() == 0) { throw new IllegalArgumentException("ReplyTo cannot be the empty string"); } if (body == null) { body = EMPTY_BODY; } else if (body.length > this.getMaxPayload() && this.getMaxPayload() > 0) { throw new IllegalArgumentException( "Message payload size exceed server configuration " + body.length + " vs " + this.getMaxPayload()); } NatsMessage msg = new NatsMessage(subject, replyTo, body, options.supportUTF8Subjects()); if ((this.status == Status.RECONNECTING || this.status == Status.DISCONNECTED) && !this.writer.canQueue(msg, options.getReconnectBufferSize())) { throw new IllegalStateException( "Unable to queue any more messages during reconnect, max buffer is " + getMaxPayload()); } queueOutgoing(msg); }
public void publish(String subject, String replyTo, byte[] body) { if (isClosed()) { throw new IllegalStateException("Connection is Closed"); } else if (blockPublishForDrain.get()) { throw new IllegalStateException("Connection is Draining"); // Ok to publish while waiting on subs } if (subject == null || subject.length() == 0) { throw new IllegalArgumentException("Subject is required in publish"); } if (replyTo != null && replyTo.length() == 0) { throw new IllegalArgumentException("ReplyTo cannot be the empty string"); } if (body == null) { body = EMPTY_BODY; } else if (body.length > this.getMaxPayload() && this.getMaxPayload() > 0) { throw new IllegalArgumentException( "Message payload size exceed server configuration " + body.length + " vs " + this.getMaxPayload()); } NatsMessage msg = new NatsMessage(subject, replyTo, body, options.supportUTF8Subjects()); if ((this.status == Status.RECONNECTING || this.status == Status.DISCONNECTED) && !this.writer.canQueue(msg, options.getReconnectBufferSize())) { throw new IllegalStateException( "Unable to queue any more messages during reconnect, max buffer is " + getMaxPayload()); } queueOutgoing(msg); }
@Test public void testPropertyIntOptions() { Properties props = new Properties(); props.setProperty(Options.PROP_MAX_RECONNECT, "100"); props.setProperty(Options.PROP_MAX_PINGS, "200"); props.setProperty(Options.PROP_RECONNECT_BUF_SIZE, "300"); props.setProperty(Options.PROP_MAX_CONTROL_LINE, "400"); Options o = new Options.Builder(props).build(); assertEquals("default verbose", false, o.isVerbose()); // One from a different type assertEquals("property max reconnect", 100, o.getMaxReconnect()); assertEquals("property ping max", 200, o.getMaxPingsOut()); assertEquals("property reconnect buffer size", 300, o.getReconnectBufferSize()); assertEquals("property max control line", 400, o.getMaxControlLine()); }
@Test public void testChainedIntOptions() { Options o = new Options.Builder().maxReconnects(100).maxPingsOut(200).reconnectBufferSize(300).build(); assertEquals("default verbose", false, o.isVerbose()); // One from a different type assertEquals("chained max reconnect", 100, o.getMaxReconnect()); assertEquals("chained ping max", 200, o.getMaxPingsOut()); assertEquals("chained reconnect buffer size", 300, o.getReconnectBufferSize()); }
@Test public void testDefaultOptions() { Options o = new Options.Builder().build(); assertEquals("default one server", 1, o.getServers().size()); assertEquals("default url", Options.DEFAULT_URL, o.getServers().toArray()[0].toString()); assertEquals("default data port type", Options.DEFAULT_DATA_PORT_TYPE, o.getDataPortType()); assertEquals("default verbose", false, o.isVerbose()); assertEquals("default pedantic", false, o.isPedantic()); assertEquals("default norandomize", false, o.isNoRandomize()); assertEquals("default oldstyle", false, o.isOldRequestStyle()); assertEquals("default noEcho", false, o.isNoEcho()); assertEquals("default UTF8 Support", false, o.supportUTF8Subjects()); assertNull("default username", o.getUsername()); assertNull("default password", o.getPassword()); assertNull("default token", o.getToken()); assertNull("default connection name", o.getConnectionName()); assertNull("default ssl context", o.getSslContext()); assertEquals("default max reconnect", Options.DEFAULT_MAX_RECONNECT, o.getMaxReconnect()); assertEquals("default ping max", Options.DEFAULT_MAX_PINGS_OUT, o.getMaxPingsOut()); assertEquals("default reconnect buffer size", Options.DEFAULT_RECONNECT_BUF_SIZE, o.getReconnectBufferSize()); assertEquals("default reconnect wait", Options.DEFAULT_RECONNECT_WAIT, o.getReconnectWait()); assertEquals("default connection timeout", Options.DEFAULT_CONNECTION_TIMEOUT, o.getConnectionTimeout()); assertEquals("default ping interval", Options.DEFAULT_PING_INTERVAL, o.getPingInterval()); assertEquals("default cleanup interval", Options.DEFAULT_REQUEST_CLEANUP_INTERVAL, o.getRequestCleanupInterval()); assertNull("error handler", o.getErrorListener()); assertNull("disconnect handler", o.getConnectionListener()); }