Skip Headers
NetBeans Developing Applications with NetBeans IDE
Release 8.0

E50452-06
Go to Documentation Home
Home
Go to Book List
Book List
Go to Table of Contents
Contents
Go to Feedback page
Contact Us

Go to previous page
Previous
Go to next page
Next
PDF · Mobi · ePub

15 Developing with Enterprise Beans

This chapter describes how to use the IDE to implement Enterprise JavaBeans (EJB) technology in applications.

This chapter contains the following sections:

15.1 About Developing with Enterprise Beans

Written in the Java programming language, an enterprise bean is a server-side component that encapsulates the business logic of an application. The business logic is the code that fulfills the purpose of the application. In an inventory control application, for example, the enterprise beans might implement the business logic in methods called checkInventoryLevel and orderProduct. By invoking these methods, remote clients can access the inventory services the application provides. See Section 15.3, "Creating an Enterprise Bean."

Enterprise beans provide the following advantages when developing large, distributed applications:

You can develop the following types of enterprise beans:

For additional information see Section 15.2.1, "How to Create an EJB Module Project," and Section 15.1, "About Developing with Enterprise Beans."

15.2 Creating an EJB Module Project

The following steps summarize enterprise development tasks.

  1. Create the EJB module project.

    • Register a Java EE application server. You cannot deploy EJB modules to the Tomcat server.

    • Create the project for the EJB module.

    • Add the EJB module to an enterprise application.

  2. Code the enterprise beans.

    • Create the enterprise beans.

    • Access the database.

      For Java EE 5 applications, use Java Persistence.

      If you want to use a database schema, make sure the database schema file exists in your project's conf folder.

    • Call enterprise beans from web application modules and other enterprise beans.

    • Add business methods to your enterprise beans.

    • Add select methods and finder methods to your entity beans.

    • Use server resources like databases and JMS messages.

  3. Configure the application.

    • Edit the EJB module's deployment descriptors.

    • Configure container-managed relationships between enterprise beans.

  4. Build the project.

    • Set the project as the main project and choose Run > Build Main Project (F11) from the main menu or right-click the project node in the Projects window and choose Build.

  5. Deploy the module.

    1. For stand-alone EJB modules, right-click the EJB module's project node and choose Run.

    2. For EJB modules that are part of an enterprise application, right-click the enterprise application and choose Run.

15.2.1 How to Create an EJB Module Project

The IDE provides wizards to help you create EJB module projects for EJB 3.1 enterprise beans (EJB 2.1 is a mandatory part of the EJB 3.1 specification). The wizard can also help you create an EJB module if you already have existing source code.

To create an EJB module project:

  1. Choose File > New Project (Ctrl+Shift+N) from the main window.

  2. From the Java EE category, select one of the following project templates:

  3. Follow the steps in the rest of the wizard.

For related information see Section 5.2.3, "Creating Standard Projects," and Section 11.2.1, "How to Create a Web Application Project."

Note:

For standard EJB module projects, the IDE does not support J2EE 1.3 EJB modules and automatically updates the version number of your EJB module's ejb-jar.xml to 1.4. The J2EE 1.4 specification is fully backward-compatible, so upgrading the specification level does not change the behavior of your code.

When you import an individual EJB module into a standard project, the module can have any folder structure. When importing an entire application using the Enterprise Application with Existing Sources template, the project's folder structure must adhere to the application structure shown in Figure 15-1.

Figure 15-1 J2EE Blueprint Conventions for Application Structures

J2EE blueprint conventions for apps

15.3 Creating an Enterprise Bean

The IDE provides templates for creating session beans, message-driven beans, entity beans, and legacy CMP entity beans.

15.3.1 How to Create Enterprise Beans

Enterprise Java Beans can be added to EJB module projects.

To create enterprise beans:

  1. In the Projects window, select any existing EJB module project. See Section 15.2.1, "How to Create an EJB Module Project."

  2. Choose File > New from the main menu.The New File wizard opens.

  3. From the Enterprise JavaBeans category, select one of the following EJB templates:

    • Session Bean

    • Timer Session Bean

    • Message-Driven Bean

    • Session Beans for Entity Classes

  4. Follow the directions in the rest of the wizard.

When you create an enterprise bean, the IDE does the following:

  • Java EE projects:

    • Adds a node for the enterprise bean under the project's Enterprise Beans node. This node can contain nodes for bean methods and fields.

    • Creates the bean class and the interfaces under the Source Packages node

    • Opens the bean class in the Source Editor

  • J2EE 1.4 projects:

    • Adds a node for the enterprise bean under the project's Enterprise Beans node. This node can contain nodes for bean methods and fields.

    • Updates the deployment descriptors for the EJB module and the Enterprise Application module that contains the EJB module. If your target server is the GlassFish server, the IDE also configures the server-specific deployment descriptors. For all other application servers, you have to write the server-specific deployment descriptors yourself.

    • Opens the bean class in the Source Editor

For more information on enterprise projects and beans, see Section 11.2, "Creating Web Application Projects," and Section 13.5.1, "How to Build an Enterprise Application Project and Sub-Projects."

15.3.2 How to Generate Session Beans for Entity Classes

The IDE provides a wizard to generate session beans for entity classes. In the wizard choose the entity classes that require session beans. The wizard creates a session facade as well as the selected interfaces for the selected entity class.

To generate session beans from entity classes:

  1. Right-click the module project node in the Projects window and choose New > Other.

  2. In the New File wizard, go to the Persistence category and select Session Beans for Entity Classes. See Section 16.1, "About Developing with Java Persistence."

  3. Select any entity classes from the list in the left pane and click the Add button. Any referenced entity classes are automatically added to the list in the right pane. The IDE generates session beans for each entity class listed in the right pane.

    Deselect Include Referenced Classes if you do not want session beans created for referenced classes.

  4. Click Next.

  5. Select the location where you want to save the session beans.

  6. Select an existing package from the Package drop-down menu or type the name of a new package.

  7. Select the session facade interfaces that you want the wizard to generate.

  8. Click Finish.

When you click Finish, the IDE creates session beans for each of the entity classes you specified in the wizard.

15.3.3 How to Define a Business Method for an Enterprise Bean

A business method is a method inside an enterprise bean that performs some function and can be called from outside the bean. Business methods are written in the bean class and exposed through local or remote interfaces. If a business method is meant to be called by beans and clients that are running in the same JVM (on the same server), they are registered in the bean's local interface. If the method is meant to be called from a remote client, it is registered in the bean's remote interface.

A session bean's business methods typically define tasks that the bean performs for clients. They are not necessarily associated with data in a database.

In EJB 2.1 programming, an entity bean represents a persistent business object whose data is stored in a database. The bean adds behavior specific to that data. Therefore, an entity bean's business methods are typically accessor and mutator methods that enable you to read and update information in a database that represents a business concept. Entity bean business methods are almost always local methods, since remote clients should only access an entity bean through a facade session bean.

Business Methods in the IDE

When you create an EJB 2.1 enterprise bean (J2EE 1.4), the IDE creates a special BeanInterfaceBusiness interface in which it registers business methods. The bean interface extends the Business interface and the bean class implements both the bean interface and the RemoteBusiness interface.

The advantage of this approach is that it lets you separate the business logic from implementation logic, and that it lets you check at compile-time that your bean implements the given interfaces. If you want to register business methods directly in the bean's interfaces, delete the Business interface files. Then edit the bean interface to not extend the Business interface and edit the bean class to not implement the Business interface.

When working with EJB 3.0 or EJB 3.1 enterprise beans, there is no need for a separate Business interface because the enterprise beans are regular Java objects and therefore have only the remote and local interfaces.

To define a business method for an enterprise bean:

  1. Open the bean class in the Source Editor.

  2. Right-click anywhere in the Source Editor and choose Insert Code > Add Business Method.

  3. In the Add Business Method dialog box, define the method signature:

    1. Name the method.

    2. Specify the return type.

    3. Specify the method parameters in the Parameters tab.

    4. Specify the exceptions that the method throws in the Exceptions tab.

  4. Specify whether to add the business method to the local interface, remote interface, or both.

  5. Click OK to create the method.

  6. The IDE adds the method signature to the interfaces and the method body to the bean class.

  7. Edit your new business method.

You can also just code a regular method and then click the IDE hint in the left margin and choose an interface from the popup menu.

Note:

When the IDE adds a business method to an enterprise bean, it does not save the bean class or the bean interfaces. Only when you compile or save the class is the new method saved to disk.

15.3.4 How to Send JMS Messages

