powered by Jive Software

Sharing a connection among threads

Greetings,

Please consider the following contrived class that can be run in its own thread.

class Echo implements Runnable {
    private Message itsMsg;
    private XMPPConnection itsConn;
        Echo(Message msg, XMPPConnection conn) {
        itsMsg = msg;
        itsConn = conn;
    }
        public void run() {
        Message echo = new Message();
        echo.setFrom(itsMsg.getTo());
        echo.setTo(itsMsg.getFrom());
        echo.setSubject("[echo]: " + itsMsg.getSubject());
        echo.setBody(itsMsg.getBody());
        itsConn.sendPacket(echo);     }
}

Let’'s say that I will receive a steady stream of messages. For each message received, wrap it and a connection used for sending in an instance of the Echo class. Place the Echo instance in a work queue, from which it will be extracted and run by a thread from a thread pool.

I’‘m considering a design that involves both Jabber and threading. I’‘m not yet very well versed in either of these topics, though I have found good APIs with which to work (Smack and Doug Lea’‘s util.concurrent package). I would greatly appreciate anyone’'s thoughts on sharing a connection among threads in light of the example above. For example, will it work well? Must/should I use synchronization on the connection?

Many thanks for your time,

Craig

Craig,

I don’‘t quite understand what you’‘re trying to do. In general, Smack is designed such that you don’'t have to worry about creating your own threads – the library will do all that heavy lifting for you. Check out the PacketCollector and PacketListener classes for more information. If you provide more info, I might understand the problem a bit better.

Regards,

Matt

I’'ve been asked to use Jabber as a messaging framework over which a few applications will communicate. I have one application that will listen for requests for authorization to do a certain task. Authorization may take long enough that I want to be able to continue to receive messages, and I want to process multiple requests simultaneously. So I want to perform each authorization in its own thread, as in the example Echo class, which will reply when ready.

Let me add that I’‘m not very fond of using Jabber this way; it just doesn’'t feel right to me. However, I want to investigate thoroughly.

Thanks,

Craig

Message was edited by: cdemyanovich

If you were doing this in Smack, I’‘d recommend registering a packet listener and then spawning a new thread to do authentication when a new message comes in. It should be faily simple. You don’'t need multiple connections to the server.

Regards,

Matt

All set on the PacketListener; I already have running code that uses it as you advised.

As in the Echo example that I included earlier, can each new thread (java.lang.Runnable) be created with a reference to the connection and use that same connection without interfering with one another and the ability to listen for more messages?

I’'m sorry if this has become a Java question instead of a Smack question. I was simply hoping for insight from the author(s) of XMPPConnection on using it this way.

Many thanks,

Craig

Message was edited by: cdemyanovich

Yes, you can share the Connection object with no problems.

Regards,

Matt

I think this fact should be stated in the javadocs for XMPPConnection, SSLXMPPConnection and the Smack Documentation.

Thanks,

Juan Carlos