Class NetworkLatencyStrategyFactory
java.lang.Object
services.moleculer.service.MoleculerComponent
services.moleculer.strategy.StrategyFactory
services.moleculer.strategy.ArrayBasedStrategyFactory
services.moleculer.strategy.NetworkLatencyStrategyFactory
- All Implemented Interfaces:
MoleculerLifecycle
Factory of lowest network latency strategy. This strategy comes from a random
strategy, but preferably communicates with the "closest" nodes (nodes with
the lowest response/ping time).
- See Also:
-
Nested Class Summary
Nested Classes -
Field Summary
FieldsModifier and TypeFieldDescriptionprotected intNumber of samples used for average calculation.protected Stringprotected intPing period time, in SECONDS.protected longPing timeout time, in MILLISECONDS.protected ScheduledFuture<?> Cancelable timer of ping loopprotected Stringprotected final ConcurrentHashMap<String, NetworkLatencyStrategyFactory.Samples> protected intThis strategy compares number of 'sampleCount' random node.protected ScheduledExecutorServiceprotected TransporterFields inherited from class ArrayBasedStrategyFactory
preferLocalFields inherited from class MoleculerComponent
broker, logger, name -
Constructor Summary
ConstructorsConstructorDescriptionConstructor that calls for service based primarily on its own (latency-based) logic.NetworkLatencyStrategyFactory(boolean preferLocal) Constructor that can be configured to use local services if possible. -
Method Summary
Modifier and TypeMethodDescriptioncreate()protected longgetAverageResponseTime(String nextNodeID) intintlongintprotected voidprotected voidvoidsetCollectCount(int collectCount) voidsetPingInterval(int pingInterval) voidsetPingTimeout(long pingTimeout) voidsetSampleCount(int sampleCount) voidstarted(ServiceBroker broker) Initializes strategy instance.voidstopped()Methods inherited from class ArrayBasedStrategyFactory
isPreferLocal, setPreferLocalMethods inherited from class MoleculerComponent
getBroker, getLogger, getName
-
Field Details
-
sampleCount
protected int sampleCountThis strategy compares number of 'sampleCount' random node. -
pingInterval
protected int pingIntervalPing period time, in SECONDS. -
pingTimeout
protected long pingTimeoutPing timeout time, in MILLISECONDS. -
collectCount
protected int collectCountNumber of samples used for average calculation. -
scheduler
-
transporter
-
nodeID
-
responseTimes
-
pingTimer
Cancelable timer of ping loop -
previousNodeID
-
-
Constructor Details
-
NetworkLatencyStrategyFactory
public NetworkLatencyStrategyFactory()Constructor that calls for service based primarily on its own (latency-based) logic. Does not matter if the service is available locally. -
NetworkLatencyStrategyFactory
public NetworkLatencyStrategyFactory(boolean preferLocal) Constructor that can be configured to use local services if possible.- Parameters:
preferLocal- invoke local actions if possible
-
-
Method Details
-
started
Initializes strategy instance.- Specified by:
startedin interfaceMoleculerLifecycle- Overrides:
startedin classMoleculerComponent- Parameters:
broker- parent ServiceBroker- Throws:
Exception
-
stopped
public void stopped()- Specified by:
stoppedin interfaceMoleculerLifecycle- Overrides:
stoppedin classMoleculerComponent
-
sendNextPing
protected void sendNextPing() -
sendPing
-
getAverageResponseTime
-
create
- Specified by:
createin classStrategyFactory
-
getSampleCount
public int getSampleCount() -
setSampleCount
public void setSampleCount(int sampleCount) -
getPingInterval
public int getPingInterval() -
setPingInterval
public void setPingInterval(int pingInterval) -
getPingTimeout
public long getPingTimeout() -
setPingTimeout
public void setPingTimeout(long pingTimeout) -
getCollectCount
public int getCollectCount() -
setCollectCount
public void setCollectCount(int collectCount)
-