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
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 (page 28). 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 (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,
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: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.ear
in thedist
directory.Deploying the Application
To deploy the
clientsession.ear
file, 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.jar
in theclient-jar
directory.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
executePublish
andexecuteQuery
methods, wrapped in logging information.After you run the example, use the following command to undeploy the application:
You can use the
run-delete
target in thesimple
directory to delete the organization that was published.