簡體   English   中英

如何從客戶端創建遠程會話EJB

[英]how to create a remote session EJB from a client

根據EJB客戶端應用程序上Netbeans教程 ,我似乎無法調用該方法:

編譯錯誤:

-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

客戶:

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";
    }
}

遠程接口:

package ejb;

import javax.ejb.Remote;

@Remote
public interface MySessionRemote {

}

現在,如果接口被修改:

package ejb;

import javax.ejb.Remote;

@Remote
public interface MySessionRemote {

    public String getResult();
}

Bean現在可以@Override方法:

package ejb;

import javax.ejb.Stateless;

@Stateless
public class MySession implements MySessionRemote {

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

但是,有一個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$ 

如何正確調用該方法? EJB是否未實例化?

我從頭開始。 我能想到的唯一區別是,我沒有創建EJB應用程序,而是為bean創建了EJB模塊。 否則,我認為是相同的。

結構體:

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$ 

客戶代碼:

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

}

豆角,扁豆:

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";
    }

}

遠程接口:

package hello;

import javax.ejb.Remote;

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM