One of the goals of deployment descriptors in the Java EE platform is to provide a standard way of describing external facilities that need to be available for successful execution of the enterprise application. The declaration in the deployment descriptor references the expected interface and the name used to locate this instance. This binding and declaration mechanism allows the late binding of resources. (In Java EE role terms, the deployer can change the actual resource used without changing the source code or the mandatory deployment descriptors.) A resource is an external entity required by an enterprise application that is referenced using the standard deployment descriptor. A large number of resources can be used, but some of the most common are JDBC (via the javax.sql.DataSource interface), JMS (via the javax.jms.* interfaces), and enterprise beans (using the interfaces exposed by the bean developer). Several steps are required to incorporate a resource into an application:
NetBeans IDE provides commands to automate the use of resources. Typically, you realize that a resource should be used while you are writing Java code. The IDE makes it easy to add a resource as you are coding by providing an Enterprise Resources submenu on the contextual menu in the Source Editor for Java classes contained in enterprise projects (web applications and EJB Modules). See Figure 13-19. Figure 13-19. Source Editor with the contextual menu open and the Enterprise Resources submenu selected
The Enterprise Resources menu provides a set of commands that automate the use of JMS, JDBC, enterprise beans, and email. Subsequent sections in this chapter describe what happens when the wizards are invoked and how to use the wizards. All the actions are able to incorporate the Java BluePrints Service Locator pattern, which encapsulates and caches the JNDI initial context lookup. This pattern aggregates all boilerplate lookup code and provides the potential for performance enhancements. NetBeans IDE provides two different service locator strategies: caching and non-caching. The caching strategy uses the singleton pattern and caches both the initial context and the results from the JNDI lookup. The non-caching locator does not cache the lookup results, but instead reuses the initial context. The Service Locator templates are available under the Enterprise node in the New File wizard, as shown in Figure 13-20. Figure 13-20. New File wizard with the Caching Service Locator template selectedThe Service Locator template generates a Java source file that can be further customized. The service locator last used for the project is stored with the project settings, allowing the last locator to be reused easily. The service locator naming conventions provided in the templates are incorporated into the enterprise resource commands; therefore, changing the name of the public lookup methods after generation may require manual intervention following the use of the enterprise resource commands. The caching service locator strategy is most useful from a web module where resource definitions are shared across the entire module. The singleton pattern can be used effectively in this scenario to reuse lookup instances. In an EJB module, resource declarations are scoped to a single enterprise bean; thus, the singleton pattern is not applicable. An EJB module will commonly use a non-caching service locator. Using Enterprise BeansIf you want to use an enterprise bean, perform the following steps:
The Call Enterprise Bean command (which you can access by right-clicking the Source Editor and choosing Enterprise Resources | Call Enterprise Bean) automates this process. The dialog box shown in Figure 13-21 provides a way to select the enterprise bean to invoke. This dialog further enables you to specify the enterprise bean, the desired lookup strategy, and whether checked exceptions should be converted to runtime exceptions. Figure 13-21. Dialog box for the Call Enterprise Bean command
Using the Call Enterprise Bean command in an EJB Module project or a Web project will result in the following:
Using a DatabaseThe Use Database command automates the process of incorporating a relational database into an application. If you intend to use a database, you must go through the following steps:
The Enterprise Resources | Use Database command (available by right-clicking a file in the Source Editor) automates this process. When you choose this command, the Choose Database dialog box (shown in Figure 13-22) appears and prompts you to select the database to use in either a web module or an EJB module. In the dialog box, specify a JNDI name (used as the resource reference name), the connection to use, and whether a service locator should be used or inline code should be generated (lookup code is generated in the current class). The list of connections is the same as what is provided in the Runtime window. Figure 13-22. Choose Database dialog box
After you fill in the Choose Database dialog box, the following occurs:
Sending a JMS MessageThe Send JMS Message command automates the process of sending a message to a queue or topic. The J2EE 1.4 specification defines the concept of a message destination, which provides a way to specify logical destinations for messages. A message destination allows the decoupling of the message consumer and message producer. The most common scenario is to have a message-driven bean (MDB) linked to the message destination. However, the message destination mechanism allows only the logical message destination to be used by the module sending the message. Although this concept defines a generic linking mechanism, additional information, such as the type of destination (the J2EE 1.4 specification allows other messaging systems to be used), and the message format must be exchanged. If you want to use JMS, you need to do the following:
The Send JMS Message command automates this process. To use this command, right-click in the Source Editor and choose Enterprise Resources | Send JMS Message. The Select Message Destination dialog box (shown in Figure 13-23) appears and provides a way to select a message destination and to specify whether a service locator should be used or inline code should be generated (lookup code is generated in the current class). Only projects that can supply a destination are shown in the list of projects (web and EJB Modules). Figure 13-23. Select Message Destination dialog box
After you complete the Select Message Destination dialog box, the following occurs:
|