Package berlin.yuna.natsserver.logic
Class Nats
- java.lang.Object
-
- berlin.yuna.natsserver.logic.Nats
-
- All Implemented Interfaces:
NatsInterface,AutoCloseable
public class Nats extends Object implements NatsInterface
- Since:
- 1.0
- Author:
- Yuna Morgenstern
- See Also:
SystemUtil,NatsInterface
-
-
Field Summary
Fields Modifier and Type Field Description protected Map<NatsConfig,MapValue>configMapprotected Stringnamestatic StringNATS_PREFIXprotected AtomicReference<berlin.yuna.clu.logic.Terminal>terminalprotected LongtimeoutMs
-
Constructor Summary
Constructors Constructor Description Nats()Throws all exceptions asNatsStartExceptionwhich is aRuntimeException
Possible wrapped exceptions:
IOExceptionifNatsis not found or unsupported on theSystemUtil
BindExceptionif port is already taken
PortUnreachableExceptionifNatsis not starting cause port is not freeNats(int port)Starts the server ifNatsConfig.NATS_AUTOSTART== true Throws all exceptions asNatsStartExceptionwhich is aRuntimeException
Possible wrapped exceptions:
IOExceptionifNatsis not found or unsupported on theSystemUtil
BindExceptionif port is already taken
PortUnreachableExceptionifNatsis not starting cause port is not freeNats(NatsOptionsBuilder natsOptions)Starts the server ifNatsConfig.NATS_AUTOSTART== true Throws all exceptions asNatsStartExceptionwhich is aRuntimeException
Possible wrapped exceptions:
IOExceptionifNatsis not found or unsupported on theSystemUtil
BindExceptionif port is already taken
PortUnreachableExceptionifNatsis not starting cause port is not freeNats(NatsOptions natsOptions)Starts the server ifNatsConfig.NATS_AUTOSTART== true Throws all exceptions asNatsStartExceptionwhich is aRuntimeException
Possible wrapped exceptions:
IOExceptionifNatsis not found or unsupported on theSystemUtil
BindExceptionif port is already taken
PortUnreachableExceptionifNatsis not starting cause port is not free
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected voidaddConfig(NatsConfig key, Object value)protected voidaddConfig(ValueSource source, NatsConfig key, String value)Pathbinary()voidclose()Map<NatsConfig,String>config()PathconfigFile()PathconfigPropertyFile()String[]customArgs()booleandebug()protected voiddeletePidFile()protected PathdownloadNats()StringdownloadUrl()Nats download url which is usually a zip fileStringgetValue(NatsConfig key)Gets resolved config value from keyStringgetValue(NatsConfig key, Supplier<String> or)Gets resolved config value from keybooleanjetStream()Loggerlogger()LevelloggingLevel()intpid()get process idPathpidFile()get process id file which only exists when the process is runningintport()protected StringprepareCommand()Processprocess()protected voidsendStopSignal()protected voidsetConfigFromNatsOptions(NatsOptions natsOptions)protected voidsetConfigFromProperties()protected voidsetDefaultConfig()protected voidsetEnvConfig()protected intsetNextFreePort()protected voidshutdown()Natsstart()Starts the server if not already started e.g.protected voidstartProcess(String command)StringtoString()Stringurl()nats server URL from bind to host addressprotected voidwaitForShutDown(long timeoutMs)
-
-
-
Field Detail
-
name
protected final String name
-
timeoutMs
protected final Long timeoutMs
-
configMap
protected final Map<NatsConfig,MapValue> configMap
-
terminal
protected final AtomicReference<berlin.yuna.clu.logic.Terminal> terminal
-
NATS_PREFIX
public static final String NATS_PREFIX
- See Also:
- Constant Field Values
-
-
Constructor Detail
-
Nats
public Nats()
Throws all exceptions asNatsStartExceptionwhich is aRuntimeException
Possible wrapped exceptions:
IOExceptionifNatsis not found or unsupported on theSystemUtil
BindExceptionif port is already taken
PortUnreachableExceptionifNatsis not starting cause port is not free
-
Nats
public Nats(int port)
Starts the server ifNatsConfig.NATS_AUTOSTART== true Throws all exceptions asNatsStartExceptionwhich is aRuntimeException
Possible wrapped exceptions:
IOExceptionifNatsis not found or unsupported on theSystemUtil
BindExceptionif port is already taken
PortUnreachableExceptionifNatsis not starting cause port is not free- Parameters:
port- the port to start on or <=0 to use an automatically allocated port
-
Nats
public Nats(NatsOptionsBuilder natsOptions)
Starts the server ifNatsConfig.NATS_AUTOSTART== true Throws all exceptions asNatsStartExceptionwhich is aRuntimeException
Possible wrapped exceptions:
IOExceptionifNatsis not found or unsupported on theSystemUtil
BindExceptionif port is already taken
PortUnreachableExceptionifNatsis not starting cause port is not free- Parameters:
natsOptions- nats options
-
Nats
public Nats(NatsOptions natsOptions)
Starts the server ifNatsConfig.NATS_AUTOSTART== true Throws all exceptions asNatsStartExceptionwhich is aRuntimeException
Possible wrapped exceptions:
IOExceptionifNatsis not found or unsupported on theSystemUtil
BindExceptionif port is already taken
PortUnreachableExceptionifNatsis not starting cause port is not free- Parameters:
natsOptions- nats options
-
-
Method Detail
-
start
public Nats start()
Starts the server if not already started e.g. byNatsConfig.NATS_AUTOSTART== true
TThrows all exceptions asNatsStartExceptionwhich is aRuntimeException
Possible wrapped exceptions:
IOExceptionifNatsis not found or unsupported on theSystemUtil
BindExceptionif port is already taken
PortUnreachableExceptionifNatsis not starting cause port is not free- Returns:
Nats
-
process
public Process process()
- Specified by:
processin interfaceNatsInterface
-
customArgs
public String[] customArgs()
- Specified by:
customArgsin interfaceNatsInterface
-
logger
public Logger logger()
- Specified by:
loggerin interfaceNatsInterface
-
loggingLevel
public Level loggingLevel()
- Specified by:
loggingLevelin interfaceNatsInterface
-
binary
public Path binary()
- Specified by:
binaryin interfaceNatsInterface- Returns:
- Path to binary file
seeNatsConfig.NATS_BINARY_PATH
-
port
public int port()
- Specified by:
portin interfaceNatsInterface- Returns:
- Port (if <=0, the port will be visible after
start()- see alsoNatsConfig.NATS_AUTOSTART)
seeNatsConfig.PORT
-
jetStream
public boolean jetStream()
- Specified by:
jetStreamin interfaceNatsInterface- Returns:
- true if Jetstream is enabled
seeNatsConfig.JETSTREAM
-
debug
public boolean debug()
- Specified by:
debugin interfaceNatsInterface- Returns:
- true if "DV", "DVV" or "DEBUG" is set
seeNatsConfig.DVseeNatsConfig.DVVseeNatsConfig.DEBUG
-
configFile
public Path configFile()
- Specified by:
configFilein interfaceNatsInterface- Returns:
- custom nats config file
seeNatsConfig.CONFIG
-
configPropertyFile
public Path configPropertyFile()
- Returns:
- custom property config file
seeNatsConfig.NATS_PROPERTY_FILE
-
close
public void close()
- Specified by:
closein interfaceAutoCloseable
-
getValue
public String getValue(NatsConfig key)
Gets resolved config value from key- Parameters:
key- config key- Returns:
- config key value
-
getValue
public String getValue(NatsConfig key, Supplier<String> or)
Gets resolved config value from key- Parameters:
key- config keyor- lazy loaded fallback value- Returns:
- config key value
-
pid
public int pid()
get process id- Returns:
- process id or -1 if process is not running
-
pidFile
public Path pidFile()
get process id file which only exists when the process is running- Returns:
- process id file path
-
downloadUrl
public String downloadUrl()
Nats download url which is usually a zip file- Returns:
- nats download url
-
url
public String url()
nats server URL from bind to host address- Specified by:
urlin interfaceNatsInterface- Returns:
- nats server url
-
config
public Map<NatsConfig,String> config()
- Returns:
- nats configuration
-
setConfigFromNatsOptions
protected void setConfigFromNatsOptions(NatsOptions natsOptions)
-
setConfigFromProperties
protected void setConfigFromProperties()
-
setDefaultConfig
protected void setDefaultConfig()
-
setEnvConfig
protected void setEnvConfig()
-
addConfig
protected void addConfig(NatsConfig key, Object value)
-
addConfig
protected void addConfig(ValueSource source, NatsConfig key, String value)
-
setNextFreePort
protected int setNextFreePort()
-
downloadNats
protected Path downloadNats() throws IOException
- Throws:
IOException
-
prepareCommand
protected String prepareCommand()
-
shutdown
protected void shutdown()
-
sendStopSignal
protected void sendStopSignal()
-
waitForShutDown
protected void waitForShutDown(long timeoutMs)
-
deletePidFile
protected void deletePidFile()
-
startProcess
protected void startProcess(String command)
-
-