A Java Message Service (JMS) message is an object that communicates information between JMS clients. The message is sent from one client to a second client that listens for messages through a JMS destination on an application server. In the IDE, you can automatically generate code to send a JMS message to any message-driven bean in an open project.

To send a JMS message:

  1. (Optional) Create a service locator class to handle getting the reference to the database. See Section 15.3.5, "How to Use a Service Locator."

  2. Open the Java file from which you want to send the JMS message. The file must be in an EJB module project or a standard web application project. You cannot generate lookup code in a free-form web application project. See Section 15.3.4, "How to Send JMS Messages," and Section 22.3.9, "How to Set up a JMS Resource."

  3. In the Source Editor, right-click in the file and choose Insert Code > Send JMS Message.

  4. Select the message-driven bean that is the destination for the message. You can switch between open projects using the Project drop-down menu.

  5. Under Service Locator Strategy, specify whether to generate inline lookup code or use an existing service locator. Click Browse to search for the service locator class name.

  6. Click OK. The IDE creates two methods:

    • createJMSMessageForNewMessageDestination. The method that creates the message that is sent. You should implement your business logic in this method.

    • sendJMSMessageToNewMessageDestination. The method that handles the message you created in the method above. You should not modify this message.

      The IDE also registers the resource reference and message destination reference in your module's general deployment descriptor.

Note:

If your project's target server is the GlassFish/Sun Java System Application Server, the IDE automatically configures a destination and connection factory for the message-driven bean when you create the bean. If you are deploying to a different application server, you have to configure the server-specific deployment descriptors and the server resources yourself.

15.3.5 How to Use a Service Locator

A service locator is a class that provides a single, reusable way to obtain a reference to the following:

  • An enterprise bean's local and/or remote home interfaces

  • A JMS connection factory and topic destination

  • An e-mail session

  • A JDBC data source

  • The URL, name, or boolean value for an environment entry

You can create a regular service locator or one that caches the obtained reference for further use. Generally you use a caching service regulator in web applications and a regular service locator in the business tier. Enterprise beans especially need non-caching service locators. Because each enterprise bean has a unique JNDI name space, a resource reference with the same name can be declared in multiple enterprise beans, potentially with different types.

To create a service locator:

  1. Choose File > New.

  2. From the Enterprise category, select Service Locator or Caching Service Locator.

To use a service locator:

Run any of the following commands:

In the Service Locator Strategy section, select Use Existing Class and specify the class name of the service locator. Use the Browse button to search for the class.

Note:

The service locator template is fully functional. You do not need to modify the code, although you can customize its caching behavior.

You can only create a service locator in a web application project or EJB module project.

For more information, see Section 15.3.6, "How to Access a Connection Pool from a Java Class."

15.3.6 How to Access a Connection Pool from a Java Class

Applications that are deployed to servers often need to access databases that reside on remote machines. You therefore have to set up a JDBC connection pool on the server that points to the database. You then create a data source that connects to the connection pool and use the data source's JNDI name to acquire a connection to the database. See Section 22.3.7, "How to Set up a Connection Pool."

To access a database from an enterprise application:

  1. (Optional) Create a service locator class to handle getting the reference to the database.

  2. Open the Java file from which you want to access the database. In the Source Editor, right-click the file and choose Insert Code > Use Database. For more information see Section 23.1, "About Working and Connecting with Databases."

  3. Enter a JNDI name for the database connection. This name must be the same as the JNDI name of the JDBC data source for the database on the target server. See Section 23.3.2, "How to Connect to the Java DB Database."

    If you are deploying to the GlassFish Server, you can also generate a connection pool and data source for the database connection by selecting the Create Server Resources checkbox. The resources are created under the project's Server Resources node.

  4. Select the database to which you want to connect in the Connection drop-down list. If the database is not listed, do the following:

    1. If the JDBC driver for the database server is not registered in the IDE, click Add Driver.

    2. Click Add Connection to connect to the database.

  5. Under Service Locator Strategy, specify whether to generate inline lookup code or use an existing service locator. Click Browse to search for the service locator class name.

  6. Click OK.

The Use Database command is only available in EJB module and web application projects.

