6 changed files with 252 additions and 19 deletions
@ -0,0 +1,42 @@ |
|||||||
|
package org.leolo.rail; |
||||||
|
|
||||||
|
import java.sql.SQLException; |
||||||
|
import java.util.Date; |
||||||
|
|
||||||
|
import org.apache.logging.log4j.LogManager; |
||||||
|
import org.apache.logging.log4j.Logger; |
||||||
|
import org.leolo.rail.dao.MetadataDao; |
||||||
|
import org.quartz.Job; |
||||||
|
import org.quartz.JobExecutionContext; |
||||||
|
import org.quartz.JobExecutionException; |
||||||
|
import org.quartz.SchedulerException; |
||||||
|
import org.quartz.Trigger; |
||||||
|
import org.quartz.TriggerKey; |
||||||
|
import org.quartz.impl.matchers.GroupMatcher; |
||||||
|
|
||||||
|
public class LongTermScheduleJob implements Job{ |
||||||
|
|
||||||
|
private Logger log = LogManager.getLogger(LongTermScheduleJob.class); |
||||||
|
|
||||||
|
@Override |
||||||
|
public void execute(JobExecutionContext context) throws JobExecutionException { |
||||||
|
MetadataDao mDao = new MetadataDao(); |
||||||
|
log.atInfo().log("LongTermScheduleJob started."); |
||||||
|
|
||||||
|
try { |
||||||
|
TriggerKey triggerKey = TriggerKey.triggerKey(Constants.Scheduler.LTSJ_CRON_TRIGGER, Constants.Scheduler.DEFAULT_GROUP_NAME); |
||||||
|
Trigger trigger = context.getScheduler().getTrigger(triggerKey); |
||||||
|
Date nextFireTime = trigger.getNextFireTime(); |
||||||
|
log.info("Next fire at "+nextFireTime); |
||||||
|
long timeToNextFire = nextFireTime.getTime() - System.currentTimeMillis(); |
||||||
|
if(timeToNextFire > Constants.Generic.LTR_SKIP_THRESHOLD && !ConfigurationManager.getInstance().getBoolean("general.debug", false)) { |
||||||
|
log.always().log("Too close to next fire time. Skipping"); |
||||||
|
return; |
||||||
|
} |
||||||
|
} catch (SchedulerException e) { |
||||||
|
log.atError().log(e.getMessage(),e); |
||||||
|
} |
||||||
|
|
||||||
|
} |
||||||
|
|
||||||
|
} |
||||||
@ -0,0 +1,123 @@ |
|||||||
|
package org.leolo.rail.dao; |
||||||
|
|
||||||
|
import java.sql.Connection; |
||||||
|
import java.sql.PreparedStatement; |
||||||
|
import java.sql.ResultSet; |
||||||
|
import java.sql.SQLException; |
||||||
|
import java.util.Date; |
||||||
|
|
||||||
|
import org.leolo.rail.DatabaseManager; |
||||||
|
|
||||||
|
public class MetadataDao { |
||||||
|
|
||||||
|
|
||||||
|
public String getString(String key) throws SQLException{ |
||||||
|
return getString(key, null); |
||||||
|
} |
||||||
|
|
||||||
|
public String getString(String key, String defaultValue) throws SQLException{ |
||||||
|
try( |
||||||
|
Connection conn = DatabaseManager.getInstance().getConnection(); |
||||||
|
PreparedStatement pstmt = conn.prepareStatement("SELECT metadata_value FROM metadata WHERE metadata_name = ?"); |
||||||
|
){ |
||||||
|
pstmt.setString(1, key); |
||||||
|
try(ResultSet rs = pstmt.executeQuery()){ |
||||||
|
if(rs.next()) { |
||||||
|
return rs.getString("metadata_value"); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
return defaultValue; |
||||||
|
} |
||||||
|
|
||||||
|
public long getLong(String key) throws SQLException{ |
||||||
|
return getLong(key, 0); |
||||||
|
} |
||||||
|
|
||||||
|
public long getLong(String key, long defaultValue) throws SQLException{ |
||||||
|
try( |
||||||
|
Connection conn = DatabaseManager.getInstance().getConnection(); |
||||||
|
PreparedStatement pstmt = conn.prepareStatement("SELECT metadata_value FROM metadata WHERE metadata_name = ?"); |
||||||
|
){ |
||||||
|
pstmt.setString(1, key); |
||||||
|
try(ResultSet rs = pstmt.executeQuery()){ |
||||||
|
if(rs.next()) { |
||||||
|
return rs.getLong("metadata_value"); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
return defaultValue; |
||||||
|
} |
||||||
|
|
||||||
|
public Date getDate(String key) throws SQLException{ |
||||||
|
return getDate(key, null); |
||||||
|
} |
||||||
|
|
||||||
|
public Date getDate(String key, Date defaultValue) throws SQLException{ |
||||||
|
long val = getLong(key, Long.MIN_VALUE); |
||||||
|
if(val==Long.MIN_VALUE) { |
||||||
|
return defaultValue; |
||||||
|
} |
||||||
|
return new Date(val); |
||||||
|
} |
||||||
|
|
||||||
|
public void setString(String key, String value) throws SQLException{ |
||||||
|
if(value!=null) { |
||||||
|
try( |
||||||
|
Connection conn = DatabaseManager.getInstance().getConnection(); |
||||||
|
PreparedStatement pstmt = conn.prepareStatement("REPLACE INTO metadata (metadata_name, metadata_value) VALUES (?,?)"); |
||||||
|
){ |
||||||
|
pstmt.setString(1, key); |
||||||
|
pstmt.setString(2, value); |
||||||
|
pstmt.executeUpdate(); |
||||||
|
conn.commit(); |
||||||
|
} |
||||||
|
}else { |
||||||
|
try( |
||||||
|
Connection conn = DatabaseManager.getInstance().getConnection(); |
||||||
|
PreparedStatement pstmt = conn.prepareStatement("DELETE FROM metadata WHERE metadata_name=?"); |
||||||
|
){ |
||||||
|
pstmt.setString(1, key); |
||||||
|
pstmt.executeUpdate(); |
||||||
|
conn.commit(); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
public void setLong(String key, long value) throws SQLException{ |
||||||
|
try( |
||||||
|
Connection conn = DatabaseManager.getInstance().getConnection(); |
||||||
|
PreparedStatement pstmt = conn.prepareStatement("REPLACE INTO metadata (metadata_name, metadata_value) VALUES (?,?)"); |
||||||
|
){ |
||||||
|
pstmt.setString(1, key); |
||||||
|
pstmt.setLong(2, value); |
||||||
|
pstmt.executeUpdate(); |
||||||
|
conn.commit(); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
public void setDate(String key, Date value) throws SQLException{ |
||||||
|
if(value!=null) { |
||||||
|
try( |
||||||
|
Connection conn = DatabaseManager.getInstance().getConnection(); |
||||||
|
PreparedStatement pstmt = conn.prepareStatement("REPLACE INTO metadata (metadata_name, metadata_value) VALUES (?,?)"); |
||||||
|
){ |
||||||
|
pstmt.setString(1, key); |
||||||
|
pstmt.setLong(2, value.getTime()); |
||||||
|
pstmt.executeUpdate(); |
||||||
|
conn.commit(); |
||||||
|
} |
||||||
|
}else { |
||||||
|
try( |
||||||
|
Connection conn = DatabaseManager.getInstance().getConnection(); |
||||||
|
PreparedStatement pstmt = conn.prepareStatement("DELETE FROM metadata WHERE metadata_name=?"); |
||||||
|
){ |
||||||
|
pstmt.setString(1, key); |
||||||
|
pstmt.executeUpdate(); |
||||||
|
conn.commit(); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
} |
||||||
Loading…
Reference in new issue