Okay, I figured it out.  I had misunderstood what the ThreadLocal actually does, I've now just set it up with simple static getters and setters:

public class Scope {
    private static IoSession jabberSession;
    public static IoSession getJabberSession() {
        return jabberSession;
    public static void setJabberSession(IoSession session) {
        Scope.jabberSession = session;

It works perfectly now.  Thanks to anyone that was looking into this for me.


On 12/19/05, Chris Allen <mrchrisallen@gmail.com> wrote:

First of all, sorry for such a long email.  Also, I'm really happy
with using MINA, it's saving me a whole bunch of time; thanks very
much for creating this wonderful tool.

Anyway, now for my problem: I'm using MINA to create a Jabber XMPP
client that acts as a proxy to post the messages to a web server.
Another feature of this application is that it allows one to post to a
servlet an XMPP packet to be sent to the Jabber server. I'm using
Jetty for this.  The Jabber client running with MINA works great, and
Jetty also works great as I can run a servlet that accepts these http

The problem that I'm experiencing is that I can't seem to get a
reference to the IoSession object that MINA is using to write to the
Jabber server.  I'm using the following to set up the socket
connection and it does return an IoSession:

  ConnectFuture future = connector.connect(new InetSocketAddress(
(IoHandler) sessionHandler);

             session = future.getSession();
             //also set the globally accessable version for other threads:

//here is the Scope class:
public class Scope {

     private static ThreadLocal<IoSession> jabberSessionLocal = new

     public static IoSession getJabberSession() {
         System.out.println("getting the Jabber session");
         return jabberSessionLocal.get();

     public static void setJabberSession(IoSession session) {
         System.out.println("Setting the jabber session....");

//  and here is how it's being used in the servlet:

  public void doGet(HttpServletRequest request, HttpServletResponse response)
             throws ServletException, IOException {
         PrintWriter out = response.getWriter();
         response.setContentType ("text/xml");
         IoSession session = Scope.getJabberSession();
         //test sending this message
         session.write("<presence from=\"bridge@xmppdev.partners.org\"

         //out.println("<error>Hi! no post data was passed to the

  I get a NullPointerException to the  Scope.getJabberSession(); when
called from the servlet.  I've tried a few different approaches to get
access to the IoSession besides using the ThreadLocal technique above,
all of which have the same NullPointerException.  One of these
approaches was using Spring applicationContext to get access to the
IoSession as a property of that bean.  Still, no dice. ;-)

Any ideas on why this is happening?

Thanks very much in advance for any suggestions.