Java 如何从应用程序客户端连接远程 EJB 模块

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2986993/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 15:17:34  来源:igfitidea点击:

How to connect remote EJB module from application client

javanetbeansjakarta-eejava-ee-6

提问by Zeck

I have a EJB module in remote Glassfish server and application client in my computer. I want to connect from the application client to the remote EJB.

我的计算机中的远程 Glassfish 服务器和应用程序客户端中有一个 EJB 模块。我想从应用程序客户端连接到远程 EJB。

Here is the my EJB interface:

这是我的 EJB 接口:

@Remote
public interface BookEJBRemote
{
    public String getTitle();
}

Here is the my ejb:

这是我的 ejb:

@Stateless
public class BookEJB implements BookEJBRemote
{

    @Override
    public String getTitle()
    {
        return "Twenty Thousand Leagues Under the Sea";
    }
}

I have several questions :

我有几个问题:

  1. Can I use Dependency Injection in the remote application client to connect to the ejb? If so what can i do to achieve this. Do i need to configure in the sun-ejb-jar.xml and sun-application-client.xml? In other words, if i use Dependency Injection like @EJB BookEJBRemote book; How application client container know what ejb to be injected? Where should i specify the information?
  2. How can i run the application client? I tried to run package-appclient in the glassfish server to get appclient.jar and copy it to my computer. Then i type appclient.jar -client myAppClient.jar . It didn't work. How do i point the target server?
  3. if i cannot use Dependency Injection in the client then i guess i have to use JNDI lookup. Do i need to configure jndi name in sun-ejb-jar.xml or in the sun-application-client.xml?
  1. 我可以在远程应用程序客户端中使用依赖注入连接到 ejb 吗?如果是这样,我该怎么做才能实现这一目标。我需要在sun-ejb-jar.xml 和sun-application-client.xml 中配置吗?换句话说,如果我像@EJB BookEJBRemote book那样使用依赖注入;应用程序客户端容器如何知道要注入什么 ejb?我应该在哪里指定信息?
  2. 如何运行应用程序客户端?我尝试在 glassfish 服务器中运行 package-appclient 以获取 appclient.jar 并将其复制到我的计算机。然后我输入 appclient.jar -client myAppClient.jar 。它没有用。我如何指向目标服务器?
  3. 如果我不能在客户端使用依赖注入,那么我想我必须使用 JNDI 查找。我是否需要在 sun-ejb-jar.xml 或 sun-application-client.xml 中配置 jndi 名称?

No matter how i try i never manage to run application client ? Can you guys put some working example? And thank you for every advises and examples?

无论我如何尝试,我都无法运行应用程序客户端?你们能举一些有效的例子吗?谢谢你的每一个建议和例子?

采纳答案by stacker

Check this tutorial Creating a Java Stand-Alone Client. Basically you need to

检查本教程创建 Java 独立客户端。基本上你需要

  • setup JNDI by yourself since your client can't use the containers environment.
  • Lookup the remote interface.
  • And of course include the required jar files in your classpath
  • 自己设置 JNDI,因为您的客户端不能使用容器环境。
  • 查找远程接口。
  • 当然,在类路径中包含所需的 jar 文件