EJB 3.1的JNDI名称在JBoss 7上运行 [英] JNDI name for EJB 3.1 running on JBoss 7
问题描述
我尝试测试的EJB是:Calculator。当我在JBoss 7上部署它时,显示服务器控制台中的下一行:
java:global / CalculadoraProject / Calculator!demo。 CalculatorRemote
java:app / CalculadoraProject / Calculator!demo.CalculatorRemote
java:module / Calculator!demo.CalculatorRemote
java:jboss / exports / CalculadoraProject / Calculator!demo.CalculatorRemote
java:全局/ CalculadoraProject / Calculator
java:app / CalculadoraProject / Calculator
java:模块/计算器
计算器bean代码是这样的:
包演示;
import javax.ejb.Stateless;
@Stateless
public class Calculator implements CalculatorRemote
{
@Override
public double sum(double a,double b)
{
返回a + b;
}
}
远程接口代码是:
包演示;
import javax.ejb.Remote;
@Remote
public interface CalculatorRemote
{
public double sum(double a,double b);
}
必须通过JNDI连接EJB的主要代码是:
包演示;
import java.util.Properties;
import javax.naming.Context;
import javax.naming.InitialContext;
public class Test
{
public static void main(String [] args)throws异常
{
上下文ctx = getInitialContext();
String jndiName =???
CalculatorRemote c =(CalculatorRemote)ctx.lookup(jndiName);
System.out.println(c.sum(2,2));
}
private static上下文getInitialContext()throws异常
{
属性p = new Properties();
p.put(java.naming.factory.initial,org.jboss.naming.remote.client.InitialContextFactory);
p.put(java.naming.provider.url,remote:// localhost:4447);
返回新的InitialContext(p);
}
}
第一个错误是:身份验证失败。所以,我在服务器中创建一个用于ApplicationRealm的管理用户用户:usr:test,pwd:test123然后修改getInitialContext()看起来像这样:
属性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:????????
所以,我想问:我必须用什么名字来查找我的Calculator bean?
谢谢!
1)通过使用全局JNDI名称。
java:global / CalculadoraProject / Calculator!demo.CalculatorRemote
2 )通过使用ejb JNDI名称。如果您仍然收到错误,请稍加斜线。应该是正确的,如果你有所需的libs,并没有语法错误在其他代码。
ejb:/ CalculadoraProject /Calculator!demo.CalculatorRemote
JNDI结构
对于无状态bean: p>
ejb:< app-name> /< module-name> /< distinct-name> /< bean-name> ;!<完全限定-类名的最远程接口>
对于有状态的bean:
EJB:其中APP-名称> /<模块名称> /<不同-名称> /<豆名称><完全限定-类名的最远程! -interface>?stateful
值得阅读的链接:
< a href =https://docs.jboss.org/author/display/AS71/EJB+invocations+from+a+remote+client+using+JNDI =nofollow> https://docs.jboss。 org / author / display / AS71 / EJB + invocations + from + a + remote + client + using + JNDI
https://docs.jboss.org/作者/显示/ AS71 /远程+ EJB +调用+通过+ JNDI + - + EJB +客户端+ API +或+远程命名+项目
希望这有助于:)
I'm trying to run this EJB example on JBoss 7 but it always throws NameNotFoundException.
The EJB that I try to test is: Calculator. When I deploy it on JBoss 7 show this next lines in the server console:
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
The Calculator bean code is this:
package demo;
import javax.ejb.Stateless;
@Stateless
public class Calculator implements CalculatorRemote
{
@Override
public double sum(double a, double b)
{
return a+b;
}
}
And the remote interface code is:
package demo;
import javax.ejb.Remote;
@Remote
public interface CalculatorRemote
{
public double sum(double a,double b);
}
The main code which must connect the EJB through JNDI is:
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);
}
}
First error is: Authentication failed. So, I create an "Management User" user for "ApplicationRealm" into the server: usr: test, pwd: test123 and then modify getInitialContext() to look like this:
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");
The next error is: javax.naming.NameNotFoundException: ?????????????
So, I want ask: What name must I use to lookup my Calculator bean?
Thanks!
1) by using global JNDI name.
java:global/CalculadoraProject/Calculator!demo.CalculatorRemote
2) by using ejb JNDI name. Play with slashes a little if you still get the error. Should be correct if you have the required libs and don't have syntax errors in other places of code.
ejb:/CalculadoraProject/Calculator!demo.CalculatorRemote
JNDI structure
For stateless beans:
ejb:<app-name>/<module-name>/<distinct-name>/<bean-name>!<fully-qualified-classname-of-the-remote-interface>
For stateful beans:
ejb:<app-name>/<module-name>/<distinct-name>/<bean-name>!<fully-qualified-classname-of-the-remote-interface>?stateful
Links worth reading:
https://docs.jboss.org/author/display/AS71/EJB+invocations+from+a+remote+client+using+JNDI
https://docs.jboss.org/author/display/AS71/Remote+EJB+invocations+via+JNDI+-+EJB+client+API+or+remote-naming+project
Hope this helped :)
这篇关于EJB 3.1的JNDI名称在JBoss 7上运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!