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

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

This section explains how to write, compile, package, deploy, and run a JavaEE application that uses the JMS API in conjunction with a session bean.The application contains the following components:

  • An application client that invokes a session bean

  • A session bean that publishes several messages to a topic

  • A message-driven bean that receives and processes the messages using a durable topic subscriber and a message selector

The section covers the following topics:

You will find the source files for this section in the directorytut-install/javaeetutorial5/examples/jms/clientsessionmdb/. Path names in this section are relative to this directory.

Writing the Application Components for theclientsessionmdb Example

This application demonstrates how to send messages from an enterprise bean (in thiscase, a session bean) rather than from an application client, as in theexample inChapter 23, A Message-Driven Bean Example.Figure 32-1 illustrates the structure of this application.

Figure 32-1 A Java EE Application: Client to Session Bean to Message-Driven Bean

Diagram of application showing an application client calling a session bean, which publishes a message that is consumed by a message-driven bean

The Publisher enterprise bean in this example is the enterprise-application equivalent of awire-service news feed that categorizes news events into six news categories. The message-drivenbean could represent a newsroom, where the sports desk, for example, would setup a subscription for all news events pertaining to sports.

The application client in the example injects the Publisher enterprise bean’s remote homeinterface and then calls the bean’s business method. The enterprise bean creates 18text messages. For each message, it sets aString property randomly to oneof six values representing the news categories and then publishes the message toa topic. The message-driven bean uses a message selector for the property tolimit which of the published messages it receives.

Writing the components of the application involves the following:

Coding the Application Client:MyAppClient.java

The application client program,clientsessionmdb-app-client/src/java/MyAppClient.java, performs no JMS API operations and sois simpler than the client program inChapter 23, A Message-Driven Bean Example. The program uses dependency injectionto obtain the Publisher enterprise bean’s business interface:

@EJB(name="PublisherRemote")static private PublisherRemote publisher;

The program then calls the bean’s business method twice.

Coding the Publisher Session Bean

The Publisher bean is a stateless session bean that has one business method.The Publisher bean uses a remote interface rather than a local interface becauseit is accessed from the application client.

The remote interface,clientsessionmdb-ejb/src/java/sb/PublisherRemote.java, declares a single business method,publishNews.

The bean class,clientsessionmdb-ejb/src/java/sb/PublisherBean.java, implements thepublishNews method and its helper methodchooseType.The bean class also injectsSessionContext,ConnectionFactory, andTopic resources and implements@PostConstruct and@PreDestroy callback methods. The bean class begins as follows:

@Stateless@Remote({PublisherRemote.class})public class PublisherBean implements PublisherRemote {    @Resource    private SessionContext sc;    @Resource(mappedName="jms/ConnectionFactory")    private ConnectionFactory connectionFactory;    @Resource(mappedName="jms/Topic")    private Topic topic;    ...

The@PostConstruct callback method of the bean class,makeConnection, creates theConnection usedby the bean. The business methodpublishNews creates aSession and aMessageProducer and publishes the messages.

The@PreDestroy callback method,endConnection, deallocates the resources that were allocated by the@PostConstruct callback method. In this case, the method closes theConnection.

Coding the Message-Driven Bean:MessageBean.java

The message-driven bean class,clientsessionmdb-ejb/src/java/mdb/MessageBean.java, is almost identical to the one inChapter 23, A Message-Driven Bean Example. However, the@MessageDriven annotation is different, because instead of a queue thebean is using a topic with a durable subscription, and it is alsousing a message selector. Therefore, the annotation sets the activation config propertiesmessageSelector,subscriptionDurability,clientId, andsubscriptionName, as follows:

@MessageDriven(mappedName="jms/Topic",activationConfig={ @ActivationConfigProperty(propertyName="messageSelector",    propertyValue="NewsType = ’Sports’ OR NewsType = ’Opinion’"),  @ActivationConfigProperty(    propertyName="subscriptionDurability",    propertyValue="Durable"),  @ActivationConfigProperty(propertyName="clientId",    propertyValue="MyID"),  @ActivationConfigProperty(propertyName="subscriptionName",    propertyValue="MySub") })

The JMS resource adapter uses these properties to create a connection factory forthe message-driven bean that allows the bean to use a durable subscriber.

Creating Resources for theclientsessionmdb Example

This example uses the topic namedjms/Topic and the connection factoryjms/ConnectionFactory, whichyou created inCreating JMS Administered Objects for the Synchronous Receive Example. If you deleted the connection factory or topic, you cancreate them again using targets in thebuild.xml file for this example. Usethe following commands to create the resources:

ant create-cf ant create-topic

Building, Deploying, and Running theclientsessionmdb Example Using NetBeans IDE

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

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

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

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

  4. Select theclientsessionmdb folder.

  5. Select the Open as Main Project check box and the Open Required Projects check box.

  6. Click Open Project.

  7. Right-click theclientsessionmdb project and choose Build.

    This task creates the following:

    • An application client JAR file that contains the client class file and the session bean’s remote interface, along with a manifest file that specifies the main class

    • An EJB JAR file that contains both the session bean and the message-driven bean

    • An application EAR file that contains the two JAR files

  8. Right-click the project and choose Undeploy and Deploy.

  9. Right-click the project and choose Run.

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

The output of the application client in the Output pane looks likethis:

To view the bean output, check <install_dir>/domains/domain1/logs/server.log.

The output from the enterprise beans appears in the server log (domain-dir/logs/server.log),wrapped in logging information. The Publisher session bean sends two sets of 18messages numbered 0 through 17. Because of the message selector, the message-driven beanreceives only the messages whoseNewsType property isSports orOpinion.

Undeploy the application after you finish running the client. To undeploy the application,follow these steps:

  1. Click the Services tab.

  2. Expand the Servers node.

  3. Expand the Application Server node.

  4. Expand the Applications node.

  5. Expand the Enterprise Applications node.

  6. Right-clickclientsessionmdb and choose Undeploy.

To remove the generated files, right-click theclientsessionmdb project and choose Clean.

Building, Deploying, and Running theclientsessionmdb Example 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:

    tut-install/javaeetutorial5/examples/jms/clientsessionmdb/
  3. To compile the source files and package the application, use the following command:

    ant

Theant command creates the following:

  • An application client JAR file that contains the client class file and the session bean’s remote interface, along with a manifest file that specifies the main class

  • An EJB JAR file that contains both the session bean and the message-driven bean

  • An application EAR file that contains the two JAR files

Theclientsessionmdb.ear file is created in theclientsessionmdb/dist directory.

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

ant run

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

The client displays these lines:

running application client container.To view the bean output, check <install_dir>/domains/domain1/logs/server.log.

The output from the enterprise beans appears in the server log (domain-dir/logs/server.log),wrapped in logging information. The Publisher session bean sends two sets of 18messages numbered 0 through 17. Because of the message selector, the message-driven beanreceives only the messages whoseNewsType property isSports orOpinion.

Undeploy the application after you finish running the client. Use the following command:

ant undeploy

To remove the generated files, use the following command in theclientsessionmdb,clientsessionmdb-app-client, andclientsessionmdb-ejb directories:

ant clean
PreviousContentsNext

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


[8]ページ先頭

©2009-2025 Movatter.jp