English 中文(简体)
如何确保只建立XMPPConnection? 使用单一州?
原标题:How to ensure that only one XMPPConnection will be created? Use singletons?

I m处理设立一个网络服务,接收通过XMPP发送信息的请求。 然而,所有电文都将从一个账户(服务器记录仪上向用户发送通知)发送。

问题何在——如何执行? 我正试图将XMPPConnection级作为单一州,但我 st忙地向包含东道名称、港口、JID等的建筑商提出论点。

页: 1 因此,我想到的是解决该问题如下(即某种工厂?):

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?

问题回答

这完全与错误做法一样。 为什么不能简单地把世界峰会的要求放在一个格言中,而只有一面读到这些要求,并通过xmpp书写信息。 只有一个联系,但没有必要制定任何习俗法典,以建立单一州的联系。





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签