Using JAXR Clients in Java EE Applications
You can create Java EE applications that use JAXR clients to access registries. This section explains how to write, compile, package, deploy, and run a Java EE 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>/javaeetutorial5/examples/jaxr/clientsession. Path names in this section are relative to this directory.The example has a
build.xmlfile that refers to files in the following directory:Coding the Application Client: MyAppClient.java
The application client class,
clientsession-app-client/src/java/MyAppClient.java, accesses thePubQueryenterprise bean's remote interface,clientsession-app-client/src/java/PubQueryRemote.java. The program calls the bean's two business methods,executePublishandexecuteQuery.Coding the PubQuery Session Bean
The
PubQuerybean is a stateless session bean that has two business methods. The bean uses remote interfaces rather than local interfaces because it is accessed from the application client.The remote interface,
clientsession-ejb/src/java/PubQueryRemote.java, declares two business methods:executePublishandexecuteQuery. The bean class,clientsession-ejb/src/java/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 injects a
ConnectionFactoryresource. It implements a@PostConstructmethod namedmakeConnection, which uses theConnectionFactoryto create theConnection. Finally, a@PreDestroymethod namedendConnectioncloses 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 (see Before You Compile the Examples). Feel free to change any of the organization data in the file.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 (page 28). To verify that the Registry Server is deployed, use the
asadmincommand as follows:%asadmin list-componentsXindice <web-module> RegistryServer <web-module> Command list-components executed successfully.Creating JAXR Resources
To use JAXR in a Java EE 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. There are no Ant targets to create these resources, so you need to use either the Admin Console or the
asadmincommand. Using the Admin Console is less likely to result in errors.If you have not done so, start the Admin Console as described in Starting the Admin Console (page 29).
To create the connector connection pool, perform the following steps:
To create the connector resource, perform the following steps:
To create the connection pool using the asadmin command,
typethe following command (all on one line):asadmin create-connector-connection-pool --raname jaxr-ra --connectiondefinition com.sun.connector.jaxr.JaxrConnectionFactory jaxr-poolTo create the connector resource using the
asadmincommand, type the following command:Compiling the Source Files and Packaging the Application
To create and package the application, use the following command:
This target does the following:
It creates a file named
clientsession.earin thedistdirectory.Deploying the Application
To deploy the
clientsession.earfile, use the following command:To return a client JAR file, use the following command:
These commands deploy the application and return a JAR file named
clientsessionClient.jarin theclient-jardirectory.Running the Application Client
To run the client, use the following command:
The output in the terminal window looks like this:
[echo] running application client container. [exec] To view the bean output, [exec] 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 following command to undeploy the application:
You can use the
run-deletetarget in thesimpledirectory to delete the organization that was published.