15.3.7 How to Send an Email from a Java File

  1. (Optional) Create a service locator class to handle getting the reference to the database.

  2. Open the Java file from which you want to send the e-mail message. The file must be in an EJB module project or a standard web application project. You cannot generate lookup code in a free-form web application project.

  3. In the Source Editor, right-click the file and choose Insert Code > Send E-mail.

  4. Specify the JNDI name of the mail resource on the application server.

  5. Under Service Locator Strategy, specify whether to generate inline lookup code or use an existing service locator. Click Browse to search for the service locator class name.

  6. Click OK.

    The IDE creates two methods:

    • getSession. This method looks up the mail resource and creates a mail session.

    • sendMail. This method constructs and sends the e-mail.

    The IDE also registers the resource reference in your module's general deployment descriptor.

  7. In your code, call the sendMail method and pass the e-mail address, subject, and body as parameters.

Note:

You have to configure the JavaMail resource on the application server yourself. If your project's target server is the GlassFish server, you can use the JavaMail Resource template found in the New File wizard GlassFish.

If your project's target server is the GlassFish server, the IDE automatically configures the resource reference in the application-specific deployment descriptor. If you are deploying to a different application server, you have to configure the server-specific deployment descriptors yourself.

For additional information see Section 11.10.2, "How to Edit Deployment Descriptors," and Section 15.5.3, "How to Edit the Deployment Descriptors of an EJB Module."

15.4 Calling an Enterprise Bean

Calling an enterprise bean is the process of getting a reference to the enterprise bean so that you can call its methods. The IDE can assist you in calling an enterprise bean from any Java class in an EJB module, web application, or enterprise application client.

15.4.1 How to call an Enterprise Bean

  1. Open the IDE project that contains the enterprise bean that you want to call.

  2. Open the file from which you want to call the enterprise bean.

  3. In the Source Editor, right-click anywhere in the body of the class and choose Insert Code > Call Enterprise Bean.

  4. In the dialog box, select the enterprise bean that you want to call.

  5. (Optional) Type a new name for the reference.

  6. Specify whether to reference the local interface or the remote interface. To reference a bean's local interface, the class that is referencing the bean must be packaged in the same enterprise application as the enterprise bean.

  7. Click OK.

When you call an enterprise bean, the following occurs:

Java EE projects:

  • The IDE uses the @EJB annotation to indicate a dependency on another bean. In the Java class from which you are calling the enterprise bean, the IDE adds code similar to the following:

    @EJB

    private NewSessionRemote newSessionBean;

  • The enterprise bean project is added to the caller project's classpath.

J2EE 1.4 projects:

  • A lookup method is created in the caller class.

  • The enterprise bean project is added to the caller project's classpath.

  • A reference to the entity or session bean being called is added to the deployment descriptor of the caller project. If and where the reference is added depends on which of the following applies:

    • If the lookup is made from an enterprise bean implementation class, the reference is added to the corresponding bean.

    • If the lookup is made from any class of a web application, the reference is added to the web application.

    • If the lookup is made from a plain Java class of an EJB module, no reference is added. In this case, the reference has to be added manually to the bean calling the plain Java class.

Note:

The IDE cannot assist you when calling an enterprise bean from a free-form web application project.

15.5 Building and Deploying an EJB Module

There are two ways to build an EJB module:

When an EJB module is part of an enterprise application, its enterprise beans can be accessed from external clients and web application modules through its remote interfaces, and by other modules in the enterprise application through its local interfaces. All of the JAR files needed for execution are packaged in the enterprise application's EAR file and the classpath to these JAR files are written in the EJB module's manifest file. This is the most common way of deploying an EJB module.

There are two deployment commands:

15.5.1 How to Build an EJB Module as Part of an Enterprise Application

The IDE packages all of the necessary JAR files as part of the Java EE application and not as part of the individual modules. This is the most common way of building an EJB module.

  1. In the Projects window, expand the enterprise application's Java EE Modules node and make sure that the EJB module is listed. If you have not added the EJB module to the enterprise application, right-click the Java EE Modules node, choose Add Java EE Module, and select the EJB module.

  2. Right-click the enterprise application project and choose Build. Or, if the enterprise application project is the main project, choose Run > Build Main Project (F11) from the main menu.

    Note:

    To set a project as the main project right-click the project in the Projects window and choose Set as Main Project, or, choose Run > Set Main Project from the menu bar and select the project in the sub-menu.

