没有合适的资源?快使用搜索试试~ 我知道了~
jms_tutorial-1_3.pdf
需积分: 0 5 下载量 95 浏览量
2008-01-03
21:20:10
上传
评论
收藏 1.45MB PDF 举报
温馨提示
试读
266页
jms_tutorial-1_3.pdf
资源详情
资源评论
资源推荐
Java
™
Message Service API
Tutorial
by Kim Haase
Sun Microsystems, Inc.
Copyright 2001 Sun Microsystems, Inc.
901 San Antonio Road, Palo Alto, California 94303 U.S.A.
All rights reserved.
This release and related documentation are protected by copyright and distributed under licenses
restricting its use, copying, distribution, and decompilation. No part of this product or related documen-
tation may be reproduced in any form by any means without prior written authorization of Sun and its
licensors, if any.
Portions of this product may be derived from the UNIX® and Berkeley 4.3 BSD systems, licensed from
UNIX System Laboratories, Inc., a wholly owned subsidiary of Novell, Inc., and the University of Cal-
ifornia, respectively. Third-party font software in this product is protected by copyright and licensed
from Sun’s font suppliers.
RESTRICTED RIGHTS LEGEND: Use, duplication, or disclosure by the United States Government is
subject to the restrictions set forth in DFARS 252.227-7013 (c)(1)(ii) and FAR 52.227-19.
The release described in this manual may be protected by one or more U.S. patents, foreign patents, or
pending applications.
TRADEMARKS
Sun, the Sun logo, Sun Microsystems, Solaris, Java, JavaServer Pages, Enterprise JavaBeans, Java
Naming and Directory Interface, J2SE, J2EE, EJB, and JSP are trademarks or registered trademarks of
Sun Microsystems, Inc. in the U.S. and certain other countries. The "Duke" character is a trademark of
Sun Microsystems, Inc., and Copyright (c) 1992-2001 Sun Microsystems, Inc. All Rights Reserved.
UNIX is a registered trademark in the United States and other countries, exclusively licensed through
X/Open Company, Ltd. OPEN LOOK is a registered trademark of Novell, Inc. All other product names
mentioned herein are the trademarks of their respective owners.
THIS PUBLICATION IS PROVIDED “AS IS” WITHOUT WARRANTY OF ANY KIND, EITHER
EXPRESS OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGE-
MENT.
THIS PUBLICATION COULD INCLUDE TECHNICAL INACCURACIES OR TYPOGRAPHICAL
ERRORS. CHANGES ARE PERIODICALLY ADDED TO THE INFORMATION HEREIN; THESE
CHANGES WILL BE INCORPORATED IN NEW EDITIONS OF THE PUBLICATION. SUN
MICROSYSTEMS, INC., MAY MAKE IMPROVEMENTS AND/OR CHANGES IN THE PROD-
UCT(S) AND/OR THE PROGRAM(S) DESCRIBED IN THIS PUBLICATION AT ANY TIME.
Contents
iii
1 Overview. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .9
1.1 What Is Messaging? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
1.2 What Is the JMS API? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
1.3 When Can You Use the JMS API?. . . . . . . . . . . . . . . . . . . . . . . . . . . 11
1.4 How Does the JMS API Work with the J2EE
™
Platform?. . . . . . . . . 12
2 Basic JMS API Concepts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .15
2.1 JMS API Architecture . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
2.2 Messaging Domains . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
2.2.1 Point-to-Point Messaging Domain. . . . . . . . . . . . . . . . . . . . 17
2.2.2 Publish/Subscribe Messaging Domain . . . . . . . . . . . . . . . . 17
2.3 Message Consumption . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
3 The JMS API Programming Model. . . . . . . . . . . . . . . . . . . . . . . . . .21
3.1 Administered Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
3.1.1 Connection Factories . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
3.1.2 Destinations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
3.2 Connections . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
3.3 Sessions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
3.4 Message Producers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
3.5 Message Consumers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
3.5.1 Message Listeners . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
3.5.2 Message Selectors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
3.6 Messages. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
3.6.1 Message Headers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
3.6.2 Message Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30
3.6.3 Message Bodies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30
3.7 Exception Handling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
iv
4 Writing Simple JMS Client Applications. . . . . . . . . . . . . . . . . . . . .33
4.1 Setting Your Environment for Running Applications . . . . . . . . . . . . 34
4.2 A Simple Point-to-Point Example . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
4.2.1 Writing the PTP Client Programs . . . . . . . . . . . . . . . . . . . . 35
4.2.2 Compiling the PTP Clients . . . . . . . . . . . . . . . . . . . . . . . . . 43
4.2.3 Starting the JMS Provider . . . . . . . . . . . . . . . . . . . . . . . . . . 43
4.2.4 Creating the JMS Administered Objects . . . . . . . . . . . . . . . 43
4.2.5 Running the PTP Clients . . . . . . . . . . . . . . . . . . . . . . . . . . . 44
4.2.6 Deleting the Queue . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45
4.3 A Simple Publish/Subscribe Example . . . . . . . . . . . . . . . . . . . . . . . . 45
4.3.1 Writing the Pub/Sub Client Programs . . . . . . . . . . . . . . . . . 46
4.3.2 Compiling the Pub/Sub Clients . . . . . . . . . . . . . . . . . . . . . . 54
4.3.3 Starting the JMS Provider . . . . . . . . . . . . . . . . . . . . . . . . . . 55
4.3.4 Creating the JMS Administered Objects . . . . . . . . . . . . . . . 55
4.3.5 Running the Pub/Sub Clients. . . . . . . . . . . . . . . . . . . . . . . . 55
4.3.6 Deleting the Topic and Stopping the Server . . . . . . . . . . . . 57
4.4 Running JMS Client Programs on Multiple Systems. . . . . . . . . . . . . 57
5 Creating Robust JMS Applications . . . . . . . . . . . . . . . . . . . . . . . . . .59
5.1 Using Basic Reliability Mechanisms . . . . . . . . . . . . . . . . . . . . . . . . . 60
5.1.1 Controlling Message Acknowledgment. . . . . . . . . . . . . . . . 60
5.1.2 Specifying Message Persistence . . . . . . . . . . . . . . . . . . . . . 62
5.1.3 Setting Message Priority Levels . . . . . . . . . . . . . . . . . . . . . 63
5.1.4 Allowing Messages to Expire . . . . . . . . . . . . . . . . . . . . . . . 64
5.1.5 Creating Temporary Destinations . . . . . . . . . . . . . . . . . . . . 64
5.2 Using Advanced Reliability Mechanisms . . . . . . . . . . . . . . . . . . . . . 65
5.2.1 Creating Durable Subscriptions. . . . . . . . . . . . . . . . . . . . . . 65
5.2.2 Using JMS API Local Transactions. . . . . . . . . . . . . . . . . . . 68
6 Using the JMS API in a J2EE Application . . . . . . . . . . . . . . . . . . .71
6.1 Using Enterprise Beans to Produce and Synchronously Receive
Messages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71
6.2 Using Message-Driven Beans . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73
6.3 Managing Distributed Transactions . . . . . . . . . . . . . . . . . . . . . . . . . . 75
6.4 Using the JMS API with Application Clients and Web Components 77
7 A Simple J2EE Application that Uses the JMS API . . . . . . . . . . .79
7.1 Writing and Compiling the Application Components . . . . . . . . . . . . 79
7.1.1 Coding the Application Client:
SimpleClient.java . . . . . . 80
7.1.2 Coding the Message-Driven Bean:
MessageBean.java . . . . 83
v
7.1.3 Compiling the Source Files . . . . . . . . . . . . . . . . . . . . . . . . . 86
7.2 Creating and Packaging the Application . . . . . . . . . . . . . . . . . . . . . . 86
7.2.1 Starting the J2EE Server and Deploytool . . . . . . . . . . . . . . 86
7.2.2 Creating a Queue . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87
7.2.3 Creating the J2EE Application. . . . . . . . . . . . . . . . . . . . . . . 87
7.2.4 Packaging the Application Client . . . . . . . . . . . . . . . . . . . . 88
7.2.5 Packaging the Message-Driven Bean . . . . . . . . . . . . . . . . . 90
7.2.6 Checking the JNDI Names. . . . . . . . . . . . . . . . . . . . . . . . . . 94
7.3 Deploying and Running the Application . . . . . . . . . . . . . . . . . . . . . . 94
7.3.1 Looking at the Deployment Descriptor . . . . . . . . . . . . . . . . 94
7.3.2 Adding the Server . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 96
7.3.3 Deploying the Application. . . . . . . . . . . . . . . . . . . . . . . . . . 97
7.3.4 Running the Client . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 97
7.3.5 Undeploying the Application. . . . . . . . . . . . . . . . . . . . . . . . 98
7.3.6 Removing the Application and Stopping the Server . . . . . . 99
8 A J2EE Application that Uses the JMS API with a Session
Bean
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .101
8.1 Writing and Compiling the Application Components . . . . . . . . . . . 102
8.1.1 Coding the Application Client:
MyAppClient.java . . . . . . 103
8.1.2 Coding the Publisher Session Bean . . . . . . . . . . . . . . . . . . 104
8.1.3 Coding the Message-Driven Bean:
MessageBean.java . . . 109
8.1.4 Compiling the Source Files . . . . . . . . . . . . . . . . . . . . . . . . 112
8.2 Creating and Packaging the Application . . . . . . . . . . . . . . . . . . . . . 112
8.2.1 Starting the J2EE Server and Deploytool . . . . . . . . . . . . . 113
8.2.2 Creating a Topic. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113
8.2.3 Creating a Connection Factory . . . . . . . . . . . . . . . . . . . . . 114
8.2.4 Creating the J2EE Application. . . . . . . . . . . . . . . . . . . . . . 114
8.2.5 Packaging the Application Client . . . . . . . . . . . . . . . . . . . 115
8.2.6 Packaging the Session Bean. . . . . . . . . . . . . . . . . . . . . . . . 116
8.2.7 Packaging the Message-Driven Bean . . . . . . . . . . . . . . . . 119
8.2.8 Specifying the JNDI Names. . . . . . . . . . . . . . . . . . . . . . . . 121
8.3 Deploying and Running the Application . . . . . . . . . . . . . . . . . . . . . 121
8.3.1 Adding the Server . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 122
8.3.2 Deploying the Application. . . . . . . . . . . . . . . . . . . . . . . . . 122
8.3.3 Running the Client . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 123
8.3.4 Undeploying the Application. . . . . . . . . . . . . . . . . . . . . . . 124
8.3.5 Removing the Application and Stopping the Server . . . . . 124
剩余265页未读,继续阅读
tempbaby
- 粉丝: 0
- 资源: 12
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论0