Client View of Bean  «Prev  Next»

Lesson 1

Client view of Stateful Session Bean

This module will provide the code for Enterprise Java Beans. This code is only for the client with a glimpse at the home interface[1] in the container. We will code the bean and associated classes and interfaces later. Note that session beans consume the rest of this course; entity beans and transactions are covered in part two of this series, Entity Beans, Transactions, and Security.

Looking up and creating the Bean Instance

To be able to invoke the business methods of a bean, the client will need to look up the remote object in the naming service[2] and download the stub. Once it has the stub, the client can use it to create an instance of the bean using the home object[3]. I will explain the responsibilities and illustrate how the home object is used in some detail.

Invoking the bean's Business Methods

We will then look at how the business methods are called by the client using the bean's EJBObject[4]. As the home object, the bean instance, the EJBObject, the container, or the transport may throw one or more exceptions. We will look at what they are and how to catch them. The final topic covers beanhandles[5].

Services of Beans

To illustrate these aspects of session beans, we will be using a bean called Hello. If it were a class in your program, rather than a remote object, it would be programmed as follows:
The code below discusses Java Exception Handling

public class Hello {
 private String name = null;
 public Hello() {
  // ----
 }

 public Hello(String name) {
  this.name = name;
 }

 public String sayHello() throws HelloException {
  if (name == null)
   throw new HelloException("name is null");
  return "Hello " + name;
 }

 public String sayHello(String name) {
  this.name = name;
  return "Hello " + name;
 }
}

public class HelloException extends Exception {
 HelloException(String reason) {
  super(reason);
 }
}

Please review this code before moving on to the next lesson.
In the next lesson, you will be introduced to the client/bean relationship.

[1] Home interface: The Java interface that is implemented by the deployer to build the home object.
[2] Naming service: An entity that provides a lookup service to enable clients to find specific remote objects.
[3] Home object: The EJB bean factory (see above). It listens on the network for client create() and remove() requests.
[4] EJBObject: That skeleton of the bean that implements the bean's interface.
[5] Handle: An object which contains all the information required to access an existing remote session bean.

Ad Developing Middleware in Java EE 8