Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,27 +17,33 @@
*/
public final class Loggers {

// =========================================================================
// CONSTRUCTOR
// =========================================================================
private Loggers() {
}

// =========================================================================
// ATTRIBUTES
// =========================================================================
public final static Logger XLLOG = LoggerFactory.getLogger("XLLOG");
public static final Logger XLLOG = LoggerFactory.getLogger("XLLOG");

public final static Logger DEBUGLOG = LoggerFactory.getLogger("DEBUGLOG");
public static final Logger DEBUGLOG = LoggerFactory.getLogger("DEBUGLOG");

public final static Logger SYSTEMLOG = LoggerFactory.getLogger("SYSTEMLOG");
public static final Logger SYSTEMLOG = LoggerFactory.getLogger("SYSTEMLOG");

public final static Logger AUDITOUTLINE = LoggerFactory.getLogger("AUDITOUTLINE");
public static final Logger AUDITOUTLINE = LoggerFactory.getLogger("AUDITOUTLINE");

public final static Logger IOLOG = LoggerFactory.getLogger("IOLOG");
public static final Logger IOLOG = LoggerFactory.getLogger("IOLOG");

public final static Logger PARTNERLOG = LoggerFactory.getLogger("PARTNERLOG");
public static final Logger PARTNERLOG = LoggerFactory.getLogger("PARTNERLOG");

public final static Logger SECURITY = LoggerFactory.getLogger("SECURITY");
public static final Logger SECURITY = LoggerFactory.getLogger("SECURITY");

public final static Logger SCRIPTS = LoggerFactory.getLogger("SCRIPTS");
public static final Logger SCRIPTS = LoggerFactory.getLogger("SCRIPTS");

public final static Logger BOOTSTRAP = LoggerFactory.getLogger("BOOTSTRAP");
public static final Logger BOOTSTRAP = LoggerFactory.getLogger("BOOTSTRAP");

public final static Logger CHRONOLOG = LoggerFactory.getLogger("CHRONOLOG");
public static final Logger CHRONOLOG = LoggerFactory.getLogger("CHRONOLOG");

}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@
* @author patrickguillerm
* @since 24 mars 2018
*/
public interface CallableTimeoutResult<V> extends Callable<V> {
V getTimeoutResult();
public interface CallableWithErrorResult<V> extends Callable<V> {
default V getTimeoutResult() {
return null;
}

default V getErrorResult(Exception error) {
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand All @@ -21,12 +20,12 @@
import java.util.concurrent.Future;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.BiFunction;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.wiedza.monitoring.api.exceptions.Asserts;
import org.wiedza.monitoring.api.loggers.Loggers;
import org.wiedza.monitoring.api.time.Chrono;

/**
Expand All @@ -40,6 +39,8 @@ public class RunAndCloseService<T> implements ThreadFactory {
// =========================================================================
// ATTRIBUTES
// =========================================================================
private static final Logger LOGGER = LoggerFactory.getLogger(RunAndCloseService.class);

private final String threadsName;

private final List<Callable<T>> tasks;
Expand Down Expand Up @@ -89,11 +90,11 @@ public RunAndCloseService(final String threadsName, final long timeout, final in
int howManyThreads = tasks.size() < nbThreads ? tasks.size() : nbThreads;
this.threadsName = threadsName;
this.timeout = timeout;
this.onError = onError == null ? this::handlerError : onError;
this.onError = onError;
tasksAndFutures = new HashMap<>();
threadGroup = Thread.currentThread().getThreadGroup();
executor = Executors.newFixedThreadPool(howManyThreads, this);
completion = new ExecutorCompletionService<T>(executor);
completion = new ExecutorCompletionService<>(executor);
}

// =========================================================================
Expand All @@ -116,8 +117,6 @@ public List<T> run() {
tasksLeft = tasksLeft - 1;
}
} catch (ExecutionException | InterruptedException error) {
Callable<T> task = resolveTask(itemFuture);
taskData = handlerError(error, task);
tasksLeft = tasksLeft - 1;
}

Expand All @@ -142,15 +141,40 @@ private long computeTimeLeft(long timeLeft, final Chrono chrono) {
private List<Future<T>> sumitTask() {
final List<Future<T>> result = new ArrayList<>();
for (Callable<T> task : tasks) {
final Future<T> future = completion.submit(task);
final Future<T> future = completion.submit(new CallableTask(task, this));
result.add(future);
tasksAndFutures.put(future, task);
}
return result;
}

private class CallableTask<U> implements Callable<U> {
private final Callable<U> task;

private final RunAndCloseService<U> runAndCloseService;

public CallableTask(Callable<U> task, RunAndCloseService<U> runAndCloseService) {
this.task = task;
this.runAndCloseService = runAndCloseService;
}

@Override
public U call() throws Exception {
U result = null;
try {
result = task.call();
} catch (Exception e) {
LOGGER.error(e.getMessage(), e);
result = runAndCloseService.processHandlerError(e, task);
}

return result;
}

}

// =========================================================================
// OVERRIDES
// new Thread
// =========================================================================
@Override
public Thread newThread(Runnable runnable) {
Expand All @@ -163,16 +187,12 @@ public Thread newThread(Runnable runnable) {
// =========================================================================
// ERRORS
// =========================================================================
private T handlerError(Exception error, Callable<T> task) {
Loggers.DEBUGLOG.error(error.getMessage(), error);
return null;
}

private List<T> handlerTimeoutTask() {
List<T> result = new ArrayList<>();
for (Map.Entry<Future<T>, Callable<T>> entry : tasksAndFutures.entrySet()) {
if (!entry.getKey().isDone()) {
T taskData = onError.apply(new TimeoutException(), entry.getValue());
final Callable<T> task = entry.getValue();
T taskData = processHandlerError(null, task);
if (taskData != null) {
result.add(taskData);
}
Expand All @@ -181,9 +201,27 @@ private List<T> handlerTimeoutTask() {
return result;
}

private Callable<T> resolveTask(Future<T> itemFuture) {
// TODO Auto-generated method stub
return null;
private synchronized T processHandlerError(Exception error, Callable<T> task) {
T result = null;
if (onError == null) {
result = handlerError(error, task);
} else {
result = onError.apply(error, task);
}
return result;
}

private T handlerError(Exception error, Callable<T> task) {
T result = null;
if (task instanceof CallableWithErrorResult) {
if (error == null) {
result = ((CallableWithErrorResult<T>) task).getTimeoutResult();
} else {
result = ((CallableWithErrorResult<T>) task).getErrorResult(error);
}
}

return result;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/* WIEDZA
* -----------------
* Apache License
* Version 2.0, January 2004
* http://www.apache.org/licenses/
*/
package org.wiedza.monitoring.api.services.threads;

/**
* TimeoutTaskException
*
* @author patrickguillerm
* @since 25 mars 2018
*/
public class TimeoutTaskException extends Exception {

// =========================================================================
// ATTRIBUTES
// =========================================================================
private static final long serialVersionUID = 4991745549592164932L;

// =========================================================================
// CONSTRUCTORS
// =========================================================================

public TimeoutTaskException() {
super();
}

public TimeoutTaskException(String message, Throwable cause) {
super(message, cause);
}

public TimeoutTaskException(String message) {
super(message);
}

public TimeoutTaskException(Throwable cause) {
super(cause);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/* WIEDZA
* -----------------
* Apache License
* Version 2.0, January 2004
* http://www.apache.org/licenses/
*/
package org.wiedza.monitoring.api.services.threads;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Callable;

/**
* ComplexTask
*
* @author patrickguillerm
* @since 25 mars 2018
*/
public class ComplexTask implements CallableWithErrorResult<String> {

// =========================================================================
// ATTRIBUTES
// =========================================================================
private final String value;

// =========================================================================
// METHODS
// =========================================================================
public ComplexTask(String value) {
this.value = value;
}

// =========================================================================
// OVERRIDES
// =========================================================================
@Override
public String call() throws Exception {
List<Callable<String>> tasks = buildTask();
//@formatter:off
List<String> data = new RunAndCloseService<>("test" + value,
1500L,
2,
tasks).run();
//@formatter:on
return String.join(" | ", data);
}

private List<Callable<String>> buildTask() {
final List<Callable<String>> result = new ArrayList<>();

if ("1".equals(value)) {
result.add(new SimpleTask(500, "1.1"));
result.add(new SimpleTask(400, "1.2"));
result.add(new SimpleTask(1000, "1.3"));
result.add(new SimpleTask(500, "1.4", true));
} else {
result.add(new SimpleTask(100, value + ".1"));
result.add(new SimpleTask(500, value + ".2"));
result.add(new SimpleTask(2000, value + ".3"));
result.add(new SimpleTask(100, value + ".4"));
}
return result;
}

// =========================================================================
// GETTERS & SETTERS
// =========================================================================
@Override
public String getTimeoutResult() {
return "null";
}
}
Loading