Hi
In the case of EJB , where should the remote exceptions be thorwn. Is it ok for the remote exception to be thrown in Home , Remote and EJB ?
Printable View
Hi
In the case of EJB , where should the remote exceptions be thorwn. Is it ok for the remote exception to be thrown in Home , Remote and EJB ?
What kind of bean are you creating? For session beans (processes, not data persistence):
Home
- A utility interface for creating (or finding) implemented-beans. The naming standard for the home interface is "<BEAN_NAME>Home"
The spec mandates that the create() [and create(String name) in the case of stateful session beans] must throw a RemoteException:
(The code in blue is only present in stateful session beans)Code://MyBeanHome.java
import java.rmi.*;
import javax.ejb.*;
public interface MyBeanHome extends EJBHome {
public MyBean create() throws RemoteException, CreateException;
public MyBean create(String name) throws RemoteException, CreateException;
}
_____________
Remote
-A general interface that defines the methods your bean actually will have. If you were writing a CreditCardVerifier bean, then here within, you may have a "VerifyDetails(String cc_number)" method.
The naming standard for the remote interface is "<BEAN_NAME>"
All methods in the Remote interface must also throw remote exception:
Code://MyBean.java
import java.rmi.*;
import javax.ejb.*;
public interface MyBean extends EJBObject {
public String getMyMethod() throws RemoteException;
public void setMyMethod(String myParameter) throws RemoteException;
}
_____________
Implementation
-Here is here you actually write code for the methods. The naming standard for the implementation class is "<BEAN_NAME>EJB".
Note that methods here do not throw the RemoteException, because the Home and Remote "interfaces" are not interfaces in the java sense; they do not mandate what the method signatures here must look like. The EJB container will generate extra classes to make everything work:
Everything in blue, above, is my implementation of the business logic. THe black is the general skeleton of an implementation bean. Note that even though my methods were defined as throwing remote exception in the remote interface, and the home interface mandated that thre create() methods also throw remoteexceptions, the implementation of them do not throw remoteexceptions, and the create() methods only throw CreateExceptions.Code:import java.rmi.*;
import javax.ejb.*;
public class MyBeanEJB implements SessionBean {
private String myString;
private SessionContext context;
public MyBeanEJB() {}
public void setSessionContext(SessionContext c) {
context = c;
}
public void ejbCreate() throws CreateException {
myString = "Hello World"
}
public void ejbCreate(String name) throws CreateException {
myString = name
}
public void ejbActivate() {}
public void ejbPassivate() {}
public void ejbRemove() {}
public String getMyMethod() {
return myString;
}
public void setMyMethod(String myParameter) {
myString = myParameter;
}
}
It is this way because, as note, the EJB container uses the home/remote to generate extra classes, that need to use RemoteException, but your implementation does not use RemoteException as it never sees the finer points of the RMI that makes it work..
Thanks a lot cjard
.That was a very useful explanation. Is the naming standard defined by java ?