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.xml
file 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 thePubQuery
enterprise bean's remote interface,clientsession-app-client/src/java/PubQueryRemote.java
. The program calls the bean's two business methods,executePublish
andexecuteQuery
.Coding the PubQuery Session Bean
The
PubQuery
bean 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:executePublish
andexecuteQuery
. The bean class,clientsession-ejb/src/java/PubQueryBean.java
, implements theexecutePublish
andexecuteQuery
methods and their helper methodsgetName
,getDescription
, andgetKey
. These methods are very similar to the methods of the same name in the simple examplesJAXRQuery.java
andJAXRPublish.java
. TheexecutePublish
method uses information in the filePubQueryBeanExample.properties
to create an organization named The Coffee Enterprise Bean Break. TheexecuteQuery
method uses the organization name, specified in the application client code, to locate this organization.The bean class injects a
ConnectionFactory
resource. It implements a@PostConstruct
method namedmakeConnection
, which uses theConnectionFactory
to create theConnection
. Finally, a@PreDestroy
method namedendConnection
closes theConnection
.Editing the Properties File
Before you compile the application, edit the
clientsession-ejb/src/java/PubQueryBeanExamples.properties
file in the same way you edited theJAXRExamples.properties
file 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. To verify that the Registry Server is deployed, use the
asadmin
command as follows:%asadmin list-components
Xindice <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
asadmin
command. 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.
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,type
the 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
asadmin
command, type the following command:Building, Deploying, and Running the Application Using NetBeans 5.5
To build, deploy, and run the application using NetBeans 5.5, do the following:
- In NetBeans 5.5, choose Open Project from the File menu.
- In the Open Project dialog, navigate to
<
INSTALL
>/javaeetutorial5/examples/jaxr/
.- Select the
clientsession
folder.- Select the Open as Main Project checkbox and the Open Required Projects checkbox.
- Click Open Project Folder.
- Right-click the
clientsession
project and choose Build Project.This task compiles and packages the session bean and application client, then packages the EAR file.
- Right-click the project and choose Deploy Project.
- Right-click the project and choose Run Project.
This command returns a JAR file named
clientsessionClient.jar
and then executes it.The output of the application client in the Output pane looks like this:
In the server log, you will find the output from the
executePublish
andexecuteQuery
methods, wrapped in logging information.To undeploy the application, follow these steps:
If you wish, you can run the
JAXRDelete
program in thesimple
project to delete the organization that was published.Building, Deploying, and Running the Application Using Ant
To build the application using Ant, do the following:
To deploy the application and run the client using Ant, use the following command:
Ignore the message that states that the application is deployed at a URL.
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
executePublish
andexecuteQuery
methods, wrapped in logging information.After you run the example, use the following command to undeploy the application:
If you wish, you can use the
run-delete
target in thesimple
directory to delete the organization that was published.To remove the generated files, use the following command: