How to create a remote EJB session from a client

According to the Netbeans Tutorial on EJB Client Applications , I cannot call the method:

Compilation Error:

-do-compile:
    [mkdir] Created dir: /home/thufir/NetBeansProjects/EntAppClient/build/empty
    [mkdir] Created dir: /home/thufir/NetBeansProjects/EntAppClient/build/generated-sources/ap-source-output
    [javac] Compiling 1 source file to /home/thufir/NetBeansProjects/EntAppClient/build/jar
    [javac] /home/thufir/NetBeansProjects/EntAppClient/src/java/entappclient/Main.java:16: error: cannot find symbol
    [javac]         System.err.println("result = " + mySession.getResult());
    [javac]                                                   ^
    [javac]   symbol:   method getResult()
    [javac]   location: variable mySession of type MySessionRemote
    [javac] 1 error

BUILD FAILED

customer:

package entappclient;

import ejb.MySessionRemote;
import javax.ejb.EJB;

public class Main {

    @EJB
    private static MySessionRemote mySession;

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {

        System.err.println("result = " + mySession.getResult());

    }

}

EJB:

package ejb;

import javax.ejb.Stateless;

@Stateless
public class MySession implements MySessionRemote {

    public String getResult() {
        return "This is My Session Bean";
    }
}

remote interface:

package ejb;

import javax.ejb.Remote;

@Remote
public interface MySessionRemote {

}

now if the interface is changed:

package ejb;

import javax.ejb.Remote;

@Remote
public interface MySessionRemote {

    public String getResult();
}

bean can now @Overrideuse the method:

package ejb;

import javax.ejb.Stateless;

@Stateless
public class MySession implements MySessionRemote {

    @Override
    public String getResult() {
        return "This is My Session Bean";
    }
}

however there is NPE:

-run:
     [java] java.lang.reflect.InvocationTargetException
     [java]     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
     [java]     at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
     [java]     at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
     [java]     at java.lang.reflect.Method.invoke(Method.java:606)
     [java]     at org.glassfish.appclient.client.acc.AppClientContainer.launch(AppClientContainer.java:446)
     [java]     at org.glassfish.appclient.client.AppClientFacade.main(AppClientFacade.java:166)
     [java] Caused by: java.lang.NullPointerException
     [java]     at entappclient.Main.main(Main.java:16)
     [java]     ... 6 more
     [java] Java Result: 1

run:

BUILD SUCCESSFUL
Total time: 18 seconds
thufir@dur:~/NetBeansProjects/EntAppClient$ 

How can I call the method correctly? EJB is not created?

+1
source share
1 answer

I started from scratch. The only difference I can think of is that instead of creating an EJB application, I just created an EJB module for the bean. Otherwise, I think the same thing.

Structure:

thufir@dur:~/NetBeansProjects$ 
thufir@dur:~/NetBeansProjects$ tree HelloLibrary/
HelloLibrary/
β”œβ”€β”€ build.xml
β”œβ”€β”€ nbproject
β”‚   β”œβ”€β”€ build-impl.xml
β”‚   β”œβ”€β”€ genfiles.properties
β”‚   β”œβ”€β”€ private
β”‚   β”‚   └── private.properties
β”‚   β”œβ”€β”€ project.properties
β”‚   └── project.xml
└── src
    └── hello
        └── HelloBeanRemote.java

4 directories, 7 files
thufir@dur:~/NetBeansProjects$ 
thufir@dur:~/NetBeansProjects$ tree HelloEJB/
HelloEJB/
β”œβ”€β”€ build.xml
β”œβ”€β”€ nbproject
β”‚   β”œβ”€β”€ ant-deploy.xml
β”‚   β”œβ”€β”€ build-impl.xml
β”‚   β”œβ”€β”€ genfiles.properties
β”‚   β”œβ”€β”€ private
β”‚   β”‚   └── private.properties
β”‚   β”œβ”€β”€ project.properties
β”‚   └── project.xml
└── src
    β”œβ”€β”€ conf
    β”‚   └── MANIFEST.MF
    └── java
        └── hello
            └── HelloBean.java

6 directories, 9 files
thufir@dur:~/NetBeansProjects$ 
thufir@dur:~/NetBeansProjects$ tree HelloClient/
HelloClient/
β”œβ”€β”€ build.xml
β”œβ”€β”€ nbproject
β”‚   β”œβ”€β”€ ant-deploy.xml
β”‚   β”œβ”€β”€ build-impl.xml
β”‚   β”œβ”€β”€ genfiles.properties
β”‚   β”œβ”€β”€ private
β”‚   β”‚   └── private.properties
β”‚   β”œβ”€β”€ project.properties
β”‚   └── project.xml
β”œβ”€β”€ src
β”‚   β”œβ”€β”€ conf
β”‚   β”‚   β”œβ”€β”€ application-client.xml
β”‚   β”‚   └── MANIFEST.MF
β”‚   └── java
β”‚       └── helloclient
β”‚           └── Main.java
└── test

7 directories, 10 files
thufir@dur:~/NetBeansProjects$ 
thufir@dur:~/NetBeansProjects$ 

client code:

package helloclient;

import hello.HelloBeanRemote;
import javax.ejb.EJB;

public class Main {
    @EJB
    private static HelloBeanRemote helloBean;

    public static void main(String... args) {
        System.out.println(helloBean.Hi());
    }

}

bean:

package hello;

import javax.ejb.Stateless;

@Stateless
public class HelloBean implements HelloBeanRemote {

    @Override
    public String Hi() {
        return "hello world";
    }

    @Override
    public String Bye() {
        return "goodbye";
    }

}

remote interface:

package hello;

import javax.ejb.Remote;

@Remote
public interface HelloBeanRemote {
    public String Hi();
    public String Bye();
}
0
source

All Articles