[jdev] Regarding JSO and Threads
Matthew A. Miller
linuxwolf at outer-planes.net
Fri Feb 18 09:14:41 CST 2005
I'll try to answer for first question. I'm sure others with come forth
with the answers to the second.
To have a single thread handle more clients depends on if you need SSL
or not[1].
JSO was designed so that a single thread could indeed handle the
processing of multiple sessions (or at least appear that way). If you
use the ChannelStreamSource, then all processing is done on the thread
that calls Stream.process(). If you use the SocketStreamSource (or its
derivatives), then there is actually another thread running to handle
the I/O.
Assuming there are no SSL requirements, you could do this using the
ChannelStreamSource and a java.nio.channels.Selector. Just ensure the
selectable channel for the source is set to non-blocking and register it
with the Selector (preferrably passing along the Stream that will be
using it for the attachment):
conn = JSOImplementation.getInstance().createStream("jabber:client");
src = ChannelStreamSource.createSocket(host, port);
conn.connect(src);
conn.open();
src.register(selector, SelectionKey.OP_READ, conn);
Then call Selector.select() to retrieve the available selection keys,
and process the attached Streams:
while (selector.select() > 0) {
Iterator itr = selector.selectedKeys().iterator();
SelectionKey key;
Stream conn;
while (itr.hasNext()) {
key = (SelectionKey)itr.next();
conn = (Stream)key.attachment();
conn.process();
if (!conn.getCurrentStatus().isConnected())
key.cancel(); //Stream disconnected; remove from selector
}
}
I believe there is an example to this effect in example/EchoServer.java.
Hope this helps.
[1] With J2SE 1.4, there is no open implementation of TLS that works
with non-blocking I/O (that I am aware of). With J2SE 5, there is, but
no work as been done with it in JSO (although it is fairly easy to
implement, I believe).
--
- LW
GOT JABBER™? <http://www.jabber.org/>
More information about the JDev
mailing list