应用客户端使用@EJB注释和Maven在Glassfish上 [英] Application client using @EJB annotation and Maven on Glassfish

查看:249
本文介绍了应用客户端使用@EJB注释和Maven在Glassfish上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在NetBeans站点中有一个示例,请如何创建应用程序客户端,使用简单的项目(不含Maven)。需要4个项目(EJB,EAR,Lib,Program)。这个教程很简单,工作完美。



我想问一下如何使用Maven?我无法设法正确地获取所有依赖项,所以当我尝试调用EJB方法时,它会给我 NullPointerException 。任何人都可以告诉我,需要做的关键步骤(使用NetBeans)因为我很困惑,需要创建多少个项目?我知道,我需要应用程序项目,EAR和EJB项目吗?需要在这些项目中编写什么特殊的配置文件?p < b

我不想明确JNDI我想要使用@EJB注释。

解决方案

这里是步骤:


  1. 创建使用Maven的New Project菜单的文件夹保存界面类的Java类库。在Maven文件夹下选择Java应用程序。

  2. 按照NB的教程创建企业应用程序。唯一的区别是您必须使用Maven的新项目菜单中的文件夹

  3. 构建类库

  4. 确保类库是一个依赖关系企业应用程序。

  5. 运行Enterpise应用程序。 NB将部署到GF服务器

  6. 使用Maven的文件夹创建应用程序客户端。不要使用插入代码 NB的功能在此处注入Stateless EJB,因为它崩溃(至少在我的版本:NB 7.2)。相反,只需复制并粘贴教程中显示的代码。您不需要任何部署/ ejb描述符。

  7. 修改应用程序客户端的POM,以便使用maven-assembly-plugin来获取具有依赖关系的jar。如果您不执行此步骤,则部署将失败,因为GF无法加载接口类。将以下行添加到插件选项卡(根据需要更改主类):

     < plugin> 
    < artifactId> maven-assembly-plugin< / artifactId>
    < version> 2.4< / version>
    < configuration>
    < descriptorRefs>
    < descriptorRef> jar-with-dependencies< / descriptorRef>
    < / descriptorRefs>
    < archive>
    < manifest>
    < mainClass> com.entapp.entappclient.Main< / mainClass>
    < / manifest>
    < / archive>
    < / configuration>
    <执行>
    < execution>
    < id> make-assembly< / id>
    < phase> package< / phase>
    < goals>
    < goal> single< / goal>
    < / goals>
    < / execution>
    < / executions>
    < / plugin>


  8. 使用NB构建应用程序客户端项目


  9. 使用GF的应用程序客户端命令运行应用程序客户端: appclient -jar EntAppClient-1.0-SNAPSHOT-jar-with-dependencies.jar

有用的链接: Java EE的埋藏宝藏:应用程序客户端容器 by Jason Lee



重要说明



为了将客户机部署到其他JVMs 你必须在每个客户端机器上安装appclient 并设置target-server属性。 appclient似乎有一个非常复杂的结构,您不能简单地通过添加这些行(加上EclipseLink持久性工件)来生成:

 <依赖性> 
< groupId> org.glassfish.appclient< / groupId>
< artifactId> gf-client< / artifactId>
< version> 3.1.1< / version>
< type> pom< / type>
< scope> compile< / scope>
< / dependency>

将这些工件添加到客户端完美编译,但是jar不起作用。这是可以理解的,因为文件sun-acc.xml丢失(此文件是必需的,因为包含目标服务器属性)。 因此,我认为唯一的方法是根据链接的文档使用 package-appclient 脚本。


There is an example in NetBeans site how to create Application Client using simple projects (without Maven). There are 4 projects needed (EJB, EAR, Lib, Program). This tutorial is simple and works perfectly.

I want to ask how to do the same with Maven? I can't manage to get all the dependencies correctly so when I try to call EJB method, it gives me NullPointerException. Can anyone tell me, the key steps (preffered using NetBeans) that needs to be done? Because I am confused, about how many projects needs to be created? I know, that I need Application Project, EAR and EJB projects and thats it? What special configs needs to be written in these projects pom.xml files?

EDIT1:

I don't want to explicit JNDI I want to be able to use @EJB annotations.

解决方案

Here are the steps:

  1. Create the Java Class library for holding the interface class using Maven's folder of New Project's menu. Choose Java Application under Maven folder.
  2. Create the Enterprise Application following the NB's tutorial. The only difference is that you have to use Maven's folder of New Project's menu
  3. Build the class library
  4. Ensure that the class library is a dependancy in the Enterprise Application.
  5. Run the Enterpise Application. NB will deploy it to GF server
  6. Create the Application Client by use of Maven's folder. Don't use the insert code NB's feature for injecting the Stateless EJB here, because it crashes (at least in my version: NB 7.2). Instead simply copy and paste the code shown in the tutorial. You don't need any deployment / ejb descriptor.
  7. Modify the application client's POM in order to use maven-assembly-plugin for obtaining a jar with dependencies. If you don't to this step, the deploy will fail because GF is not able to load the interface class. Add the following lines to the plugins tab (change the main class as appropriate):

        <plugin>
            <artifactId>maven-assembly-plugin</artifactId>
            <version>2.4</version>
            <configuration>
                <descriptorRefs>
                    <descriptorRef>jar-with-dependencies</descriptorRef>
                </descriptorRefs>
                <archive>
                    <manifest>
                        <mainClass>com.entapp.entappclient.Main</mainClass>
                    </manifest>
                </archive>
            </configuration>
            <executions>
                <execution>
                    <id>make-assembly</id> 
                    <phase>package</phase> 
                    <goals>
                        <goal>single</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    

  8. Build the application client project with NB

  9. Run the application client using the GF's application client command: appclient -jar EntAppClient-1.0-SNAPSHOT-jar-with-dependencies.jar

Useful link: Java EE's Buried Treasure: the Application Client Container by Jason Lee

Important Note

In order to deploy the client to oher JVMs you have to install the appclient on each client machine and set the target-server property. The appclient seems to have a very complicated structure, which you cannot produce simply by adding these lines (plus the EclipseLink persistence artifacts):

<dependency>
    <groupId>org.glassfish.appclient</groupId>
    <artifactId>gf-client</artifactId>
    <version>3.1.1</version>
    <type>pom</type>
    <scope>compile</scope>
</dependency>

Adding these artifacts to the client compiles perfectly but the jar doesn't work. And this is understandable, since the file sun-acc.xml is missing (this file is necessary because contains the target-server property). Therefore I think that the only way is using the package-appclient script as per the linked documentation.

这篇关于应用客户端使用@EJB注释和Maven在Glassfish上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