简体   繁体   中英

How create a client application that uses EJB Component?

I created an EJB component with 1 interface (Converter.java) ...

package converter;

public interface Converter {
    public double euroToOtherCurrency(double amount, String currencyCode);
}

... and 1 class (ConverterBean.java) that implements Converter :

package converter;

@Stateless
@Remote(Converter.class)
public class ConverterBean implements Converter {

    public double euroToOtherCurrency(double amount, String currencyCode) {
        // implementation
    }
}

Then, I deployed this application in my Glassfish Server.

Now, I want to do a independent client application that uses my EJB Component but that will be deployed in an independent JVM.

I copied the interface Convert in the client application, and here is the main class :

import javax.naming.InitialContext;
import java.util.Scanner;

public class Client {

    public static void main (String[] args){

        Converter converter = (Converter) InitialContext.doLookup("java:global/Converter/Converter-ejb/ConverterBean");

        Scanner sc = new Scanner(System.in);
        System.out.println("Amount :");
        Double amount = sc.nextDouble();
        System.out.println("Currency :");
        sc.nextLine();
        String currency = sc.nextLine(); 

        System.out.println(converter.euroToOtherCurrency(amount, currency));        
    }   
}

I have the following error :

Client.java:8: error: cannot find symbol Converter converter = (Converter) InitialContext.doLookup("java: global/Converter/Converter-ejb/ConverterBean"); symbol: class Converter location: class Client

However, I thought to change the ClassPath environnement:

set CLASSPATH=C:\Program Files\glassfish-4.1.1\glassfish\lib\appserv-rt;jndi.properties;.

Here is the jndi.properties file :

java.naming.factory.initial=com.sun.enterprise.naming.SerialInitContextFactory
org.omg.CORBA.ORBInitialHost=localhost
org.omg.CORBA.ORBInitialPort=3700

Can you tell me what's wrong ? Thank you.

Glassfish server provides libraries to develop client applications. Using these, you should be able to access components running in the application server as if your code was running in the application server too (I believe that also annotations would work).

Have a look at official development documentation for Glassfish 4 - Section 10: Developing Java Clients

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM