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:

  • Coding the application client: MyAppClient.java

  • Coding the PubQuery session bean

  • Editing the properties file

  • Starting the Application Server

  • Creating JAXR resources

  • Building, deploying, and running the application using NetBeans 5.5

  • Building, deploying, and running the application using Ant

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 clientsession-ejb/src/java/PubQueryBeanExamples.properties file in the same way you edited the JAXRExamples.properties file to run the simple examples (see Before You Compile the Examples, page 722). 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 27). 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, page 696) 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 28).

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:

a. Type jaxr-pool in the Name field.

b. Choose jaxr-ra from the Resource Adapter drop-down list.

c. Choose com.sun.connector.jaxr.JaxrConnectionFactory (the only choice) from the Connection Definition drop-down list.

d. 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


Building, Deploying, and Running the Application Using NetBeans 5.5

To build, deploy, and run the application using NetBeans 5.5, do the following:

1.

In NetBeans 5.5, choose Open Project from the File menu.

2.

In the Open Project dialog, navigate to <INSTALL>/javaeetutorial5/examples/jaxr/.

3.

Select the clientsession folder.

4.

Select the Open as Main Project checkbox and the Open Required Projects checkbox.

5.

Click Open Project Folder.

6.

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.

7.

Right-click the project and choose Deploy Project.

8.

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:

   To view the bean output,     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.

To undeploy the application, follow these steps:

1.

Click the Runtime tab.

2.

Expand the Servers node.

3.

Expand the Sun Java System Application Server node.

4.

Expand the Applications node.

5.

Expand the Enterprise Applications node.

6.

Right-click clientsession and choose Undeploy.

If you wish, you can run the JAXRDelete program in the simple 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:

1.

Start the Application Server, if it is not already running.

2.

Go to the following directory:

<INSTALL>/javaeetutorial5/examples/jaxr/clientsession/


3.

To compile the source files and package the application, use the following command:

ant


This task does the following:

a. Compiles and packages the session bean

b. Compiles and packages the application client

c. Packages the EAR file, creating a file named clientsession.ear in the dist directory.

To deploy the application and run the client using Ant, use the following command:

   ant run


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

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

   ant undeploy


If you wish, you can use the run-delete target in the simple directory to delete the organization that was published.

To remove the generated files, use the following command:

   ant clean




The JavaT EE 5 Tutorial
The JavaT EE 5 Tutorial
ISBN: N/A
EAN: N/A
Year: 2004
Pages: 309

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net