When you build an EJB module as part of an enterprise application, the IDE does the following:

  • Compiles the classes to the project's build/jar-module folder.

  • Places any JAR files, libraries, or directories that are on your project's classpath and are scheduled for inclusion in deployment in the enterprise project's build folder.

  • Builds the EJB module's JAR file to the EJB module project's dist folder and copies it to the enterprise application project's build folder. The EJB module JAR file does not contain any of the classpath elements.

  • Puts a Class-path entry in the EJB module's manifest file for all included JAR files and puts a . entry if there is at least one directory on the project's classpath scheduled for inclusion in deployment. This mechanism ensures that the JAR files and directories can be used from the EJB module even if they reside in the EAR project and not in the EJB module itself.

  • Builds the enterprise application EAR file to the enterprise application project's dist folder.

15.5.2 How to Build a Stand-alone EJB Module

The IDE packages the necessary JAR files with the EJB module JAR file.

To build a stand-alone EJB module:

Right-click the EJB module project in the Projects window and choose Build. Or, select the EJB module project in the Projects window and choose Run > Build Project (Build Main Project (F11) if the EJB module project is set as the main project).

When you build a EJB module project as a stand-alone module, the IDE does the following:

  • Compiles the classes to the project's build/jar folder.

  • Places any JAR files, libraries, or directories that are on your project's classpath and are scheduled for inclusion in deployment into the build/jar folder.

  • Builds the EJB module's JAR file to the EJB module's dist folder. The JAR file includes all classpath items scheduled for inclusion in deployment.

Note:

To mark a classpath item for inclusion in deployment in the EJB module's Project Properties dialog box, right-click the module's Libraries node and choose Properties, then select the checkbox for each classpath item you want to include in deployment.

15.5.3 How to Edit the Deployment Descriptors of an EJB Module

If you are developing enterprise beans on the Java EE 5 platform and deploying to a Java EE 5 compliant application server, you develop enterprise beans using EJB 3.0 technology.

If you are developing enterprise beans on the Java EE 6 or Java EE 7 platform and deploying to a Java EE-compliant application server, you develop enterprise beans using EJB 3.1 technology. EJB 3.0 and EJB 3.1 technology support the use of annotations in your code to describe behavior that was described using deployment descriptors in EJB 2.1 technology. As a result, deployment descriptors files are no longer required when deploying to a Java EE-compliant server. EJB 3.0 and EJB 3.1 technology also support the use of deployment descriptors. You may need to use a combination of annotations and deployment descriptors in your project when you configure your project, for example to configure security settings.

For EJB modules, there are two types of deployment descriptors:

  • The general Java EE deployment descriptor that configures deployment settings on any Java EE-compliant implementation. The general EJB deployment descriptor is called ejb-jar.xml. The IDE provides a visual editor for the general deployment descriptor and automatically updates the descriptor file when you edit your code.

  • The server-specific deployment descriptor that configures deployment settings for the application server to which you are deploying. For the Sun Java System Application Server, the IDE automatically updates the deployment descriptor files as you edit your files. For all other application servers, you have to write the server-specific deployment descriptors yourself.

The IDE provides a visual deployment descriptor editor to help you edit some descriptor files. You can also use the XML editor to edit your descriptor files.

To edit ejb-jar.xml:

  1. In the Projects window, expand the Configuration Files node for your EJB module project.

  2. Double-click ejb-jar.xml to open the file in the visual editor.

  3. Select one of the following tabs:

    • General. This tab enables you to specify general information about the EJB module, details about security roles and descriptor details about each of the enterprise beans in the module.

    • CMP Relationships. This tab enables you to view and modify CMP relationships for CMP beans in your module.

    • XML. This tab enables you to edit the descriptor file in the XML editor.

  4. Edit the properties.

  5. Save your changes.

To edit glassfish-ejb-jar.xml:

  1. In the Projects window, expand the Configuration Files node for your EJB module project.

  2. Double-click glassfish-ejb-jar.xml to open the file in the visual editor.

  3. Select a tab at the top of the editor.

  4. Edit the descriptor file as necessary and save your changes.

To edit the server-specific deployment descriptor for any other application server:

  1. Double-click the deployment descriptor node in the Projects window.

  2. The IDE opens the XML source of the deployment descriptor in the Source Editor.

  3. If you enter any XML syntax errors the IDE automatically alerts you. To fully ensure your changes have not cause any errors, you should verify the EJB module.

  4. In the Projects window, right-click the enterprise application project and choose Run or Deploy.

