In a session bean method of an EJB, I need to know the IP address with which the client called the method. Is it possible to obtain this information in JBoss 4.2.3?
-
This is a question already asked in stakcoverflow before : http://stackoverflow.com/questions/1014358/how-can-you-get-the-calling-ip-address-on-an-ejb-call – Ben Mar 16 '12 at 06:43
1 Answers
What I am suggesting is a pure hack but it might work.
You should get a thread dump of JBoss 4.2.3 while you have these remote clients active. The thread dump looks something like this (please don't take this thread dump literally, its just a sample for you to get an idea of how a thread dump looks like)
"Attach Listener" daemon prio=9 tid=7f8624000000 nid=0x10eeed000 waiting on condition [00000000]
java.lang.Thread.State: RUNNABLE
Locked ownable synchronizers:
- None
"ajp-127.0.0.1-8009-Acceptor-0" daemon prio=5 tid=7f863d9c5000 nid=0x119033000 runnable [119032000]
java.lang.Thread.State: RUNNABLE
at java.net.PlainSocketImpl.socketAccept(Native Method)
at java.net.PlainSocketImpl.accept(PlainSocketImpl.java:408)
- locked <7b20d3d28> (a java.net.SocksSocketImpl)
at java.net.ServerSocket.implAccept(ServerSocket.java:462)
at java.net.ServerSocket.accept(ServerSocket.java:430)
at org.apache.tomcat.util.net.DefaultServerSocketFactory.acceptSocket(DefaultServerSocketFactory.java:61)
at org.apache.tomcat.util.net.JIoEndpoint$Acceptor.run(JIoEndpoint.java:314)
at java.lang.Thread.run(Thread.java:680)
Locked ownable synchronizers:
- None
"http-127.0.0.1-8080-Acceptor-0" daemon prio=5 tid=7f8633b9f000 nid=0x1187ae000 runnable [1187ad000]
java.lang.Thread.State: RUNNABLE
at java.net.PlainSocketImpl.socketAccept(Native Method)
at java.net.PlainSocketImpl.accept(PlainSocketImpl.java:408)
- locked <7b20d3f08> (a java.net.SocksSocketImpl)
at java.net.ServerSocket.implAccept(ServerSocket.java:462)
at java.net.ServerSocket.accept(ServerSocket.java:430)
at org.apache.tomcat.util.net.DefaultServerSocketFactory.acceptSocket(DefaultServerSocketFactory.java:61)
at org.apache.tomcat.util.net.JIoEndpoint$Acceptor.run(JIoEndpoint.java:314)
at java.lang.Thread.run(Thread.java:680)
As you can see, each thread has an associated protocol/ip address and more detail around the thread. For RMI specific threads you'll find a thread name contain the client IP as well (its something like this WorkerThread#0[192.168.0.108:55208]) You can parse the thread "NAME" and extract the client IP.
This is quite a hack because thread names may change between different versions but for a specific version, this should work.
Here is an official JBoss Wiki [1] that explains with sample code.
Hope this helps.
Good luck!
[1] https://community.jboss.org/wiki/HowtogettheClientipaddressinanEJB3Interceptor

- 3,659
- 2
- 20
- 23
-
1OP is asking for client IP, not server IP. Even more, there are much cleaner ways to get server IP. – BalusC Mar 19 '12 at 17:18
-
JBoss creates a new thread for every RMI connection and my understanding is that the client IP is embedded in the thread name. I agree this is NOT a standard or ideal way of doing it BUT as you mentioned if there are better ways you should share that with the OP. – uaarkoti Mar 19 '12 at 17:49
-
BTW, here is the official Wiki that uses exactly what I proposed. https://community.jboss.org/wiki/HowtogettheClientipaddressinanEJB3Interceptor – uaarkoti Mar 19 '12 at 17:53
-
Hmm, but those thread dumps which you've shown are of the web server, not the EJB client. – BalusC Mar 19 '12 at 18:04
-
The thread dumps I showed was just to get a rough idea. As I clearly pointed out in the second sentence "You should get a thread dump ... while you have these rmi clients active" to see the active value of the thread names. This thread dump is not be taken literally. Also you mentioned there are better ways of doing this and if you do have ideas, please post them so everyone can benefit. – uaarkoti Mar 19 '12 at 18:10
-
OP asked how to get the client IP. The thread dumps in your example contain the server IP. There are cleaner ways to get server IP. But OP isn't asking how to get the server IP. That's all what I tried to make clear. I think I now understand that your thread dumps were just exemplary (and actually misleading as they don't contain the client IP). – BalusC Mar 19 '12 at 18:15
-
That's fair. I will add a comment in my answer to not take the thread dump literally. – uaarkoti Mar 19 '12 at 18:18
-