3

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.

Minutis
  • 1,193
  • 1
  • 17
  • 47
  • "No JNDI but EJB" ... sounds to me like "I want to run a java program, but without JVM". Even if you don't manipulate it explicitly, JNDI is required when using EJB. – ben75 Mar 17 '13 at 14:20
  • Yes sorry, my fault, I edited my answer. – Minutis Mar 17 '13 at 14:26
  • Can you be more specific about the NPE you are getting? Is it caused by a missing dependancy (i.e. your appclient jar missing the dependancy to the interface class)? If yes, you get an error during deployment of the appclient jar. – perissf Mar 18 '13 at 09:48
  • NPE is really simple, `@EJB private static TestBeanRemote testBean;` stays null, does not get injected I think. This is the trace: `Exception in thread "main" java.lang.NullPointerException` and second line points to the line i mentioned previously. – Minutis Mar 27 '13 at 08:38

2 Answers2

3

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.

perissf
  • 15,979
  • 14
  • 80
  • 117
  • 1
    Yes I have tried it and it does not work. It is strange to me, why should I run Application Client using GF command (9 step). If I distribute app client to other users, they will not have GF installed in their computers and therefore will not be able to use it. Also the dependencies at step 4 is little unclear, isn't it the other way around? Because if I do the dependency way you descibed, then when creating bean and specifying Lib project as Remote it says 'Cyclic project dependencies'. – Minutis Mar 27 '13 at 08:29
  • Sure. I have rephrased point 4 and added links to the guide for distributing the client application to other client machines without GlassFish – perissf Mar 27 '13 at 09:06
  • Your links are basically good, but could you edit your nine step or extend the guide? Because, the info in those link is pretty abstract, I mean there are much GF commands, which I do not need to use if I develop App Client without maven. – Minutis Mar 27 '13 at 10:23
  • 1
    Your answer is perfect :) I just reviewed it once more and everything worked. I will edit your answer a little, since, if there are more newbies like me, it will be easier for them to understand. – Minutis Apr 01 '13 at 12:55
  • where can I find more information about `package-appclient`? – Thufir Oct 12 '14 at 04:40
0

There is a useful EJB FAQ which mention about how to use the @EJB to access the remote EJB by using the ejb-ref together with sun-web.xml or now it is a glassfish-web.xml as the following link: -

What if I have multiple instances of the Appserver running and I want to access a Remote EJB component between them?

If you would like to compare between ejb-ref and ejb-local-ref, you may see further information at What is the relationship between @EJB and ejb-ref/ejb-local-ref?

I hope this may help.

Charlee Chitsuk
  • 8,847
  • 2
  • 56
  • 71
  • The problem itself lies not in the @EJB annotation, but in Maven's dependencies I think. Because, without maven everything works fine. – Minutis Mar 27 '13 at 08:37