For additional information see Section 22.4.1, "How to Register a Server Instance."

To deploy an enterprise application:

If the module is part of an enterprise application, right-click the enterprise application project and choose Debug.

If the module is a stand-alone EJB module, right-click the EJB module project and choose Debug.

15.5.4 How to Verify the Deployment Descriptors of an EJB Module

Before you deploy an enterprise application or any stand-alone web or EJB module to the application server, you should verify it, to ensure it is properly implemented to the Java EE specification. When the target server for the project is the Sun Java System Application Server or GlassFish application server, use the verifier tool to validate both Java EE and application server-specific deployment descriptors against their corresponding DTD files and display errors and warnings if a module or application is not compliant. You can verify deployment descriptors in EAR, WAR, RAR, and JAR files.

The verifier tool is not simply an XML syntax verifier. Rules and interdependencies between various elements in the deployment descriptors are verified. Where needed, classes in your module are examined to ensure that what is referred to in the deployment descriptors actually exists and works when deployed.

To verify a project deployment descriptors:

  1. Right-click any web application module, EJB module, or enterprise application project in the Projects window and choose Verify.

  2. The Verifier window opens and displays all the results.

  3. Filter the results according to your needs:

    • Click Failures Only to display only failures.

    • Click Failures and Warnings Only to display only failures and warnings.

  4. Make appropriate changes, if any, and verify the project again.

Note:

Not all failures to comply with the Java EE specification cause the application to fail. However, modules that fail to comply with the specification might not be portable between application servers. The verifier tool helps you ensure your code is not locked into running only on a specific application server.

15.5.5 How to Deploy a Stand-alone EJB Module

Enterprise beans in a stand-alone EJB modules can only be accessed through their remote interfaces. All of the JAR files needed for execution are packaged in the EJB JAR file itself.

To deploy a stand-alone EJB module:

In the Projects window, right-click the EJB module project and choose Run or Deploy.

15.5.6 How to Debug an EJB Module

There are two ways to debug an EJB module:

  • As a stand-alone EJB module

  • As part of an enterprise application

To debug a stand-alone EJB module:

  • In the Projects window, right-click the EJB module project and choose Debug.

When you debug a stand-alone EJB module, the IDE does the following:

  • Compiles the EJB module if necessary.

  • Stops the application server and starts it in debug mode.

  • Deploys the EJB module to the application server.

  • Starts a debugging session, attaches the debugger to the server, and opens the debugger windows at the bottom of the IDE screen. Since an EJB module does not have an executable class, there are no local variables or calls on the call stack. To debug the functionality in the EJB module, use an application client or web application to access the EJB module's methods.

To debug an enterprise application:

In the Projects window, right-click the enterprise application project and choose Debug.

When you debug an enterprise application, the IDE does the following:

  • Compiles the EAR file if necessary.

  • Stops the application server and starts it in debug mode.

  • Deploys the enterprise application to the application server.

  • Starts a debugging session, attaches the debugger to the server, and opens the debugger windows at the bottom of the IDE screen. If the application has a designated web application and URL to run, the IDE opens the URL in the external browser.

Note:

If your EJB module is part of an enterprise application, you should always debug it by running the debug command on the enterprise application project. Since the IDE does not know which enterprise applications an EJB module project belongs to, running the Debug command on an EJB module project deploys it as a stand-alone module.

When debugging an enterprise application, if your application contains any projects not listed in the Packaging page of the enterprise application, you must explicitly specify these projects if you want the IDE to include them in the classpath when you debug the application. To specify the projects to include, right-click the enterprise application project node in the Projects window and choose Properties. In the Properties dialog box, select the Libraries category and add the projects to the Embedded Classpath Elements list.

The debugger windows filter out any information from method calls running on the server.

For additional information see Section 22.3.1, "How to Register a Server Instance."

15.5.7 How to Profile an EJB Module

There are two ways to profile an EJB module:

  • As a stand-alone EJB module

  • As part of an enterprise application

If your EJB module is part of an enterprise application, you should running the profile command on the enterprise application project. Since the IDE does not know which enterprise applications an EJB module project belongs to, running the Profile command on an EJB module project deploys it as a stand-alone module.

