Advertisement
Guest User

Untitled

a guest
Jul 24th, 2014
11
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.59 KB | None | 0 0
  1.  
  2. package dpc2.base.consumers;
  3.  
  4. import dpc2.base.exceptions.AutomaticExecutionFailedException;
  5. import dpc2.base.utils.Config;
  6. import dpc2.base.utils.ManualUtils;
  7. import java.nio.file.Path;
  8. import java.util.concurrent.ExecutionException;
  9. import java.util.concurrent.ExecutorService;
  10. import java.util.concurrent.Executors;
  11. import java.util.concurrent.Future;
  12. import java.util.function.Consumer;
  13.  
  14. /**
  15.  *
  16.  * @author Frank van Heeswijk
  17.  */
  18. @FunctionalInterface
  19. public interface BaseConsumer extends Consumer<Path> {
  20.     @Override
  21.     default void accept(final Path path) {
  22.         String name = path.getFileName().toString();
  23.         ExecutorService service = Executors.newSingleThreadExecutor(runnable -> {
  24.             Thread thread = new Thread(runnable, "documentId=" + name);
  25.             thread.setDaemon(true);
  26.             return thread;
  27.         });
  28.         Future<?> future = service.submit(() -> {
  29.             try {
  30.                 baseAccept(path);
  31.             } catch (AutomaticExecutionFailedException ex) {
  32.                 //do not rethrow, failure was expected, cleanup has been done
  33.             } catch (Exception ex) {
  34.                 ManualUtils.moveToManual(Config.REJECTED_DIRECTORY, path);
  35.                 throw new RuntimeException(ex);
  36.             }
  37.         });
  38.         try {
  39.             future.get();
  40.         } catch (InterruptedException ex) {
  41.             Thread.currentThread().interrupt();
  42.         } catch (ExecutionException ex) {
  43.             throw (RuntimeException)ex.getCause();
  44.         }
  45.     }
  46.    
  47.     void baseAccept(final Path path) throws Exception;
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement