database engine initially works

This commit is contained in:
Wolfgang Hottgenroth 2017-11-21 22:16:34 +01:00
parent 5896212ed9
commit 2179311a1d
6 changed files with 97 additions and 24 deletions

View File

@ -1,7 +1,7 @@
;mqtt.broker = tcp://172.16.2.15:1883 mqtt.broker = tcp://172.16.2.15:1883
mqtt.broker = tcp://eupenstrasse20.dynamic.hottis.de:2883 ;mqtt.broker = tcp://eupenstrasse20.dynamic.hottis.de:2883
mqtt.username = tron ;mqtt.username = tron
mqtt.password = geheim123 ;mqtt.password = geheim123
mbus.dataparser.1 = light,Light,de.hottis.measurementCollector.FinderOnePhasePowerMeter mbus.dataparser.1 = light,Light,de.hottis.measurementCollector.FinderOnePhasePowerMeter
mbus.dataparser.2 = computer,Computer,de.hottis.measurementCollector.FinderOnePhasePowerMeter mbus.dataparser.2 = computer,Computer,de.hottis.measurementCollector.FinderOnePhasePowerMeter

View File

@ -1,4 +1,8 @@
db.period = 3600 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.broker = tcp://localhost:61616
jms.clientid = mdb jms.clientid = mdb

View File

@ -1,10 +1,16 @@
package de.hottis.measurementCollector; package de.hottis.measurementCollector;
import java.sql.Connection; import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement; import java.sql.PreparedStatement;
import java.sql.SQLException; import java.sql.SQLException;
import java.sql.Timestamp;
import java.time.LocalDateTime;
import java.util.Collection;
import java.util.HashMap;
import java.util.Properties; import java.util.Properties;
import java.util.Timer; import java.util.Timer;
import java.util.stream.Collectors;
import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.Logger;
@ -12,6 +18,9 @@ import org.apache.logging.log4j.Logger;
public class DatabaseEngine extends Thread implements ITriggerable { public class DatabaseEngine extends Thread implements ITriggerable {
static final String DATABASE_ENGINE_PERIOD_PROP = "db.period"; static final String DATABASE_ENGINE_PERIOD_PROP = "db.period";
static final String DATABASE_URL_PROP = "db.url"; 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(); final protected Logger logger = LogManager.getRootLogger();
@ -22,6 +31,8 @@ public class DatabaseEngine extends Thread implements ITriggerable {
private Timer timer; private Timer timer;
private int period; private int period;
private String dbUrl; private String dbUrl;
private String dbUsername;
private String dbPassword;
public DatabaseEngine(Properties config, MyQueue<ADataObject> queue) { public DatabaseEngine(Properties config, MyQueue<ADataObject> queue) {
super("MeasurementCollector.DatabaseEngine"); super("MeasurementCollector.DatabaseEngine");
@ -31,6 +42,8 @@ public class DatabaseEngine extends Thread implements ITriggerable {
this.triggerFlag = false; this.triggerFlag = false;
this.period = Integer.parseInt(this.config.getProperty(DATABASE_ENGINE_PERIOD_PROP)); this.period = Integer.parseInt(this.config.getProperty(DATABASE_ENGINE_PERIOD_PROP));
this.dbUrl = this.config.getProperty(DATABASE_URL_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() { public void requestShutdown() {
@ -53,12 +66,48 @@ public class DatabaseEngine extends Thread implements ITriggerable {
public void init() throws MeasurementCollectorException { public void init() throws MeasurementCollectorException {
timer = new Timer("DatabaseEngineTrigger"); timer = new Timer("DatabaseEngineTrigger");
timer.schedule(new TriggerTimer(this), 0, period * 1000); 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 MeasurementCollectorException("Database driver class not found", e);
}
} }
private String createStatementFromDataObject(ADataObject ado) { private String createStatementFromDataObject(ADataObject ado) {
return ""; 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 @Override
public synchronized void run() { public synchronized void run() {
while (! stop) { while (! stop) {
@ -73,46 +122,62 @@ public class DatabaseEngine extends Thread implements ITriggerable {
logger.debug("DatabaseEngine has received trigger"); logger.debug("DatabaseEngine has received trigger");
Connection dbCon = null; Connection dbCon = null;
HashMap<String, PreparedStatement> preparedStatements = new HashMap<String, PreparedStatement>();
try { try {
int itemCnt = 0; int itemCnt = 0;
while (true) { while (true) {
PreparedStatement pstmt = null;
try { try {
ADataObject ado = queue.dequeue(); ADataObject ado = queue.dequeue();
if (ado == null) { if (ado == null) {
logger.warn("DatabaseEngine found no data"); logger.warn("DatabaseEngine found no data");
break; break;
} }
String stmtTxt = createStatementFromDataObject(ado); dbCon = DriverManager.getConnection(dbUrl, dbUsername, dbPassword);
pstmt = dbCon.prepareStatement(stmtTxt);
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()) { for (Object o : ado.getValues().values()) {
bindValue(pstmt, pos, o);
pos++;
} }
itemCnt++; itemCnt++;
logger.info("DatabaseEngine received (" + itemCnt + ") " + ado); logger.info("DatabaseEngine received (" + itemCnt + ") " + ado);
logger.info("Statement is " + pstmt.toString());
pstmt.execute();
logger.info("Database insert executed");
pstmt.clearParameters();
} catch (SQLException e) { } catch (SQLException e) {
logger.error("SQLException in inner database engine loop", e); logger.error("SQLException in inner database engine loop", e);
} finally {
if (pstmt != null) {
try {
pstmt.close();
pstmt = null;
} catch (SQLException e) {
logger.warn("SQLException when closing statement, nothing will be done");
}
}
} }
} }
} catch (Exception e) { } catch (Exception e) {
logger.error("Exception in outer database engine loop", e); logger.error("Exception in outer database engine loop", e);
} finally { } finally {
if (dbCon ! null) { 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 { try {
dbCon.close(); dbCon.close();
dbCon = null;
} catch (SQLException e) { } catch (SQLException e) {
logger.warn("SQLException when closing connection, nothing will be done"); logger.warn("SQLException when closing connection, nothing will be done");
} finally {
dbCon = null;
} }
} }
} }

View File

@ -1,7 +1,7 @@
;mqtt.broker = tcp://172.16.2.15:1883 mqtt.broker = tcp://172.16.2.15:1883
mqtt.broker = tcp://eupenstrasse20.dynamic.hottis.de:2883 ;mqtt.broker = tcp://eupenstrasse20.dynamic.hottis.de:2883
mqtt.username = tron ;mqtt.username = tron
mqtt.password = geheim123 ;mqtt.password = geheim123
mbus.dataparser.1 = light,Light,de.hottis.measurementCollector.FinderOnePhasePowerMeter mbus.dataparser.1 = light,Light,de.hottis.measurementCollector.FinderOnePhasePowerMeter
mbus.dataparser.2 = computer,Computer,de.hottis.measurementCollector.FinderOnePhasePowerMeter mbus.dataparser.2 = computer,Computer,de.hottis.measurementCollector.FinderOnePhasePowerMeter

View File

@ -1,4 +1,8 @@
db.period = 3600 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.broker = tcp://localhost:61616
jms.clientid = mdb jms.clientid = mdb