NatsConnectionReader(NatsConnection connection) { this.connection = connection; this.running = new AtomicBoolean(false); this.stopped = new CompletableFuture<>(); this.stopped.complete(Boolean.TRUE); // we are stopped on creation this.protocolBuffer = ByteBuffer.allocate(this.connection.getOptions().getMaxControlLine()); this.msgLineChars = new char[this.connection.getOptions().getMaxControlLine()]; this.opArray = new char[MAX_PROTOCOL_OP_LENGTH]; this.buffer = new byte[connection.getOptions().getBufferSize()]; this.bufferPosition = 0; this.utf8Mode = connection.getOptions().supportUTF8Subjects(); }
NatsConnectionReader(NatsConnection connection) { this.connection = connection; this.running = new AtomicBoolean(false); this.stopped = new CompletableFuture<>(); this.stopped.complete(Boolean.TRUE); // we are stopped on creation this.protocolBuffer = ByteBuffer.allocate(this.connection.getOptions().getMaxControlLine()); this.msgLineChars = new char[this.connection.getOptions().getMaxControlLine()]; this.opArray = new char[MAX_PROTOCOL_OP_LENGTH]; this.buffer = new byte[connection.getOptions().getBufferSize()]; this.bufferPosition = 0; this.utf8Mode = connection.getOptions().supportUTF8Subjects(); }
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 testPropertiesBooleanBuilder() { Properties props = new Properties(); props.setProperty(Options.PROP_VERBOSE, "true"); props.setProperty(Options.PROP_PEDANTIC, "true"); props.setProperty(Options.PROP_NORANDOMIZE, "true"); props.setProperty(Options.PROP_USE_OLD_REQUEST_STYLE, "true"); props.setProperty(Options.PROP_OPENTLS, "true"); props.setProperty(Options.PROP_NO_ECHO, "true"); props.setProperty(Options.PROP_UTF8_SUBJECTS, "true"); Options o = new Options.Builder(props).build(); assertNull("default username", o.getUsername()); assertEquals("property verbose", true, o.isVerbose()); assertEquals("property pedantic", true, o.isPedantic()); assertEquals("property norandomize", true, o.isNoRandomize()); assertEquals("property oldstyle", true, o.isOldRequestStyle()); assertEquals("property noecho", true, o.isNoEcho()); assertEquals("property utf8", true, o.supportUTF8Subjects()); assertNotNull("property opentls", o.getSslContext()); }
@Test public void testChainedBooleanOptions() throws NoSuchAlgorithmException { Options o = new Options.Builder().verbose().pedantic().noRandomize().supportUTF8Subjects().noEcho().oldRequestStyle().build(); assertNull("default username", o.getUsername()); assertEquals("chained verbose", true, o.isVerbose()); assertEquals("chained pedantic", true, o.isPedantic()); assertEquals("chained norandomize", true, o.isNoRandomize()); assertEquals("chained oldstyle", true, o.isOldRequestStyle()); assertEquals("chained noecho", true, o.isNoEcho()); assertEquals("chained utf8", true, o.supportUTF8Subjects()); }
@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()); }