Proxy Object in EJB

I am reading an Enterprise JavaBeans 3.1 book and I wonder if I understood the concept of an EJB proxy object correctly. Now I know that it follows the proxy template, and I read something about it.

When we create interfaces for beans, we do this because we want a proxy template to be implemented. This helps us, because clients are only worried about what we can do and are not directly attached to the class, but rather to an interface that can act as if it were located where the real object is.

Thus, the container probably creates proxy objects that implement the corresponding interface and adds some magic code (network code) before calling for the real EJB for us, because the proxy object is automatically created correctly?

I misunderstood the concept? If this is the case, can someone tell me what is wrong?

+7
source share
2 answers

Correctly. The interfaces you write for your beans would be enough if your application was limited to a local JVM. In this case, a proxy is not needed, since the implementing class can be created and passed directly.

EJB clients cannot work on their implementing classes because they do not have them in their class path. EJBs are transparent by location, you can invoke them over the network or from another application located on the same server, but isolated by various class loaders. In such cases, you need to have proxy objects for marshalling, sending over the network, and decoupling the parameters that you submit to EJB calls and the results of those calls that you receive. And on the client side, you need a dummy implementation of the EJB interface that redirects your calls to the server on which this EJB is installed.

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

EDIT : if you are wondering what EXACTLY such proxies could do, take a look at the RMI overviews in Java and AOP (either in AspectJ or Spring). This will give you an idea of ​​what tasks can be implemented in this way.

+4
source

Do you mean proxy interfaces for a session without state (and state) beans and message-driven beans?

If so, I think your understanding is correct. The only thing you seem to have missed is the concept of instance pools for stateless beans. The container does not affect these requests, but instead provides an implementation if necessary.

In addition, the use of a proxy allows you to manage events in the container: transaction management, asynchronous thread management, etc.

+3
source

All Articles