Using JAXR Clients in J2EE Applications
You can create J2EE applications that use JAXR clients to access registries. This section explains how to write, compile, package, deploy, and run a J2EE application that uses JAXR to publish an organization to a registry and then query the registry for that organization. The application in this section uses two components: an application client and a stateless session bean.
The section covers the following topics:
You will find the source files for this section in the directory
<INSTALL>/j2eetutorial14/examples/jaxr/clientsession. Path names in this section are relative to this directory.The following directory contains a built version of this application:
If you run into difficulty at any time, you can open the EAR file in
deploytooland compare that file to your own version. (You cannot deploy this EAR file, however. It uses an unedited version of the required properties file.)Coding the Application Client: MyAppClient.java
The application client class,
src/MyAppClient.java, obtains a handle to thePubQueryenterprise bean's remote home interface, using the JNDI API naming contextjava:comp/env. The program then creates an instance of the bean and calls the bean's two business methods:executePublishandexecuteQuery.Coding the PubQuery Session Bean
The
PubQuerybean is a stateless session bean that has onecreatemethod and two business methods. The bean uses remote interfaces rather than local interfaces because it is accessed from the application client.The remote home interface source file is
src/PubQueryHome.java.The remote interface,
src/PubQueryRemote.java, declares two business methods:executePublishandexecuteQuery. The bean class,src/PubQueryBean.java, implements theexecutePublishandexecuteQuerymethods and their helper methodsgetName,getDescription, andgetKey. These methods are very similar to the methods of the same name in the simple examplesJAXRQuery.javaandJAXRPublish.java. TheexecutePublishmethod uses information in the filePubQueryBeanExample.propertiesto create an organization named The Coffee Enterprise Bean Break. TheexecuteQuerymethod uses the organization name, specified in the application client code, to locate this organization.The bean class also implements the required methods
ejbCreate,setSessionContext,ejbRemove,ejbActivate, andejbPassivate.The
ejbCreatemethod of the bean class allocates resources--in this case, by looking up theConnectionFactoryand creating theConnection.The
ejbRemovemethod must deallocate the resources that were allocated by theejbCreatemethod. In this case, theejbRemovemethod closes theConnection.Editing the Properties File
Before you compile the application, edit the
PubQueryBeanExamples.propertiesfile in the same way you edited theJAXRExamples.propertiesfile to run the simple examples.
- Uncomment the
query.urlandpublish.urllines for the registry you wish to use. The default is the IBM registry.- Provide values for the
registry.usernameandregistry.passwordproperties to specify the user name and password you obtained when you registered with the registry.- Change the values for the
http.proxyHostandhttps.proxyHostentries so that they specify the system on your network through which you access the Internet.- Feel free to change any of the organization data in the remainder of the file. Try to make the organization name unusual so that queries will return relatively few results.
Compiling the Source Files
To compile the application source files, go to the directory
<INSTALL>/j2eetutorial14/examples/jaxr/clientsession. Use the following command:The
compiletarget places the properties file and the class files in thebuilddirectory.Starting the Application Server
To run this example, you need to start the Application Server. Follow the instructions in Starting and Stopping the Application Server.
Creating JAXR Resources
To use JAXR in a J2EE application that uses the Application Server, you need to access the JAXR resource adapter (see Implementing a JAXR Client) through a connector connection pool and a connector resource. You can create these resources in the Admin Console.
If you have not done so, start the Admin Console as described in Starting the Admin Console.
To create the connector connection pool, perform the following steps:
- In the tree component, expand the Resources node, then expand the Connectors node.
- Click Connector Connection Pools.
- Click New.
- On the Create Connector Connection Pool page:
- On the next page, choose
javax.xml.registry.ConnectionFactory(the only choice) from the Connection Definition drop-down list, and click Next.- On the next page, click Finish.
To create the connector resource, perform the following steps:
If you are in a hurry, you can create these objects by executing the following command (from the directory
<INSTALL>/j2eetutorial14/examples/jaxr/clientsession):Creating and Packaging the Application
Creating and packaging this application involve four steps:
Starting deploytool and Creating the Application
- Start
deploytool. For instructions, see Starting the deploytool Utility.- Choose File
New
Application.
- Click Browse (next to the Application File Name field), and use the file chooser to locate the directory
clientsession.- In the File Name field, type
ClientSessionApp.- Click New Application.
- Click OK.
Packaging the Session Bean
- Choose File
New
Enterprise Bean to start the Enterprise Bean wizard. Then click Next.
- In the EJB JAR General Settings screen:
- Select Create New JAR Module in Application, and make sure that the application is
ClientSessionApp.- In the JAR Display Name field, type
PubQueryJAR.- Click the Edit Contents button.
- In the dialog box, locate the
clientsession/builddirectory. SelectPubQueryBean.class,PubQueryHome.class,PubQueryRemote.class, andPubQueryBeanExample.propertiesfrom the Available Files tree. Click Add, and then OK.- In the Bean General Settings screen:
- From the Enterprise Bean Class menu, choose
PubQueryBean.- Verify that the Enterprise Bean Name is
PubQueryBeanand that the Enterprise Bean Type is Stateless Session.- In the Remote Interfaces area, choose
PubQueryHomefrom the Remote Home Interface drop-down list, and choosePubQueryRemotefrom the Remote Interface drop-down list.After you finish the wizard, perform the following steps:
Packaging the Application Client
- Choose File
New
Application Client to start the Application Client Wizard. Then click Next.
- In the JAR File Contents screen:
- Verify that Create New AppClient Module in Application is selected and that the application is
ClientSessionApp.- In the AppClient Display Name field, type
MyAppClient.- Click the Edit Contents button.
- In the dialog box, locate the
clientsession/builddirectory. SelectMyAppClient.classfrom the Available Files tree. Click Add, and then OK.- In the General screen, select
MyAppClientfrom the Main Class drop-down list.After you finish the wizard, click the EJB Ref's tab, and then click Add in the inspector pane. In the dialog box, follow these steps:
- Type
ejb/remote/PubQueryin the Coded Name field.- Choose Session from the EJB Type drop-down list.
- Choose Remote from the Interfaces drop-down list.
- Choose
PubQueryHomefrom the Home Interface combo box.- Choose
PubQueryRemotefrom the Local/Remote Interface combo box.- In the Target EJB area, select JNDI Name and choose
PubQueryBeanfrom the combo box. The session bean uses remote interfaces, so the client accesses the bean through the JNDI name rather than the bean name.Checking the JNDI Names
Select the application, click Sun-specific Settings on the General page, and verify that the JNDI names for the application components are correct. They should appear as shown in Tables 10-3 and 10-4.
Table 10-3 Application Pane for ClientSessionApp Component Type Component JNDI Name EJBPubQueryBeanPubQueryBean
Table 10-4 References Pane for ClientSessionApp Ref. Type Referenced By Reference Name JNDI Name EJB RefMyAppClientejb/remote/PubQueryPubQueryBean ResourcePubQueryBeaneis/JAXReis/JAXR
Deploying the Application
- Save the application.
- Choose Tools
Deploy.
- In the dialog box, type your administrative user name and password (if they are not already filled in), and click OK.
- In the Application Client Stub Directory area, select the Return Client Jar checkbox, and make sure that the directory is
clientsession.- Click OK.
- In the Distribute Module dialog box, click Close when the process completes. You will find a file named
ClientSessionAppClient.jarin the specified directory.Running the Application Client
To run the client, use the following command:
The program output in the terminal window looks like this:
Looking up EJB reference Looked up home Narrowed home Got the EJB To view the bean output, check <install_dir>/domains/domain1/logs/server.log.In the server log, you will find the output from the
executePublishandexecuteQuerymethods, wrapped in logging information.After you run the example, use the
run-deletetarget in thesimpledirectory to delete the organization that was published.