initial
This commit is contained in:
26
.classpath
Normal file
26
.classpath
Normal file
@ -0,0 +1,26 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<classpath>
|
||||||
|
<classpathentry kind="src" output="target/classes" path="src/main/java">
|
||||||
|
<attributes>
|
||||||
|
<attribute name="optional" value="true"/>
|
||||||
|
<attribute name="maven.pomderived" value="true"/>
|
||||||
|
</attributes>
|
||||||
|
</classpathentry>
|
||||||
|
<classpathentry kind="src" output="target/test-classes" path="src/test/java">
|
||||||
|
<attributes>
|
||||||
|
<attribute name="optional" value="true"/>
|
||||||
|
<attribute name="maven.pomderived" value="true"/>
|
||||||
|
</attributes>
|
||||||
|
</classpathentry>
|
||||||
|
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8">
|
||||||
|
<attributes>
|
||||||
|
<attribute name="maven.pomderived" value="true"/>
|
||||||
|
</attributes>
|
||||||
|
</classpathentry>
|
||||||
|
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
|
||||||
|
<attributes>
|
||||||
|
<attribute name="maven.pomderived" value="true"/>
|
||||||
|
</attributes>
|
||||||
|
</classpathentry>
|
||||||
|
<classpathentry kind="output" path="target/classes"/>
|
||||||
|
</classpath>
|
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
|||||||
|
target
|
23
.project
Normal file
23
.project
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<projectDescription>
|
||||||
|
<name>MeasurementDatabaseEngine</name>
|
||||||
|
<comment></comment>
|
||||||
|
<projects>
|
||||||
|
</projects>
|
||||||
|
<buildSpec>
|
||||||
|
<buildCommand>
|
||||||
|
<name>org.eclipse.jdt.core.javabuilder</name>
|
||||||
|
<arguments>
|
||||||
|
</arguments>
|
||||||
|
</buildCommand>
|
||||||
|
<buildCommand>
|
||||||
|
<name>org.eclipse.m2e.core.maven2Builder</name>
|
||||||
|
<arguments>
|
||||||
|
</arguments>
|
||||||
|
</buildCommand>
|
||||||
|
</buildSpec>
|
||||||
|
<natures>
|
||||||
|
<nature>org.eclipse.jdt.core.javanature</nature>
|
||||||
|
<nature>org.eclipse.m2e.core.maven2Nature</nature>
|
||||||
|
</natures>
|
||||||
|
</projectDescription>
|
5
.settings/org.eclipse.jdt.core.prefs
Normal file
5
.settings/org.eclipse.jdt.core.prefs
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
eclipse.preferences.version=1
|
||||||
|
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8
|
||||||
|
org.eclipse.jdt.core.compiler.compliance=1.8
|
||||||
|
org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning
|
||||||
|
org.eclipse.jdt.core.compiler.source=1.8
|
4
.settings/org.eclipse.m2e.core.prefs
Normal file
4
.settings/org.eclipse.m2e.core.prefs
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
activeProfiles=
|
||||||
|
eclipse.preferences.version=1
|
||||||
|
resolveWorkspaceProjects=true
|
||||||
|
version=1
|
3
.vscode/settings.json
vendored
Normal file
3
.vscode/settings.json
vendored
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
{
|
||||||
|
"java.configuration.updateBuildConfiguration": "automatic"
|
||||||
|
}
|
9
measurementDataEngine.props
Normal file
9
measurementDataEngine.props
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
db.period = 3600
|
||||||
|
db.url = jdbc:mysql://localhost/smarthome
|
||||||
|
db.driver = com.mysql.jdbc.Driver
|
||||||
|
db.user = smarthome
|
||||||
|
db.password = smarthome123
|
||||||
|
|
||||||
|
jms.broker = tcp://localhost:61616
|
||||||
|
jms.clientid = mdb
|
||||||
|
jms.parseddata.topic = IoT/Measurement
|
54
pom.xml
Normal file
54
pom.xml
Normal file
@ -0,0 +1,54 @@
|
|||||||
|
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
<groupId>de.hottis.measurementDatabaseEngine</groupId>
|
||||||
|
<artifactId>MeasurementDatabaseEngine</artifactId>
|
||||||
|
<packaging>jar</packaging>
|
||||||
|
<version>1.0-SNAPSHOT</version>
|
||||||
|
<name>MeasurementDatabaseEngine</name>
|
||||||
|
<url>http://maven.apache.org</url>
|
||||||
|
<properties>
|
||||||
|
<maven.compiler.source>1.8</maven.compiler.source>
|
||||||
|
<maven.compiler.target>1.8</maven.compiler.target>
|
||||||
|
</properties>
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>junit</groupId>
|
||||||
|
<artifactId>junit</artifactId>
|
||||||
|
<version>3.8.1</version>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>de.hottis.common</groupId>
|
||||||
|
<artifactId>HottisLibJava</artifactId>
|
||||||
|
<version>1.0-SNAPSHOT</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>de.hottis.smarthomelib</groupId>
|
||||||
|
<artifactId>SmarthomeLib</artifactId>
|
||||||
|
<version>1.0-SNAPSHOT</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>mysql</groupId>
|
||||||
|
<artifactId>mysql-connector-java</artifactId>
|
||||||
|
<version>5.1.44</version>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
<build>
|
||||||
|
<plugins>
|
||||||
|
<plugin>
|
||||||
|
<artifactId>maven-assembly-plugin</artifactId>
|
||||||
|
<configuration>
|
||||||
|
<archive>
|
||||||
|
<manifest>
|
||||||
|
<mainClass>de.hottis.measurementDatabaseEngine.MeasurementDatabaseEngine</mainClass>
|
||||||
|
</manifest>
|
||||||
|
</archive>
|
||||||
|
<descriptorRefs>
|
||||||
|
<descriptorRef>jar-with-dependencies</descriptorRef>
|
||||||
|
</descriptorRefs>
|
||||||
|
</configuration>
|
||||||
|
</plugin>
|
||||||
|
</plugins>
|
||||||
|
</build>
|
||||||
|
</project>
|
@ -0,0 +1,190 @@
|
|||||||
|
package de.hottis.measurementDatabaseEngine;
|
||||||
|
|
||||||
|
import java.sql.Connection;
|
||||||
|
import java.sql.DriverManager;
|
||||||
|
import java.sql.PreparedStatement;
|
||||||
|
import java.sql.SQLException;
|
||||||
|
import java.sql.Timestamp;
|
||||||
|
import java.time.LocalDateTime;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Properties;
|
||||||
|
import java.util.Timer;
|
||||||
|
|
||||||
|
import org.apache.logging.log4j.LogManager;
|
||||||
|
import org.apache.logging.log4j.Logger;
|
||||||
|
|
||||||
|
import de.hottis.common.ITriggerable;
|
||||||
|
import de.hottis.common.MyQueue;
|
||||||
|
import de.hottis.common.TriggerTimer;
|
||||||
|
import de.hottis.smarthomelib.ADataObject;
|
||||||
|
|
||||||
|
public class DatabaseEngine extends Thread implements ITriggerable {
|
||||||
|
static final String DATABASE_ENGINE_PERIOD_PROP = "db.period";
|
||||||
|
static final String DATABASE_URL_PROP = "db.url";
|
||||||
|
static final String DATABASE_USER_PROP = "db.user";
|
||||||
|
static final String DATABASE_PASSWORD_PROP = "db.password";
|
||||||
|
static final String DATABASE_DRIVER_PROP = "db.driver";
|
||||||
|
|
||||||
|
final protected Logger logger = LogManager.getRootLogger();
|
||||||
|
|
||||||
|
private Properties config;
|
||||||
|
private MyQueue<ADataObject> queue;
|
||||||
|
private boolean stop;
|
||||||
|
private boolean triggerFlag;
|
||||||
|
private Timer timer;
|
||||||
|
private int period;
|
||||||
|
private String dbUrl;
|
||||||
|
private String dbUsername;
|
||||||
|
private String dbPassword;
|
||||||
|
|
||||||
|
public DatabaseEngine(Properties config, MyQueue<ADataObject> queue) {
|
||||||
|
super("MeasurementDatabaseEngine.DatabaseEngine");
|
||||||
|
this.config = config;
|
||||||
|
this.queue = queue;
|
||||||
|
this.stop = false;
|
||||||
|
this.triggerFlag = false;
|
||||||
|
this.period = Integer.parseInt(this.config.getProperty(DATABASE_ENGINE_PERIOD_PROP));
|
||||||
|
this.dbUrl = this.config.getProperty(DATABASE_URL_PROP);
|
||||||
|
this.dbUsername = this.config.getProperty(DATABASE_USER_PROP);
|
||||||
|
this.dbPassword = this.config.getProperty(DATABASE_PASSWORD_PROP);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void requestShutdown() {
|
||||||
|
logger.info("Shutdown of database engine requested");
|
||||||
|
this.stop = true;
|
||||||
|
try {
|
||||||
|
this.join();
|
||||||
|
logger.info("Database engine is down");
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
logger.error("Waiting for shutdown of database engine interrupted");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public synchronized void trigger() {
|
||||||
|
logger.debug("DatabaseEngine triggered");
|
||||||
|
triggerFlag = true;
|
||||||
|
notify();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void init() throws MeasurementDatabaseEngineException {
|
||||||
|
timer = new Timer("DatabaseEngineTrigger");
|
||||||
|
timer.schedule(new TriggerTimer(this), 0, period * 1000);
|
||||||
|
try {
|
||||||
|
Class.forName(config.getProperty(DATABASE_DRIVER_PROP));
|
||||||
|
} catch (ClassNotFoundException e) {
|
||||||
|
logger.error("Database driver class not found", e);
|
||||||
|
throw new MeasurementDatabaseEngineException("Database driver class not found", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private String createStatementFromDataObject(ADataObject ado) {
|
||||||
|
StringBuffer sb = new StringBuffer();
|
||||||
|
sb.append("INSERT INTO " + ado.getTableName());
|
||||||
|
sb.append("(name,ts,");
|
||||||
|
sb.append(String.join(",", ado.getValues().keySet()));
|
||||||
|
sb.append(") ");
|
||||||
|
sb.append("VALUES(?,?,");
|
||||||
|
|
||||||
|
String[] marks = (String[]) ado.getValues().values().stream()
|
||||||
|
.map(c -> "?")
|
||||||
|
.toArray(String[]::new);
|
||||||
|
sb.append(String.join(",", marks));
|
||||||
|
|
||||||
|
sb.append(")");
|
||||||
|
return sb.toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void bindValue(PreparedStatement pstmt, int pos, Object o) throws SQLException {
|
||||||
|
if (o instanceof Integer) {
|
||||||
|
pstmt.setInt(pos, (Integer)o);
|
||||||
|
} else if (o instanceof Byte) {
|
||||||
|
pstmt.setByte(pos, (Byte)o);
|
||||||
|
} else if (o instanceof Double) {
|
||||||
|
pstmt.setDouble(pos, (Double)o);
|
||||||
|
} else if (o instanceof String) {
|
||||||
|
pstmt.setString(pos, (String)o);
|
||||||
|
} else if (o instanceof LocalDateTime) {
|
||||||
|
pstmt.setTimestamp(pos, Timestamp.valueOf(((LocalDateTime)o)));
|
||||||
|
} else {
|
||||||
|
throw new SQLException("illegal parameter type: " + o.getClass().getName());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public synchronized void run() {
|
||||||
|
while (! stop) {
|
||||||
|
logger.debug("DatabaseEngine is about to wait for (regularly) " + period + "s");
|
||||||
|
while (! triggerFlag) {
|
||||||
|
try {
|
||||||
|
wait();
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
}
|
||||||
|
}
|
||||||
|
triggerFlag = false;
|
||||||
|
logger.debug("DatabaseEngine has received trigger");
|
||||||
|
|
||||||
|
Connection dbCon = null;
|
||||||
|
HashMap<String, PreparedStatement> preparedStatements = new HashMap<String, PreparedStatement>();
|
||||||
|
try {
|
||||||
|
int itemCnt = 0;
|
||||||
|
|
||||||
|
while (true) {
|
||||||
|
try {
|
||||||
|
ADataObject ado = queue.dequeue();
|
||||||
|
if (ado == null) {
|
||||||
|
logger.warn("DatabaseEngine found no data");
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
dbCon = DriverManager.getConnection(dbUrl, dbUsername, dbPassword);
|
||||||
|
|
||||||
|
String key = ado.getClass().getName();
|
||||||
|
PreparedStatement pstmt;
|
||||||
|
if (! preparedStatements.containsKey(key)) {
|
||||||
|
pstmt = dbCon.prepareStatement(createStatementFromDataObject(ado));
|
||||||
|
preparedStatements.put(key, pstmt);
|
||||||
|
} else {
|
||||||
|
pstmt = preparedStatements.get(key);
|
||||||
|
}
|
||||||
|
bindValue(pstmt, 1, ado.getName());
|
||||||
|
bindValue(pstmt, 2, ado.getTimestamp());
|
||||||
|
int pos = 3;
|
||||||
|
for (Object o : ado.getValues().values()) {
|
||||||
|
bindValue(pstmt, pos, o);
|
||||||
|
pos++;
|
||||||
|
}
|
||||||
|
itemCnt++;
|
||||||
|
logger.info("DatabaseEngine received (" + itemCnt + ") " + ado);
|
||||||
|
logger.info("Statement is " + pstmt.toString());
|
||||||
|
pstmt.execute();
|
||||||
|
logger.info("Database insert executed");
|
||||||
|
pstmt.clearParameters();
|
||||||
|
} catch (SQLException e) {
|
||||||
|
logger.error("SQLException in inner database engine loop", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (Exception e) {
|
||||||
|
logger.error("Exception in outer database engine loop", e);
|
||||||
|
} finally {
|
||||||
|
for (PreparedStatement p : preparedStatements.values()) {
|
||||||
|
try {
|
||||||
|
p.close();
|
||||||
|
} catch (SQLException e) {
|
||||||
|
logger.warn("SQLException when closing prepared statement, nothing will be done");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
preparedStatements.clear();
|
||||||
|
if (dbCon != null) {
|
||||||
|
try {
|
||||||
|
dbCon.close();
|
||||||
|
} catch (SQLException e) {
|
||||||
|
logger.warn("SQLException when closing connection, nothing will be done");
|
||||||
|
} finally {
|
||||||
|
dbCon = null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
logger.info("Database engine is terminating");
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,36 @@
|
|||||||
|
package de.hottis.measurementDatabaseEngine;
|
||||||
|
|
||||||
|
import java.io.FileInputStream;
|
||||||
|
import java.util.Properties;
|
||||||
|
|
||||||
|
import org.apache.logging.log4j.LogManager;
|
||||||
|
import org.apache.logging.log4j.Logger;
|
||||||
|
|
||||||
|
import de.hottis.common.JmsTopic;
|
||||||
|
import de.hottis.smarthomelib.ADataObject;
|
||||||
|
|
||||||
|
|
||||||
|
public class MeasurementDatabaseEngine {
|
||||||
|
static final String PROPS_FILENAME = "measurementDataEngine.props";
|
||||||
|
static final Logger logger = LogManager.getRootLogger();
|
||||||
|
|
||||||
|
|
||||||
|
public static void main(String[] args) throws Exception {
|
||||||
|
logger.info("MeasurementDatabaseEngine starting");
|
||||||
|
|
||||||
|
final Properties config = new Properties();
|
||||||
|
try (FileInputStream propsFileInputStream = new FileInputStream(PROPS_FILENAME)) {
|
||||||
|
config.load(propsFileInputStream);
|
||||||
|
}
|
||||||
|
logger.debug("Configuration loaded");
|
||||||
|
|
||||||
|
JmsTopic<ADataObject> queue = new JmsTopic<ADataObject>(config, JmsTopic.Mode.CONSUMER);
|
||||||
|
queue.init();
|
||||||
|
|
||||||
|
DatabaseEngine databaseEngine = new DatabaseEngine(config, queue);
|
||||||
|
databaseEngine.init();
|
||||||
|
databaseEngine.start();
|
||||||
|
logger.debug("DatabaseEngine started");
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,14 @@
|
|||||||
|
package de.hottis.measurementDatabaseEngine;
|
||||||
|
|
||||||
|
public class MeasurementDatabaseEngineException extends Exception {
|
||||||
|
private static final long serialVersionUID = 1L;
|
||||||
|
|
||||||
|
public MeasurementDatabaseEngineException(String msg, Throwable cause) {
|
||||||
|
super(msg, cause);
|
||||||
|
}
|
||||||
|
|
||||||
|
public MeasurementDatabaseEngineException(String msg) {
|
||||||
|
super(msg);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
17
src/main/resources/log4j2.xml
Normal file
17
src/main/resources/log4j2.xml
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<Configuration debug="false">
|
||||||
|
<Appenders>
|
||||||
|
<Console name="ConsoleAppender" target="SYSTEM_OUT">
|
||||||
|
<PatternLayout pattern="%d [%t, %F, %L] %-5level - %msg%n%throwable"/>
|
||||||
|
</Console>
|
||||||
|
<File name="FileAppender" fileName="/tmp/MeasurementCollector.log">
|
||||||
|
<PatternLayout pattern="%d [%t, %F, %L] %-5level - %msg%n%throwable"/>
|
||||||
|
</File>
|
||||||
|
</Appenders>
|
||||||
|
<Loggers>
|
||||||
|
<Root level="debug">
|
||||||
|
<AppenderRef ref="ConsoleAppender"/>
|
||||||
|
<AppenderRef ref="FileAppender"/>
|
||||||
|
</Root>
|
||||||
|
</Loggers>
|
||||||
|
</Configuration>
|
@ -0,0 +1,38 @@
|
|||||||
|
package de.hottis.measurementDatabaseEngine;
|
||||||
|
|
||||||
|
import junit.framework.Test;
|
||||||
|
import junit.framework.TestCase;
|
||||||
|
import junit.framework.TestSuite;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Unit test for simple App.
|
||||||
|
*/
|
||||||
|
public class AppTest
|
||||||
|
extends TestCase
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Create the test case
|
||||||
|
*
|
||||||
|
* @param testName name of the test case
|
||||||
|
*/
|
||||||
|
public AppTest( String testName )
|
||||||
|
{
|
||||||
|
super( testName );
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return the suite of tests being tested
|
||||||
|
*/
|
||||||
|
public static Test suite()
|
||||||
|
{
|
||||||
|
return new TestSuite( AppTest.class );
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Rigourous Test :-)
|
||||||
|
*/
|
||||||
|
public void testApp()
|
||||||
|
{
|
||||||
|
assertTrue( true );
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user