Application Server-JBoss Technical Support Document (2)

Source: Internet
Author: User

1. Download the example source code

The source code of all examples can be downloaded directly from www.jboss.org. After the download is complete, put it in a directory. Download URL: http://www.jboss.org/docs/manual/files/documentation-example.zip

1.1 create BEAN

This section mainly creates a simple EJB and allows you to view the Code. This "Interest" example is a simple stateless session EJB. It aims to calculate interest for all the borrowed money based on the interest rate. In fact, there is only one line of functionality in the entire package code.
1.2 Review EJBs

Before we can view the code, we should first review the EJB. The minimum EJB type must also have three classes: remote interface, home interface, and bean implementation class.

Remote interface provides the method in EJB to the outside world for external code to call. In this example, the class name is org. jboss. interest. Interrest.

Home interface is a class used to manage remote interfaces. Including creation and deletion. In this example, the class name is org. jboss. interest. InterrestHome.

Bean implementation class provides the implementation of all methods of home interface and remote interface. In this example, the class name is org. jboss. interest. InterrestBean.

Of course, a Bean may include other classes or even other packages. However, you must have these three classes. Other classes are created on top of these three classes. All classes are packaged into a JAR file, which uses a directory structure to reflect the package hierarchy. In this example, all classes are packaged in the org. jboss. interest package, so they must be in the org/jboss/interest/directory.

There must be a META-INF directory before jar files that contain all classes are created. This directory stores the deployment descriptor (usually "ejb-jar.xml"), and other optional XML files. These files tell the server to clarify service information about the application. For JBoss, the file name must be "jboss. xml ".

Create a jar file and deploy it on JBossServer. On the client side, you need a jndi. properties file, which tells your client where to initialize and find the JNDI Naming Service. From this service, the client finds the Interest bean and returns the home interface of the bean. Homeinterface is used to obtain a remote interface of bean. It can use remote interfaces to access the commercial methods provided by bean.

1.3 EJB class

We need three classes: remote interface, home interface, and bean implementation class. Remote interface class, with the file name Interest. java. The Code is as follows:

Package org.jboss.doc s. interest;
Import javax. ejb. EJBObject;
Import java. rmi. RemoteException;
/**
This interface defines the 'remote interface for the 'interest EJB. Its
Single method is the only method exposed to the outside world. The class
InterestBean implements the method.
*/
Public interface Interest extends EJBObject
{
/** Calulates the compound interest on the sum 'prinle le, with interestrate per period 'rate over' periods time periods. this method alsoprints a message to standard output; this is picked up by the EJBserver and logged. in this way we can demonstrate that the method isactually being executed on the server, rather than the client. */

Public double calculateCompoundInterest (double principle, double rate, double periods) throws RemoteException;

}

The remote interface has only one commercial method: calculateCompoundInterest. The Home interface file name is InterestHome. java. The Code is as follows:

Package org.jboss.doc s. interest;
Import java. io. Serializable;
Import java. rmi. RemoteException;
Import javax. ejb. CreateException;
Import javax. ejb. EJBHome;

/**
This interface defines the home interface for the Interest EJB.
*/
Public interface InterestHome extends EJBHome
{
/** Creates an instance of the 'interestbean class on the server, andreturns a remote reference to an Interest interface on the client .*/
Interest create () throws RemoteException, CreateException;
}

Finally, we provide the bean implementation class, the file name: InterestBean. java. The Code is as follows:

Package org.jboss.doc s. interest;
Import java. rmi. RemoteException;
Import javax. ejb. SessionBean;
Import javax. ejb. SessionContext;

/**
Thisclass contains the implementation for the calculateCompoundInterestmethod exposed by this Bean. it uses des empty method bodies for themethods prescribe by the SessionBean interface; these dont need to doanything in this simple example. */public class InterestBean implementsSessionBean
{
/** Calulates the compound interest on the sum 'prinle le, with interest rate per period 'rate over' periods timeperiods. this method also prints a message to standard output; this ispicked up by the EJB server and logged. in this way we can demonstratethat the method is actually being executed on the server, rather thanthe client. */
Public double calculateCompoundInterest (double principle, double rate, double periods)
{
System. out. println ("Someone called 'calculatecompoundinterest! ");
Return principle * Math. pow (1 + rate, periods)-principle;
}
/** Empty method body */
Public void ejbCreate (){}
/** Every ejbCreate () method ALWAYS needs a corresponding ejbPostCreate () method with exactly the same parameter types .*/
Public void ejbPostCreate (){}
/** Empty method body */
Public void ejbRemove () {}/ ** Empty method body */
Public void ejbActivate () {}/ ** Empty method body */
Public void ejbPassivate () {}/ ** Empty method body */
Public void setSessionContext (SessionContext SC ){}

}

Note that most methods are empty. Because these methods are described in the SessionBean interface, they must exist in InterestBean, but in this simple example, no specific content is required.



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.