Android using rabbitmq singleton connection -
i need design patter create rabbitmq singleton connection can restart on connection loss/ net provider switch. problem connection realised on thread (asynctask) due android main thread policy. utilize connection 2 services consume , push.
public class rabbitsingletonconnection { public static rabbitsingletonconnection instance; private connection connection; private rabbitsingletonconnection() { } public static rabbitsingletonconnection getinstance() { if (instance == null) { instance = new rabbitsingletonconnection(); } homecoming instance; } public void connect(rabbitmqconnectioncallback callback) { if (connection != null && connection.isopen()) { logutil.hecslog("rabbitsingletonconnection", "already connected"); callback.onconnect(connection); return; } new rabbitconnectasync(callback).execute("ip","user", "pass"); } public void setconnection(connection connection) { this.connection = connection; } public connection getconnection() { homecoming connection; } }
problem logutil.hecslog("rabbitsingletonconnection", "already connected");
never occurs. have been digging , discovered "thread safe singleton using classloader declaration or enum approach", not include callback method.
edit
rabbitsingletonconnection.getinstance().connect(new rabbitmqconnectioncallback() { @override public void onconnect(connection result) { if (result != null && result.isopen()){ rabbitsingletonconnection.getinstance().setconnection(result); logutil.hecslog(log, "service has started"); } } });
this illustration is necessary rebuild rabbitmq connection each time message sent cannot create connection in constructor because needs thread handle timeoutexception
.
my goal start service after rabbitmq async task connects.
do need dependency injection?
i think figured out, starting/stopping services on connectivity gain/loss. looked @ facebook app , services never stop, logic reply reset workflow in service not service itslef.
android connection singleton rabbitmq
No comments:
Post a Comment