Client View of Bean  «Prev  Next»

Lesson 6Calling the bean's business method
ObjectiveWrite Java Code to invoke Business Methods

Write Java Code to invoke Business Methods

Write the Java code to invoke a business method on the EJBObject.
On the creation of a new bean instance, the create() returns a remote reference to the bean's EJBObject instance.
Remember that there is a one-to-one relationship between the instances of the EJBObject and the bean.
Calling the bean's business methods
Calling the bean's business methods

For our Hello bean, the remote interface used by the deployer to build the EJBObject will be similar to the following code skeleton:

View the code below for the EJBObject interface.
public interface Hello extends EJBObject {
 public String sayHello() throws RemoteException, HelloException;
 public String sayHello(String name) throws RemoteException;
}

Using the remote reference to the bean's EJBObject instance that was returned from create() (see above) and stored in the variable called bean, we can invoke the methods as follows:
try {
 String s = bean.sayHello("fred");
 System.out.println(s);
 String s = bean.sayHello();
 System.out.println(s);
} 
catch (Exception e) {
 System.out.println(e);
}

If no exceptions were thrown, then running the client would produce:
Hello fred
Hello fred
All remote methods throw java.rmi.RemoteException.
They can also throw bean-specific exceptions such as HelloException, which is thrown when the name in the bean instance has not been initialized.


java.rmi.RemoteException has not been removed from Jakarta EE 10.
Here's why:
  • java.rmi.RemoteException is a core Java SE class: It belongs to the java.rmi package, which is part of the Java Standard Edition (Java SE) platform. Jakarta EE builds on top of Java SE. Jakarta EE specifications can deprecate or remove their own APIs (those typically in jakarta.* or previously javax.* packages), but they cannot remove classes from Java SE itself.
  • RMI is still a fundamental remoting mechanism: While modern applications might use other remoting protocols (like REST, gRPC), RMI (Remote Method Invocation) is still a foundational technology.
  • Enterprise JavaBeans (EJB) remote interfaces, for instance, traditionally relied on RMI for communication between different JVMs.
  • Even if EJB remote business interfaces (since EJB 3.0) don't require methods to declare throws RemoteException (the container handles wrapping), the underlying communication can still be RMI-based, and a RemoteException (or a subclass) could occur and be wrapped in an EJBException.

Usage in Jakarta EE APIs:
While you might not see throws java.rmi.RemoteException as frequently in new Jakarta EE API method signatures (especially with EJB's POJO remoting model), it can still be the root cause of runtime exceptions like jakarta.ejb.EJBException when remote calls fail due to network issues, marshalling problems, etc. Some lower-level or older Jakarta EE specifications that directly leverage RMI might still explicitly deal with it.

In summary:
java.rmi.RemoteException is part of Java SE, which Jakarta EE 10 relies upon. Therefore, it's still present and can occur, especially in distributed scenarios involving technologies like EJB remoting, even if it's often wrapped by higher-level Jakarta EE exceptions. The focus in modern Jakarta EE has shifted towards making its declaration less boilerplate for developers, but the exception itself remains.

Removing a bean

To remove the bean instance we use the remove method of the EJBObject:
try {
  bean.remove();
} 
catch (javax.ejb.NoSuchObjectException e) {
  // ----- 
}
catch (java.rmi.RemoteException e) {
  //-----
}

If the bean instance has already been removed, the NoSuchObjectException will be thrown.

Build Ejb Client - Exercise

Click the Exercise link below to practice building the code for a complete client.
Build EJB Client - Exercise
In the next lesson, you will be introduced to the subtleties of exception handling in the bean.

SEMrush Software