*
The moose likes EJB and other Java EE Technologies and the fly likes EJB listening on a client socket for callbacks Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » EJB and other Java EE Technologies
Bookmark "EJB listening on a client socket for callbacks" Watch "EJB listening on a client socket for callbacks" New topic
Author

EJB listening on a client socket for callbacks

Adam Michalik
Ranch Hand

Joined: Feb 18, 2008
Posts: 128
Hi!

I stumbled across a following problem:
1) An EJB opens a client socket to a (non-Java EE) server
2) The EJB should asynchronously receive messages from the server
The idea in "standard Java" would be to construct the EJB object, start a new thread, in that thread open a socket and put the incoming messages to some queue. The EJB then gets the messages from the queue for processing.
But Java EE forbids creating new threads. Do you have any ideas how to solve the problem?
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 30085
    
149

This sounds like what a Message Driven Bean would do. An MDB listens from messages on a queue and acts on them. The application server is handling the threads for you.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Adam Michalik
Ranch Hand

Joined: Feb 18, 2008
Posts: 128
Surely it is a problem easily solved by a MDB, but only if the server could send JMS messages... Unfortunately the third-party non-Java server does not support that, only raw socket connections are available.
Reza Rahman
author
Ranch Hand

Joined: Feb 01, 2005
Posts: 580
    
    5
Adam,

Creating threads is not that big of deal. You just have to be aware that the application server might not be able to scale as well since it won't have complete control over thread management.

Regards,
Reza


Independent Consultant — Author, EJB 3 in Action — Expert Group Member, Java EE 6 and EJB 3.1
Alok Kushwah
Ranch Hand

Joined: Jul 10, 2007
Posts: 31
If client on other hand can send data only on raw socket then go ahead a create a server socket and listen to that socket and create a slave thread for each connection/request to serve the request. The slave thread can call EJB directly (synchronouse) or send JMS messge (asynchronous) for further processing.

Don't create your own thread in ejb. Don't manage threads in EJB.

Adam Michalik
Ranch Hand

Joined: Feb 18, 2008
Posts: 128
Thanks for the replies!

I think I'll stick to the spec and will not tinker with thread management on the server. I thought about a standalone mini-app talking to the remote server and forwarding the messages via JMS to the EJB. But I came across technologies like resource adapters and connectors - does that sound applicable to you?
Reza Rahman
author
Ranch Hand

Joined: Feb 01, 2005
Posts: 580
    
    5
Adam,

Not saying one way or the other, but just to make sure, the spec does not forbid thread managemt, it discourage them. I've actually specifically talked with some vendors about this. Without going into great detail, it's more about SLA aggreements for an application server rather than a serious technical concern.

Just food for thought/options to consider.

Regards,
Reza
Alok Kushwah
Ranch Hand

Joined: Jul 10, 2007
Posts: 31
EJB spec says that enterprise bean must not attempt to listen on a socket, so no server socket is allowed in EJB. But you can open client socket to connect to server socket. However socket must be closed when the ejb is passivated. Socket connection must be established when the ejb is activated.

I will suggets avoid opening any type of socket in EJB.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: EJB listening on a client socket for callbacks
 
Similar Threads
Multithread-call center support
Design Question (Database Polling)
Java socket programming
serverSocket
Socket Server only processing one request, then it just queues them up