Class NatsTransporter
java.lang.Object
services.moleculer.service.MoleculerComponent
services.moleculer.transporter.Transporter
services.moleculer.transporter.NatsTransporter
- All Implemented Interfaces:
io.nats.client.ConnectionListener, io.nats.client.MessageHandler, MetricConstants, MoleculerLifecycle
public class NatsTransporter
extends Transporter
implements io.nats.client.MessageHandler, io.nats.client.ConnectionListener
NATS Transporter. NATS Server is a simple, high performance open source
messaging system for cloud native applications, IoT messaging, and
microservices architectures (website: https://nats.io). Tested with NATS
server version 1.3.0. Usage:
// https://mvnrepository.com/artifact/io.nats/jnats
compile group: 'io.nats', name: 'jnats', version: '2.6.6'
ServiceBroker broker = ServiceBroker.builder().nodeID("node1")
.transporter(new NatsTransporter("localhost")).build();
Required dependency:// https://mvnrepository.com/artifact/io.nats/jnats
compile group: 'io.nats', name: 'jnats', version: '2.6.6'
- See Also:
-
Nested Class Summary
Nested classes/interfaces inherited from interface io.nats.client.ConnectionListener
io.nats.client.ConnectionListener.Events -
Field Summary
FieldsModifier and TypeFieldDescriptionprotected booleanTurn on advanced stats, primarily for test/benchmarks.protected io.nats.client.AuthHandlerOptional AuthHandler.protected intSets the initial size for buffers in the connection.protected io.nats.client.Connectionprotected longSet the timeout for connection attempts in MILLISECONDS.protected io.nats.client.Dispatcherprotected intSet the maximum number of pings the client can have in flight.protected booleanTurn off echo.protected booleanTurn off server pool randomization.protected booleanTurn on the old request style that uses a new inbox and subscriber for each request.protected booleanSet the SSL context to one that accepts any server certificate and has no client certificate.protected StringPassword for basic authentication.protected booleanTurn on pedantic mode for the server.protected longSet the interval between attempts to pings the server in MILLISECONDS.protected booleanUse the default SSL Context, if it exists.protected SSLContextOptional SSL Context.protected final AtomicBooleanprotected String[]Nats server url(s).protected StringUsername for basic authentication.protected booleanEnable UTF8 channels.protected booleanTurn on verbose mode with the server.Fields inherited from class Transporter
checkTimeoutTimer, counterTransporterPacketsReceivedBytes, counterTransporterPacketsReceivedTotal, counterTransporterPacketsSentBytes, counterTransporterPacketsSentTotal, debug, debugHeartbeats, disconnectChannel, discoverBroadcastChannel, discoverChannel, eventbus, eventChannel, executor, gaugeTransitConnected, heartbeatChannel, heartbeatInterval, heartbeatTimeout, heartBeatTimer, infoBroadcastChannel, infoChannel, infoScheduled, infoScheduledAt, instanceID, lastReceivedMessageAt, metrics, monitor, namespace, nodeID, nodes, offlineTimeout, PACKET_DISCONNECT, PACKET_DISCOVER, PACKET_EVENT, PACKET_HEARTBEAT, PACKET_INFO, PACKET_PING, PACKET_PONG, PACKET_REQUEST, PACKET_RESPONSE, pingChannel, pongChannel, preferHostname, prefix, protocolVersion, registry, requestChannel, responseChannel, scheduler, serializer, serviceInvoker, subscriptionTimeout, uidGenerator, usingJsonSerializerFields inherited from class MoleculerComponent
broker, logger, nameFields inherited from interface MetricConstants
MOLECULER_CACHER_CLEAN_TIME, MOLECULER_CACHER_CLEAN_TIME_DESC, MOLECULER_CACHER_CLEAN_TOTAL, MOLECULER_CACHER_CLEAN_TOTAL_DESC, MOLECULER_CACHER_DEL_TIME, MOLECULER_CACHER_DEL_TIME_DESC, MOLECULER_CACHER_DEL_TOTAL, MOLECULER_CACHER_DEL_TOTAL_DESC, MOLECULER_CACHER_EXPIRED_TOTAL, MOLECULER_CACHER_EXPIRED_TOTAL_DESC, MOLECULER_CACHER_FOUND_TOTAL, MOLECULER_CACHER_FOUND_TOTAL_DESC, MOLECULER_CACHER_GET_TIME, MOLECULER_CACHER_GET_TIME_DESC, MOLECULER_CACHER_GET_TOTAL, MOLECULER_CACHER_GET_TOTAL_DESC, MOLECULER_CACHER_SET_TIME, MOLECULER_CACHER_SET_TIME_DESC, MOLECULER_CACHER_SET_TOTAL, MOLECULER_CACHER_SET_TOTAL_DESC, MOLECULER_CIRCUIT_BREAKER_HALF_OPENED_ACTIVE, MOLECULER_CIRCUIT_BREAKER_HALF_OPENED_ACTIVE_DESC, MOLECULER_CIRCUIT_BREAKER_OPENED_ACTIVE, MOLECULER_CIRCUIT_BREAKER_OPENED_ACTIVE_DESC, MOLECULER_CIRCUIT_BREAKER_OPENED_TOTAL, MOLECULER_CIRCUIT_BREAKER_OPENED_TOTAL_DESC, MOLECULER_EVENT_BROADCAST_TOTAL, MOLECULER_EVENT_BROADCAST_TOTAL_DESC, MOLECULER_EVENT_BROADCASTLOCAL_TOTAL, MOLECULER_EVENT_BROADCASTLOCAL_TOTAL_DESC, MOLECULER_EVENT_EMIT_TOTAL, MOLECULER_EVENT_EMIT_TOTAL_DESC, MOLECULER_EVENT_RECEIVED_ACTIVE, MOLECULER_EVENT_RECEIVED_ACTIVE_DESC, MOLECULER_EVENT_RECEIVED_ERROR_TOTAL, MOLECULER_EVENT_RECEIVED_ERROR_TOTAL_DESC, MOLECULER_EVENT_RECEIVED_TIME, MOLECULER_EVENT_RECEIVED_TIME_DESC, MOLECULER_EVENT_RECEIVED_TOTAL, MOLECULER_EVENT_RECEIVED_TOTAL_DESC, MOLECULER_EXECUTOR, MOLECULER_REQUEST_ACTIVE, MOLECULER_REQUEST_ACTIVE_DESC, MOLECULER_REQUEST_ERROR_TOTAL, MOLECULER_REQUEST_ERROR_TOTAL_DESC, MOLECULER_REQUEST_LEVELS, MOLECULER_REQUEST_LEVELS_DESC, MOLECULER_REQUEST_RETRY_ATTEMPTS_TOTAL, MOLECULER_REQUEST_RETRY_ATTEMPTS_TOTAL_DESC, MOLECULER_REQUEST_TIME, MOLECULER_REQUEST_TIME_DESC, MOLECULER_REQUEST_TIMEOUT_TOTAL, MOLECULER_REQUEST_TIMEOUT_TOTAL_DESC, MOLECULER_REQUEST_TOTAL, MOLECULER_REQUEST_TOTAL_DESC, MOLECULER_SCHEDULER, MOLECULER_TRANSIT_CONNECTED, MOLECULER_TRANSIT_CONNECTED_DESC, MOLECULER_TRANSIT_ORPHAN_RESPONSE_TOTAL, MOLECULER_TRANSIT_ORPHAN_RESPONSE_TOTAL_DESC, MOLECULER_TRANSIT_REQUESTS_ACTIVE, MOLECULER_TRANSIT_REQUESTS_ACTIVE_DESC, MOLECULER_TRANSIT_STREAMS_RECEIVE_ACTIVE, MOLECULER_TRANSIT_STREAMS_RECEIVE_ACTIVE_DESC, MOLECULER_TRANSPORTER_PACKETS_RECEIVED_BYTES, MOLECULER_TRANSPORTER_PACKETS_RECEIVED_BYTES_DESC, MOLECULER_TRANSPORTER_PACKETS_RECEIVED_TOTAL, MOLECULER_TRANSPORTER_PACKETS_RECEIVED_TOTAL_DESC, MOLECULER_TRANSPORTER_PACKETS_SENT_BYTES, MOLECULER_TRANSPORTER_PACKETS_SENT_BYTES_DESC, MOLECULER_TRANSPORTER_PACKETS_SENT_TOTAL, MOLECULER_TRANSPORTER_PACKETS_SENT_TOTAL_DESC -
Constructor Summary
ConstructorsConstructorDescriptionNatsTransporter(String... urls) NatsTransporter(String username, String password, boolean secure, String... urls) -
Method Summary
Modifier and TypeMethodDescriptionvoidconnect()voidconnectionEvent(io.nats.client.Connection conn, io.nats.client.ConnectionListener.Events type) protected voidprotected voidAny I/O error occurred.io.nats.client.AuthHandlerintlongintlongio.nats.client.StatisticsString[]getUrls()booleanbooleanisNoEcho()booleanbooleanbooleanbooleanbooleanisSecure()booleanbooleanvoidonMessage(io.nats.client.Message msg) voidprotected voidvoidsetAdvancedStats(boolean advancedStats) voidsetAuthHandler(io.nats.client.AuthHandler authHandler) voidsetBufferSize(int bufferSize) voidsetConnectionTimeout(long connectionTimeout) voidsetMaxPingsOut(int maxPingsOut) voidsetNoEcho(boolean noEcho) voidsetNoRandomize(boolean noRandomize) voidsetOldRequestStyle(boolean oldRequestStyle) voidsetOpentls(boolean opentls) voidsetPassword(String password) voidsetPedantic(boolean pedantic) voidsetPingInterval(long pingInterval) voidsetSecure(boolean secure) voidsetSslContext(SSLContext sslContext) voidvoidsetUsername(String username) voidsetUtf8Support(boolean utf8Support) voidsetVerbose(boolean verbose) voidstopped()Closes transporter.io.datatree.PromiseMethods inherited from class Transporter
broadcastInfoPacket, broadcastNodeConnected, broadcastNodeDisconnected, broadcastNodeUpdated, broadcastTransporterConnected, broadcastTransporterDisconnected, channel, checkTimeouts, connected, connected, createPingPacket, getAddress, getAllNodeIDs, getCpuUsage, getDescriptor, getHeartbeatInterval, getHeartbeatTimeout, getInstanceID, getLastHeartbeatTime, getNamespace, getOfflineTimeout, getPrefix, getSerializer, getSubscriptionTimeout, isDebug, isDebugHeartbeats, isOnline, isPreferHostname, processReceivedMessage, processReceivedMessage, publish, received, scheduleInfoPacket, sendClosePacket, sendDataPacket, sendDisconnectPacket, sendDiscoverPacket, sendErrorPacket, sendEventPacket, sendHeartbeatPacket, sendInfoPacket, sendPongPacket, sendRequestPacket, setDebug, setDebugHeartbeats, setHeartbeatInterval, setHeartbeatTimeout, setNamespace, setOfflineTimeout, setPreferHostname, setPrefix, setSerializer, setSubscriptionTimeout, started, subscribe, updateNodeInfoMethods inherited from class MoleculerComponent
getBroker, getLogger, getName
-
Field Details
-
username
Username for basic authentication. -
password
Password for basic authentication. -
secure
protected boolean secureUse the default SSL Context, if it exists. -
urls
Nats server url(s). -
sslContext
Optional SSL Context. -
noRandomize
protected boolean noRandomizeTurn off server pool randomization. -
maxPingsOut
protected int maxPingsOutSet the maximum number of pings the client can have in flight. -
pingInterval
protected long pingIntervalSet the interval between attempts to pings the server in MILLISECONDS. -
connectionTimeout
protected long connectionTimeoutSet the timeout for connection attempts in MILLISECONDS. -
verbose
protected boolean verboseTurn on verbose mode with the server. -
bufferSize
protected int bufferSizeSets the initial size for buffers in the connection. -
authHandler
protected io.nats.client.AuthHandler authHandlerOptional AuthHandler. -
noEcho
protected boolean noEchoTurn off echo. -
utf8Support
protected boolean utf8SupportEnable UTF8 channels. -
pedantic
protected boolean pedanticTurn on pedantic mode for the server. -
advancedStats
protected boolean advancedStatsTurn on advanced stats, primarily for test/benchmarks. -
opentls
protected boolean opentlsSet the SSL context to one that accepts any server certificate and has no client certificate. -
oldRequestStyle
protected boolean oldRequestStyleTurn on the old request style that uses a new inbox and subscriber for each request. -
client
protected io.nats.client.Connection client -
dispatcher
protected io.nats.client.Dispatcher dispatcher -
started
-
-
Constructor Details
-
NatsTransporter
public NatsTransporter() -
NatsTransporter
-
NatsTransporter
-
-
Method Details
-
connect
public void connect()- Specified by:
connectin classTransporter
-
connectionEvent
public void connectionEvent(io.nats.client.Connection conn, io.nats.client.ConnectionListener.Events type) - Specified by:
connectionEventin interfaceio.nats.client.ConnectionListener
-
disconnect
protected void disconnect() -
reconnect
protected void reconnect() -
error
Description copied from class:TransporterAny I/O error occurred. Implementation-specific error handling goes here (reconnection, etc.).- Overrides:
errorin classTransporter- Parameters:
cause- I/O error
-
stopped
public void stopped()Closes transporter.- Specified by:
stoppedin interfaceMoleculerLifecycle- Overrides:
stoppedin classTransporter
-
subscribe
- Specified by:
subscribein classTransporter
-
onMessage
public void onMessage(io.nats.client.Message msg) - Specified by:
onMessagein interfaceio.nats.client.MessageHandler
-
publish
- Specified by:
publishin classTransporter
-
getStatistics
public io.nats.client.Statistics getStatistics()- Returns:
- Statistics
-
getConnectionTimeout
public long getConnectionTimeout()- Returns:
- the connectionTimeout
-
setConnectionTimeout
public void setConnectionTimeout(long connectionTimeout) - Parameters:
connectionTimeout- the connectionTimeout to set
-
getBufferSize
public int getBufferSize()- Returns:
- the bufferSize
-
setBufferSize
public void setBufferSize(int bufferSize) - Parameters:
bufferSize- the bufferSize to set
-
getAuthHandler
public io.nats.client.AuthHandler getAuthHandler()- Returns:
- the authHandler
-
setAuthHandler
public void setAuthHandler(io.nats.client.AuthHandler authHandler) - Parameters:
authHandler- the authHandler to set
-
isNoEcho
public boolean isNoEcho()- Returns:
- the noEcho
-
setNoEcho
public void setNoEcho(boolean noEcho) - Parameters:
noEcho- the noEcho to set
-
isUtf8Support
public boolean isUtf8Support()- Returns:
- the utf8Support
-
setUtf8Support
public void setUtf8Support(boolean utf8Support) - Parameters:
utf8Support- the utf8Support to set
-
isPedantic
public boolean isPedantic()- Returns:
- the pedantic
-
setPedantic
public void setPedantic(boolean pedantic) - Parameters:
pedantic- the pedantic to set
-
isAdvancedStats
public boolean isAdvancedStats()- Returns:
- the advancedStats
-
setAdvancedStats
public void setAdvancedStats(boolean advancedStats) - Parameters:
advancedStats- the advancedStats to set
-
isOpentls
public boolean isOpentls()- Returns:
- the opentls
-
setOpentls
public void setOpentls(boolean opentls) - Parameters:
opentls- the opentls to set
-
getUsername
- Returns:
- the username
-
setUsername
- Parameters:
username- the username to set
-
getPassword
- Returns:
- the password
-
setPassword
- Parameters:
password- the password to set
-
isSecure
public boolean isSecure()- Returns:
- the secure
-
setSecure
public void setSecure(boolean secure) - Parameters:
secure- the secure to set
-
getUrls
- Returns:
- the urls
-
setUrls
- Parameters:
urls- the urls to set
-
getSslContext
- Returns:
- the sslContext
-
setSslContext
- Parameters:
sslContext- the sslContext to set
-
isNoRandomize
public boolean isNoRandomize()- Returns:
- the noRandomize
-
setNoRandomize
public void setNoRandomize(boolean noRandomize) - Parameters:
noRandomize- the noRandomize to set
-
getMaxPingsOut
public int getMaxPingsOut()- Returns:
- the maxPingsOut
-
setMaxPingsOut
public void setMaxPingsOut(int maxPingsOut) - Parameters:
maxPingsOut- the maxPingsOut to set
-
getPingInterval
public long getPingInterval()- Returns:
- the pingInterval
-
setPingInterval
public void setPingInterval(long pingInterval) - Parameters:
pingInterval- the pingInterval to set
-
isVerbose
public boolean isVerbose()- Returns:
- the verbose
-
setVerbose
public void setVerbose(boolean verbose) - Parameters:
verbose- the verbose to set
-
isOldRequestStyle
public boolean isOldRequestStyle()- Returns:
- the oldRequestStyle
-
setOldRequestStyle
public void setOldRequestStyle(boolean oldRequestStyle) - Parameters:
oldRequestStyle- the oldRequestStyle to set
-