|
Home > Archive > Apache Directory Project > December 2005 > [MINA] getting accesss to the IoSession from another Thread
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
[MINA] getting accesss to the IoSession from another Thread
|
|
| Chris Allen 2005-12-19, 8:45 pm |
| 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.=20
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 =3D connector.connect(new InetSocketAddress(
bridgeAccountDAO.getJabberServerHostName(),
bridgeAccountDAO.getJabberServerPort()),
(IoHandler) sessionHandler);
future.join();
session =3D 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 =3D 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 respons=
e)
throws ServletException, IOException {
PrintWriter out =3D response.getWriter();
response.setContentType("text/xml");
IoSession session =3D Scope.getJabberSession();
//test sending this message
session.write("<presence from=3D\"bridge-km3ckdJIV1DOjDMxwrMkJWD2FQJk+8+b@public.gmane.org\"
type=3D\"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
| |
| Chris Allen 2005-12-20, 5:45 pm |
| 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-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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-km3ckdJIV1DOjDMxwrMkJWD2FQJk+8+b@public.gmane.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
>
| |
| Trustin Lee 2005-12-20, 8:45 pm |
| Sorry for not replying to your message. I was busy catching up previous
messages. 
It's good to hear that you figured out how to do it.
Cheers,
Trustin
2005/12/21, Chris Allen <mrchrisallen-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>:
>
> 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-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:
>
>
--
what we call human nature is actually human habit
--
http://gleamynode.net/
PGP Key ID: 0x854B996C
| |
| Chris Allen 2005-12-21, 2:45 am |
| Hey Trustin,
No problem at all. Sometimes just writing out the problem in an email
like this is helpful enough to figure out the problem.
In another topic, I'm curious about what you guys are doing in terms
of Spring integration. I saw that this was listed on your road map
and I would like to learn more about it.
Anyway, I must say again, MINA is really incredible. It has saved me
a ton of time and greatly simplified the NIO stuff on this project,
and I'm also working on Red5 ( http://osflash.org/red5 ), and I can't
say how much we appreciate having this library there.
Keep up the great work!
-Chris
On 12/20/05, Trustin Lee <trustin-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:
> Sorry for not replying to your message. I was busy catching up previous
> messages. 
>
> It's good to hear that you figured out how to do it.
>
> Cheers,
> Trustin
>
>
> 2005/12/21, Chris Allen <mrchrisallen-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>:
lly[vbcol=seagreen]
> does, I've now just set it up with simple static getters and setters:
r[vbcol=seagreen]
> me.
a[vbcol=seagreen]
> bridgeAccountDAO.getJabberServerHostName(),
> bridgeAccountDAO.getJabberServerPort()),
> threads:
> response)
g\"[vbcol=seagreen]
t[vbcol=seagreen]
,[vbcol=seagreen]
>
>
>
> --
> what we call human nature is actually human habit
> --
> http://gleamynode.net/
> PGP Key ID: 0x854B996C
| |
| Niklas Therning 2005-12-21, 2:45 am |
| Chris Allen wrote:
> ...
>
> In another topic, I'm curious about what you guys are doing in terms
> of Spring integration. I saw that this was listed on your road map
> and I would like to learn more about it.
Hi Chris,
At the moment there are a number of Spring FactoryBeans in
org.apache.mina.integration.spring which simplifies the configuration of
IoAcceptors and IoConnectors using Spring. There are also some
FactoryBeans which helps you set up KeyStores and SSLContexts for use
with the SSLFilter. Have a look at the Javadocs for
SocketAcceptorFactoryBean for a short example on how to use it. If you
have any further questions or suggestions for improvements please let me
know.
BTW, the Spring integration package was introduced in 0.9.0.
/Niklas
| |
| Chris Allen 2005-12-21, 5:45 pm |
| Thanks Niklas,
I will check it out.
-Chris
On 12/21/05, Niklas Therning <niklas-8FIgwK2HfyIwFerOooGFRg@public.gmane.org> wrote:
>
> Chris Allen wrote:
>
> Hi Chris,
>
> At the moment there are a number of Spring FactoryBeans in
> org.apache.mina.integration.spring which simplifies the configuration of
> IoAcceptors and IoConnectors using Spring. There are also some
> FactoryBeans which helps you set up KeyStores and SSLContexts for use
> with the SSLFilter. Have a look at the Javadocs for
> SocketAcceptorFactoryBean for a short example on how to use it. If you
> have any further questions or suggestions for improvements please let me
> know.
>
> BTW, the Spring integration package was introduced in 0.9.0.
>
> /Niklas
>
|
|
|
|
|