directory-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Chris Allen <mrchrisal...@gmail.com>
Subject Re: [MINA] getting accesss to the IoSession from another Thread
Date Tue, 20 Dec 2005 18:20:33 GMT
Hey,

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.

-Chris

On 12/19/05, Chris Allen <mrchrisallen@gmail.com> wrote:
>
> Hi,
>
> 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
> requests.
>
> 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(
>                      bridgeAccountDAO.getJabberServerHostName(),
>                      bridgeAccountDAO.getJabberServerPort()),
> (IoHandler) sessionHandler);
>
>              future.join();
>              session = future.getSession();
>              //also set the globally accessable version for other threads:
>              Scope.setJabberSession(session);
>
> //here is the Scope class:
> public class Scope {
>
>      private static ThreadLocal<IoSession> jabberSessionLocal = new
> ThreadLocal<IoSession>();
>
>      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....");
>          jabberSessionLocal.set(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\"
> type=\"available\"/>");
>
>          //out.println("<error>Hi! no post data was passed to the
> servlet</error>");
>      }
>
>   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.
>
> -Chris
>

Mime
View raw message