File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Sockets and Internet Protocols and the fly likes Can EJB's use Sockets? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Sockets and Internet Protocols
Bookmark "Can EJB Watch "Can EJB New topic

Can EJB's use Sockets?

Brian Heisler

Joined: Apr 13, 2001
Posts: 4
Hello -
I need my EJB to communicate via sockets with a java application. Can this be done? Can I place the code for a client socket somewhere in the Bean (in a method?) and have the server code establish a connection and listen for requests from client on the java application?
Thanks for the help!
Jim Baiter
Ranch Hand

Joined: Jan 05, 2001
Posts: 532
No, this is against the EJB spec. There are several things you can do - a popular one is to use a JMS queue, have the bean act as the producer to the queue and another non-EJB class be the consumer. The bean can send a message to the queue and the consumer can open the socket and write the message to it.
Pearlo Muthukumaran
Ranch Hand

Joined: Sep 17, 2002
Posts: 79
But this takes out the charm of the request-response type of behavior available in the Socket Communications. JMS is typically asynchronous and if the reply is expected back by the EJB from the socket server synchronously, JMS will have all round abouts of creating a temp queue, block on it and stuff like that which is not at all desirable for a 24X7 type of applications.
Please refer point 5 of section EJB RESTRICTIONS of this link:
it sayeth, EJBs CAN open Client socket for communication.
I have personally used URLConnection object to extract dynamic content like currency rate, weather info etc., from within a Stateless Session Bean.
I am not sure if it works for pure Sockets. Ultimately, URLConnection must boil down to a Socket under the covers right?
Anybody who had successfully implemented a client socket from within EJBs, please share your experience. Please....
This is a major concern when we have External Socket Server is available as a product and we have no way to reengineer or add a layer of JMS as suggested.
Socket client is just another resource connector to an external world. But this topic is always being seen as BIG NO NO..... in EJB Context :-)
I agree. Here's the link:
subject: Can EJB's use Sockets?
It's not a secret anymore!