Finish Hotfix-1
All checks were successful
IntegryManagementSystem_Multi/pipeline/head This commit looks good
All checks were successful
IntegryManagementSystem_Multi/pipeline/head This commit looks good
This commit is contained in:
@@ -15,6 +15,8 @@ public class TaskModel implements Runnable {
|
|||||||
|
|
||||||
private final Logger logger = LogManager.getLogger();
|
private final Logger logger = LogManager.getLogger();
|
||||||
|
|
||||||
|
private boolean mInterrupted = false;
|
||||||
|
|
||||||
public TaskModel(Runnable runnable, long delayTimeInMillis, String tagName) {
|
public TaskModel(Runnable runnable, long delayTimeInMillis, String tagName) {
|
||||||
mRunnable = runnable;
|
mRunnable = runnable;
|
||||||
mDelayTime = delayTimeInMillis;
|
mDelayTime = delayTimeInMillis;
|
||||||
@@ -28,14 +30,14 @@ public class TaskModel implements Runnable {
|
|||||||
public void run() {
|
public void run() {
|
||||||
Date lastExecution = new Date(0L);
|
Date lastExecution = new Date(0L);
|
||||||
|
|
||||||
while (!mThread.isInterrupted()) {
|
while (!mInterrupted) {
|
||||||
try {
|
try {
|
||||||
if (new Date().getTime() - lastExecution.getTime() > mDelayTime) {
|
if (new Date().getTime() - lastExecution.getTime() > mDelayTime) {
|
||||||
mRunnable.run();
|
mRunnable.run();
|
||||||
lastExecution = new Date();
|
lastExecution = new Date();
|
||||||
}
|
}
|
||||||
|
|
||||||
Thread.sleep(10);
|
if(!mInterrupted) Thread.sleep(10);
|
||||||
} catch (InterruptedException iex) {
|
} catch (InterruptedException iex) {
|
||||||
//Do nothing
|
//Do nothing
|
||||||
} catch (Exception ex) {
|
} catch (Exception ex) {
|
||||||
@@ -46,7 +48,7 @@ public class TaskModel implements Runnable {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void interrupt() {
|
public void interrupt() {
|
||||||
this.mThread.interrupt();
|
mInterrupted = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getTagName() {
|
public String getTagName() {
|
||||||
|
|||||||
@@ -3,27 +3,23 @@ package it.integry.ems.looper.service;
|
|||||||
import com.annimon.stream.Stream;
|
import com.annimon.stream.Stream;
|
||||||
import it.integry.ems.looper.dto.LoopDTO;
|
import it.integry.ems.looper.dto.LoopDTO;
|
||||||
import it.integry.ems.looper.dto.TaskModel;
|
import it.integry.ems.looper.dto.TaskModel;
|
||||||
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
|
import org.apache.logging.log4j.LogManager;
|
||||||
import org.springframework.context.annotation.Scope;
|
import org.apache.logging.log4j.Logger;
|
||||||
|
import org.springframework.context.ApplicationListener;
|
||||||
|
import org.springframework.context.event.ContextClosedEvent;
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
import javax.annotation.PreDestroy;
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Random;
|
import java.util.Random;
|
||||||
|
|
||||||
@Component
|
@Component
|
||||||
@Scope(value = ConfigurableBeanFactory.SCOPE_SINGLETON)
|
public class LooperService implements ApplicationListener<ContextClosedEvent> {
|
||||||
public class LooperService {
|
|
||||||
private final ArrayList<LoopDTO> mLoopsHandler = new ArrayList<>();
|
private final ArrayList<LoopDTO> mLoopsHandler = new ArrayList<>();
|
||||||
private final ArrayList<TaskModel> mLoopsTask = new ArrayList<>();
|
private final ArrayList<TaskModel> mLoopsTask = new ArrayList<>();
|
||||||
|
|
||||||
|
private final Logger logger = LogManager.getLogger();
|
||||||
|
private boolean isShuttingDown = false;
|
||||||
|
|
||||||
@PreDestroy
|
|
||||||
public void preDestroy() {
|
|
||||||
for (TaskModel taskModel : mLoopsTask) {
|
|
||||||
taskModel.interrupt();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public int add(Runnable runnable, int delayTimeInMillis, String tagName) {
|
public int add(Runnable runnable, int delayTimeInMillis, String tagName) {
|
||||||
int newId = -1;
|
int newId = -1;
|
||||||
@@ -47,5 +43,29 @@ public class LooperService {
|
|||||||
return newId;
|
return newId;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onApplicationEvent(ContextClosedEvent event) {
|
||||||
|
if (!isShuttingDown) {
|
||||||
|
isShuttingDown = true;
|
||||||
|
|
||||||
|
logger.info("Cleaning up LooperService");
|
||||||
|
try {
|
||||||
|
destroy();
|
||||||
|
} catch (Exception e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void destroy() throws Exception {
|
||||||
|
logger.trace("Destroying " + mLoopsHandler.size() + " looping threads");
|
||||||
|
|
||||||
|
for (int i = 0; i < mLoopsTask.size(); i++) {
|
||||||
|
|
||||||
|
TaskModel taskModel = mLoopsTask.get(i);
|
||||||
|
taskModel.interrupt();
|
||||||
|
|
||||||
|
logger.trace("Destroyed " + (i + 1) + " loop (" + taskModel.getTagName() + ")");
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user