I am writing a stateless session bean, and it needs to communicate with the backend system through socket, I know that you cannot use socket inside EJB, so I wrote a java class to handle this, but not quite sure how to handle it, right now, for each method call, I open socket and close it upon finish, this is certainly not a efficient way to do it, I would like to create a socket pool for this purpose, but need guidelines, can any gurus out there give me some leads? some sample code if possible? :)
Also what issues i need to be aware when dealing with socket in EJB?
Any help is greately appreciated!!!
I don't know what made you think you can't use sockets in EJBs. You can, to connect. You can't use sockets to listen on a port in EJBs, and this includes all the classes called by your EJB. So if you want to listen on ports, using this seperate class won't help.
As for the socket pooling part:
1. Your app server may pool sockets automatically, in which case you don't have to do anything. Check the docs before doing it yourself.
2. You can't change the socket implementation within an EJB, so if your server does not support pooling you can't make the pool "transparent". The best thing you can do, I think, is simply code a class that has a static Vector of sockets it gives to the EJBs. This will probably *work* in all App servers, but there is no guarantee of how many beans will share a common pool. Obviously, beans on different cluster nodes won't, for instance. On App servers that rigidly implement the spec, no two bean instances will share the pool, so you will get no performance boost from it. However, I'm not aware of any App server in which implementation is so rigid. Usually, beans on the same machine are likely to see the same static var (atleast those running in the same process). This can be avoided by using different class loaders, but most server won't go to that trouble. If this is the case in your server, you can boost up performance.
Note that this implementation, is not completely supported by the EJB spec. You shouldn't write to any static fields. However, it'll probably work on all servers.
Interesting reply, Gal.
What's the 'right' (i.e. standards-approved) way to do something like this on a server which doesn't support socket pools?
Its great to see this query here but am confused after looking at this. When i read the EJB-specs am confirmed that We cannot use Socket,Serversockets in BEANS but the answer to this query is quite different. I am not able to decide which one is right. CAN WE USE SOCKET,SERVERSOCKET classes in EJB BEANS.
Pls clarify coz i read twice in EJB-SPECS forbiding this classes.
Thanks in advance