Springboot Implementation Timer Timer Processing Tasks

   Recently encountered a problem in the project. For a newly created activity, the activity set the start time and end time, which is just a status code change in the database. However, there is a problem here, how to How can I change the activity state to the time?

1. At the beginning, I thought about the timing task, but I couldn’t think of the configuration time issue. For the activity, the configured timing task There must be a delay, which caused the time to come, but the activity has not yet started or ended.
2. Later, after thinking hard, I got a stupid way to get along, which is to add in front of each interface called externally. A method is provided to handle the start and end of the activity. In this case, the interface can query the normal start or end data, but the activity displayed on the page is still delayed. It was almost over, but the page display is in progress. This is also the case. There will be problems, that is, you may be complained of false activities.

3. Finally, after being scolded by the supervisor, I learned that there is a timer in the juc package, that is, to create one Timed threads, use ScheduledExecutorService to execute regularly, so that the activity status will be changed at the specified time, so that even if the task is successfully completed, part of the code is attached below as a reminder

//Execute timing object pool< /span>

private ScheduledExecutorService scheduleExecutor = Executors.newScheduledThreadPool(2);

//All timed tasks
@SuppressWarnings("rawtypes")
private Map> currentJob = new< /span> HashMap<>();

/**
* Add start timing task
*
@param id
*
@param millSecond
*
@param clearFlag
*
@return
*/
@SuppressWarnings(
"rawtypes")
public synchronized Future addStartJob(String id, Long millSecond , boolean clearFlag) {
if (clearFlag) {
//Clear tasks
clearJobsById(id);
}
if (null == currentJob.get(id)) {
currentJob.put(id,
new ArrayList<>());
}

ScheduledFuture
scheduledFuture = scheduleExecutor.schedule(new Runnable() {
@Override
public void run() {
logger.debug(
"/r/n The event takes effect regularly. Event ID: "+ id + "Time:" + millSecond);
try {
startTask(id);
}
catch (Exception e) {
e.printStackTrace();
logger.error(e.getMessage(), e.fillInStackTrace());
}
}
}, millSecond, TimeUnit.MILLISECONDS);
//Join the collection
currentJob.get(id).add(scheduledFuture);

return null;
}

/** * Clear tasks by id* @param id */public void clearJobsById(String id) {if (null != currentJob .get(id)) {for (@SuppressWarnings("rawtypes") Future future: currentJob.get(id)) {if (null != future && !future.isDone()) {future.cancel(false); / /Is it running?}} currentJob.remove(id);} logger.debug("/r/n Activity clearing timed task! Task ID: "+ id); currentJob.put(id, new ArrayList<>()) ;}}

//Execute timing object pool

private ScheduledExecutorService scheduleExecutor = Executors.newScheduledThreadPool(2);

//All timed tasks
@SuppressWarnings("rawtypes")
private Map> currentJob = new< /span> HashMap<>();

/**
* Add start timing task
*
@param id
*
@param millSecond
*
@param clearFlag
*
@return
*/
@SuppressWarnings(
"rawtypes")
public synchronized Future addStartJob(String id, Long millSecond , boolean clearFlag) {
if (clearFlag) {
//Clear tasks
clearJobsById(id);
}
if (null == currentJob.get(id)) {
currentJob.put(id,
new ArrayList<>());
}

ScheduledFuture
scheduledFuture = scheduleExecutor.schedule(new Runnable() {
@Override
public void run() {
logger.debug(
"/r/n The event takes effect regularly. Event ID: "+ id + "Time:" + millSecond);
try {
startTask(id);
}
catch (Exception e) {
e.printStackTrace();
logger.error(e.getMessage(), e.fillInStackTrace());
}
}
}, millSecond, TimeUnit.MILLISECONDS);
//Join the collection
currentJob.get(id).add(scheduledFuture);

return null;
}

/** * Clear tasks by id* @param id */public void clearJobsById(String id) {if (null != currentJob .get(id)) {for (@SuppressWarnings("rawtypes") Future future: currentJob.get(id)) {if (null != future && !future.isDone()) {future.cancel(false); / /Is it running?}} currentJob.remove(id);} logger.debug("/r/n Activity clearing timed task! Task ID: "+ id); currentJob.put(id, new ArrayList<>()) ;}}

WordPress database error: [Table 'yf99682.wp_s6mz6tyggq_comments' doesn't exist]
SELECT SQL_CALC_FOUND_ROWS wp_s6mz6tyggq_comments.comment_ID FROM wp_s6mz6tyggq_comments WHERE ( comment_approved = '1' ) AND comment_post_ID = 4708 ORDER BY wp_s6mz6tyggq_comments.comment_date_gmt ASC, wp_s6mz6tyggq_comments.comment_ID ASC

Leave a Comment

Your email address will not be published.