jb7、weblogic环境下开发实体bean遇到jndi问题?
ltz 2003-03-20 04:59:33 CustomersBean 实体bean
CustomersRemoteHome 本地接口
CustomersRemote 远程接口(jndi名称:MyCustomersRemote)
启动服务器,发布ejb后,
运行测试程序:
public class CustomersTestClient
{
private CustomersRemoteHome customersRemoteHome = null;
//Construct the EJB test client
public CustomersTestClient()
{
try
{
//get naming context
Context ctx = getInitialContext();
//look up jndi name
Object ref = ctx.lookup("MyCustomersRemote");
//cast to Home interface
customersRemoteHome = (CustomersRemoteHome) PortableRemoteObject.narrow(ref, CustomersRemoteHome.class);
}
catch(Exception e)
{
e.printStackTrace();
}
}
private Context getInitialContext() throws Exception
{
String url = "t3://beelink-weihai: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)
{
System.out.println("Unable to connect to WebLogic server at " + url);
System.out.println("Please make sure that the server is running.");
throw e;
}
}
//----------------------------------------------------------------------------
// Utility Methods
//----------------------------------------------------------------------------
public CustomersRemoteHome getHome()
{
return customersRemoteHome;
}
//Main method
public static void main(String[] args)
{
try
{
CustomersTestClient client = new CustomersTestClient();
// Use the getHome() method of the client object to call Home interface
// methods that will return a Remote interface reference. Then
// use that Remote interface reference to access the EJB.
CustomersRemoteHome home=client.getHome();
CustomersRemote r=home.findByPrimaryKey("12");
System.out.println(r.getAddr()+":"+r.getName());
}
catch(RemoteException ex)
{
ex.printStackTrace();
}
catch(FinderException ex)
{
ex.printStackTrace();
}
}
}
出现错误:
javax.naming.NameNotFoundException: Unable to resolve 'MyCustomersRemote' Resolved: '' Unresolved:'MyCustomersRemote' ; remaining name 'MyCustomersRemote'
at weblogic.rmi.internal.BasicOutboundRequest.sendReceive(BasicOutboundRequest.java:109)
at weblogic.rmi.cluster.ReplicaAwareRemoteRef.invoke(ReplicaAwareRemoteRef.java:262)
at weblogic.rmi.cluster.ReplicaAwareRemoteRef.invoke(ReplicaAwareRemoteRef.java:229)
at weblogic.jndi.internal.ServerNamingNode_WLStub.lookup(Unknown Source)
at weblogic.jndi.internal.WLContextImpl.lookup(WLContextImpl.java:337)
at weblogic.jndi.internal.WLContextImpl.lookup(WLContextImpl.java:332)
at javax.naming.InitialContext.lookup(InitialContext.java:345)
at demo3.com.CustomersTestClient.<init>(CustomersTestClient.java:21)
at demo3.com.CustomersTestClient.main(CustomersTestClient.java:73)
java.lang.NullPointerException
at demo3.com.CustomersTestClient.main(CustomersTestClient.java:78)
Exception in thread "main"