我的测试程序是相同的,但在不同的客户端运行
测试程序入下
package sessbeann;import javax.naming.*;
import java.util.Properties;
import javax.rmi.PortableRemoteObject;
import javax.ejb.Handle;
import javax.ejb.EJBMetaData;
import javax.ejb.HomeHandle;
import java.rmi.RemoteException;
import javax.ejb.RemoveException;public class SeaaionBNTestClient1 extends Object {
private static final String ERROR_NULL_REMOTE = "Remote interface reference is null. It must be created by calling one of the Home interface methods first.";
private static final int MAX_OUTPUT_LINE_LENGTH = 100;
private boolean logging = true;
private SeaaionBNHome seaaionBNHome = null;
private SeaaionBN seaaionBN = null; //Construct the EJB test client
public SeaaionBNTestClient1() {
initialize();
} public void initialize() {
long startTime = 0;
if (logging) {
log("Initializing bean access.");
startTime = System.currentTimeMillis();
} try {
//get naming context
Context context = getInitialContext(); //look up jndi name
Object ref = context.lookup("SeaaionBN");
//look up jndi name and cast to Home interface
seaaionBNHome = (SeaaionBNHome) PortableRemoteObject.narrow(ref, SeaaionBNHome.class);
if (logging) {
long endTime = System.currentTimeMillis();
log("Succeeded initializing bean access through Home interface.");
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://ANUBIS:7001";
String user = "hexiaofeng";
String password = "12345678";
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 SeaaionBN create() {
long startTime = 0;
if (logging) {
log("Calling create()");
startTime = System.currentTimeMillis();
}
try {
seaaionBN = seaaionBNHome.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(): " + seaaionBN + ".");
}
return seaaionBN;
} public void remove(Handle p0) {
long startTime = 0;
if (logging) {
log("Calling remove(" + p0 + ")");
startTime = System.currentTimeMillis();
} try {
seaaionBNHome.remove(p0);
if (logging) {
long endTime = System.currentTimeMillis();
log("Succeeded: remove(" + p0 + ")");
log("Execution time: " + (endTime - startTime) + " ms.");
}
} catch (Exception e) {
if (logging) {
log("Failed: remove(" + p0 + ")");
}
e.printStackTrace();
}
} public void remove(Object p0) {
long startTime = 0;
if (logging) {
log("Calling remove(" + p0 + ")");
startTime = System.currentTimeMillis();
} try {
seaaionBNHome.remove(p0);
if (logging) {
long endTime = System.currentTimeMillis();
log("Succeeded: remove(" + p0 + ")");
log("Execution time: " + (endTime - startTime) + " ms.");
}
} catch (Exception e) {
if (logging) {
log("Failed: remove(" + p0 + ")");
}
e.printStackTrace();
}
} public EJBMetaData getEJBMetaData() {
EJBMetaData returnValue = null; long startTime = 0;
if (logging) {
log("Calling getEJBMetaData()");
startTime = System.currentTimeMillis();
} try {
returnValue = seaaionBNHome.getEJBMetaData();
if (logging) {
long endTime = System.currentTimeMillis();
log("Succeeded: getEJBMetaData()");
log("Execution time: " + (endTime - startTime) + " ms.");
}
} catch (Exception e) {
if (logging) {
log("Failed: getEJBMetaData()");
}
e.printStackTrace();
} if (logging) {
log("Return value from getEJBMetaData(): " + returnValue + ".");
}
return returnValue;
} public HomeHandle getHomeHandle() {
HomeHandle returnValue = null; long startTime = 0;
if (logging) {
log("Calling getHomeHandle()");
startTime = System.currentTimeMillis();
} try {
returnValue = seaaionBNHome.getHomeHandle();
if (logging) {
long endTime = System.currentTimeMillis();
log("Succeeded: getHomeHandle()");
log("Execution time: " + (endTime - startTime) + " ms.");
}
} catch (Exception e) {
if (logging) {
log("Failed: getHomeHandle()");
}
e.printStackTrace();
} if (logging) {
log("Return value from getHomeHandle(): " + returnValue + ".");
}
return returnValue;
} //----------------------------------------------------------------------------
// Methods that use Remote interface methods to access data through the bean
//---------------------------------------------------------------------------- public String getUserName() {
String returnValue = ""; if (seaaionBN == null) {
System.out.println("Error in getUserName(): " + ERROR_NULL_REMOTE);
return returnValue;
} long startTime = 0;
if (logging) {
log("Calling getUserName()");
startTime = System.currentTimeMillis();
} try {
returnValue = seaaionBN.getUserName();
if (logging) {
long endTime = System.currentTimeMillis();
log("Succeeded: getUserName()");
log("Execution time: " + (endTime - startTime) + " ms.");
}
} catch (Exception e) {
if (logging) {
log("Failed: getUserName()");
}
e.printStackTrace();
} if (logging) {
log("Return value from getUserName(): " + returnValue + ".");
}
return returnValue;
}
测试程序入下
package sessbeann;import javax.naming.*;
import java.util.Properties;
import javax.rmi.PortableRemoteObject;
import javax.ejb.Handle;
import javax.ejb.EJBMetaData;
import javax.ejb.HomeHandle;
import java.rmi.RemoteException;
import javax.ejb.RemoveException;public class SeaaionBNTestClient1 extends Object {
private static final String ERROR_NULL_REMOTE = "Remote interface reference is null. It must be created by calling one of the Home interface methods first.";
private static final int MAX_OUTPUT_LINE_LENGTH = 100;
private boolean logging = true;
private SeaaionBNHome seaaionBNHome = null;
private SeaaionBN seaaionBN = null; //Construct the EJB test client
public SeaaionBNTestClient1() {
initialize();
} public void initialize() {
long startTime = 0;
if (logging) {
log("Initializing bean access.");
startTime = System.currentTimeMillis();
} try {
//get naming context
Context context = getInitialContext(); //look up jndi name
Object ref = context.lookup("SeaaionBN");
//look up jndi name and cast to Home interface
seaaionBNHome = (SeaaionBNHome) PortableRemoteObject.narrow(ref, SeaaionBNHome.class);
if (logging) {
long endTime = System.currentTimeMillis();
log("Succeeded initializing bean access through Home interface.");
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://ANUBIS:7001";
String user = "hexiaofeng";
String password = "12345678";
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 SeaaionBN create() {
long startTime = 0;
if (logging) {
log("Calling create()");
startTime = System.currentTimeMillis();
}
try {
seaaionBN = seaaionBNHome.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(): " + seaaionBN + ".");
}
return seaaionBN;
} public void remove(Handle p0) {
long startTime = 0;
if (logging) {
log("Calling remove(" + p0 + ")");
startTime = System.currentTimeMillis();
} try {
seaaionBNHome.remove(p0);
if (logging) {
long endTime = System.currentTimeMillis();
log("Succeeded: remove(" + p0 + ")");
log("Execution time: " + (endTime - startTime) + " ms.");
}
} catch (Exception e) {
if (logging) {
log("Failed: remove(" + p0 + ")");
}
e.printStackTrace();
}
} public void remove(Object p0) {
long startTime = 0;
if (logging) {
log("Calling remove(" + p0 + ")");
startTime = System.currentTimeMillis();
} try {
seaaionBNHome.remove(p0);
if (logging) {
long endTime = System.currentTimeMillis();
log("Succeeded: remove(" + p0 + ")");
log("Execution time: " + (endTime - startTime) + " ms.");
}
} catch (Exception e) {
if (logging) {
log("Failed: remove(" + p0 + ")");
}
e.printStackTrace();
}
} public EJBMetaData getEJBMetaData() {
EJBMetaData returnValue = null; long startTime = 0;
if (logging) {
log("Calling getEJBMetaData()");
startTime = System.currentTimeMillis();
} try {
returnValue = seaaionBNHome.getEJBMetaData();
if (logging) {
long endTime = System.currentTimeMillis();
log("Succeeded: getEJBMetaData()");
log("Execution time: " + (endTime - startTime) + " ms.");
}
} catch (Exception e) {
if (logging) {
log("Failed: getEJBMetaData()");
}
e.printStackTrace();
} if (logging) {
log("Return value from getEJBMetaData(): " + returnValue + ".");
}
return returnValue;
} public HomeHandle getHomeHandle() {
HomeHandle returnValue = null; long startTime = 0;
if (logging) {
log("Calling getHomeHandle()");
startTime = System.currentTimeMillis();
} try {
returnValue = seaaionBNHome.getHomeHandle();
if (logging) {
long endTime = System.currentTimeMillis();
log("Succeeded: getHomeHandle()");
log("Execution time: " + (endTime - startTime) + " ms.");
}
} catch (Exception e) {
if (logging) {
log("Failed: getHomeHandle()");
}
e.printStackTrace();
} if (logging) {
log("Return value from getHomeHandle(): " + returnValue + ".");
}
return returnValue;
} //----------------------------------------------------------------------------
// Methods that use Remote interface methods to access data through the bean
//---------------------------------------------------------------------------- public String getUserName() {
String returnValue = ""; if (seaaionBN == null) {
System.out.println("Error in getUserName(): " + ERROR_NULL_REMOTE);
return returnValue;
} long startTime = 0;
if (logging) {
log("Calling getUserName()");
startTime = System.currentTimeMillis();
} try {
returnValue = seaaionBN.getUserName();
if (logging) {
long endTime = System.currentTimeMillis();
log("Succeeded: getUserName()");
log("Execution time: " + (endTime - startTime) + " ms.");
}
} catch (Exception e) {
if (logging) {
log("Failed: getUserName()");
}
e.printStackTrace();
} if (logging) {
log("Return value from getUserName(): " + returnValue + ".");
}
return returnValue;
}
解决方案 »
- myeclipse 折叠代码注解是什么
- 请教关于spring的ibatisTemplate问题?
- 100分求助:org.hibernate.exception.GenericJDBCException: could not execute query
- 为什么tomcat进入admin管理界面不能给某个context加个log??
- 基于struts+hibernate框架开发的WEB程序发布时,登录都过不了,急死人了!
- java的大数据量处理,使用ibatis插入500万条数据,效率低,如何解决
- 请教java和delphi的socket连接
- DatagramSocket发送的报文一般控制在多大比较合适
- 谈谈J2EE(马上给分)
- 关于LOCAL接口和REMOTE接口的同时兼顾的问题
- 各位大侠,请教一下,在jsp中怎样使用RMI,在线等
- 我struts中用了一个<logic:greaterThan name="user" 标签但是提示找不到bean
if (seaaionBN == null) {
System.out.println("Error in setUserName(): " + ERROR_NULL_REMOTE);
return;
} long startTime = 0;
if (logging) {
log("Calling setUserName(" + uname + ")");
startTime = System.currentTimeMillis();
} try {
seaaionBN.setUserName(uname);
if (logging) {
long endTime = System.currentTimeMillis();
log("Succeeded: setUserName(" + uname + ")");
log("Execution time: " + (endTime - startTime) + " ms.");
}
} catch (Exception e) {
if (logging) {
log("Failed: setUserName(" + uname + ")");
}
e.printStackTrace();
}
} //----------------------------------------------------------------------------
// 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) throws RemoteException, RemoveException, RemoteException {
SeaaionBNTestClient1 client = new SeaaionBNTestClient1();
SeaaionBN r = client.create();
String uname = r.getUserName();
System.out.println(uname);
r.setUserName("AAA"); //不同的客户端set值不同
uname = r.getUserName();
System.out.println(uname);
r.remove(); // Use the client object to call one of the Home interface wrappers
// above, to create a Remote interface reference to the bean.
// If the return value is of the Remote interface type, you can use it
// to access the remote interface methods. You can also just use the
// client object to call the Remote interface wrappers.
}
}