关于网友提出的“ Ejb中怎么访问远程接口函数?”问题疑问,本网通过在网上对“ Ejb中怎么访问远程接口函数?”有关的相关答案进行了整理,供用户进行参考,详细问题解答如下:
问题: Ejb中怎么访问远程接口函数?描述:
我用jbuider7按照书上的例子做了一个最简单的ejb程序,只声明了一个属性,myProperty,String类型.
然后用Ejb test Client 生成了一个客户端测试程序。
里面自动生成了main主函数。
我想知道怎么调用我的远程接口中的两个成员函数getmyProperty()和setmyProperty().
我现在是这样调用的,
public static void main(String[] args) {
myBean1TestClient1 client = new myBean1TestClient1();//系统自动生成的
client.create();//这里是我加的
String s = new String();//这里是我加的
s = client.getMyProperty();//这里是我加的
System.out.print(s);//这里是我加的
}
但是提示Remote interface reference is null. It must be created by calling one of the Home interface methods first."
下面是这个客户测试的部源码
package j7myejb;
import javax.naming.*;
import javax.rmi.PortableRemoteObject;
public class myBean1TestClient1 {
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 myBean1Home myBean1HomeObject = null;
private myBean1Remote myBean1RemoteObject = null;
//Construct the EJB test client
public myBean1TestClient1() {
long startTime = 0;
if (logging) {
log("Initializing bean access.");
startTime = System.currentTimeMillis();
}
try {
//get naming context
Context ctx = new InitialContext();
//look up jndi name
Object ref = ctx.lookup("myBean1");
//cast to Home interface
myBean1HomeObject = (myBean1Home) PortableRemoteObject.narrow(ref, myBean1Home.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();
}
}
//----------------------------------------------------------------------------
// Methods that use Home interface methods to generate a Remote interface reference
//----------------------------------------------------------------------------
public myBean1Remote create() {
long startTime = 0;
if (logging) {
log("Calling create()");
startTime = System.currentTimeMillis();
}
try {
myBean1RemoteObject = myBean1HomeObject.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(): " + myBean1RemoteObject + ".");
}
return myBean1RemoteObject;
}
//----------------------------------------------------------------------------
// Methods that use Remote interface methods to access data through the bean
//----------------------------------------------------------------------------
public String getMyProperty() {
String returnValue = "";
if (myBean1RemoteObject == null) {
System.out.println("Error in getMyProperty(): " + ERROR_NULL_REMOTE);
return returnValue;
}
long startTime = 0;
if (logging) {
log("Calling getMyProperty()");
startTime = System.currentTimeMillis();
}
try {
returnValue = myBean1RemoteObject.getMyProperty();
if (logging) {
long endTime = System.currentTimeMillis();
log("Succeeded: getMyProperty()");
log("Execution time: " + (endTime - startTime) + " ms.");
}
}
catch(Exception e) {
if (logging) {
log("Failed: getMyProperty()");
}
e.printStackTrace();
}
if (logging) {
log("Return value from getMyProperty(): " + returnValue + ".");
}
return returnValue;
}
public void setMyProperty(String myProperty) {
if (myBean1RemoteObject == null) {
System.out.println("Error in setMyProperty(): " + ERROR_NULL_REMOTE);
return ;
}
long startTime = 0;
if (logging) {
log("Calling setMyProperty(" + myProperty + ")");
startTime = System.currentTimeMillis();
}
try {
myBean1RemoteObject.setMyProperty(myProperty);
if (logging) {
long endTime = System.currentTimeMillis();
log("Succeeded: setMyProperty(" + myProperty + ")");
log("Execution time: " + (endTime - startTime) + " ms.");
}
}
catch(Exception e) {
if (logging) {
log("Failed: setMyProperty(" + myProperty + ")");
}
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) {
myBean1TestClient1 client = new myBean1TestClient1();//系统生成的
// 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.
///myBean1TestClient1.setMyProperty();
//myBean1RemoteObject.
client.create();//我自已加的
String s = new String();//我自已加的
s = client.getMyProperty();//我自已加的
System.out.print(s);//我自已加的
}
}
请兄弟们帮帮我,我这里怎么写才能成功通过ejb访问我的myProperty的值。
也就是说怎么调用setmyProperty,getmyProperty才能成功?
谢谢各位,
up的有分
解决方案1:
client.create();//我自已加的
String s = new String();//我自已加的
s = client.getMyProperty();//我自已加的
System.out.print(s);//我自已加的
===都是空值
你取的当然是空的了.