我是ejb的新手,我正在尝试运行简单的ejb应用程序。我的应用程序包括类Hello(远程接口),HelloBean(实现Hello接口的类)和HelloClient(我运行)。程序应该写入控制台" Hello world"但是我总是收到错误" Name [HelloBeanName] is not bound in this Context. Unable to find [HelloBeanName]
" ...这是3个班级
Hello Interface:
package vezba2;
public interface Hello {
public String hello();
}
为helloBean:
package vezba2;
import javax.ejb.Remote;
import javax.ejb.Stateless;
@Stateless(name="HelloBeanName")
@Remote(Hello.class)
public class HelloBean implements Hello{
@Override
public String hello() {
// TODO Auto-generated method stub
System.out.println("hello()");
return "Hello,World!";
}
}
HelloClient:
package vezba2;
import java.util.Properties;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
public class HelloClient {
public static void main(String[] args) throws NamingException {
// TODO Auto-generated method stub
System.setProperty(Context.INITIAL_CONTEXT_FACTORY,
"org.apache.naming.java.javaURLContextFactory");
//i first didn't have system.setProperty and i was getting error:Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file: java.naming.factory.initial//
Context ctx = new InitialContext();
Hello hello=(Hello) ctx.lookup("HelloBeanName");
System.out.println(hello.hello());
}
}
我使用java8,ApacheTomee加上1.7.0作为服务器,在服务器文件夹中我有文件server.xml,context.xml文件,tomcat-users.xml,web.xml文件,在我的项目中我有ejb -jar.xml,并没有我没有编辑和更改过的..我也运行这个应用程序"运行HelloClient"在eclipse中的绿色箭头上。(当我启动服务器时,它并没有提供在服务器上运行它...)
是否有人知道这个问题,我怎么能让我的程序工作?
我应该编辑xml文件吗?我应该尝试在服务器上运行我的应用程序吗?(某种程度上)...
是否还有一些关于如何运行和使用ejb的详细文献?谢谢advance.`
(P.S。当我把它放在HelloClient中时:
System.setProperty(Context.INITIAL_CONTEXT_FACTORY,"org.apache.openejb.client.RemoteInitialContextFactory"); System.setProperty(Context.PROVIDER_URL,"http://localhost:8080/tomee/ejb");
Context ctx = new InitialContext();
Hello hello=(Hello) ctx.lookup("HelloBeanName");
System.out.println(hello.hello());
我收到以下错误:
RequestFailed{server=http://localhost:8080/tomee/ejb} JNDI_LOOKUPnull:/HelloBeanName {error=Cannot open input stream to server: }
Exception in thread "main" org.apache.openejb.client.ClientRuntimeException: Invalid response from server: -1
at org.apache.openejb.client.JNDIContext.lookup(JNDIContext.java:297)
at javax.naming.InitialContext.lookup(InitialContext.java:417)
at vezba2.HelloClient.main(HelloClient.java:18)
现在我想我连接了apache服务器和运行HelloClient的程序..)
答案 0 :(得分:0)
如果您的主要不在容器中,则需要指定如何连接到服务器。对于远程上下文工厂,在那里解释http://tomee.apache.org/advanced/client/jndi.html
附注:自版本1.7.4和7.x以来,远程处理需要更多配置以避免0天漏洞。见http://tomee.apache.org/security/tomee.html