Enterprise JavaBeans (EJBs) are Java server-side components.Ý Systems generally use them as middleware components in large distributed applications, connecting the data at the back end to the users at the front end. In the Enterprise JavaBeans model there are two main kinds of beans, entity beans and session beans. Entity beans were designed to represent persistent objects. Session beans generally represent an abstraction of a client that resides on an application server or a transient service in an environment that provides scalability. For example, in an e-commerce application, entity beans might be used to represent products to be purchased, while a session bean would be used to represent a shopping cart.
Note that rather than using entity beans to represent persistent objects, it is instead possible to use session beans and store the persistent data explicitly in a database. This requires slightly more work since it requires moving the data explicitly in and out of a database, but it gives more control over the database operations and can often result in more efficient programs. Thus it is not uncommon for enterprise applications to be built entirely out of session beans.
In AppComposer, you can build capsules that represent session EJBs. When you save a session EJB capsule as a jar, AppComposer generates all the required parts of the EJB: the bean implementation, the home interface, the remote interface, and the deployment descriptor. You can then use this bean in other capsules and programs. AppComposer creates only session beans, not entity beans. You can use any standard programming environment to create entity beans, if desired.
This example builds a session bean that follows an example first introduced in the Using Enterprise JavaBeans tutorial. The SimpleBank bean interacts with a database to perform basic banking transactions such as withdrawing or depositing funds, or obtaining an account balance. You will redesign the SimpleBank entity bean illustrated in that example as a stateful session bean that stores data in a database.
This tutorial assumes that you are familiar with Enterprise JavaBeans and AppComposer. For an introduction to EJB, see the Using Enterprise JavaBeans tutorial or Sun's website. For an introduction to AppComposer, see the Introductory Tutorial.
Note: Enterprise JavaBeans require an EJB container or application server in order to run. AppComposer includes the JBoss EJB application server, which must be enabled (in the AppComposer preferences) for this example to work.
AppComposer allows you to build three types of session beans:
You would implement these method calls to perform whatever additional tasks you want to take place at each point in the transaction.
To help you gain familiarity with how session bean capsules work, this tutorial walks you through creating a simple stateless session bean that has a single method defined. Stateless session beans can be as complex as needed for their tasks. In this case all you need to do is demonstrate the principle of how to use session bean capsules, so this one is as simple as possible.
Start AppComposer and enable JBoss if necessary. The console displays a tab for JBoss messages when it is running.
An empty capsule of the correct type (stateless session bean, in this example) provides default implementations of all of the required EJB methods. Later, when you save the capsule as a jar file, AppComposer compiles it and generates the appropriate interfaces and the required deployment descriptor that application servers use to run that type of bean. Of course, unless you add some functionality to it, the capsule will compile, but will not actually do anything.
To make the bean useful, it needs least one method.
Notice that two other attributes are available on your method. The permissions and transaction level attributes are only available for EJB capsule methods. Use them to build a deployment descriptor for an EJB. At runtime, the EJB container uses Permissions to restrict access to methods. If you are a novice EJB user, you do not need to worry about setting this yet. If you are an experienced user, you can set this field to any String value.
The transaction level attribute tells the EJB container how the method should behave with respect to transactions. The two most common settings are:
Others settings include:
Click on the OK button to finish defining the capsule method. Now that you have declared the method on the capsule, the capsule needs to know what to do when this method is called. You need to add a behavior that returns a string to the caller of the getTestString method..
Your simple capsule outline should now look like this:
You now have a simple EJB capsule that you can deploy and use in AppComposer or other EJB environments. If you have used session beans before, you may notice that AppComposer does not require you to define an ejbCreate method for this bean. AppComposer implements the ejbCreate method for stateless session beans automatically. You may still add functionality to the method, but you do not have to define it.
To deploy the session bean capsule, you need to save it as a jar file.
If you open the EJBGen menu in the Palette pane, you see an entry for MyFirstEJB in the acejb package. This means you successfully imported and deployed your EJB capsule. From this point, it works like any other EJB created in an outside development environment or acquired from a component vendor. If you look in the beans subdirectory of the main AppComposer directory, you will see a file called MyFirstEJB1.jar, which is the JavaBean stub used to talk remotely to the EJB (the home and remote interface for the EJB, in JavaBean format). The actual EJB itself is deployed on the JBoss application server, down in the JBoss subdirectory of the main AppComposer directory (for example, in C:\AppComposer\jBoss\ server\default\deploy).
For testing purposes, AppComposer includes a servlet called MyEJBServlet.zac that uses your EJB. Try out the simple method you created.
That is all there is to it. Congratulations! You just created and ran your first enterprise session bean capsule! AppComposer takes care of the mechanical parts of creating an EJB, so you can concentrate on building your functionality.
Normally the methods you define in an EJB would be more complex and would leverage the benefits of the EJB framework. In this case, you built a very simple method just to explore the creation, deployment and testing process. You would not normally implement this simple functionality as an EJB, since it requires none of the features provided by the Enterprise JavaBeans framework.
The rest of this tutorial takes you through building and using a more complex and useful session bean. You will build a stateful session bean that interacts with a database and can send dynamic output to a servlet. The data used is from the SimpleBank database, and you will create a session bean that can handle a customer withdrawing money from their account. As the first step in that process, the next section of this tutorial shows you how to set up some variables to hold the beans state, and connect the bean to the database so it can assign information from the database to those variables.