Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package dpc2.base.consumers;
- import dpc2.base.exceptions.AutomaticExecutionFailedException;
- import dpc2.base.utils.Config;
- import dpc2.base.utils.ManualUtils;
- import java.nio.file.Path;
- import java.util.concurrent.ExecutionException;
- import java.util.concurrent.ExecutorService;
- import java.util.concurrent.Executors;
- import java.util.concurrent.Future;
- import java.util.function.Consumer;
- /**
- *
- * @author Frank van Heeswijk
- */
- @FunctionalInterface
- public interface BaseConsumer extends Consumer<Path> {
- @Override
- default void accept(final Path path) {
- String name = path.getFileName().toString();
- ExecutorService service = Executors.newSingleThreadExecutor(runnable -> {
- Thread thread = new Thread(runnable, "documentId=" + name);
- thread.setDaemon(true);
- return thread;
- });
- Future<?> future = service.submit(() -> {
- try {
- baseAccept(path);
- } catch (AutomaticExecutionFailedException ex) {
- //do not rethrow, failure was expected, cleanup has been done
- } catch (Exception ex) {
- ManualUtils.moveToManual(Config.REJECTED_DIRECTORY, path);
- throw new RuntimeException(ex);
- }
- });
- try {
- future.get();
- } catch (InterruptedException ex) {
- Thread.currentThread().interrupt();
- } catch (ExecutionException ex) {
- throw (RuntimeException)ex.getCause();
- }
- }
- void baseAccept(final Path path) throws Exception;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement