Class JdbcNodeServiceAuditor
java.lang.Object
net.solarnetwork.central.common.dao.jdbc.JdbcNodeServiceAuditor
- All Implemented Interfaces:
NodeServiceAuditor,net.solarnetwork.service.PingTest,net.solarnetwork.service.ServiceLifecycleObserver
- Direct Known Subclasses:
JdbcUserServiceAuditor
public class JdbcNodeServiceAuditor
extends Object
implements NodeServiceAuditor, net.solarnetwork.service.PingTest, net.solarnetwork.service.ServiceLifecycleObserver
JDBC based implementation of
NodeServiceAuditor.
This service coalesces updates per node/service/hour in memory and flushes these to the database via a single "writer" thread after a small delay. This design is meant to support better throughput of audit updates, but has the potential to drop some count values if the service is restarted.
-
Nested Class Summary
Nested classes/interfaces inherited from interface net.solarnetwork.service.PingTest
net.solarnetwork.service.PingTest.Result -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final PatternA regular expression that matches if a JDBC statement is aCallableStatement.static final longThe default value for theconnecitonRecoveryDelayproperty.static final longThe default value for theflushDelayproperty.static final StringThe default value for thenodeServiceIncrementSqlproperty.static final longThe default value for theupdateDelayproperty.protected final org.slf4j.LoggerA class-level logger. -
Constructor Summary
ConstructorsConstructorDescriptionJdbcNodeServiceAuditor(DataSource dataSource) Constructor.JdbcNodeServiceAuditor(DataSource dataSource, ConcurrentMap<net.solarnetwork.domain.datum.DatumId, AtomicInteger> nodeServiceCounters, Clock clock, net.solarnetwork.util.StatTracker statCounter) Constructor. -
Method Summary
Modifier and TypeMethodDescriptionvoidauditNodeService(Long nodeId, String service, int count) Audit the creation of an instruction for a node.voidDisable writing.voidEnable writing, and wait until the writing thread is going.Get the clock used for auditing.longnet.solarnetwork.service.PingTest.ResultvoidCause the writing thread to re-connect to the database with a new connection.voidvoidvoidsetConnectionRecoveryDelay(long connectionRecoveryDelay) Set the delay, in milliseconds, to wait after a JDBC connection error before trying to recover and connect again.voidsetFlushDelay(long flushDelay) Set the delay, in milliseconds, between flushing cached audit data.voidThe JDBC statement to execute for incrementing a count for a single date, node, and source.voidsetStatLogUpdateCount(int statLogUpdateCount) Set the statistic log update count.voidsetUpdateDelay(long updateDelay) Set the delay, in milliseconds, to wait after executing JDBC statements within a loop before executing another statement.
-
Field Details
-
DEFAULT_UPDATE_DELAY
public static final long DEFAULT_UPDATE_DELAYThe default value for theupdateDelayproperty.- See Also:
-
DEFAULT_FLUSH_DELAY
public static final long DEFAULT_FLUSH_DELAYThe default value for theflushDelayproperty.- See Also:
-
DEFAULT_CONNECTION_RECOVERY_DELAY
public static final long DEFAULT_CONNECTION_RECOVERY_DELAYThe default value for theconnecitonRecoveryDelayproperty.- See Also:
-
DEFAULT_NODE_SERVICE_INCREMENT_SQL
The default value for thenodeServiceIncrementSqlproperty.- See Also:
-
CALLABLE_STATEMENT_REGEX
A regular expression that matches if a JDBC statement is aCallableStatement. -
log
protected final org.slf4j.Logger logA class-level logger.
-
-
Constructor Details
-
JdbcNodeServiceAuditor
Constructor.- Parameters:
dataSource- the JDBC DataSource- Throws:
IllegalArgumentException- if any argument is null
-
JdbcNodeServiceAuditor
public JdbcNodeServiceAuditor(DataSource dataSource, ConcurrentMap<net.solarnetwork.domain.datum.DatumId, AtomicInteger> nodeServiceCounters, Clock clock, net.solarnetwork.util.StatTracker statCounter) Constructor.- Parameters:
dataSource- the JDBC DataSourcenodeServiceCounters- the node source counters mapclock- the clock to use- Throws:
IllegalArgumentException- if any argument is null
-
-
Method Details
-
serviceDidStartup
public void serviceDidStartup()- Specified by:
serviceDidStartupin interfacenet.solarnetwork.service.ServiceLifecycleObserver
-
serviceDidShutdown
public void serviceDidShutdown()- Specified by:
serviceDidShutdownin interfacenet.solarnetwork.service.ServiceLifecycleObserver
-
getAuditClock
Description copied from interface:NodeServiceAuditorGet the clock used for auditing.This clock may bucket time into discreet intervals.
- Specified by:
getAuditClockin interfaceNodeServiceAuditor- Returns:
- the clock never null
-
auditNodeService
Description copied from interface:NodeServiceAuditorAudit the creation of an instruction for a node.- Specified by:
auditNodeServicein interfaceNodeServiceAuditor- Parameters:
nodeId- the node IDservice- the service namecount- the amount to add
-
reconnectWriter
public void reconnectWriter()Cause the writing thread to re-connect to the database with a new connection. -
enableWriting
public void enableWriting()Enable writing, and wait until the writing thread is going. -
disableWriting
public void disableWriting()Disable writing. -
getPingTestId
- Specified by:
getPingTestIdin interfacenet.solarnetwork.service.PingTest
-
getPingTestName
- Specified by:
getPingTestNamein interfacenet.solarnetwork.service.PingTest
-
getPingTestMaximumExecutionMilliseconds
public long getPingTestMaximumExecutionMilliseconds()- Specified by:
getPingTestMaximumExecutionMillisecondsin interfacenet.solarnetwork.service.PingTest
-
performPingTest
- Specified by:
performPingTestin interfacenet.solarnetwork.service.PingTest- Throws:
Exception
-
setFlushDelay
public void setFlushDelay(long flushDelay) Set the delay, in milliseconds, between flushing cached audit data.- Parameters:
flushDelay- the delay, in milliseconds; defaults toDEFAULT_FLUSH_DELAY- Throws:
IllegalArgumentException- ifflushDelayis < 0
-
setConnectionRecoveryDelay
public void setConnectionRecoveryDelay(long connectionRecoveryDelay) Set the delay, in milliseconds, to wait after a JDBC connection error before trying to recover and connect again.- Parameters:
connectionRecoveryDelay- the delay, in milliseconds; defaults t[DEFAULT_CONNECTION_RECOVERY_DELAY- Throws:
IllegalArgumentException- ifconnectionRecoveryDelayis < 0
-
setUpdateDelay
public void setUpdateDelay(long updateDelay) Set the delay, in milliseconds, to wait after executing JDBC statements within a loop before executing another statement.- Parameters:
updateDelay- the delay, in milliseconds; defaults t[DEFAULT_UPDATE_DELAY- Throws:
IllegalArgumentException- ifupdateDelayis < 0
-
setNodeServiceIncrementSql
The JDBC statement to execute for incrementing a count for a single date, node, and source.The statement must accept the following parameters:
- long - the node ID
- string - the service name
- timestamp - the audit date
- integer - the instruction count to add
- Parameters:
sql- the SQL statement to use; defaults toinvalid reference
#DEFAULT_NODE_SOURCE_INCREMENT_SQL
-
setStatLogUpdateCount
public void setStatLogUpdateCount(int statLogUpdateCount) Set the statistic log update count.Setting this to something greater than 0 will cause INFO level statistic log entries to be emitted every
statLogUpdateCountrecords have been updated in the database.- Parameters:
statLogUpdateCount- the update count; defaults toinvalid reference
#DEFAULT_STAT_LOG_UPDATE_COUNT
-