Merge branch 'master' into feature/RefactoringGestioneColli
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 boolean mInterrupted = false;
|
||||
|
||||
public TaskModel(Runnable runnable, long delayTimeInMillis, String tagName) {
|
||||
mRunnable = runnable;
|
||||
mDelayTime = delayTimeInMillis;
|
||||
@@ -28,14 +30,14 @@ public class TaskModel implements Runnable {
|
||||
public void run() {
|
||||
Date lastExecution = new Date(0L);
|
||||
|
||||
while (!mThread.isInterrupted()) {
|
||||
while (!mInterrupted) {
|
||||
try {
|
||||
if (new Date().getTime() - lastExecution.getTime() > mDelayTime) {
|
||||
mRunnable.run();
|
||||
lastExecution = new Date();
|
||||
}
|
||||
|
||||
Thread.sleep(10);
|
||||
if(!mInterrupted) Thread.sleep(10);
|
||||
} catch (InterruptedException iex) {
|
||||
//Do nothing
|
||||
} catch (Exception ex) {
|
||||
@@ -46,7 +48,7 @@ public class TaskModel implements Runnable {
|
||||
}
|
||||
|
||||
public void interrupt() {
|
||||
this.mThread.interrupt();
|
||||
mInterrupted = true;
|
||||
}
|
||||
|
||||
public String getTagName() {
|
||||
|
||||
@@ -3,27 +3,23 @@ package it.integry.ems.looper.service;
|
||||
import com.annimon.stream.Stream;
|
||||
import it.integry.ems.looper.dto.LoopDTO;
|
||||
import it.integry.ems.looper.dto.TaskModel;
|
||||
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
|
||||
import org.springframework.context.annotation.Scope;
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
import org.springframework.context.ApplicationListener;
|
||||
import org.springframework.context.event.ContextClosedEvent;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import javax.annotation.PreDestroy;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Random;
|
||||
|
||||
@Component
|
||||
@Scope(value = ConfigurableBeanFactory.SCOPE_SINGLETON)
|
||||
public class LooperService {
|
||||
public class LooperService implements ApplicationListener<ContextClosedEvent> {
|
||||
private final ArrayList<LoopDTO> mLoopsHandler = 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) {
|
||||
int newId = -1;
|
||||
@@ -47,5 +43,29 @@ public class LooperService {
|
||||
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