Now, we will introduce the development of enterprise Bean from the following aspects:
1. Key steps for developing ejbs
Ii. How to Use JBuilder
3. Use other development tools other than JBuilder
4. Detailed details on how to develop eneterprise beans
1. Main steps for developing ejbs:
Generally, the entire development process (development, configuration, and assembly) includes the following aspects:
Development: you must first define three classes: Bean class, bean local (home) and remote (remote) interface class.
Configuration: the configuration includes generating the configuration descriptor-an XML file, declaring the attributes of the enterprise Bean, and binding the Bean class file (including the stub file and the skeleton file ). Finally, put these configurations in a jar file. You also need to define the environment attribute in the configurator.
Assemble the application: This includes installing enterprise beans to the server to test the connection at each layer. The assembler combines several enterprise beans with other components (servlet, applet, script, and so on. Combine them into a complete application. You can also combine several enterprise beans into a complex enterprise Bean.
Manage enterprise beans.
Ii. Use JBuilder:
JBuilder and EJB container can be seamlessly connected. JBuilder and inprise application servers include all tools for developing and configuring enterprise beans and required libraries:
The container that runs and manages enterprise beans.
Naming Service.
Transaction Service.
Java database.
APIS required for enterprise beans development.
An enhanced Java-to-IIOP compiler that supports value types and RMI signals.
JBuilder also provides a tool and Wizard for rapid development of application enterprise beans. Through simple and intuitive steps, the wizard helps you create an enterprise Bean. Some default values are set by ourselves to generate bean templates. On the above, we can add our own application logic.
JBuilder also provides an EJB Interface Generation wizard. The wizard generates the remote interface and home interface based on the public methods of enterprise Bean. JBuilder also provides a configurator Wizard to help us gradually create an XML descriptor file. And the generated stubs is centralized into a jar file.
3. Use an integrated environment other than JBuilder:
If you use other integrated environments (IDE ). Be sure to use the container tool included in the Integrated Environment IDE. Verify whether the IDE supports the corresponding version of The EJB specification, and check whether it correctly supports the ejb api.
Determine the version of jd to the supported ejb container. You can check the Inprise installation instructions to determine the JDK version supported by the EJB container.
When configuring enterprise Bean, you must use the tool provided by the inprise application server. These tools can edit and modify the Inprise configuration descriptors provided by third-party agents. You can also verify the configuration Descriptor and the bean source code.
Iv. Development of Enterprise beans:
In this section, we mainly discuss the tasks that must be completed by the enterprise Bean provider to develop the enterprise Bean. These tasks include:
Define and compile the enterprise Bean class: This is the implementation of the internal application logic of enterprise Bean.
Compile the remote interface class of enterprise Bean.
Compile the local home interface class of enterprise Bean.
It indicates the primary key class, which is only required for entity beans. Specify the name of the primary key in the configuration descriptor of enterprise Bean.
The Enterprise beans provider defines the remote and local interfaces to implement the enterprise Bean class. The remote interface provides an interface for the customer to call the application logic functions implemented by enterprise Bean. The home interface provides methods for generating and locating remote interface instances.
There is no formal connection (for example, inheritance relationship) between enterprise Bean class implementation, local home interface, and remote interface ). However, the methods declared in the three classes must comply with the specifications defined in EJB. For example, you declare an application method or application logic in enterprise Bean. This method is also declared in the remote interface of beans, so the two must have the same name. Bean must have at least one create () method: ejbcreate (). However, there can be multiple create () methods with different parameters. In the home interface, you must have the same method definition (the same number of parameters ). The ejbcreate () method returns a container-managed Persistent Object. Both return a primary key value for container management persistence. However, the return value type in the corresponding create () method of home is the remote interface.
Note: The ejbcreate () method of Entity Bean implementation is a bit different. The Entity Bean does not define the ejbcreate () method. If the entity is added to the database only through the application or through the database management program, the Entity Bean will omit the ejbcreate () method. The value returned by ejbcreate () is of the primary key type. If the ejbcreate () method is the method for containers to Manage Persistent Object beans, its return value is of the null type. If the Entity Bean implements Bean management persistence, the ejbcreate () method returns the primary key type. We will discuss their differences in detail later.
The entereprise bean provider defines the meaning of the enterprise Bean. Container tasks combine the home interface, remote interface, and enterprise Bean implementation classes. Ensure that the implementation classes of the remote interface and bean correspond during compilation and runtime.
Inheritance relationship of enterprise Bean:
Enterprise Bean implementation classes, remote interfaces, and home interfaces must all be inherited from different base classes. The home interface is inherited from javax. EJB. ejbhome. The remote interface is inherited from javax. EJB. ejbobject. The base classes of the remote and home interfaces are javax. RMI. Remote.
A session bean must implement the base class javax. EJB. sessionbean. The Entity Bean must implement the base class javax. EJB. entiybean. The base classes of these ejbs are inherited from javax. EJB. iseisebean. Javax. EJB. iseisebean inherits from Java. Io. serializable.
Remote interface:
Each enterprise Bean must have a remote interface. The remote interface defines the logical operations that the application requires the customer to call. These are some common methods that can be called by the customer, usually implemented by the enterprise beans class. Note that enterprise Bean customers do not directly access bean. It is accessed through the remote interface.
Ejbobject base class:
The remote interface of the enterprise Bean class extends the public Java interface of the javax. EJB. ejbobject class. Javax. EJB. ejbobject is the base class of all remote interfaces. The Code is as follows:
Package javax. EJB;
Public interface ejbobject extends java. RMI. Remote {
Public ejbhome getejbhome () throws java. RMI. RemoteException;
Public object getprimarykey () throws java. RMI. RemoteException;
Public void remove () throws java. RMI. remtoeexception, java. RMI. removeexception
Public handle gethandle () throws java. RMI. RemoteException;
Boolean isidentical (ejbobject P0) throws java. RMI. RemoteException;
}
The getejbhome () method allows you to obtain a related home interface. For object beans, use the getprimarykey () method to obtain the primary key value of the object bean. Remove () can delete an enterprise Bean. The specific semantics is explained in the context in the lifecycle of various types of enterprise beans. The gethandle () method returns a persistent handle of an enterprise Bean instance. The isindentical () method allows you to compare whether enterprise beans are the same.
Method requirements:
All methods in the remote interface must be declared as public and a java. RMI. remotexception exception must be thrown. In addition, the parameters and methods defined in all remote interfaces must be valid in the RMI-IIOP. Each method defined in the remote interface must have corresponding methods in the enterprise Bean class. The corresponding method must have the same name, parameters of the same type and quantity, the same return value, and the same exception.
The following code shows the remote interface ATM of the Session Bean in an ATM example ,. It declares an application method transfer (). The black part indicates the content required in the EJB specification. The remote interface must extend the javax. EJB. ejbobject class. Every method of the enterprise Bean called from the client must be declared in the remote interface. The transfer () method throws two exceptions. Insufficientfundsexception is an exception defined by the application.
Public interface ATM extends javax. EJB. ejbobject {
Public void transfer (string source, string target, float amount)
Throws java. RMI. RemoteException, insufficientfundsexception;
}
For the definition rules of remote interfaces, the Session Bean and the Entity Bean are the same.
Home interface:
The home interface of enterprise Bean controls the bean lifecycle. The create (), find (), and remove () operations of the enterprise Bean object (that is, the instance of the enterprise Bean) are provided. The Session Bean and Entity Bean have different lifecycles. Therefore, their home interface must define different methods.
The provider of enterprise Bean must define the home interface, and the EJB container must implement the home interface.
Similar to the remote interface, parameters in methods in the home interface, return values must also be valid for RMI-IIOP. All methods must throw the java. RMI. RemoteException exception.
The home interface must define one or more create () methods. Every such create () method must be named create. In addition, its parameters, regardless of the type or quantity, must correspond to the ejbcreate () method in the bean class. Note that the return value type of the CREATE () method in the home interface is different from that of the ejbcreate () method in the bean class.
The home interface of the object bean also contains the find () method.
Base class of ejbhome:
Each home interface extends the javax. EJB. ejbhome interface. The following code defines the javax. EJB. ejbhome interface:
Package javax. EJB;
Public interface ejbhome extends java. RMI. Remote (){
Void remove (handle) throws java. RMI. RemoteException, removeexception;
Void remove (Object primarykey) throws java. RMI. RemoteException, removeexception;
Ejbmetadata getejbmetadata () throws RemoteException;
Homehandle gethomehandle () throws RemoteException;
}
Two remove () methods are provided to delete the enterprise Bean instance. The first remove method is to use a handle to delete an enterprise Bean instance. The second remove method deletes an enterprise Bean instance through the primary key.
Among many enterprise Bean instances, the handle uniquely identifies an instance. A handle has the same lifecycle as the enterprise Bean It references. Considering an object, the customer can obtain the corresponding enterprise Bean instance through a handle. A handle can correspond to multiple instances of an enterprise Bean object. For example, even if the host where the enterprise Bean object is located crashes or the enterprise Bean object is moved between different machines, the handle is still valid. The handle here is a serialized handle, which is similar to the reference of string-oriented CORBA objects in CORBA.
The second remove operation in the ejbhome interface uses its primary key to determine the enterprise Bean to be deleted. The primary key can be any type that extends the Java object class. However, you must implement the Java serializable interface. Primary keys are the primary methods used to identify object beans. Generally, a primary key is a key word in a database, which uniquely defines the data represented by the Entity Bean.
The getejbmetadata () method returns the metadata interface of the enterprise Bean object. This interface allows the customer to obtain the metadata information of the enterprise Bean. Metadata may be used when a development tool is used to compile and link an application or when a configuration tool is used for configuration. The javax. EJB. ejbmetadata interface provides the javax. EJB. ejbhome interface, home class, remote interface, and primary key obtaining methods. It also provides an issesson () method to determine whether the object on the home interface is a session bean or an entity bean. The isstatelesssession () method indicates whether the session bean is stateful or stateless. The following code shows the code for defining the javax. EJB. ejbmetadata interface.
Public javax. EJB;
Public interface ejbmetadata {
Ejbhome getejbhome ();
Class gethomeinterfaceclass ();
Class getremoteinterfaceclasss ();
Class getprimarykeyclass ();
Boolean issession ();
Boolean isstatelessssession ();
}
The home interface of the Session Bean:
As we mentioned earlier, one session bean has only one customer. This means that when a customer creates a session bean, the instance of this session bean only exists for the customer who created it (Here we refer to stateful Session Bean. Stateless session beans can be used by multiple clients because they do not maintain the session state ).
Because the home interface includes the definition of one or more create () methods, it becomes the factory of Session Bean. For each create () method, the EJB specification defines the following naming conventions:
The returned value is the type of the remote interface of the Session Bean.
The method name can only be create ().
Each ejbcreate () method in the Session Bean class must have a create () corresponding to it.
The type and quantity of parameters for each create () method must correspond to the ejbcreate () method in the Session Bean class.
The method must throw the exception of Java. RMI. RemoteException.
The method must throw the javax. RMI. createexeption exception.
Parameters of the CREATE () method are used to initialize a new session bean object.
The following code shows the different create () methods of a session bean object. The required parts are shown in bold:
Public interface ATM home extends javax. EJB. ejbhome {
ATM create () throws java. RMI. RemoteException, javax. EJB. createexception;
ATM create (profile preferredprofile)
Throws java. RMI. remoteexeption, javax. ehrows java. RMI. RemoteException, removeexception;
Ejbmetadata getejbmetadata () throws RemoteException;
Homehandle gethomehandle () throws RemoteException;
}
Two remove () methods are provided to delete the enterprise Bean instance. The first remove method is to use a handle to delete an enterprise Bean instance. The second remove method deletes an enterprise Bean instance through the primary key.
Among many enterprise Bean instances, the handle uniquely identifies an instance. A handle has the same lifecycle as the enterprise Bean It references. Considering an object, the customer can obtain the corresponding enterprise Bean instance through a handle. A handle can correspond to multiple instances of an enterprise Bean object. For example, even if the host where the enterprise Bean object is located crashes or the enterprise Bean object is moved between different machines, the handle is still valid. The handle here is a serialized handle, which is similar to the reference of string-oriented CORBA objects in CORBA.
The second remove operation in the ejbhome interface uses its primary key to determine the enterprise Bean to be deleted. The primary key can be any type that extends the Java object class. However, you must implement the Java serializable interface. Primary keys are the primary methods used to identify object beans. Generally, a primary key is a key word in a database, which uniquely defines the data represented by the Entity Bean.
The getejbmetadata () method returns the metadata interface of the enterprise Bean object. This interface allows the customer to obtain the metadata information of the enterprise Bean. Metadata may be used when a development tool is used to compile and link an application or when a configuration tool is used for configuration. The javax. EJB. ejbmetadata interface provides the javax. EJB. ejbhome interface, home class, remote interface, and primary key obtaining methods. It also provides an issesson () method to determine whether the object on the home interface is a session bean or an entity bean. The isstatelesssession () method indicates whether the session bean is stateful or stateless. The following code shows the code for defining the javax. EJB. ejbmetadata interface.
Public javax. EJB;
Public interface ejbmetadata {
Ejbhome getejbhome ();
Class gethomeinterfaceclass ();
Class getremoteinterfaceclasss ();
Class getprimarykeyclass ();
Boolean issession ();
Boolean isstatelessssession ();
}
The home interface of the Session Bean:
As we mentioned earlier, one session bean has only one customer. This means that when a customer creates a session bean, the instance of this session bean only exists for the customer who created it (Here we refer to stateful Session Bean. Stateless session beans can be used by multiple clients because they do not maintain the session state ).
Because the home interface includes the definition of one or more create () methods, it becomes the factory of Session Bean. For each create () method, the EJB specification defines the following naming conventions:
The returned value is the type of the remote interface of the Session Bean.
The method name can only be create ().
Each ejbcreate () method in the Session Bean class must have a create () corresponding to it.
The type and quantity of parameters for each create () method must correspond to the ejbcreate () method in the Session Bean class.
The method must throw the exception of Java. RMI. RemoteException.
The method must throw the javax. RMI. createexeption exception.
Parameters of the CREATE () method are used to initialize a new session bean object.
The following code shows the different create () methods of a session bean object. The required parts are shown in bold:
Public interface ATM home extends javax. EJB. ejbhome {
ATM create () throws java. RMI. RemoteException, javax. EJB. createexception;
ATM create (profile preferredprofile)
Throws java. RMI. remoteexeption, javax. ehrows java. RMI. RemoteException, removeexception;
Ejbmetadata getejbmetadata () throws RemoteException;
Homehandle gethomehandle () throws RemoteException;
}
Two remove () methods are provided to delete the enterprise Bean instance. The first remove method is to use a handle to delete an enterprise Bean instance. The second remove method deletes an enterprise Bean instance through the primary key.
Among many enterprise Bean instances, the handle uniquely identifies an instance. A handle has the same lifecycle as the enterprise Bean It references. Considering an object, the customer can obtain the corresponding enterprise Bean instance through a handle. A handle can correspond to multiple instances of an enterprise Bean object. For example, even if the host where the enterprise Bean object is located crashes or the enterprise Bean object is moved between different machines, the handle is still valid. The handle here is a serialized handle, which is similar to the reference of string-oriented CORBA objects in CORBA.
The second remove operation in the ejbhome interface uses its primary key to determine the enterprise Bean to be deleted. The primary key can be any type that extends the Java object class. However, you must implement the Java serializable interface. Primary keys are the primary methods used to identify object beans. Generally, a primary key is a key word in a database, which uniquely defines the data represented by the Entity Bean.
The getejbmetadata () method returns the metadata interface of the enterprise Bean object. This interface allows the customer to obtain the metadata information of the enterprise Bean. Metadata may be used when a development tool is used to compile and link an application or when a configuration tool is used for configuration. The javax. EJB. ejbmetadata interface provides the javax. EJB. ejbhome interface, home class, remote interface, and primary key obtaining methods. It also provides an issesson () method to determine whether the object on the home interface is a session bean or an entity bean. The isstatelesssession () method indicates whether the session bean is stateful or stateless. The following code shows the code for defining the javax. EJB. ejbmetadata interface.
Public javax. EJB;
Public interface ejbmetadata {
Ejbhome getejbhome ();
Class gethomeinterfaceclass ();
Class getremoteinterfaceclasss ();
Class getprimarykeyclass ();
Boolean issession ();
Boolean isstatelessssession ();
}
The home interface of the Session Bean:
As we mentioned earlier, one session bean has only one customer. This means that when a customer creates a session bean, the instance of this session bean only exists for the customer who created it (Here we refer to stateful Session Bean. Stateless session beans can be used by multiple clients because they do not maintain the session state ).
Because the home interface includes the definition of one or more create () methods, it becomes the factory of Session Bean. For each create () method, the EJB specification defines the following naming conventions:
The returned value is the type of the remote interface of the Session Bean.
The method name can only be create ().
Each ejbcreate () method in the Session Bean class must have a create () corresponding to it.
The type and quantity of parameters for each create () method must correspond to the ejbcreate () method in the Session Bean class.
The method must throw the exception of Java. RMI. RemoteException.
The method must throw the javax. RMI. createexeption exception.
Parameters of the CREATE () method are used to initialize a new session bean object.
The following code shows the different create () methods of a session bean object. The required parts are shown in bold:
Public interface ATM home extends javax. EJB. ejbhome {
ATM create () throws java. RMI. RemoteException, javax. EJB. createexception;
ATM create (profile preferredprofile)
Throws java. RMI. remoteexeption, javax. EJB. createexception;
}
Note that the home interface of the Session Bean does not define the finder method to locate the object. Because a stateful Session Bean is only used by customers who create it. If it is not a session bean created by the customer, it is unnecessary or not allowed to locate such a session bean.
The home interface of the object Bean:
Like the home interface of the Session Bean, the home interface of the object bean provides the CREATE () method. In addition, the entity
The home interface of bean also provides the finder method, so that the customer can locate and use the object of the Entity Bean. Finder operations are necessary because entity beans survive for a long time and can be used by multiple customers. For most applications, Entity Bean instances exist. You only need to find one for use.
The home interface of an entity bean must provide a default finder method: finderbyprimary (primarykey ). This method allows the customer to locate the Entity Bean through the primary key. The method has only one unique parameter: primary key. The Return Value Type of the method is the remote interface type of the object bean. The primary key type can be any Java type that extends the Java object type. In the configuration descriptor, you must inform the container of the type of the primary key. Note: As defined, the findbyprimarykey () method always returns a single entity object. Other finder () methods can return the set of Entity objects.
The findbyprimarykey () method is defined as follows:
Findbyprimarykey (key)
Throws java. RMI. RemoteException, finderexception;
The home interface can also define other finder () methods. Each finder () method must have a corresponding implementation in the enterprise Bean class. Each finder method must comply with the following conventions.
The return value type is the remote interface type, or the finder method can return more than one entity object, or a collection type that uses the remote interface as the content type. Valid Java Collection types are Java. util. Enumeration interfaces (jdk1.1 specifications) or Java. util. Collection interfaces (Java 2 specifications ).
The Finder method always starts with "find. In the Entity Bean class, it is prefixed with ejbfind.
Java. RMI. RemoteException must be thrown.
Javax. EJB. finderexception must be thrown.
The throws clause in the home interface must also correspond to the throws clause of the ejbcreate () method of the object Bean class.