Add Async target skor dokter

Pembuatan service generating target skor dokter dan pembuatan timer setiap awal tahun
This commit is contained in:
Salman Manoe 2021-12-31 09:33:17 +07:00
parent dc17e38d70
commit 6e906c6192
2 changed files with 145 additions and 0 deletions

View File

@ -0,0 +1,61 @@
package com.jasamedika.medifirst2000.asynctask;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import com.jasamedika.medifirst2000.asynctask.timer.TargetSkorDokterTimer;
import com.jasamedika.medifirst2000.controller.base.LocaleController;
import com.jasamedika.medifirst2000.service.IkiDanRemunerasiService;
import com.jasamedika.medifirst2000.util.CommonUtil;
import com.jasamedika.medifirst2000.vo.TargetSkorDokterVO;
/**
* @author salmanoe
* @since Dec 29, 2021
*/
@Component
public class TargetSkorDokterGenerating extends LocaleController<TargetSkorDokterVO> {
private final static Logger LOGGER = LoggerFactory.getLogger(TargetSkorDokterGenerating.class);
@Autowired
private IkiDanRemunerasiService remunerasiService;
public TargetSkorDokterGenerating() {
int the1st = 1;
int at0hrs = 0;
TargetSkorDokterTimer.schedule(new Runnable() {
@Override
public void run() {
try {
LOGGER.info("Task Target Skor Dokter : Running Task Target Skor Dokter");
generateTargetSkorDokter();
} catch (Exception ex) {
LOGGER.error("Task Target Skor Dokter : Task Target Skor Dokter " + ex.getMessage());
} finally {
LOGGER.info("Task Target Skor Dokter : Finishing Target Skor Dokter");
}
}
}, the1st, at0hrs);
}
@Async
public void generateTargetSkorDokter() {
try {
if (CommonUtil.isNotNullOrEmpty(remunerasiService)) {
LOGGER.info("Task Target Skor Dokter : Running Check/Generate Target Skor Dokter");
remunerasiService.autoSaveTargetCapaianLayananJamKerja();
} else {
LOGGER.warn("Task Target Skor Dokter : Undefined service");
}
} catch (Exception ex) {
LOGGER.error("Task Target Skor Dokter : Check/Generate Target Skor Dokter " + ex.getMessage());
} finally {
LOGGER.info("Task Target Skor Dokter : Finishing Check/Generate Target Skor Dokter");
}
}
}

View File

@ -0,0 +1,84 @@
package com.jasamedika.medifirst2000.asynctask.timer;
import java.util.Calendar;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* @author salmanoe
* @since Dec 29, 2021
*/
public class TargetSkorDokterTimer {
private final static Logger LOGGER = LoggerFactory.getLogger(TargetSkorDokterTimer.class);
// What to do
private final Runnable whatToDo;
// when
private final int dayOfMonth;
private final int hourOfDay;
// The current timer
private Timer current = new Timer();
public void cancelCurrent() {
LOGGER.info("TargetSkorDokterTimer : Cancel current execution");
current.cancel();
LOGGER.info("TargetSkorDokterTimer : Removes the timertask so it can be garbage collected");
current.purge();
}
public static TargetSkorDokterTimer schedule(Runnable runnable, int dayOfMonth, int hourOfDay) {
LOGGER.info("TargetSkorDokterTimer : Create a new instance");
return new TargetSkorDokterTimer(runnable, dayOfMonth, hourOfDay);
}
private TargetSkorDokterTimer(Runnable runnable, int day, int hour) {
this.whatToDo = runnable;
this.dayOfMonth = day;
this.hourOfDay = hour;
schedule();
}
private void schedule() {
cancelCurrent();
LOGGER.info(
"TargetSkorDokterTimer : Assigning a new instance of Timer, allow the previous Timer to be garbage collected");
current = new Timer();
current.schedule(new TimerTask() {
public void run() {
try {
LOGGER.info("TargetSkorDokterTimer : Running schedule");
whatToDo.run();
} finally {
LOGGER.info("TargetSkorDokterTimer : Schedule for the next year");
schedule();
}
}
}, nextDate());
}
private Date nextDate() {
Calendar runDate = Calendar.getInstance();
runDate.set(Calendar.MONTH, 0);
runDate.set(Calendar.DAY_OF_MONTH, dayOfMonth);
runDate.set(Calendar.HOUR_OF_DAY, hourOfDay);
runDate.set(Calendar.MINUTE, 0);
runDate.set(Calendar.SECOND, 0);
runDate.set(Calendar.MILLISECOND, 0);
runDate.add(Calendar.YEAR, 1);
LOGGER.info("TargetSkorDokterTimer : Set to next year " + runDate.getTime());
return runDate.getTime();
}
}