Movatterモバイル変換


[0]ホーム

URL:


Document Information

Preface

Part I Introduction

1.  Overview

2.  Using the Tutorial Examples

Part II The Web Tier

3.  Getting Started with Web Applications

4.  Java Servlet Technology

5.  JavaServer Pages Technology

6.  JavaServer Pages Documents

7.  JavaServer Pages Standard Tag Library

8.  Custom Tags in JSP Pages

9.  Scripting in JSP Pages

10.  JavaServer Faces Technology

11.  Using JavaServer Faces Technology in JSP Pages

12.  Developing with JavaServer Faces Technology

13.  Creating Custom UI Components

14.  Configuring JavaServer Faces Applications

15.  Internationalizing and Localizing Web Applications

Part III Web Services

16.  Building Web Services with JAX-WS

17.  Binding between XML Schema and Java Classes

18.  Streaming API for XML

19.  SOAP with Attachments API for Java

Part IV Enterprise Beans

20.  Enterprise Beans

21.  Getting Started with Enterprise Beans

22.  Session Bean Examples

23.  A Message-Driven Bean Example

Part V Persistence

24.  Introduction to the Java Persistence API

25.  Persistence in the Web Tier

26.  Persistence in the EJB Tier

27.  The Java Persistence Query Language

Part VI Services

28.  Introduction to Security in the Java EE Platform

29.  Securing Java EE Applications

30.  Securing Web Applications

31.  The Java Message Service API

32.  Java EE Examples Using the JMS API

A Java EE Application That Uses the JMS API with a Session Bean

Writing the Application Components for theclientsessionmdb Example

Coding the Application Client:MyAppClient.java

Coding the Publisher Session Bean

Coding the Message-Driven Bean:MessageBean.java

Creating Resources for theclientsessionmdb Example

Building, Deploying, and Running theclientsessionmdb Example Using NetBeans IDE

Building, Deploying, and Running theclientsessionmdb Example Using Ant

A Java EE Application That Uses the JMS API with an Entity

Overview of theclientmdbentity Example Application

Writing the Application Components for theclientmdbentity Example

Coding the Application Client:HumanResourceClient.java

Coding the Message-Driven Beans for theclientmdbentity Example

Coding the Entity Class for theclientmdbentity Example

Creating Resources for theclientmdbentity Example

Building, Deploying, and Running theclientmdbentity Example Using NetBeans IDE

Building, Deploying, and Running theclientmdbentity Example Using Ant

An Application Example That Consumes Messages from a Remote Server

Overview of theconsumeremote Example Modules

Writing the Module Components for theconsumeremote Example

Creating Resources for theconsumeremote Example

Using Two Application Servers for theconsumeremote Example

Building, Deploying, and Running theconsumeremoteModules Using NetBeans IDE

Building, Deploying, and Running theconsumeremote Modules Using Ant

An Application Example That Deploys a Message-Driven Bean on Two Servers

Overview of thesendremote Example Modules

Writing the Module Components for thesendremote Example

Coding the Application Client:MultiAppServerClient.java

Coding the Message-Driven Bean:ReplyMsgBean.java

Creating Resources for thesendremote Example

Using Two Application Servers for thesendremote Example

Building, Deploying, and Running thesendremote Modules Using NetBeans IDE

Building, Deploying, and Running thesendremote Modules Using Ant

33.  Transactions

34.  Resource Connections

35.  Connector Architecture

Part VII Case Studies

36.  The Coffee Break Application

37.  The Duke's Bank Application

Part VIII Appendixes

A.  Java Encoding Schemes

B.  About the Authors

Index

 

The Java EE 5 Tutorial

Java Coffee Cup logo
PreviousContentsNext

An Application Example That Deploys a Message-Driven Bean on Two Servers

This section, like the preceding one, explains how to write, compile, package, deploy,and run a pair of Java EE modules that use the JMSAPI and run on two Java EE servers. The modules are slightly morecomplex than the ones in the first example.

The modules use the following components:

  • An application client that is deployed on the local server. It uses two connection factories, one ordinary one and one that is configured to communicate with the remote server, to create two publishers and two subscribers and to publish and to consume messages.

  • A message-driven bean that is deployed twice: once on the local server, and once on the remote one. It processes the messages and sends replies.

In this section, the termlocal server means the server on which both theapplication client and the message-driven bean are deployed (earth in the preceding example).The termremote server means the server on which only the message-driven bean isdeployed (jupiter in the preceding example).

The section covers the following topics:

You will find the source files for this section intut-install/javaeetutorial5/examples/jms/sendremote/. Pathnames in this section are relative to this directory.

Overview of thesendremote Example Modules

This pair of modules is somewhat similar to the modules inAn Application Example That Consumes Messages from a Remote Serverin that the only components are a client and a message-driven bean. However,the modules here use these components in more complex ways. One module consistsof the application client. The other module contains only the message-driven bean andis deployed twice, once on each server.

The basic steps of the modules are as follows.

  1. You start two Java EE servers, one on each system.

  2. On the local server (earth), you create two connection factories: one local and one that communicates with the remote server (jupiter). On the remote server, you create a connection factory that has the same name.

  3. The application client looks up the two connection factories (the local one and the one that communicates with the remote server) to create two connections, sessions, publishers, and subscribers. The subscribers use a message listener.

  4. Each publisher publishes five messages.

  5. Each of the local and the remote message-driven beans receives five messages and sends replies.

  6. The client’s message listener consumes the replies.

Figure 32-4 illustrates the structure of this application. M1 represents the first message sentusing the local connection factory, and RM1 represents the first reply message sent bythe local MDB. M2 represents the first message sent using the remoteconnection factory, and RM2 represents the first reply message sent by the remoteMDB.

Figure 32-4 A Java EE Application That Sends Messages to Two Servers

Diagram of application showing an application client sending messages to two servers and receiving the replies

Writing the Module Components for thesendremote Example

Writing the components of the modules involves two tasks:

Coding the Application Client:MultiAppServerClient.java

The application client class,multiclient/src/java/MultiAppServerClient.java, does the following.

  1. It injects resources for two connection factories and a topic.

  2. For each connection factory, it creates a connection, a publisher session, a publisher, a subscriber session, a subscriber, and a temporary topic for replies.

  3. Each subscriber sets its message listener,ReplyListener, and starts the connection.

  4. Each publisher publishes five messages and creates a list of the messages the listener should expect.

  5. When each reply arrives, the message listener displays its contents and removes it from the list of expected messages.

  6. When all the messages have arrived, the client exits.

Coding the Message-Driven Bean:ReplyMsgBean.java

The message-driven bean class,replybean/src/ReplyMsgBean.java, does the following:

  1. Uses the@MessageDriven annotation:

    @MessageDriven(mappedName="jms/Topic")
  2. Injects resources for theMessageDrivenContext and for a connection factory. It does not need a destination resource because it uses the value of the incoming message’sJMSReplyTo header as the destination.

  3. Uses a@PostConstruct callback method to create the connection, and a@PreDestroy callback method to close the connection.

TheonMessage method of the message-driven bean class does the following:

  1. Casts the incoming message to aTextMessage and displays the text

  2. Creates a connection, a session, and a publisher for the reply message

  3. Publishes the message to the reply topic

  4. Closes the connection

On both servers, the bean will consume messages from the topicjms/Topic.

Creating Resources for thesendremote Example

This example uses the connection factory namedjms/ConnectionFactory and the topic namedjms/Topic. These objects must exist on both the local and the remote servers.

This example uses an additional connection factory,jms/JupiterConnectionFactory, which communicates with theremote system; you created it inCreating Administered Objects for Multiple Systems. This connection factory must exist onthe local server.

Thebuild.xml file for themulticlient module contains targets that you can useto create these resources if you deleted them previously.

Using Two Application Servers for thesendremote Example

If you are using NetBeans IDE, you need to add the remoteserver in order to deploy the message-driven bean there. To do so, performthese steps:

  1. In NetBeans IDE, click the Services tab.

  2. Right-click the Servers node and choose Add Server. In the Add Server Instance dialog, perform these steps:

    1. Select Sun Java System Application Server (the default) from the Server list.

    2. In the Name field, specify a name different from that of the local server, such asRemote Server.

    3. Click Next.

    4. For the Platform Folder location, you can either browse to the location of the Application Server on the remote system or, if that location is not visible from the local system, use the default location on the local system.

    5. Select the Register Remote Domain radio button.

    6. Click Next.

    7. Type the system name of the host in the Host field.

    8. Click Next.

    9. Type the administrative username and password for the remote system in the Admin Username and Admin Password fields.

    10. Click Finish.

      There may be a delay while NetBeans IDE registers the remote domain.

Building, Deploying, and Running thesendremote Modules Using NetBeans IDE

To package the modules using NetBeans IDE, perform these steps:

  1. In NetBeans IDE, choose Open Project from the File menu.

  2. In the Open Project dialog, navigate totut-install/javaeetutorial5/examples/jms/sendremote/.

  3. Select thereplybean folder.

  4. Select the Open as Main Project check box.

  5. Click Open Project.

  6. Right-click thereplybean project and choose Build.

    This command creates a JAR file that contains the bean class file.

  7. Choose Open Project from the File menu.

  8. Select themulticlient folder.

  9. Select the Open as Main Project check box.

  10. Click Open Project.

  11. Right-click themulticlient project and choose Build.

    This command creates a JAR file that contains the client class file and a manifest file.

To deploy themulticlient module on the local server, perform these steps:

  1. Right-click themulticlient project and choose Properties.

  2. Select Run from the Categories tree.

  3. From the Server list, select Sun Java System Application Server (the local server).

  4. Click OK.

  5. Right-click themulticlient project and choose Undeploy and Deploy.

To deploy thereplybean module on the local and remote servers, perform thesesteps:

  1. Right-click thereplybean project and choose Properties.

  2. Select Run from the Categories tree.

  3. From the Server list, select Sun Java System Application Server(the local server).

  4. Click OK.

  5. Right-click thereplybean project and choose Undeploy and Deploy.

  6. Right-click thereplybean project again and choose Properties.

  7. Select Run from the Categories tree.

  8. From the Server list, select Sun Java System Application Server (1) (the remote server).

  9. Click OK.

  10. Right-click thereplybean project and choose Undeploy and Deploy.

You can use the Services tab to verify thatmulticlient is deployed asan App Client Module on the local server and thatreplybean is deployedas an EJB Module on both servers.

To run the application client, right-click themulticlient project and choose Run.

This command returns a JAR file namedmulticlientClient.jar and then executes it.

On the local system, the output of theappclient command looks something likethis:

running application client container.Sent message: text: id=1 to local app serverSent message: text: id=2 to remote app serverReplyListener: Received message: id=1, text=ReplyMsgBean processed message: text: id=1 to local app serverSent message: text: id=3 to local app serverReplyListener: Received message: id=3, text=ReplyMsgBean processed message: text: id=3 to local app serverReplyListener: Received message: id=2, text=ReplyMsgBean processed message: text: id=2 to remote app serverSent message: text: id=4 to remote app serverReplyListener: Received message: id=4, text=ReplyMsgBean processed message: text: id=4 to remote app serverSent message: text: id=5 to local app serverReplyListener: Received message: id=5, text=ReplyMsgBean processed message: text: id=5 to local app serverSent message: text: id=6 to remote app serverReplyListener: Received message: id=6, text=ReplyMsgBean processed message: text: id=6 to remote app serverSent message: text: id=7 to local app serverReplyListener: Received message: id=7, text=ReplyMsgBean processed message: text: id=7 to local app serverSent message: text: id=8 to remote app serverReplyListener: Received message: id=8, text=ReplyMsgBean processed message: text: id=8 to remote app serverSent message: text: id=9 to local app serverReplyListener: Received message: id=9, text=ReplyMsgBean processed message: text: id=9 to local app serverSent message: text: id=10 to remote app serverReplyListener: Received message: id=10, text=ReplyMsgBean processed message: text: id=10 to remote app serverWaiting for 0 message(s) from local app serverWaiting for 0 message(s) from remote app serverFinishedClosing connection 1Closing connection 2

On the local system, where the message-driven bean receives the odd-numbered messages, theoutput in the server log looks like this (wrapped in logging information):

ReplyMsgBean: Received message: text: id=1 to local app serverReplyMsgBean: Received message: text: id=3 to local app serverReplyMsgBean: Received message: text: id=5 to local app serverReplyMsgBean: Received message: text: id=7 to local app serverReplyMsgBean: Received message: text: id=9 to local app server

On the remote system, where the bean receives the even-numbered messages, the outputin the server log looks like this (wrapped in logging information):

ReplyMsgBean: Received message: text: id=2 to remote app serverReplyMsgBean: Received message: text: id=4 to remote app serverReplyMsgBean: Received message: text: id=6 to remote app serverReplyMsgBean: Received message: text: id=8 to remote app serverReplyMsgBean: Received message: text: id=10 to remote app server

Undeploy the modules after you finish running the client. To undeploy the modules,perform these steps:

  1. Click the Services tab.

  2. Expand the Servers node.

  3. Expand the Application Server node (the local system).

  4. Expand the Applications node.

  5. Expand the EJB Modules node.

  6. Right-clickreplybean and choose Undeploy.

  7. Expand the App Client Modules node.

  8. Right-clickmulticlient and choose Undeploy.

  9. Expand the node for the remote system.

  10. Expand the Applications node.

  11. Expand the EJB Modules node.

  12. Right-clickreplybean and choose Undeploy.

