企业JavaBeansTM学习指南:
建立你的第一个无状态(stateless)会话Bean
- 关于本学习指南
- 关于示例
- 步骤1: 安装企业JavaBeans 服务器
- 步骤2: 指定企业JavaBeans 远程界面
- 步骤3: 指定宿主界面
- 步骤4: 编写企业JavaBean 类
- 步骤5: 创建ejb-jar 文件
- 步骤6: 配置DemoBean企业JavaBeans
- 步骤7: 编写企业JavaBean 客户程序
- 步骤8: 运行客户程序
3. 如何建立你的第一个无状态(stateless)会话Bean
步骤 5: 创建 ejb-jar 文件
Javac ejb/Demo/demohome.java Javac ejb/Demo/demo.java Javac ejb/Demo/demobean.java
(SessionDescriptor
; This file must start with SessionDescriptor or
; EntityDescriptor
; Indicate the name which the bean will be bound
; into the JNDI name as
beanHomeName demo.DemoHome
; The enterprise Java Bean class (see step 4)
enterpriseBeanClassName ejb.demo.DemoBean
homeInterfaceClassName ejb.demo.DemoHome
; The home interface implemented by a class
; generated by the container provided tools
; see step 3
remoteInterfaceClassName ejb.demo.Demo
; See step 2
isReentrant false
; Always false for session beans
stateManagementType STATELESS_SESSION
; Either STATELESS_SESSION or STATEFUL_SESSION.
; DemoBean is a stateless session bean
sessionTimeout 5
; seconds
(controlDescriptors
; This section decides the run-time properties when
; a method is called. The DEFAULT sub-section applies
; to all methods, but can be overridden on a per-method
; basis, similar to the "accessControlEntries" above.
(DEFAULT
isolationLevel TRANSACTION_SERIALIZABLE
transactionAttribute TX_REQUIRED
runAsMode CLIENT_IDENTITY
)
; end isolationLevel
)
; end controlDescriptors
(environmentProperties
maxBeansInFreePool 100
)
; end environmentProperties
)
; end SessionDescriptor
-
· 企业 bean 类 bean 所依赖的所有其它类(步骤 4)。
· 企业bean的远程界面 (步骤 2)。
· 企业bean的宿主界面 (步骤 3)。
· 一个配置描述符(见上)。
· java.util.Properties 的一个实例(如果 bean 需要的话)。
· 一个在 ejb-jar 文件中用来鉴别配置描述符的清单文件。
Name: ejb/demo/DemoBeanDD.ser Enterprise-Bean: True
jar cvfm Demo.jar ejb/demo/manifest.txt ejb/demo/*.class /
ejb/demo/*.ser
对 Demo.jar 的检查应该产生类似于下面的输出:
jar tf Demo.jar
META-INF/MANIFEST.MF
Ejb/demo/Demo.class
Ejb/demo/DemoBean.class
Ejb/demo/DemoHome.class
Ejb/demo/DemoBeanDD.ser
步骤6: 配置DemoBean 企业JavaBean
ejb/demo/DemoBeanEOImpl.class
ejb/demo/DemoBeanHomeImpl.class
ejb/demo/Skel5k5x705r2x671nd1i1vy2v524ua5y.class
ejb/demo/Skel5q585f5sfzo601q4e725b233m5140.class
ejb/demo/Stub5k5x705r2x671nd1i1vy2v524ua5y.class
ejb/demo/Stub5q585f5sfzo601q4e725b233m5140.class
/export/weblogic/startTengah.sh
#!/bin/sh
#
# Shell script to manually start Tengah
# Server on UNIX systems
CLASSPATH=$CLASSPATH:/export/weblogic/classes/Demo.jar
echo $CLASSPATH
java -ms16m -mx16m -verbosegc weblogic.Server
cd /export/weblogic/classes jar xvf Demo.jar ejb/demo/DemoBeanDD.ser
包括新的企业JavaBeans 的装载和启动的有关说明。
/export/weblogic/weblogic.properties
# # # # # # # # # # # # # # # # # # # # # # # # # #
# Tengah Enterprise JavaBeans DEMO PROPERTIES
# -------------------------------------------------
# Uncomment the appropriate lines below and modify
# DBMS-related info and paths to match your particular
# installation.
#
# Deploys the Enterprise JavaBean examples. Uncomment to use:
weblogic.ejb.deploy=/
/export/weblogic/classes/beanManaged.jar,/
/export/weblogic/classes/containerManaged.jar,/
/export/weblogic/classes/statefulSession.jar,/
/export/weblogic/classes/ejb/demo/DemoBeanDD.ser
#
# weblogic.properties file continues below...
#
步骤7: 编写企业JavaBean 客户程序(Client)
-
· 建立 JNDI 初始上下文环境。
· 用 JNDI 对 Enterprise JavaBean 的宿主界面定位。
· 用宿主界面指示容器来创建一个 Enterprise JavaBean 实例。
· 用远程界面指示容器来执行 Enterprise JavaBeans bean的方法。
DemoClient.java (源代码)
/**
* DemoClient -- demonstrates using a minimal
* Java application to talk to the DemoBean
* stateless session bean
*/
package ejb.demo;
import javax.ejb.*;
import javax.naming.*;
import java.rmi.*;
import java.util.Properties;
import examples.ejb.statelessSession.interfaces.*;
/**
* DemoClient demonstrates using a minimal stateless session
* bean.
* Remember view session beans as an extension of your client
* running in the server.
*/
public class demoClient {
public static void main(String[] args) {
System.out.println("/nBegin demoClient.../n");
parseArgs(args);
try {
// Create A DemoBean object, in the server
// Note: the name of the class corresponds to the JNDI
// property declared in the DeploymentDescriptor
// From DeploymentDescriptor ...
// beanHomeName demo.DemoHome
Context ctx = getInitialContext();
DemoHome dhome = (DemoHome) ctx.lookup("demo.DemoHome");
// Now you have a reference to the DemoHome object factory
// use it to ask the container to creat an instance of
// the Demo bean
System.out.println("Creating Demo/n");
Demo demo = dhome.create();
// Here is the call that executes the method on the
// server side object
System.out.println("The result is "
demo.demoSelect();
}
catch (Exception e) {
System.out.println(" = Error <=");
e.printStackTrace();
}
System.out.println("/nEnd demoClient.../n");
}
static void parseArgs(String args[]) {
if ((args == null) || (args.length == 0))
return;
for (int i = 0; i < args.length; i ) {
if (args[i].equals("-url"))
Integer.parseInt(args[ i]);
else if (args[i].equals("-user"))
user = args[ i];
else if (args[i].equals("-password"))
password = args[ i];
}
}
static String user = null;
static String password = null;
static String url = "t3://localhost:7001";
/**
* Gets an initial context.
*
* @return Context
* @exception java.lang.Exception if there is
* an error in getting a Context
*/
static public Context getInitialContext() throws Exception {
Properties p = new Properties();
p.put(Context.INITIAL_CONTEXT_FACTORY,
"weblogic.jndi.T3InitialContextFactory");
p.put(Context.PROVIDER_URL, url);
if (user != null) {
System.out.println ("user: " user);
p.put(Context.SECURITY_PRINCIPAL, user);
if (password == null)
password = "";
p.put(Context.SECURITY_CREDENTIALS, password);
}
return new InitialContext(p);
}
}
步骤 8: 编译并运行客户程序(Client)
Java ejb.Demo.democlient
Begin DemoClient...
Creating Demo
The result is Hello World
End DemoClient...
4. 小结
3199



被折叠的 条评论
为什么被折叠?



