commit
86d5191601
12 changed files with 352 additions and 0 deletions
@ -0,0 +1,20 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?> |
||||
<classpath> |
||||
<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="src" output="target/classes" path="src"> |
||||
<attributes> |
||||
<attribute name="optional" value="true"/> |
||||
<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> |
||||
@ -0,0 +1,23 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?> |
||||
<projectDescription> |
||||
<name>nr data damon</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.m2e.core.maven2Nature</nature> |
||||
<nature>org.eclipse.jdt.core.javanature</nature> |
||||
</natures> |
||||
</projectDescription> |
||||
@ -0,0 +1,13 @@
|
||||
eclipse.preferences.version=1 |
||||
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled |
||||
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8 |
||||
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve |
||||
org.eclipse.jdt.core.compiler.compliance=1.8 |
||||
org.eclipse.jdt.core.compiler.debug.lineNumber=generate |
||||
org.eclipse.jdt.core.compiler.debug.localVariable=generate |
||||
org.eclipse.jdt.core.compiler.debug.sourceFile=generate |
||||
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error |
||||
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error |
||||
org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning |
||||
org.eclipse.jdt.core.compiler.release=disabled |
||||
org.eclipse.jdt.core.compiler.source=1.8 |
||||
@ -0,0 +1,4 @@
|
||||
activeProfiles= |
||||
eclipse.preferences.version=1 |
||||
resolveWorkspaceProjects=true |
||||
version=1 |
||||
@ -0,0 +1,55 @@
|
||||
<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/xsd/maven-4.0.0.xsd"> |
||||
<modelVersion>4.0.0</modelVersion> |
||||
<groupId>org.leolo.rail</groupId> |
||||
<artifactId>nr-data-damon</artifactId> |
||||
<version>0.0.1-SNAPSHOT</version> |
||||
<build> |
||||
<sourceDirectory>src</sourceDirectory> |
||||
<plugins> |
||||
<plugin> |
||||
<artifactId>maven-compiler-plugin</artifactId> |
||||
<version>3.8.0</version> |
||||
<configuration> |
||||
<source>1.8</source> |
||||
<target>1.8</target> |
||||
</configuration> |
||||
</plugin> |
||||
</plugins> |
||||
</build> |
||||
|
||||
<dependencies> |
||||
<dependency> |
||||
<groupId>org.mariadb.jdbc</groupId> |
||||
<artifactId>mariadb-java-client</artifactId> |
||||
<version>2.7.2</version> |
||||
</dependency> |
||||
<dependency> |
||||
<groupId>org.json</groupId> |
||||
<artifactId>json</artifactId> |
||||
<version>20210307</version> |
||||
</dependency> |
||||
<dependency> |
||||
<groupId>org.apache.logging.log4j</groupId> |
||||
<artifactId>log4j-core</artifactId> |
||||
<version>2.13.3</version> |
||||
</dependency> |
||||
<dependency> |
||||
<groupId>org.apache.logging.log4j</groupId> |
||||
<artifactId>log4j-api</artifactId> |
||||
<version>2.13.3</version> |
||||
</dependency> |
||||
<dependency> |
||||
<groupId>org.quartz-scheduler</groupId> |
||||
<artifactId>quartz</artifactId> |
||||
<version>2.3.2</version> |
||||
</dependency> |
||||
<dependency> |
||||
<groupId>org.apache.activemq</groupId> |
||||
<artifactId>activemq-stomp</artifactId> |
||||
<version>5.16.4</version> |
||||
</dependency> |
||||
|
||||
</dependencies> |
||||
</project> |
||||
@ -0,0 +1,14 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?> |
||||
<Configuration status="warn"> |
||||
<Appenders> |
||||
<Console name="Console" target="SYSTEM_OUT"> |
||||
<PatternLayout |
||||
pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n" /> |
||||
</Console> |
||||
</Appenders> |
||||
<Loggers> |
||||
<Root level="debug"> |
||||
<AppenderRef ref="Console" /> |
||||
</Root> |
||||
</Loggers> |
||||
</Configuration> |
||||
@ -0,0 +1,64 @@
|
||||
package org.leolo.rail; |
||||
|
||||
|
||||
import java.io.FileReader; |
||||
import java.io.IOException; |
||||
import java.util.Properties; |
||||
import java.util.function.BiConsumer; |
||||
|
||||
import org.apache.logging.log4j.LogManager; |
||||
import org.apache.logging.log4j.Logger; |
||||
|
||||
public class ConfigurationManager { |
||||
private static Logger log = LogManager.getLogger(ConfigurationManager.class); |
||||
|
||||
private static ConfigurationManager instance; |
||||
private Properties prop = new Properties(); |
||||
|
||||
public synchronized static ConfigurationManager getInstance() { |
||||
if(instance==null) { |
||||
instance = new ConfigurationManager(); |
||||
} |
||||
return instance; |
||||
} |
||||
|
||||
private ConfigurationManager() { |
||||
try(FileReader fr = new FileReader("configuration.properties")){ |
||||
log.debug("Loading properties file"); |
||||
prop.load(fr); |
||||
log.info("{} entries loaded", prop.size()); |
||||
}catch(IOException e) { |
||||
log.fatal(e.getMessage(), e); |
||||
System.exit(1); |
||||
} |
||||
|
||||
} |
||||
|
||||
public void forEach(BiConsumer<? super Object, ? super Object> action) { |
||||
prop.forEach(action); |
||||
} |
||||
|
||||
public Object get(Object key) { |
||||
return prop.get(key); |
||||
} |
||||
|
||||
public Object getOrDefault(Object key, Object defaultValue) { |
||||
return prop.getOrDefault(key, defaultValue); |
||||
} |
||||
|
||||
public String getProperty(String key, String defaultValue) { |
||||
return prop.getProperty(key, defaultValue); |
||||
} |
||||
|
||||
public String getProperty(String key) { |
||||
return prop.getProperty(key); |
||||
} |
||||
|
||||
public int size() { |
||||
return prop.size(); |
||||
} |
||||
|
||||
public boolean containsKey(Object key) { |
||||
return prop.containsKey(key); |
||||
} |
||||
} |
||||
@ -0,0 +1,11 @@
|
||||
package org.leolo.rail; |
||||
|
||||
public class Constants { |
||||
|
||||
public static class NetworkRail{ |
||||
|
||||
public static final String TOPIC_NAME_VTSP = "VSTP_ALL"; |
||||
|
||||
} |
||||
|
||||
} |
||||
@ -0,0 +1,103 @@
|
||||
package org.leolo.rail; |
||||
|
||||
|
||||
import java.sql.Connection; |
||||
import java.sql.ResultSet; |
||||
import java.sql.SQLException; |
||||
import java.sql.Statement; |
||||
|
||||
import javax.sql.XAConnection; |
||||
|
||||
import org.apache.logging.log4j.LogManager; |
||||
import org.apache.logging.log4j.Logger; |
||||
import org.mariadb.jdbc.MariaDbPoolDataSource; |
||||
|
||||
public class DatabaseManager { |
||||
|
||||
private static Logger log = LogManager.getLogger(DatabaseManager.class); |
||||
|
||||
private static DatabaseManager instance; |
||||
|
||||
private MariaDbPoolDataSource ds; |
||||
|
||||
public synchronized static DatabaseManager getInstance() { |
||||
if(instance==null) { |
||||
instance = new DatabaseManager(); |
||||
} |
||||
return instance; |
||||
} |
||||
|
||||
private DatabaseManager() { |
||||
ConfigurationManager cm = ConfigurationManager.getInstance(); |
||||
if( |
||||
!cm.containsKey("db.host")|| |
||||
!cm.containsKey("db.user")|| |
||||
!cm.containsKey("db.pwd")|| |
||||
!cm.containsKey("db.name") |
||||
) { |
||||
log.fatal("Missing required property"); |
||||
System.exit(1); |
||||
} |
||||
String url = "jdbc:mariadb://"+cm.getProperty("db.host")+ |
||||
":"+cm.getProperty("db.port", "3306")+ |
||||
"/"+cm.getProperty("db.name"); |
||||
log.info("Connecting to DB {} as {}", url, cm.get("db.user")); |
||||
try { |
||||
ds = new MariaDbPoolDataSource(url); |
||||
ds.setMaxPoolSize(Integer.parseInt(cm.getOrDefault("db.poolsize", "20").toString())); |
||||
ds.setUser(cm.getProperty("db.user").toString()); |
||||
ds.setPassword(cm.getProperty("db.pwd").toString()); |
||||
} catch (SQLException e) { |
||||
log.fatal("Cannot connect to DB",e); |
||||
System.exit(-2); |
||||
} |
||||
} |
||||
|
||||
public boolean testPool() { |
||||
try(Connection conn = ds.getConnection()){ |
||||
try(Statement stmt = conn.createStatement()){ |
||||
try (ResultSet rs = stmt.executeQuery("SELECT CONNECTION_ID()")){ |
||||
if(rs.next()) { |
||||
log.debug("Connection ID: {}", rs.getString(1)); |
||||
} |
||||
} |
||||
} |
||||
} catch (SQLException e) { |
||||
log.warn("Exception when testing the connection., e"); |
||||
return false; |
||||
} |
||||
return true; |
||||
} |
||||
|
||||
public Connection getConnection() throws SQLException { |
||||
Connection conn = ds.getConnection(); |
||||
conn.setAutoCommit(false); |
||||
return conn; |
||||
} |
||||
|
||||
public XAConnection getXAConnection() throws SQLException { |
||||
XAConnection conn = ds.getXAConnection(); |
||||
return conn; |
||||
} |
||||
|
||||
@Deprecated |
||||
public void clear() { |
||||
try( |
||||
Connection conn = getConnection(); |
||||
Statement stmt = conn.createStatement(); |
||||
){ |
||||
stmt.execute("TRUNCATE TABLE train_assoc"); |
||||
stmt.execute("TRUNCATE TABLE tiploc"); |
||||
stmt.execute("TRUNCATE TABLE train_schedule"); |
||||
stmt.execute("TRUNCATE TABLE train_schedule_detail"); |
||||
stmt.execute("TRUNCATE TABLE train_schedule_location"); |
||||
stmt.execute("TRUNCATE TABLE train_error"); |
||||
}catch(SQLException e) { |
||||
log.error(e.getMessage(), e); |
||||
} |
||||
} |
||||
|
||||
public void shutdown() { |
||||
ds.close(); |
||||
} |
||||
} |
||||
@ -0,0 +1,34 @@
|
||||
package org.leolo.rail; |
||||
|
||||
import org.apache.logging.log4j.LogManager; |
||||
import org.apache.logging.log4j.Logger; |
||||
import org.quartz.Scheduler; |
||||
import org.quartz.SchedulerException; |
||||
import org.quartz.impl.StdSchedulerFactory; |
||||
|
||||
public class NRDataDamon { |
||||
|
||||
private Logger log = LogManager.getLogger(getClass()); |
||||
|
||||
public static void main(String [] args) { |
||||
Logger log = LogManager.getLogger(NRDataDamon.class); |
||||
ConfigurationManager.getInstance().forEach((k,v)->{ |
||||
if(k.toString().endsWith(".pwd")) { |
||||
log.info("{} : {}", k , "****"); |
||||
}else { |
||||
log.info("{} : {}", k, v); |
||||
} |
||||
}); |
||||
log.info("Opening database connection"); |
||||
DatabaseManager.getInstance().testPool(); |
||||
log.info("Database connected"); |
||||
try { |
||||
Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler(); |
||||
scheduler.start(); |
||||
} catch (SchedulerException e) { |
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace(); |
||||
} |
||||
} |
||||
|
||||
} |
||||
Loading…
Reference in new issue