"ejb1TestClient1.java": Error #: 300 : class CreateException not found in class test.ejb1TestClient1 at line 162, column 17
"ejb1TestClient1.java": Error #: 300 : class Enterprise1TestClient1 not found in class test.ejb1TestClient1 at line 157, column 13
"ejb1TestClient1.java": Error #: 300 : class Enterprise1TestClient1 not found in class test.ejb1TestClient1 at line 157, column 49
"ejb1TestClient1.java": Error #: 300 : class Enterprise1 not found in class test.ejb1TestClient1 at line 158, column 13
"ejb1TestClient1.java": Error #: 556 : exception java.rmi.RemoteException is never thrown in the corresponding try block at line 160, column 10
"demo1.ejbgrpx": Spaces in the temporary directory path may cause WebLogic EJBC utility to produce fatal compile errors.
"ejb1TestClient1.java": Error #: 300 : class Enterprise1TestClient1 not found in class test.ejb1TestClient1 at line 157, column 13
"ejb1TestClient1.java": Error #: 300 : class Enterprise1TestClient1 not found in class test.ejb1TestClient1 at line 157, column 49
"ejb1TestClient1.java": Error #: 300 : class Enterprise1 not found in class test.ejb1TestClient1 at line 158, column 13
"ejb1TestClient1.java": Error #: 556 : exception java.rmi.RemoteException is never thrown in the corresponding try block at line 160, column 10
"demo1.ejbgrpx": Spaces in the temporary directory path may cause WebLogic EJBC utility to produce fatal compile errors.
解决方案 »
- struts2.1 xml 验证的错误信息总重复显示, why? 急!
- 通过路径读取XML
- 端口争用问题,一定给分
- AJAX刷新多个DIV中的内容问题????
- Can't load server.xml
- 求救
- jini中的RMI的问题,急!
- 請問高手﹕(WEBLOGIC7.0)中怎樣用(TX DATA SOURCES)連接(SQL SERVER 2000)?
- 我不知道这里怎么配置了,我用的是jboss,
- hibernate.hbm2ddl.auto 如何自动建数据库
- Session Bean中得到ResultSet对象(内含三个字段),怎样方便的传到前台jsp中?
- JBuilder7+Weblogic7中部署第一个Entity Bean出现问题
测试Entity Bean创建客户端,测试Entity Bean
利用JBuilder自动生成测试代码框架:通过菜单选择 File -> New -> Enterprise页 -> 双击EJB Test Client。修改测试代码:
把原来的main()函数,改为下面的形式:
public static void main(String[] args) {
TOperatorSesTestClient client = new TOperatorSesTestClient();
try {
client.create();
String opName = client.getOperatorName(new Integer("8"));
}
catch (Exception ex) {
ex.printStackTrace();
}
然后选中这个测试用的java,进行测试。测试用客户端代码分析
我们知道,客户端是通过调用Session Bean来调用Entity Bean的方法的,具体的实现或者说操作是什么样子的呢?下面是一个简要的分析,通过这个分析,我们可以清楚地知道客户端的运行机制,或者写出一个更为简单明了的客户端程序。相关情况:
----------
Entity Bean命名情况:
TOperator.java
TOperatorBean.java
TOperatorHome.java
Session Bean命名情况
TOperatorSes.java
TOperatorSesBean.java
TOperatorSesHome.java
测试用客户端程序为:
TOperatorSesTestClient.javaTOperatorSesTestClient.java程序中的方法及功能:
-----------------------------------------------
// 构造函数TOperatorSesTestclient(),创建客户端实例
public TOperatorSesTestClient()
// getInitialContext()初始化上下文,返回上下文信息
private Context getInitialContext() throws Exception {
// Methods that use Home interface methods to generate a Remote interface reference
// 使用Session Bean的Home产生Session Bean的远程接口,把这个远程接口做为参数返回。
public TOperatorSes create()
// 使用Session Bean的远程接口访问数据库中的数据。getOperatorName()是Client程序对
// Session Bean中的getOperatorName()方法的一个包裹方法。
public String getOperatorName(Integer operateId)
// 一个工具方法,用来打印日志信息
private void log(String message)
// main()方法,本程序的入口方法
public static void main(String[] args) TOperatorSesTestClient.java程序的运行机制:
-------------------------------------------
main方法:(入口方法)
* 调用TOperatorSesTestClient()方法,创建客户端的实例。
- 调用getInitialContext()方法初始命名空间上下文。
- 调用lookup()方法,通过JNDI名字查找资源。
- 调用PortableRemoteObject.narrow(),返回Session Bean的Home接口的引用。
---- TOperatorSesTestClient()方法的作用是:创建客户端实例,初始化成员变量一个Session Bean的Home接口。
* 调用客户端的create()方法。
- tOperatorSesHome.create()这一句返回了对Session Bean远程接口的引用tOperatorSes。
* 调用getOperatorName()方法,取得返回结果。
- tOperatorSes.getOperatorName(operateId);这个调用,调用了Session Bean中的getOperatorName()方法,执行了查询操作。
总结,客户端的整个过程是:
准备上下文,通过lookup()得出对象引用,进而取得Session Bean的Home接口变量。接着得到Session Bean的远程接口变量。然后用Session Bean的远程接口调用要使用的方法,完成操作。
TOperatorSesTestClient.java程序的源代码如下:
---------------------------------------------
package com.hs.webagent.ejb;import javax.naming.*;
import java.util.Properties;
import javax.rmi.PortableRemoteObject;public class TOperatorSesTestClient {
static final private String ERROR_NULL_REMOTE = "Remote interface reference is null. It must be created by calling one of the Home interface methods first.";
static final private int MAX_OUTPUT_LINE_LENGTH = 100;
private boolean logging = true;
private TOperatorSesHome tOperatorSesHome = null;
private TOperatorSes tOperatorSes = null; //Construct the EJB test client
public TOperatorSesTestClient() {
long startTime = 0;
if (logging) {
log("Initializing bean access.");
startTime = System.currentTimeMillis();
} try {
//get naming context
Context ctx = getInitialContext(); //look up jndi name
Object ref = ctx.lookup("TOperatorSes"); //cast to Home interface
tOperatorSesHome = (TOperatorSesHome) PortableRemoteObject.narrow(ref, TOperatorSesHome.class);
if (logging) {
long endTime = System.currentTimeMillis();
log("Succeeded initializing bean access.");
log("Execution time: " + (endTime - startTime) + " ms.");
}
}
catch(Exception e) {
if (logging) {
log("Failed initializing bean access.");
}
e.printStackTrace();
}
} private Context getInitialContext() throws Exception {
String url = "t3://1502chenxl:7001";
String user = null;
String password = null;
Properties properties = null;
try {
properties = new Properties();
properties.put(Context.INITIAL_CONTEXT_FACTORY, "weblogic.jndi.WLInitialContextFactory");
properties.put(Context.PROVIDER_URL, url);
if (user != null) {
properties.put(Context.SECURITY_PRINCIPAL, user);
properties.put(Context.SECURITY_CREDENTIALS, password == null ? "" : password);
} return new InitialContext(properties);
}
catch(Exception e) {
log("Unable to connect to WebLogic server at " + url);
log("Please make sure that the server is running.");
throw e;
}
} //----------------------------------------------------------------------------
// Methods that use Home interface methods to generate a Remote interface reference
//---------------------------------------------------------------------------- public TOperatorSes create() {
long startTime = 0;
if (logging) {
log("Calling create()");
startTime = System.currentTimeMillis();
}
try {
tOperatorSes = tOperatorSesHome.create();
if (logging) {
long endTime = System.currentTimeMillis();
log("Succeeded: create()");
log("Execution time: " + (endTime - startTime) + " ms.");
}
}
catch(Exception e) {
if (logging) {
log("Failed: create()");
}
e.printStackTrace();
} if (logging) {
log("Return value from create(): " + tOperatorSes + ".");
}
return tOperatorSes;
} //----------------------------------------------------------------------------
// Methods that use Remote interface methods to access data through the bean
//---------------------------------------------------------------------------- public String getOperatorName(Integer operateId) {
String returnValue = "";
if (tOperatorSes == null) {
System.out.println("Error in getOperatorName(): " + ERROR_NULL_REMOTE);
return returnValue;
}
long startTime = 0;
if (logging) {
log("Calling getOperatorName(" + operateId + ")");
startTime = System.currentTimeMillis();
} try {
returnValue = tOperatorSes.getOperatorName(operateId);
if (logging) {
long endTime = System.currentTimeMillis();
log("Succeeded: getOperatorName(" + operateId + ")");
log("Execution time: " + (endTime - startTime) + " ms.");
}
}
catch(Exception e) {
if (logging) {
log("Failed: getOperatorName(" + operateId + ")");
}
e.printStackTrace();
} if (logging) {
log("Return value from getOperatorName(" + operateId + "): " + returnValue + ".");
}
return returnValue;
} //----------------------------------------------------------------------------
// Utility Methods
//---------------------------------------------------------------------------- private void log(String message) {
if (message == null) {
System.out.println("-- null");
return ;
}
if (message.length() > MAX_OUTPUT_LINE_LENGTH) {
System.out.println("-- " + message.substring(0, MAX_OUTPUT_LINE_LENGTH) + " ...");
}
else {
System.out.println("-- " + message);
}
}
//Main method public static void main(String[] args) {
TOperatorSesTestClient client = new TOperatorSesTestClient();
try {
client.create();
Stri
public static void main(String[] args) {
try {
Enterprise1TestClient1 client = new Enterprise1TestClient1();
Enterprise1 remote=client.create();
System.out.print(remote.helloWorld("Demo one"));
}catch (Exception ex) {
ex.printStackTrace();
}
}