catch (RemoteException e) { throw new RuntimeException(e.getMessage( )); } } 13.2.2.3 What about the remote references? In our implementation, a remote reference to the session bean is created for each request. This isn’t a problem from a performance standpoint, because the overhead attached to creating a remote reference pales in comparison with that associated with the home. However, this doesn’t mean that you can’t cache remote references if you want. In fact, if you’re interfacing with a stateful session bean, you can’t keep creating new remote references across requests, because you won’t be calling the same bean instance each time. You can avoid creating a new remote reference for each request by caching a business delegate instance in the session. Unlike with homes, you can’t cache a remote reference as application-scope data. Even for stateless session beans, a remote reference holds information tied to the client thread that created it, so you can’t share it across user sessions. If you cache the business delegate, there are some important changes to make the user session could be serialized and restored by the web container, and a remote reference isn’t required to be serializable. For a stateless session bean, you need to be able to create a new remote reference in the event of an error or a restart of the EJB container being accessed. For a stateful session bean, you need to hold an EJB handle in your delegate instead of a remote reference, so that you can always maintain a way to access the same bean. In addition to the coverage in EJBDesignPatterns, you can find more information on the patterns discussed in this chapter in CoreJ2EEPatterns by Deepak Alur, John Crupi, and Dan Malks (Prentice Hall). 13.2.3 Using Dynamic Proxies In this section, we’ll look at one last example of something you might want to implement within your business delegate. Our current implementation works well for the Storefront application, but delegates tend to become cluttered with redundant-looking methods if they have to support an interface with more than a few methods. We declared only three methods for our overly simple Storefront model, but even they follow a somewhat monotonous pattern. With the exception of the logout( )and destroy( )methods, each business method in the delegate is implemented by calling the method with the same name on the session bean and catching a RemoteException to replace it with a DatastoreException. A dynamic proxy offers a way to get rid of this redundancy. If you ever find yourself performing the same additional steps as part of delegating a set of method calls to another object, you should consider introducing a dynamic proxy. This concept is a little difficult to grasp if you’ve never worked with one before, but its use can do away with a lot of repetitive code. Basically, a dynamic proxy is an object created at runtime using reflection that implements one or more interfaces you specify. The implementation of the interface methods consists of calling the invoke( )method of an object you also specify. This invoke( ) method is declared by the java.lang.reflect.InvocationHandler interface, which must be explicitly implemented by the object used to construct the proxy. The proxy passes parameters to the invoke( )method that identify the interface method that was called and the arguments that were passed to it. This idea is always easiest to explain by example, so Example 13-9 shows a replacement for our business delegate that can be used with a dynamic proxy.
We recommend high quality webhost to host and run your jsp application: christian web host services.