If you are deploying your EJB module or enterprise application to a local installation of the GlassFish server, go to the main menu and choose Profile > Profile Project and choose the project in the sub-menu, or, by right-click the project node in the Projects window and choosing Profile from the pop-up menu.

If you are deploying your EJB module or enterprise application to a remote server, you must attach the IDE to the remote server and profile the application in Attach mode. The Attach Wizard can help you configure the remote server to accept attachment.

To profile a stand-alone EJB module:

  1. In the Projects window, right-click the EJB module project and choose Profile.

  2. Select a profiling task and click Run.

When you profile a stand-alone EJB module, the IDE does the following:

  • Compiles the EJB module if necessary.

  • Stops the application server and starts it in profile mode.

  • Deploys the EJB module to the application server.

  • Starts a profiling session, attaches the profiler to the server, and opens the Profiler window in the IDE. After the application is deployed to the server, you should invoke its code the same way as you would for debugging or just evaluating application functionality. You can use an application client or a web application to access the EJB and invoke its code. You can view the profiling data once the application code is invoked.

To profile an enterprise application:

  • In the Projects window, right-click the enterprise application project and choose Profile.

  • Select a profiling task and click Run.

Note:

If you are analyzing application performance, choose the Profile Projects & Subprojects Classes filter to ensure that the classes in your EJB module and web module projects are profiled. If you do not choose this filter, the classes in the EJB module and web module are not profiled.

Follow this procedure to limit profiling the classes in the enterprise application:

  1. In the Analyze Performance pane, choose the Profile Projects & Subprojects Classes filter and then click Show Filter Value.

  2. Click To Quickfilter in the dialog box.

  3. In the Set Quick Filter dialog, select a filter type, edit the filter values to limit the classes that are profiled, and click OK.

  4. Make sure that Quick Filter is selected in the Filter drop-down list when you run the profiling session.

When you profile an enterprise application, the IDE does the following:

  • Compiles the EAR file if necessary.

  • Stops the application server and starts it in profile mode.

  • Deploys the enterprise application to the application server.

  • Starts a profiling session, attaches the profiler to the server, and opens the Profiler window in the IDE. If the application has a designated web application and URL to run, the IDE opens the URL in the external browser.

For more on profiling see Chapter 8, "Testing and Profiling Java Application Projects."

15.5.8 How to Test an EJB Module

To use local JUnit tests to test an EJB module that is deployed on a server, you must configure the tests to act as a remote client of the EJB module. This means you can only access the EJB module through its remote interfaces. For information on JUnit unit testing, see Section 8.3, "Creating a Unit Test."

Usually, you have some entity beans and a session bean with remote interfaces that provides clients access to the entity beans. You have to generate tests for the session bean's bean class and modify the test file to reference the session bean and test each of its business methods.

To generate tests for an EJB module:

  1. Create a service locator file somewhere in the test package source root.

  2. Open the enterprise bean's bean class in the Source Editor and choose Tools > JUnit Tests > Create Tests (Ctrl+Alt+J). Accept the default options in the dialog box and click OK.

  3. In the test class, delete the test methods that just test EJB infrastructure methods such as testEjbCreate and testEjbRemove.

  4. Declare a variable for the remote home interface.

  5. In the setUp method, write code to instantiate this variable, as in the following example:

    Example 15-1 Instantiate a Variable for the Remote Home Interface

    protected void setUp() throws NamingException, 
                           CreateException, RemoteException {
        ServiceLocator sl = new ServiceLocator();
        newSessionHome = (NewSessionRemoteHome)sl.getRemoteHome("ejb/NewSessionBean", 
                          NewSessionRemoteHome.class);
    }
    
  6. In each test method, retrieve a remote interface and test the business method, as in the following example:

    Example 15-2 Retrieve and Test a Remote Interface

    try {
        NewSessionRemote newSession = newSessionHome.create();
        assertEquals("name", newSession.getName(new Integer(1)));
    }
    catch (Exception e) {
        fail(e.toString());
    }
    

Note:

You do not have to use a service locator class. You can write the lookup code from scratch.

15.6 Developing EJB 2.1 Entity Beans

EJB 2.1 entity beans are part of the EJB 2.1 specification and are used in J2EE 1.4 enterprise applications. NetBeans IDE 8.0 no longer supports developing applications that use J2EE 1.4 technology. To develop J2EE 1.4 applications you should use NetBeans IDE 7.3 or earlier versions of the IDE.