Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

EJB listening on a client socket for callbacks

 
Adam Michalik
Ranch Hand
Posts: 128
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
author & internet detective
Marshal
Posts: 33696
316
Eclipse IDE Java VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Adam Michalik
Ranch Hand
Posts: 128
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 580
5
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Alok Kushwah
Ranch Hand
Posts: 31
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 128
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 580
5
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 31
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic