I'm dealing with creating a web service which will receive requests to send messages via XMPP. However, all messages will be sent from one account (the server logs in and sends notifications to users).
Here comes the problem - how to implement it? I was trying to implement the XMPPConnection class as a singleton, but I got stuck at passing arguments to constructors containing the hostname, port, JID etc
As I've read here, a singleton with parameters is not a singleton... Hence, I thought about solving it as follows (is it some kind of factory?):
public class XMPPConnectionSingleton
{
private volatile static XMPPConnectionSingleton anInstance;
private volatile static XMPPConnection connection;
public static XMPPConnectionSingleton getInstance() {
if(anInstance == null) {
synchronized (XMPPConnectionSingleton.class) {
if(anInstance == null)
anInstance = new XMPPConnectionSingleton();
}
}
return anInstance;
}
public void init(String server, int port, String jid, String password, String resource)
{
ConnectionConfiguration conf = new ConnectionConfiguration(server, port);
connection = new XMPPConnection(conf);
// logging in, etc.
}
}
Is it a good way to go? Or maybe it is better to make a wrapping class for XMPPConnection, accepting a constructor with no parameters?