We have built a messaging server using PubNub which listens for chat messages from Android device. For this, our J2EE based spring, we have created a Pubnub listener (which is basically a listening socket) as follows. Since this needs to be created only once and at a application startup, we are using ContextRefreshedEvent
for the same.
I am particularly doubtful about the use isContextLoaded
variable since we are observing WTF: Not initialising incoming listener
logs in our server randomly which I believe should not be there since context is initialised only once.
public class IncomingListener implements ApplicationListener < ContextRefreshedEvent > {
@Override
public void onApplicationEvent(ContextRefreshedEvent event) {
private static volatile boolean isContextLoaded = false;
@Autowired
ServletContext application;
synchronized(IncomingListener.class) {
if (isContextLoaded) {
System.out.println("WTF: Not initializing incoming listener");
return;
}
try {
initialiseIncomingListener();
isContextLoaded = true;
} catch (JSONException e) {
e.printStackTrace();
} catch (AkoshaException e) {
e.printStackTrace();
}
}
}
private void initialiseIncomingListener()
{
// Initialize our Pubnub Listener
pubnub.subscribe(serverchannel, new Callback() {
// Subscribe to pubnub
});
}
}
Aucun commentaire:
Enregistrer un commentaire