Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The proxy object in EJB

I am reading the Enterprise JavaBeans 3.1 book and I wonder if I have understood the concept of EJB proxy object correctly. I now know that it follows the proxy pattern and I have read some about it.

When we make the interfaces for the beans, we are doing it because we want to have the proxy pattern implemented. This helps us because the clients are only concerned about what we can do and not tied to directly to a class but rather a interface that can act as if it where the real object.

So, the container probably instantiate the proxy objects implementing the corresponding interface and add some magic code (networking code) before invoking upon the real EJB for us, because the proxy object is automatically made right?

Have I misunderstood the concept? If thats the case could someone tell me whats wrong?

like image 735
LuckyLuke Avatar asked Oct 14 '11 07:10

LuckyLuke


2 Answers

Correct. The interfaces you're writing for your beans would have been sufficient if your application was confined to a local JVM. In this case no proxy would be needed, as the implementing class could be instantiated and supplied directly.

Clients of EJBs cannot work on their implementing classes, as they don't have them in their classpath. EJBs are location-transparent, you can call them across the network, or from another application located on the same server, but isolated out by different class loaders. In such cases, you need to have proxy objects to marshal, send over the network, and unmarshal the parameters you supply to EJB calls and results of these calls that you receive. And on the client side, you need a dummy EJB interface implementation, that forwards your calls to the server where this EJB is installed.

Proxies also handle other functions, such as starting/ending transactions around EJB method calls.

EDIT: if you're curious what EXACTLY such proxies could do, take a look at overviews of RMI in Java and AOP (either in AspectJ or Spring). It will give you the idea what kinds of tasks can be implemented this way.

like image 179
MaDa Avatar answered Oct 21 '22 10:10

MaDa


Are you referring to the proxy interfaces to stateless (and stateful) session beans and message driven beans?

If so, I think your understanding is correct. The only thing you seemed to have missed is the concept of instance pools for stateless beans. The container does not instanciate these per request, but instead provides an implementation when needed.

Also, using proxies allows the container managed things to take place: transaction management, asynchronous thread management etc.

like image 27
tomtom Avatar answered Oct 21 '22 11:10

tomtom