简体   繁体   English

在EAR客户端中创建EJB远程引用

[英]Creating EJB remote references into a client witout EAR

Well, this is my development environment: 好吧,这是我的开发环境:

  • I am using Java 1.8 and Eclipse 4.5.1 (Mars), J2EE tools and plugins installed to work with WildFly 8 as Application Server 我正在使用Java 1.8和Eclipse 4.5.1(Mars),J2EE工具和已安装的插件,以将WildFly 8用作应用程序服务器

  • I created a Java EJB project, with a simple HelloWorld stateless session bean class. 我创建了一个Java EJB项目,其中包含一个简单的HelloWorld无状态会话bean类。 It implements 2 interfaces: @Remote and/or @Local, where I defined a String getHelloWorld() stub. 它实现2个接口:@Remote和/或@Local,在其中定义了String getHelloWorld()存根。

  • Now, I have my client for consuming the EJB: A Dynamic Web Project with one Servlet. 现在,我有了使用EJB的客户端:具有一个Servlet的动态Web项目。 Inside the servlet, I can inject the ejb class using annotations, like this: 在Servlet内,我可以使用注释注入ejb类,如下所示:

@EJB private HelloWorldLocal bean; @EJB私有HelloWorldLocal bean; or @EJB private HelloWorldRemote bean; 或@EJB私有HelloWorldRemote bean;

As you see, I declared the bean as HelloWorldLocal/HelloWorldRemote types. 如您所见,我将bean声明为HelloWorldLocal / HelloWorldRemote类型。 However, if I want to deploy and run my application, I need to put the EJB and Web projects into an EAR first. 但是,如果要部署和运行应用程序,则需要首先将EJB和Web项目放入EAR。 That allows the Servlet to know and compile the HelloWorldLocal or HelloWorldRemote bean types, by simply adding the EJB project on the Build Path as Project, or even by putting the EJB project as a Deployment Assembly directive. 通过简单地将EJB项目作为项目添加到Build Path上,或者甚至将EJB项目作为Deployment Assembly指令,Servlet即可知道并编译HelloWorldLocal或HelloWorldRemote bean类型。

I'd like to create a client outside the EAR (a remote Swing application or Remote WebSite). 我想在EAR(远程Swing应用程序或Remote WebSite)之外创建一个客户端。 That means my client will have not the chance to adding the EJB project interfaces as project references in the build path as I did with the EAR. 这意味着我的客户将没有机会像在EAR中一样将EJB项目接口添加为构建路径中的项目引用。 Even if I want to call the remote bean with JNDI, I need to cast the lookup() object to those types in order to use the bean methods. 即使我想用JNDI调用远程bean,我也需要将lookup()对象强制转换为那些类型,以便使用bean方法。

The question is: 问题是:

How can I get the HelloWorldRemote/HelloWorldLocal bean types from my remote client without EARs, if those interfaces are declared into an separate EJB Project? 如果将这些接口声明到单独的EJB项目中,那么如何从没有EAR的远程客户端中获取HelloWorldRemote / HelloWorldLocal Bean类型? (Of course, I dont want to create a .jar with the EJB project here). (当然,我不想在这里用EJB项目创建.jar)。

And what about portable JNDI lookup? 那么可移植的JNDI查找呢? https://docs.oracle.com/cd/E19798-01/821-1841/girgn/index.html It should work if the EJB in the same JVM as your client. https://docs.oracle.com/cd/E19798-01/821-1841/girgn/index.html如果EJB与您的客户端在同一JVM中,则它应该可以工作。

I'm not sure I completely understand what you're asking, and I haven't done this in years, but the normal approach for this would to to create an EJB client JAR containing only the client's bean dependencies and remote interfaces. 我不确定我是否完全理解您的要求,并且多年以来都没有做到这一点,但是通常的方法是创建仅包含客户端的bean依赖项和远程接口的EJB客户端JAR To best accomplish this, you'll want to use a tool such as Maven to build your project which contains an EJB plugin for this . 为了最好地做到这一点,您需要使用Maven之类的工具来构建您的项目,该项目包含为此EJB插件 If you're not using Maven, you're going to be limited by the tooling within Eclipse, though at one point it did contain EJB client generation support. 如果您不使用Maven,那么您将受到Eclipse中工具的限制,尽管有时它确实包含EJB客户端生成支持。 The Wildfly documentation on EJB client setup might help also, but explicitly mentions that packaging the client is out of scope. 关于EJB客户端设置的Wildfly文档可能也有帮助,但是明确提到打包客户端超出了范围。

I'd like to share the only one solution that I found: I have to create a .jar file, with all the EJB interfaces (@Remote, @Local, and so on), and use it like referenced types on my remote clients. 我想分享我发现的唯一解决方案:我必须创建一个.jar文件,其中包含所有EJB接口(@ Remote,@ Local等),并像在远程客户端上引用类型一样使用它。

This .jar would be outside any EAR. 该.jar将在任何EAR之外。 however, it needs jboss-client.jar because of the @ annotations. 但是,由于@注释,它需要jboss-client.jar。 So, I packaged all, interfaces and jboss-client.jar, exported them to a .jar and I added this new file in my EJB and clients projects as external jar! 因此,我将所有接口和jboss-client.jar打包,将它们导出到.jar,然后将此新文件添加到EJB和客户端项目中作为外部jar! Now I am able to use the HelloWorld* types on EJB classes implementations and @EJB client variables. 现在,我可以在EJB类实现和@EJB客户端变量上使用HelloWorld *类型。

If I want a remote Web client, I've placed the EJB project with a Web Project module, into a EAR - Definitely we need an EAR. 如果需要远程Web客户端,则将带有Web Project模块的EJB项目放置到EAR中-肯定需要EAR。 The Web module has Servlets, and the servlets handles the @EJB variables and responses. Web模块具有Servlet,而Servlet处理@EJB变量和响应。 So, Outside this EAR, I just need to use URL's to the servlets and, you got it: EJB method results are reached from remote web clients. 因此,在这个EAR之外,我只需要对servlet使用URL,就可以了:EJB方法结果是从远程Web客户端获得的。

In desktop applications with JNDI, I just put the interfaces .jar like external jar into the client project. 在具有JNDI的桌面应用程序中,我只是将接口.jar(如外部jar)放入客户端项目中。 In that way, I can cast the .lookup() to HelloWorld* types and using its methods. 这样,我可以将.lookup()转换为HelloWorld *类型并使用其方法。

  • I hope it helps. 希望对您有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM