Created
January 2, 2017 12:50
-
-
Save sz332/69d963917448e17619ed36fd4482444a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public interface IConnect { | |
public void connect() throws CriticalException; | |
} | |
public class ReconnectProxy implements InvocationHandler { | |
public static final long RECONNECT_DELAY = 5000; | |
public static final String MESSAGE_CONNECT = "connect"; | |
ExecutorService threadPool; | |
BlockingQueue<String> queue = new ArrayBlockingQueue<>(1024); | |
private IConnect obj; | |
private ReconnectProxy(IConnect obj) { | |
this.obj = obj; | |
BasicThreadFactory factory = new BasicThreadFactory.Builder() | |
.namingPattern(obj.getClass().getSimpleName() + "-threadpool-%d") | |
.build(); | |
threadPool = Executors.newSingleThreadExecutor(factory); | |
threadPool.execute(new Reconnector(obj, queue)); | |
this.reconnect(); | |
} | |
public static Object newInstance(IConnect obj) { | |
return Proxy.newProxyInstance(obj.getClass().getClassLoader(), obj.getClass().getInterfaces(), new ReconnectProxy(obj)); | |
} | |
@Override | |
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { | |
Object result; | |
try { | |
result = method.invoke(obj, args); | |
} catch (InvocationTargetException e) { | |
reconnect(); | |
throw new CriticalException(e.getTargetException().getMessage()); | |
} catch (Exception e) { | |
throw new CriticalException(e.getMessage()); | |
} | |
return result; | |
} | |
public void reconnect(){ | |
try{ | |
queue.put(MESSAGE_CONNECT); | |
} catch (InterruptedException e){ | |
// do nothing | |
} | |
} | |
class Reconnector implements Runnable { | |
IConnect service; | |
BlockingQueue<String> queue; | |
public Reconnector(IConnect service, BlockingQueue<String> queue) { | |
this.queue = queue; | |
this.service = service; | |
} | |
@Override | |
public void run() { | |
while (true) { | |
try { | |
queue.take(); | |
service.connect(); | |
} catch (CriticalException e) { | |
try { | |
Thread.sleep(RECONNECT_DELAY); | |
queue.put(MESSAGE_CONNECT); | |
} catch (InterruptedException e2) { | |
// do nothing | |
} | |
} catch (InterruptedException e) { | |
// do nothing | |
} | |
} | |
} | |
} | |
} | |
Usage: | |
public class TestProxy { | |
@Test | |
public void test() { | |
MyClass myClass = (MyClass) ReconnectProxy.newInstance(new MyClassImpl()); | |
try { | |
myClass.myMethod(); | |
} catch (CriticalException e) { | |
System.out.println(e.getMessage().toString()); | |
} | |
} | |
public static interface MyClass extends IConnect { | |
public void connect() throws CriticalException; | |
public void myMethod() throws CriticalException; | |
} | |
public static class MyClassImpl implements MyClass { | |
@Override | |
public void connect() throws CriticalException { | |
System.out.println("Connect method called"); | |
} | |
public void myMethod() throws CriticalException { | |
System.out.println("myMethod"); | |
throw new CriticalException("Now we call something"); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment