I am implementing a backend service using Java. I chose to apply the Singleton
pattern because there should be only one service running. However, this service is also a Socket.IO client thus there must be some kind of event being triggered when server pushed. But the event should be synchronized
in queue.
I think my implementation is not correct. Tt seems that synchronized(this)
block is NOT protecting the Backend
object but rather the Emitter.Listener
object.
private static synchronized BackendServer getInstance()
{
if(instance == null) {
instance = new BackendServer();
try {
socket = IO.socket(host_name+":"+port_frontend);
socket.on(Socket.EVENT_CONNECT, new Emitter.Listener() {
@Override
public void call(Object... args) {
}
}).on("event1", new Emitter.Listener() {
@Override
public void call(Object... args) {
try {
synchronized(this) { <--Which object is synchronized?
String timestamp = getCurrentTime();
String logging = "["+timestamp+"] ";