|
||||||||||||
Outline
|
||||||||||||
1. PrefaceThis document was written using Sun JDK 1.4.2, Eclipse 3.1, MyEclipse 4.0, and the JBoss 3.2.5 application server. All screenshots are based upon the default user interface settings for Eclipse, MyEclipse Enterprise Workbench, and Windows XP. If you experience difficulty with the instruction of this document, please see the User Feedback section for how to provide feedback to the MyEclipse documentation team. |
||||||||||||
2. IntroductionThis Tutorial presents the basic features, concepts, and techniques for rapidly getting started in the development of J2EE Enterprise Java Beans (EJB) using the MyEclipse EJB Tools. Specifically, this document presents how to:
|
||||||||||||
3. RequirementsBelow is a list of software requirements need to replicate the examples presented in this document:
|
||||||||||||
4. MyEclipse EJB Project and Tools OverviewBefore we begin, here's a quick word about MyEclipse EJB Projects and Tools. First, what is a MyEclipse EJB Project and why is it important? A MyEclipse EJB Project is: A basic Eclipse Java project that includes a META-INF directory and additional metadata required by the MyEclipse EJB Tools, such as the EJB Creation Wizards and the MyEclipse Deployment Services. Thus, standard Eclipse and 3rd party Java tools may be used within a MyEclipse EJB Project.
You can distinguish MyEclipse EJB Projects from other project
types by the project icon,
Also depicted in Figure 1 are the project's metadata files. Note
these files are typically hidden in the default Eclipse views due
to the default use of the .* filename filter that hides dot
prefixed files. The
.project and
.classpath files are standard
Eclipse project information files. The .mymetadata and .xdoclet
files are MyEclipse metadata files and are required for MyEclipse
tools to function properly. Do not delete or
modify these files. Also, if your project is managed by
a source-code management tool such as CVS be sure to include all
these files as part of the project.
MyEclipse EJB Development Tools include:
A special note about attribute-oriented programming and XDoclet technology: MyEclipse provides sophisticated support for attribute-oriented programming using XDoclet technology and tools (see References ). MyEclipse EJB Tools, such as the EJB wizards and code generation features, rely on XDoclet for much of their operation. For example, the EJB Creation Wizard generates XDoclet 1.2 compliant EJB bean classes for Session, Entity, and Message-driven EJB's. XDoclet is then employed to auto-generate and maintain the bean's home and remote interfaces as well as the ejb-jar.xml deployment descriptor. While you are not required to use MyEclipse XDoclet capabilities in your development process, we believe the productivity gains from doing so far exceed counter arguments against the use of attributes. We support our recommendation for the use of attribute annotations by noting that: 1) native attribute support was recently incorporated into the latest release of the Java SDK, and 2) attribute annotations are anticipated to be the primary mechanism employed by the upcoming EJB 3.0 specification to greatly simplify EJB definition and maintenance efforts. |
||||||||||||
5. Creating an EJB ProjectThis section introduces the steps for creating an example EJB Project named "firstejb".
The remaining fields are prepopulated from the MyEclipse EJB Project Template. This template is defined under the MyEclipse J2EE Project preferences. Table-1 describes each of the fields for the New J2EE EJB Project wizard shown in Figure 3.
Table 1. New EJB Project wizard field definitions
Completing the wizard creates and configures the
firstejb EJB Project. Figure 5 depicts the structure
of the new project from the MyEclipse perspective. To open the
MyEclipse perspective, from the MyEclipse menubar select
Window > Open Perspective > Other and select
the MyEclipse perspective,
Figure 4 depicts the new EJB project structure. Newly created EJB projects will not include a default ejb-jar.xml file. There are 2 reasons for this: 1) the deployment descriptor will be created later using XDoclet technology and 2) a valid ejb-jar.xml file requires at least 1 EJB definition. At this point in the process, no EJB's exist and a corresponding ejb-jar.xml file would appear in your project with an error marker as a result. The alternative is a simple warning message in the Problems view that identifies that the required ejb-jar.xml deployment descriptor does not yet exist in your project. This file will be created later in this tutorial.
|
||||||||||||
6. Creating a Session EJB - Part-1
EJB development in MyEclipse uses a combination of EJB wizards and
integrated XDoclet support. Every EJB consists of 3 basic
parts:
The bean class is the primary implementation concern since it provides the EJB's functionality. From the bean class using XDoclet annotations, the bean's Remote and Home interfaces can be automatically generated as well as the EJB's entry within the ejb-jar.xml deployment descriptor. In this 2-part example, we demonstrate the process for creating a stateless session bean known as the Trader EJB. Part-1 focuses on the use of the MyEclipse EJB Creation Wizard and XDoclet support to create a basic deployable Trader EJB. Part-2 will introduce the process for implementation of business services or functions to the Trader EJB. The example Trader EJB will provide simplified services for the sale and purchase of stocks. Because the scope of this document is a quick introduction to MyEclipse EJB development features, the Trader EJB does not address transactions or integration of enterprise resources such as databases or message queues. We begin by creating the Trader bean implementation class using the EJB Creation Wizard. Following creation of the Trader bean class, we will demonstrate how to generate the Remote and Home interfaces as well as the ejb-jar.xml entry for the bean.
NOTE: XDoclet's default configuration requires the package name end with '.ejb'. Other package suffixes may be used, but doing so requires updating the project's EJB XDoclet settings manually before generation.
Completing this wizard results in the creation of the TraderBean implementation class. The Java editor will be automatically opened on this class as shown in Figure 7. Note how the EJB Creation Wizard automatically inserted XDoclet EJB tags that define the EJB interface and deployment details.
|
||||||||||||
7. Using XDoclet EJB FacilitiesBefore continuing to Part-2 of the Trader EJB development process, we need to introduce the steps for the configuration and use of XDoclet EJB features. The benefit of XDoclet technology is that a tag processor is able to analyze source files annotated with XDoclet information and generate dependent source code and configuration resources specific to the annotations. XDoclet relies on an ANT script to specify its execution environment and code generation tasks. An XDoclet tag consists of an annotation model that maps 1 or more annotations to corresponding ANT subtasks. MyEclipse allows you to configure and customize the subtasks to execute and their default operating values. By default, MyEclipse EJB projects are not automatically configured to use XDoclet features. This section presents the steps for enabling and configuring the firstejb EJB project with XDoclet support and executing XDoclet code generation to derive the Trader EJB's dependent source code and deployment components. |
||||||||||||
7.1 XDoclet EJB ConfigurationWe begin by adding the Standard EJB XDoclet configuration to the firstejb project. The end goal of this section is to produce the XDoclet configuration that will be used to create the ANT script that MyEclipse will execute to generate the additional EJB artifacts required to deploy the Trader EJB. |
||||||||||||
7.1.1 Basic EJBDoclet Configuration
In addition to the preconfigured XDoclet modules provided by MyEclipse you may create your own custom XDoclet configurations. The process for integrating a custom XDoclet module is beyond the scope of this document.
Once the Standard EJB module has been added to the list, use the
mouse to select it to reveal the doclets it contains.
Notice that the
ejbdoclet appears in lower left-hand
list. The subdoclets under the
ejbdoclet's contain both required and
optional subtasks. Many of the subtasks are not applicable to
a simple stateless session bean such as Trader EJB.
During completion of XDoclet configuration process are saved to the local file named . xdoclet. Additionally if the "Use xdoclet-build.xml file" build policy is enabled then MyEclipse will generate a file named xdoclet-build.xml from your new XDoclet settings and store this file in the root directory of your project. This completes the basic XDoclet EJB configuration process. The next section demonstrates how to configure XDoclet EJB subtasks for your specific application server. |
||||||||||||
7.1.2
Configuring Application
Server XDoclet (optional)
XDoclet is able to generate application server specific deployment descriptors. This section demonstrates configuring an XDoclet @ejbdoclet with a subdoclet for the JBoss application server. The list of application servers supported by XDoclet can be viewed in the pop-up menu shown in Figure 13. Note: later in this section we will demonstrate deployment of the firstejb to a local JBoss 3.2 server. Therefore we will use the JBoss subtask.
|
||||||||||||
7.2 Running XDoclet Code GenerationOnce you've configured your project's XDoclet settings you can run the XDoclet code generator at any time. Typically you should execute XDoclet after each change to the XDoclet EJB configuration or application server settings or when you modify the XDoclet annotations of any EJB bean implementation class. Now is a good opportunity to execute XDoclet on the firstejb project and test the new XDoclet settings as Trader EJB has not yet been extended with new XDoclet-annotated business methods.
Upon XDoclet's completion the firstejb project should include new versions of the Trader and TraderHome interfaces and the ejb-jar.xml and jboss.xml deployment descriptors. Figure 17 depicts the initial generation of these new project resources. Note: When we created the TraderBean.java class during Section 5, we specified its Java package name as com.genuitec.trader.ejb and emphasized that the package name end in ".ejb". This is because the Standard EJB XDoclet packageSubstitution subtask creates a corresponding Java package name ending in ".interfaces" and adds generated EJB and Home interfaces to it. In our example, the com.genuitec.trader.interfaces package was created and it contains the for the Trader EJB and TraderHome interfaces. If you did not follow this package naming convention and are using default XDoclet setting some of the files may not be generated.
|
||||||||||||
8. Creating a Session EJB - Part-2In Part-1, Creating a Session EJB, we demonstrated the process for creating the TraderBean class. In this section we will add business methods as well as XDoclet annotations to the TraderBean class and then use XDoclet to update the Trader and TraderHome interfaces created in Section 7.2. When we created the initial TraderBean.java class, the EJB Creation Wizard added an example business method that you can use as a pattern for adding your own business methods. Notice that this method includes the javadoc tag @ejb.interface-method. This custom ejbdoclet tag identifies the method as an implementation of an EJB interface method. When XDoclet is run on the this class, the Trader.java EJB interface class will be revised to include all methods with the @ejb.interface-method annotation.
As you enter XDoclet annotations in the Java editor, note that XDoclet code completion features are available. MyEclipse extends the Java editor with the addition of this feature as well as the ability to invoke code completion on EJB APIs.
Enter
ctrl+space to invoke code completion suggestions
for this annotation.
|
||||||||||||
9. Deploying an EJB ProjectThe Enterprise Archive (EAR) is the only required deployment model specified by the J2EE specification. Yet, the specification makes provision for application server specific deployment models. Application servers such as JBoss and Weblogic support direct deployment of EJB Jar archives. A restriction of direct EJB Jar deployment is there is no provision for deployment of optional packages required by an EJB jar archive. See Section 8.2 of the J2EE 1.4 specification for optional package deployment details. This section presents abbreviated instructions for deploying the firstejb project to a local JBoss 3.2 instance as a single EJB Jar archive. Please view the Application Server Tutorial for a more thorough guide to the deployment process.
Figure 20. Console output of JBoss showing the EJB was successfully deployed |
||||||||||||
10. Testing an EJBStandalone EJB testing requires a distributed test client. Creation of a test client for the Trader EJB is a non-trivial effort that is beyond the scope of this document. For efficiency purposes we have elected to defer this topic to the MyEclipse Enterprise Application Tutorial. That document describes the process for creating an Enterprise application composed of the Trader EJB and a simple web client that are packaged and deployed as a single unit. The web client serves as a test client. |
||||||||||||
11. Customizing EJB Project PreferencesMyEclipse provides a number of customizable preferences. You are encouraged to familiarize yourself with each of these. We have already seen several EJB Project wizards and dialogs with prepopulated fields. The value of many prepopulated dialog fields is frequently derived from the MyEclipse system preferences. Two MyEclipse preferences that you should be aware of are the J2EE Project Templates preferences and the EJB Project Deployment Policies. See the Advanced Features section for a description of the later. To access the MyEclipse preferences do the following: From the menubar select Window > Preferences > MyEclipse > J2EE Project > EJB Project (See Figure 21).
Note: changing the EJB Project template folders will only affect new projects, not existing projects. |
||||||||||||
12. Advanced FeaturesThis section describes working with EJB applications that consists of an EJB Project and additional Java projects. Please note that if your EJB Project depends upon 3rd party Jars then you must use an Enterprise Project to incorporate the EJB Project and the 3rd party libraries into a single Enterprise Archive (EAR) deployment unit. To learn more about Enterprise Projects and EAR deployment visit the EAR Project Development Tutorial. |
||||||||||||
12.1
Dependent Java Project Setup and
Deployment Policy Configuration
|
||||||||||||
12.1.1 Establishing a Java Project dependency,A dependent Java project is defined through the EJB Project's Build Path properties . To access the Build Path properties dialog from the target EJB project's context-menu select Properties > Java Build Path > Projects. See Figure 22 for an example of establishing the Logging Java project as a dependent project of the firstejb project. Doing this enables firstejb project source code to have access to the classes defined by an example Logging Java project. Code completion, validation, and other tools are able to reference Logging project resources.
|
||||||||||||
12.1.2 Configuring the Deployment Policy of Dependent Java ProjectsThe MyEclipse EJB Project Deployment Service provides 2 policies that define how dependent Java projects are processed during EJB Project deployment. Table 2 describes each of these policies. Table 2. Dependent Java Project Deployment Policy descriptions
The default deployment behavior for all EJB Projects is defined by the MyEclipse system preferences (See Figure 23).
Individual EJB Projects may override the system default behavior from the MyEclipse-EJB Project properties dialog (see Figure 24).
|
||||||||||||
13. SummaryThis concludes your introduction to creating a session EJB with MyEclipse. Additional Tutorial documents are available that introduce working with Web Projects, editing, application server configuration, enterprise application projects and database development. For more information visit the MyEclipse Tutorial library. |
||||||||||||
14. User FeedbackIf you have comments or suggestions regarding this document please submit them to the MyEclipse Documentation Forum. |
||||||||||||
15. Resources
|