To remove the generated files, follow these steps:

  1. Right-click thereplybean project and choose Clean.

  2. Right-click themulticlient project and choose Clean.

Building, Deploying, and Running thesendremote Modules Using Ant

To package the modules, perform these steps:

  1. Go to the following directory:

    tut-install/javaeetutorial5/examples/jms/sendremote/multiclient/
  2. Type the following command:

    ant

    This command creates a JAR file that contains the client class file and a manifest file.

  3. Change to the directoryreplybean:

    cd ../replybean
  4. Type the following command:

    ant

    This command creates a JAR file that contains the bean class file.

To deploy thereplybean module on the local and remote servers, perform thefollowing steps:

  1. Verify that you are still in the directoryreplybean.

  2. Type the following command:

    ant deploy

    Ignore the message that states that the application is deployed at a URL.

  3. Type the following command:

    ant deploy-remote -Dsys=remote-system-name

    Replaceremote-system-name with the actual name of the remote system.

To deploy and run the client, perform these steps:

  1. Change to the directorymulticlient:

    cd ../multiclient
  2. Type the following command:

    ant run

On the local system, the output looks something like this:

running application client container.Sent message: text: id=1 to local app serverSent message: text: id=2 to remote app serverReplyListener: Received message: id=1, text=ReplyMsgBean processed message: text: id=1 to local app serverSent message: text: id=3 to local app serverReplyListener: Received message: id=3, text=ReplyMsgBean processed message: text: id=3 to local app serverReplyListener: Received message: id=2, text=ReplyMsgBean processed message: text: id=2 to remote app serverSent message: text: id=4 to remote app serverReplyListener: Received message: id=4, text=ReplyMsgBean processed message: text: id=4 to remote app serverSent message: text: id=5 to local app serverReplyListener: Received message: id=5, text=ReplyMsgBean processed message: text: id=5 to local app serverSent message: text: id=6 to remote app serverReplyListener: Received message: id=6, text=ReplyMsgBean processed message: text: id=6 to remote app serverSent message: text: id=7 to local app serverReplyListener: Received message: id=7, text=ReplyMsgBean processed message: text: id=7 to local app serverSent message: text: id=8 to remote app serverReplyListener: Received message: id=8, text=ReplyMsgBean processed message: text: id=8 to remote app serverSent message: text: id=9 to local app serverReplyListener: Received message: id=9, text=ReplyMsgBean processed message: text: id=9 to local app serverSent message: text: id=10 to remote app serverReplyListener: Received message: id=10, text=ReplyMsgBean processed message: text: id=10 to remote app serverWaiting for 0 message(s) from local app serverWaiting for 0 message(s) from remote app serverFinishedClosing connection 1Closing connection 2

On the local system, where the message-driven bean receives the odd-numbered messages, theoutput in the server log looks like this (wrapped in logging information):

ReplyMsgBean: Received message: text: id=1 to local app serverReplyMsgBean: Received message: text: id=3 to local app serverReplyMsgBean: Received message: text: id=5 to local app serverReplyMsgBean: Received message: text: id=7 to local app serverReplyMsgBean: Received message: text: id=9 to local app server

On the remote system, where the bean receives the even-numbered messages, the outputin the server log looks like this (wrapped in logging information):

ReplyMsgBean: Received message: text: id=2 to remote app serverReplyMsgBean: Received message: text: id=4 to remote app serverReplyMsgBean: Received message: text: id=6 to remote app serverReplyMsgBean: Received message: text: id=8 to remote app serverReplyMsgBean: Received message: text: id=10 to remote app server

Undeploy the modules after you finish running the client. To undeploy themulticlientmodule, perform these steps:

  1. Verify that you are still in the directorymulticlient.

  2. Type the following command:

    ant undeploy

To undeploy thereplybean module, perform these steps:

  1. Change to the directoryreplybean:

    cd ../replybean
  2. Type the following command:

    ant undeploy
  3. Type the following command:

    ant undeploy-remote -Dsys=remote-system-name

    Replaceremote-system-name with the actual name of the remote system.

To remove the generated files, use the following command in both thereplybean andmulticlient directories:

ant clean
PreviousContentsNext

Copyright © 2010, Oracle and/or its affiliates. All rights reserved.Legal Notices


[8]ページ先頭

©2009-2025 Movatter.jp