分布式系统复习题chenWord格式文档下载.docx
《分布式系统复习题chenWord格式文档下载.docx》由会员分享,可在线阅读,更多相关《分布式系统复习题chenWord格式文档下载.docx(28页珍藏版)》请在冰豆网上搜索。
3)定义客户应用,并与远程对象通信;
4)调用远程方法(参数及返回值)。
3.简述BMP实体Bean与CMP实体Bean的优缺点。
1、编程方面CMP比BMP简单
BMP需要通过Bean代码实现数据库的操作;
CMP则只需要做相关的映射就可以了。
2、功能方面BMP比CMP强大
CMP一般用于单表操作;
BMP则可用于单表和多表操作。
4.简述EJB2.1规范中实体Bean的finder方法在容器中的工作机制(要求阐述客户端调用finder方法时EJB容器调用Bean方法的过程)。
EJB2.1规范中实体Bean的finder方法在容器中的工作机制(容器调用机制)
1.create
(1)ejbCreate,若返回异常,则结束,否则转2
(2)ejbActivate
(3)ejbPostCreate
(4)ejbStore
(5)返回结果
2.find...
(1)ejbFind....,若返回异常,则结束,否则转2
(2)对
(1)返回的结果集,逐个处理(3-5)
(3)若对象已经存在于容器内存中,则直接使用,否则转4
(4)从Pool中取一个空闲对象,调用ejbActivate与主键值关联
(5)调用ejbLoad方法
(6)返回结果
5.简述无状态会话Bean的生命周期(图示或文字描述均可)。
无状态会话Bean的生命周期如下图所示:
6.简述有状态会话Bean的生命周期(图示或文字描述均可)。
有状态会话Bean的生命周期如下图所示:
7.简述EJB2.1规范实体Bean的生命周期(图示或文字描述均可)。
实体Bean的生命周期如下图所示:
8.简述EJB2.1规范中会话Bean的远程接口、Home接口和Bean实现类的作用。
1、远程接口的作用:
定义客户机可以调用的商业方法。
2、Home接口的作用:
定义客户机可调用的Create上客户机实例化所需的对象EJB。
3、Bean实现类的作用:
Bean具体实现的功能。
二、编程题
1.使用EJB2.1规范编写一个无状态会话Bean,实现Fibonacci数列第n项的求解(要求使用非递归方法实现)。
已知Remote接口和Home接口代码如下:
/*Remote接口代码*/
importjava.rmi.RemoteException;
importjavax.ejb.EJBObject;
publicinterfaceFibonacciextendsEJBObject{
//获取Fibonacci数列第n项的值(n的索引从1开始)
publiclonggetFibonacciItem(intn)throwsRemoteException;
}
/*Home接口代码*/
importjavax.ejb.*;
publicinterfaceFibonacciHomeextendsEJBHome{
publicFibonaccicreate()throwsRemoteException,CreateException;
/*EJB实现类代码*/
importjavax.ejb.SessionBean;
importjavax.ejb.SessionContext;
importjavax.ejb.CreateException;
publicclassFibonacciEJBimplementsSessionBean{
SessionContextsessionContext;
//远程接口的方法
publiclonggetFibonacciItem(intn){
longff;
if(n<
=1)
ff=1;
else
{
intx,a=0,b=l;
for(inti=2;
i<
=n;
i++)
x=a;
a=b;
b=x+a;
ff=b;
returnff;
}
//EJB框架需要的方法
publicvoidejbCreate()throwsCreateException{
publicvoidejbRemove(){
publicvoidejbActivate(){
publicvoidejbPassivate(){
publicvoidsetSessionContext(SessionContextsessionContext){
this.sessionContext=sessionContext;
请编写完整的EJB实现类(实现类命名为FibonacciEJB),实现上述功能。
附注:
Fibonacci数列为形如“1,1,2,3,5,8,13,……”的数列,数列前两项为1,当n>
2时当前项为前两项之和。
2.使用EJB2.1规范编写一个无状态会话Bean,实现从1到n所有被3整除,但不被7整除的整数求和(n由参数传入)。
publicinterfaceSumextendsEJBObject{
//求解从1到n的所有被3整除,但不被7整除的整数这和
publiclonggetSum(intn)throwsRemoteException;
publicinterfaceSumHomeextendsEJBHome{
publicSumcreate()throwsRemoteException,CreateException;
publicclassSumEJBimplementsSessionBean{
publiclonggetSum(intn){
inti;
longsum=0;
for(i=1;
=n;
if(i%3==0)&
&
(i%7!
=0)
sum+=i;
returnsum;
请编写完整的EJB实现类(实现类命名为SumEJB),实现上述功能。
3.分别编制RMI和RMI-IIOP服务器端与客户端,实现累加功能。
已知远程接口如下:
publicinterfaceSumRemoteextendsjava.rmi.Remote{
//实现从1到n(n>
0)累加
longsum(longn)throwsjava.rmi.RemoteException;
RMI服务端:
importjava.rmi.*;
publicclassSumService
extendsjava.rmi.server.UnicastRemoteObjectimplementsSumRemote{
publicSumService()throwsRemoteException{
publiclongsum(longn){
longff=0;
intI;
for(I=1;
I<
=n,I++)
ff+=I;
Returnff;
publicstaticvoidmain(String[]args){
if(System.getSecurityManager()==null)
System.setSecurityManager(newRMISecurityManager());
try{
StringmyRMIName="
SumService"
;
SumServiceservice=newSumService();
Naming.rebind(myRMIName,service);
System.out.print("
ServiceLoaded!
"
);
}catch(Exceptione){
e.printStackTrace();
RMI客户端:
publicclassSumClient{
publicSumClient(){
}
publicstaticvoidmain(String[]args){
if(System.getSecurityManager()==null)
System.setSecurityManager(newRMISecurityManager());
try{
SumRemotes=(SumRemote)Naming.lookup("
longsum=s.sum(100);
System.out.println(sum);
}catch(Exceptionex){
ex.printStackTrace();
RMI-IIOP服务器端:
importjavax.naming.*;
importjavax.rmi.*;
publicclassSumServerextendsPortableRemoteObjectimplementsSumRemote{
publicSumServer()throwsRemoteException{
super();
SumRemoteobj=newSumServer();
Contextic=newInitialContext();
ic.rebind("
SumServer"
obj);
System.out.println("
ServiceOK!
ex.printStackTrace();
RMI-IIOP客户端:
publicclassSumClient{
Objectobj=ic.lookup(“SumServer”);
SumRemoteremoteObj=(SumRemote)PortableRemoteObject.narrow(
Obj,SumRemote.class);
System.out.println(remoteObj.sum(100));
4.已知部署在JBOSS中的消息驱动Bean源码如下:
importjavax.jms.*;
@MessageDriven(mappedName="
jms/QueueMDB"
activationConfig={
@ActivationConfigProperty(propertyName="
acknowledgeMode"
propertyValue="
Auto-acknowledge"
),
destinationType"
javax.jms.Queue"
destination"
propertyValue="
queue/queueMDB"
)})
publicclassQueueMDBimplementsMessageListener{
publicvoidonMessage(Messagemsg){
System.out.println(msg);
JSP客户端源码如下:
<
%@pagelanguage="
java"
pageEncoding="
ISO-8859-1"
%>
%@pageimport="
javax.naming.*,javax.jms.*,java.util.Properties"
%
Stringpath=request.getContextPath();
StringbasePath=request.getScheme()+"
:
//"
+request.getServerName()+"
+request.getServerPort()+path+"
/"
html>
<
head>
basehref="
%=basePath%>
>
title>
MyJSP'
test.jsp'
startingpage<
/title>
/head>
body>
ThisismyJSPpage.<
br>
QueueConnectioncnn=null;
QueueSendersender=null;
QueueSessionsess=null;
Queuequeue=null;
try{
Propertiesprops=newProperties();
props.setProperty("
java.naming.factory.initial"
"
org.jnp.interfaces.NamingContextFactory"
java.naming.provider.url"
localhost:
1099"
java.naming.factory.url.pkgs"
org.jboss.naming"
InitialContextctx=newInitialContext(props);
QueueConnectionFactoryfactory=(QueueConnectionFactory)ctx.lookup("
ConnectionFactory"
cnn=factory.createQueueConnection();
sess=cnn.createQueueSession(false,QueueSession.AUTO_ACKNOWLEDGE);
queue=(Queue)ctx.lookup("
jms/QueueMDB"
}catch(Exceptione){
out.println(e.getMessage());
TextMessagemsg=sess.createTextMessage("
Thisisatestjmsmessageforqueuemdb"
sender=sess.createSender(queue);
sender.send(msg);
sess.close();
out.println("
OK"
%>
/body>
/html>
请编写一个JSP客户端,向该MDB发送一个文本消息,消息内容自定。
5.已知部署在JBOSS中的消息驱动Bean源码如下:
jms/TopicMDB"
javax.jms.Topic"
topic/myTopicMDB"
publicclassTopicMDBimplementsMessageListener{
TopicConnectioncnn=null;
TopicPublishersender=null;
TopicSessionsess=null;
Topictopic=null;
TopicConnectionFactoryfactory=(TopicConnectionFactory)ctx.lookup("
cnn=factory.createTopicConnection();
sess=cnn.createTopicSession(false,QueueSession.AUTO_ACKNOWLEDGE);
topic=(Topic)ctx.lookup("
jms/TopicMDB"
Thisisatestjmsmessagefortopicmdb"
sender=sess.createPublisher(topic);
sender