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:

<INSTALL>/javaeetutorial5/examples/bp-project/ 

Coding the Application Client: MyAppClient.java

The application client class, clientsession-app-client/src/java/MyAppClient.java, accesses the PubQuery enterprise bean's remote interface, clientsession-app-client/src/java/PubQueryRemote.java. The program calls the bean's two business methods, executePublish and executeQuery.

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 and executeQuery. The bean class, clientsession-ejb/src/java/PubQueryBean.java, implements the executePublish and executeQuery methods and their helper methods getName, getDescription, and getKey. These methods are very similar to the methods of the same name in the simple examples JAXRQuery.java and JAXRPublish.java. The executePublish method uses information in the file PubQueryBeanExample.properties to create an organization named The Coffee Enterprise Bean Break. The executeQuery 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 named makeConnection, which uses the ConnectionFactory to create the Connection. Finally, a @PreDestroy method named endConnection closes the Connection.

Editing the Properties File

Before you compile the application, edit the PubQueryBeanExamples.properties file in the same way you edited the JAXRExamples.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:

  1. In the tree component, expand the Resources node, then expand the Connectors node.
  2. Click Connector Connection Pools.
  3. Click New.
  4. On the General Settings page:
    1. Type jaxr-pool in the Name field.
    2. Choose jaxr-ra from the Resource Adapter drop-down list.
    3. Choose com.sun.connector.jaxr.JaxrConnectionFactory (the only choice) from the Connection Definition drop-down list
    4. Click Next.
  5. On the next page, click Finish.

To create the connector resource, perform the following steps:

  1. Under the Connectors node, click Connector Resources.
  2. Click New. The Create Connector Resource page appears.
  3. In the JNDI Name field, type eis/JAXR.
  4. Choose jaxr-pool from the Pool Name drop-down list.
  5. Click OK.

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-pool 

To create the connector resource using the asadmin command, type the following command:

asadmin create-connector-resource --poolname jaxr-pool eis/JAXR 

Compiling the Source Files and Packaging the Application

To create and package the application, use the following command:

ant 

This target does the following:

  1. Compiles and packages the session bean
  2. Compiles and packages the application client
  3. Packages the EAR file

It creates a file named clientsession.ear in the dist directory.

Deploying the Application

To deploy the clientsession.ear file, use the following command:

ant deploy 

To return a client JAR file, use the following command:

ant client-jar 

These commands deploy the application and return a JAR file named clientsessionClient.jar in the client-jar directory.

Running the Application Client

To run the client, use the following command:

ant run-client 

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 and executeQuery methods, wrapped in logging information.

After you run the example, use the following command to undeploy the application:

ant undeploy 

You can use the run-delete target in the simple directory to delete the organization that was published.