Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
278 views
in Technique[技术] by (71.8m points)

java - how to create a remote session EJB from a client

As per the Netbeans Tutorial on EJB Client applications, I cannot seem to invoke the method:

compile 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

client:

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 modified:

package ejb;

import javax.ejb.Remote;

@Remote
public interface MySessionRemote {

    public String getResult();
}

the bean can now @Override 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's a 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 invoke the method correctly? The EJB isn't instantiated?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I started from scratch. The only difference I can think of is that instead of making an EJB application, I just made an EJB module for the bean. Otherwise, I think it's the same.

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();
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...