perbaikan timer

This commit is contained in:
salmanoe 2021-06-19 20:50:54 +07:00
parent 8748f995a5
commit 6d6136dda4
5 changed files with 62 additions and 33 deletions

View File

@ -6,7 +6,7 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async; import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import com.jasamedika.medifirst2000.asynctask.timer.YearlyTimer; import com.jasamedika.medifirst2000.asynctask.timer.KalenderTimer;
import com.jasamedika.medifirst2000.controller.base.LocaleController; import com.jasamedika.medifirst2000.controller.base.LocaleController;
import com.jasamedika.medifirst2000.service.KalenderService; import com.jasamedika.medifirst2000.service.KalenderService;
import com.jasamedika.medifirst2000.util.CommonUtil; import com.jasamedika.medifirst2000.util.CommonUtil;
@ -22,7 +22,7 @@ public class CalendarSystemGenerating extends LocaleController<KalenderVO> {
public CalendarSystemGenerating() { public CalendarSystemGenerating() {
int the1st = 1; int the1st = 1;
int at0hrs = 0; int at0hrs = 0;
YearlyTimer.schedule(new Runnable() { KalenderTimer.schedule(new Runnable() {
@Override @Override
public void run() { public void run() {
try { try {

View File

@ -6,7 +6,7 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async; import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import com.jasamedika.medifirst2000.asynctask.timer.MonthlyTimer; import com.jasamedika.medifirst2000.asynctask.timer.JadwalKerjaTimer;
import com.jasamedika.medifirst2000.controller.base.LocaleController; import com.jasamedika.medifirst2000.controller.base.LocaleController;
import com.jasamedika.medifirst2000.service.PegawaiJadwalKerjaService; import com.jasamedika.medifirst2000.service.PegawaiJadwalKerjaService;
import com.jasamedika.medifirst2000.util.CommonUtil; import com.jasamedika.medifirst2000.util.CommonUtil;
@ -22,7 +22,7 @@ public class PegawaiJadwalKerjaGenerating extends LocaleController<PegawaiJadwal
public PegawaiJadwalKerjaGenerating() { public PegawaiJadwalKerjaGenerating() {
int the1st = 1; int the1st = 1;
int at0hrs = 0; int at0hrs = 0;
MonthlyTimer.schedule(new Runnable() { JadwalKerjaTimer.schedule(new Runnable() {
@Override @Override
public void run() { public void run() {
try { try {

View File

@ -9,7 +9,7 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async; import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import com.jasamedika.medifirst2000.asynctask.timer.MonthlyTimer; import com.jasamedika.medifirst2000.asynctask.timer.TargetLayananTimer;
import com.jasamedika.medifirst2000.controller.base.LocaleController; import com.jasamedika.medifirst2000.controller.base.LocaleController;
import com.jasamedika.medifirst2000.service.IkiDanRemunerasiService; import com.jasamedika.medifirst2000.service.IkiDanRemunerasiService;
import com.jasamedika.medifirst2000.util.CommonUtil; import com.jasamedika.medifirst2000.util.CommonUtil;
@ -25,7 +25,7 @@ public class TargetLayananGenerating extends LocaleController<TargetLayananVO> {
public TargetLayananGenerating() { public TargetLayananGenerating() {
int the4th = 4; int the4th = 4;
int at0hrs = 0; int at0hrs = 0;
MonthlyTimer.schedule(new Runnable() { TargetLayananTimer.schedule(new Runnable() {
@Override @Override
public void run() { public void run() {
try { try {

View File

@ -5,43 +5,56 @@ import java.util.Date;
import java.util.Timer; import java.util.Timer;
import java.util.TimerTask; import java.util.TimerTask;
public class MonthlyTimer { import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class JadwalKerjaTimer {
private final static Logger LOGGER = LoggerFactory.getLogger(JadwalKerjaTimer.class);
// What to do // What to do
private final Runnable whatToDo; private final Runnable whatToDo;
// when // when
private final int dayOfMonth; private final int dayOfMonth;
private final int hourOfDay; private final int hourOfDay;
// The current timer // The current timer
private Timer current = new Timer(); private Timer current = new Timer();
public void cancelCurrent() { public void cancelCurrent() {
LOGGER.info("JadwalKerjaTimer : Cancel current execution");
current.cancel(); current.cancel();
LOGGER.info("JadwalKerjaTimer : Removes the timertask so it can be garbage collected");
current.purge(); current.purge();
} }
public static MonthlyTimer schedule(Runnable runnable, int dayOfMonth, int hourOfDay) { public static JadwalKerjaTimer schedule(Runnable runnable, int dayOfMonth, int hourOfDay) {
return new MonthlyTimer(runnable, dayOfMonth, hourOfDay); LOGGER.info("JadwalKerjaTimer : Create a new instance");
return new JadwalKerjaTimer(runnable, dayOfMonth, hourOfDay);
} }
private MonthlyTimer(Runnable runnable, int day, int hour) { private JadwalKerjaTimer(Runnable runnable, int day, int hour) {
this.whatToDo = runnable; this.whatToDo = runnable;
this.dayOfMonth = day; this.dayOfMonth = day;
this.hourOfDay = hour; this.hourOfDay = hour;
schedule(); schedule();
} }
private void schedule() { private void schedule() {
cancelCurrent(); cancelCurrent();
LOGGER.info(
"JadwalKerjaTimer : Assigning a new instance of Timer, allow the previous Timer to be garbage collected");
current = new Timer(); current = new Timer();
current.schedule(new TimerTask() { current.schedule(new TimerTask() {
public void run() { public void run() {
try { try {
LOGGER.info("JadwalKerjaTimer : Running schedule");
whatToDo.run(); whatToDo.run();
} finally { } finally {
LOGGER.info("JadwalKerjaTimer : Schedule for the next month");
schedule(); schedule();
} }
} }
@ -56,7 +69,9 @@ public class MonthlyTimer {
runDate.set(Calendar.SECOND, 0); runDate.set(Calendar.SECOND, 0);
runDate.set(Calendar.MILLISECOND, 0); runDate.set(Calendar.MILLISECOND, 0);
runDate.add(Calendar.MONTH, 1); runDate.add(Calendar.MONTH, 1);
LOGGER.info("JadwalKerjaTimer : Set to next month " + runDate.getTime());
return runDate.getTime(); return runDate.getTime();
} }
} }

View File

@ -5,44 +5,56 @@ import java.util.Date;
import java.util.Timer; import java.util.Timer;
import java.util.TimerTask; import java.util.TimerTask;
public class YearlyTimer { import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class KalenderTimer {
private final static Logger LOGGER = LoggerFactory.getLogger(KalenderTimer.class);
// What to do // What to do
private final Runnable whatToDo; private final Runnable whatToDo;
// when // when
private final int dayOfMonth; private final int dayOfMonth;
private final int hourOfDay; private final int hourOfDay;
// The current timer // The current timer
private Timer current = new Timer(); private Timer current = new Timer();
public void cancelCurrent() { public void cancelCurrent() {
LOGGER.info("KalenderTimer : Cancel current execution");
current.cancel(); current.cancel();
LOGGER.info("KalenderTimer : Removes the timertask so it can be garbage collected");
current.purge(); current.purge();
} }
public static YearlyTimer schedule(Runnable runnable, int dayOfMonth, int hourOfDay) { public static KalenderTimer schedule(Runnable runnable, int dayOfMonth, int hourOfDay) {
return new YearlyTimer(runnable, dayOfMonth, hourOfDay); LOGGER.info("KalenderTimer : Create a new instance");
return new KalenderTimer(runnable, dayOfMonth, hourOfDay);
} }
private YearlyTimer(Runnable runnable, int day, int hour) { private KalenderTimer(Runnable runnable, int day, int hour) {
this.whatToDo = runnable; this.whatToDo = runnable;
this.dayOfMonth = day; this.dayOfMonth = day;
this.hourOfDay = hour; this.hourOfDay = hour;
schedule(); schedule();
} }
private void schedule() { private void schedule() {
cancelCurrent(); cancelCurrent();
LOGGER.info(
"KalenderTimer : Assigning a new instance of Timer, allow the previous Timer to be garbage collected");
current = new Timer(); current = new Timer();
current.schedule(new TimerTask() { current.schedule(new TimerTask() {
public void run() { public void run() {
try { try {
LOGGER.info("KalenderTimer : Running schedule");
whatToDo.run(); whatToDo.run();
} finally { } finally {
LOGGER.info("KalenderTimer : Schedule for the next year");
schedule(); schedule();
} }
} }
@ -58,7 +70,9 @@ public class YearlyTimer {
runDate.set(Calendar.SECOND, 0); runDate.set(Calendar.SECOND, 0);
runDate.set(Calendar.MILLISECOND, 0); runDate.set(Calendar.MILLISECOND, 0);
runDate.add(Calendar.YEAR, 1); runDate.add(Calendar.YEAR, 1);
LOGGER.info("KalenderTimer : Set to next year " + runDate.getTime());
return runDate.getTime(); return runDate.getTime();
} }
} }