我试图运行在JBoss 7这个EJB的例子,但它总是抛出的NameNotFoundException。
我尝试测试的EJB是:计算器。 当我部署它在JBoss 7显示在服务器控制台这下一行:
java:global/CalculadoraProject/Calculator!demo.CalculatorRemote
java:app/CalculadoraProject/Calculator!demo.CalculatorRemote
java:module/Calculator!demo.CalculatorRemote
java:jboss/exported/CalculadoraProject/Calculator!demo.CalculatorRemote
java:global/CalculadoraProject/Calculator
java:app/CalculadoraProject/Calculator
java:module/Calculator
该计算器豆代码是这样的:
package demo;
import javax.ejb.Stateless;
@Stateless
public class Calculator implements CalculatorRemote
{
@Override
public double sum(double a, double b)
{
return a+b;
}
}
而远程接口的代码是:
package demo;
import javax.ejb.Remote;
@Remote
public interface CalculatorRemote
{
public double sum(double a,double b);
}
必须通过JNDI连接EJB的主要代码是:
package demo;
import java.util.Properties;
import javax.naming.Context;
import javax.naming.InitialContext;
public class Test
{
public static void main(String[] args) throws Exception
{
Context ctx=getInitialContext();
String jndiName="?????????????";
CalculatorRemote c = (CalculatorRemote)ctx.lookup(jndiName);
System.out.println( c.sum(2,2) );
}
private static Context getInitialContext() throws Exception
{
Properties p=new Properties();
p.put("java.naming.factory.initial","org.jboss.naming.remote.client.InitialContextFactory");
p.put("java.naming.provider.url","remote://localhost:4447");
return new InitialContext(p);
}
}
第一个错误是:验证失败。 所以,我创建一个“管理用户”用户的“ApplicationRealm”到服务器:USR:测试密码:test123然后修改getInitialContext()看起来像这样:
Properties p=new Properties();
p.put("java.naming.factory.initial","org.jboss.naming.remote.client.InitialContextFactory");
p.put("java.naming.provider.url","remote://localhost:4447");
p.put(Context.SECURITY_PRINCIPAL,"test");
p.put(Context.SECURITY_CREDENTIALS,"test123");
下一个错误是:javax.naming.NameNotFoundException:?????????????
所以,我要问:我必须使用哪种名称查找我的计算器豆?
谢谢!