Create async slip gaji

add timer dan task
This commit is contained in:
Salman Manoe 2023-04-28 10:34:56 +07:00
parent 5fca5e2486
commit 9915458f12
2 changed files with 138 additions and 0 deletions

View File

@ -0,0 +1,57 @@
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.InitiateSlipGajiTimer;
import com.jasamedika.medifirst2000.service.SlipGajiService;
import com.jasamedika.medifirst2000.util.CommonUtil;
/**
* @author Salman
*
*/
@Component
public class InitiateSlipGaji {
private final static Logger LOGGER = LoggerFactory.getLogger(InitiateSlipGaji.class);
@Autowired
private SlipGajiService slipGajiService;
public InitiateSlipGaji() {
int the1st = 1;
int at0hrs = 0;
InitiateSlipGajiTimer.schedule(new Runnable() {
@Override
public void run() {
try {
LOGGER.info("Slip Gaji : Call initiate data");
initiateData();
} catch (Exception ex) {
LOGGER.error("Slip Gaji : Error when initiate data " + ex.getMessage());
} finally {
LOGGER.info("Slip Gaji : Finishing call initiate data");
}
}
}, the1st, at0hrs);
}
@Async
public void initiateData() {
try {
if (CommonUtil.isNotNullOrEmpty(slipGajiService)) {
LOGGER.info("Slip Gaji : Initiating");
slipGajiService.init();
} else {
LOGGER.warn("Slip Gaji : Undefined service");
}
} catch (Exception ex) {
LOGGER.error("Slip Gaji : Error when initiating " + ex.getMessage());
} finally {
LOGGER.info("Slip Gaji : Finish initiating");
}
}
}

View File

@ -0,0 +1,81 @@
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 Salman
*
*/
public class InitiateSlipGajiTimer {
private final static Logger LOGGER = LoggerFactory.getLogger(InitiateSlipGajiTimer.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("SlipGajiTimer : Cancel current execution");
current.cancel();
LOGGER.info("SlipGajiTimer : Removes the timertask so it can be garbage collected");
current.purge();
}
public static InitiateSlipGajiTimer schedule(Runnable runnable, int dayOfMonth, int hourOfDay) {
LOGGER.info("SlipGajiTimer : Create a new instance");
return new InitiateSlipGajiTimer(runnable, dayOfMonth, hourOfDay);
}
private InitiateSlipGajiTimer(Runnable runnable, int day, int hour) {
this.whatToDo = runnable;
this.dayOfMonth = day;
this.hourOfDay = hour;
schedule();
}
private void schedule() {
cancelCurrent();
LOGGER.info(
"SlipGajiTimer : 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("SlipGajiTimer : Running schedule");
whatToDo.run();
} finally {
LOGGER.info("SlipGajiTimer : Schedule for the next month");
schedule();
}
}
}, nextDate());
}
private Date nextDate() {
Calendar runDate = Calendar.getInstance();
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.MONTH, 1);
LOGGER.info("SlipGajiTimer : Set to next month " + runDate.getTime());
return runDate.getTime();
}
}