You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
135 lines
4.9 KiB
135 lines
4.9 KiB
package org.leolo.nrdatad; |
|
|
|
|
|
import org.apache.commons.cli.*; |
|
import org.apache.logging.log4j.LogManager; |
|
import org.apache.logging.log4j.Logger; |
|
import org.leolo.nrdatad.db.CORPUSDao; |
|
import org.leolo.nrdatad.db.DatabaseManager; |
|
import org.leolo.nrdatad.cron.ReferenceDataJob; |
|
import org.leolo.nrdatad.model.CORPUS; |
|
import org.quartz.*; |
|
import org.quartz.impl.StdSchedulerFactory; |
|
|
|
import java.io.IOException; |
|
import java.lang.reflect.InvocationTargetException; |
|
import java.sql.SQLException; |
|
import java.util.Collection; |
|
|
|
/** |
|
* Hello world! |
|
* |
|
*/ |
|
public class App { |
|
|
|
Logger log = LogManager.getLogger(); |
|
|
|
private ConfigurationManager config = ConfigurationManager.getInstance(); |
|
private Scheduler scheduler; |
|
public static void main( String[] args ) { |
|
Option confFile = Option.builder() |
|
.required(false) |
|
.hasArg(true) |
|
.argName("fileName") |
|
.option("c") |
|
.longOpt("config-file") |
|
.build(); |
|
Options opts = new Options(); |
|
opts.addOption(confFile); |
|
String confPath = "nrdatad.conf"; |
|
try { |
|
CommandLine cli = new DefaultParser().parse(opts, args); |
|
if(cli.hasOption(confFile)){ |
|
confPath = cli.getOptionValue(confFile); |
|
} |
|
} catch (ParseException e) { |
|
throw new RuntimeException(e); |
|
} |
|
new App().run(confPath); |
|
} |
|
|
|
private void run(String configPath){ |
|
log.always().log("System Started."); |
|
|
|
try { |
|
config.loadConfiguration(configPath); |
|
} catch (IOException e) { |
|
log.atError().withThrowable(e).log("Unable to load confog file"); |
|
return; |
|
} |
|
// log.atDebug() |
|
if(config.getProperty("general.debug","false").equals("true")){ |
|
for(String key:config.stringPropertyNames()){ |
|
if(key.endsWith("pwd")){ |
|
log.atDebug().log("Config: {} : ****({} chars)", key, config.getProperty(key).length()); |
|
|
|
}else{ |
|
log.atDebug().log("Config: {} : {}", key, config.getProperty(key)); |
|
|
|
} |
|
} |
|
} |
|
String databaseManagerClass = config.getProperty("db.manager","org.leolo.nrdatad.db.DatabaseManager"); |
|
log.atInfo().log("Creating database manager {}", databaseManagerClass); |
|
try { |
|
config.setDatabaseManager((DatabaseManager) Class.forName(databaseManagerClass).getConstructor().newInstance()); |
|
} catch ( |
|
InstantiationException|IllegalAccessException|InvocationTargetException| |
|
NoSuchMethodException|ClassNotFoundException e |
|
) { |
|
log.atFatal().log("Unable to create instance of {}", databaseManagerClass); |
|
System.exit(1); |
|
return; |
|
} catch (ClassCastException e){ |
|
log.atFatal().log("{} is not a DatabaseManager", databaseManagerClass); |
|
System.exit(1); |
|
return; |
|
} |
|
config.getDatabaseManager().initPool(); |
|
if(config.getDatabaseManager().checkConnection()) |
|
log.atInfo().log("Database connected!"); |
|
else{ |
|
log.atFatal().log("Unable to connect to database."); |
|
System.exit(1); |
|
return; |
|
} |
|
loadCronJob(); |
|
new Thread(()->{testRefData();}).start(); |
|
while(true); |
|
} |
|
|
|
private void testRefData() { |
|
log.atInfo().log("Testing reference data"); |
|
ConfigurationManager conf = ConfigurationManager.getInstance(); |
|
CORPUSDao.Query query = new CORPUSDao.QueryBuilder() |
|
.addCrsCode("ECR").addCrsCode("VIC") |
|
.addDescription("croydon").build(); |
|
try { |
|
Collection<CORPUS> list = conf.getDatabaseManager().getCORPUSDao().executeQuery(query); |
|
log.atDebug().log("{} entries found", list.size()); |
|
for(CORPUS corpus:list){ |
|
log.atDebug().log("Found entry : {}", corpus); |
|
} |
|
} catch (SQLException e) { |
|
throw new RuntimeException(e); |
|
} |
|
} |
|
|
|
private void loadCronJob() { |
|
log.atDebug().log("Start loading cron jobs"); |
|
try { |
|
scheduler = StdSchedulerFactory.getDefaultScheduler(); |
|
scheduler.start(); |
|
if(ConfigurationManager.getInstance().getProperty(Constants.Configuration.ALWAYS_RUN_REF_DATA,"").equals("true")){ |
|
log.atInfo().log("Loading Reference data now"); |
|
scheduler.scheduleJob( |
|
JobBuilder.newJob(ReferenceDataJob.class).withIdentity("J-OF-"+Constants.CronJob.REFERENCE_DATA).build(), |
|
TriggerBuilder.newTrigger().withIdentity("T-OF-"+Constants.CronJob.REFERENCE_DATA).startAt(DateBuilder.evenSecondDateAfterNow()).build() |
|
); |
|
} |
|
} catch (SchedulerException e) { |
|
log.atFatal().withThrowable(e).log("Unable to create cron jobs"); |
|
} |
|
|
|
} |
|
}
|
|
|