Following a discussion in the Netz39 IRC channel I created a short Java Enterprise Loop for those in need.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
package com.penguineering.seriousnonsense; /* * Short usage instructions: * * final Runnable loopee = ... // Acquire some runnable instance * EnterpriseLoop.forRunnable(loopee).run(); * * or put it in a thread: * * new Thread(EnterpriseLoop.forRunnable(loopee)).start(); * * * Whatever kills your design best. */ public class EnterpriseLoop implements Runnable { public static EnterpriseLoop forRunnable(final Runnable _loopee) throws NullPointerException { return new EnterpriseLoop(_loopee); } private final Runnable loopee; private EnterpriseLoop(final Runnable _loopee) throws NullPointerException { if (_loopee == null) throw new NullPointerException("Runnable _loopee must not be null!"); this.loopee = _loopee; } public Runnable getLoopee() { return loopee; } @Override public void run() { while (true) try { loopee.run(); } catch {Throwable t) { // ignore } } } |
To adhere to development standards usually found in situations leading to the need of an enterprise loop, I neither documented the code (except for a short usage instruction to save you from thinking about those lines you are going to copy) nor tested it.
I leave this to the public domain to successfully serve as a bad example.