views:

47

answers:

2

I have app on GF V3.01 server and remote methods in EJB container. When I call remote methods from my remote swing app process take long time to execute. I read about ServiceLocator , but can no find examples for remote Swing app. Someone please help! give some idea to speedup remote method calls.

I create this test and make some comments if this is not valid approach

/** Remote interface CountryManagerRemote */

@Remote public interface CountryManagerRemote extends EJBHome {

public String createCountry(Country country);

public String editCountry(Country country);

public List<Country> listAllCountry();

}

/** CountryManagerRemote implementation */

@Stateless public class CountryManagerBean implements CountryManagerRemote {

/** persistance context and other initialization */

/**
 * Default constructor.
 */
public CountryCityRegister() {
}

/** implementation of CountryManagerRemote */

public String createCountry(Country country) {
    return "massage about operation succesed/failed";
}

public String editCountry(Country country) {
    return "massage about operation succesed/failed";
}

public List<Country> listAllCountry(){
        return List<Country>
}

/** EJBHome methods without implementation */

@Override
public EJBMetaData getEJBMetaData() throws RemoteException {
    // TODO Auto-generated method stub
    return null;
}

@Override
public HomeHandle getHomeHandle() throws RemoteException {
    // TODO Auto-generated method stub
    return null;
}

@Override
public void remove(Handle handle) throws RemoteException, RemoveException {
    // TODO Auto-generated method stub

}

@Override
public void remove(Object primaryKey) throws RemoteException, RemoveException {
    // TODO Auto-generated method stub

}

}

/** remote swing app code */ public class ClientApp {

public static void main(String[] args) {

    System.setProperty("java.security.auth.login.config", "auth.conf");
    System.setProperty("org.omg.CORBA.ORBInitialPort", "***serverport***");
    System.setProperty("org.omg.CORBA.ORBInitialHost", "***serverip***");

    ProgrammaticLogin programmaticLogin = new ProgrammaticLogin();

    try {

    // GF programatic login with custom realm
    programmaticLogin.login("username", "userpass");


    /**Obtain servicelocator instance*/
    ServiceLocator service=ServiceLocator.getInstance();

    /**FIRST GAIN OF EJB BEAN IT TAKE LONG TIME FOR FIRST LOOKUP*/      
    CountryManagerRemote manager=(CountryManagerRemote manager)service.getRemoteHome("com.CountryManagerRemote", com.CountryManagerRemote.class);

     List<Country> countryList=manager.listAllCountry();

    if(countryList!=null){
    //SHOW LIST
    }


    **/**ANOTHER PLACE OF SWING APP*/**
    /**SECOND INVOCATION OF BEAN IT ONLY TAKE TIME TO GET EJBHome OBJECT FROM ServiceLocator CACHE*/        
    CountryManagerRemote manager=(CountryManagerRemote manager)service.getRemoteHome("com.CountryManagerRemote", com.CountryManagerRemote.class);


     List<Country> countryList=manager.listAllCountry();

    if(countryList!=null){
    //SHOW LIST
    }


    } catch (Exception e1) {
        System.err.println("Inform User about exception"); 

    }

    }

}

/** ServiceLocator for remote ejb */

public class ServiceLocator {

private InitialContext ic;
private Map<String, EJBHome> cache;

private static ServiceLocator me;

static {
    try {
        me = new ServiceLocator();
    } catch (ServiceLocatorException se) {
        System.err.println(se);
        se.printStackTrace(System.err);
    }
}

private ServiceLocator() throws ServiceLocatorException {
    try {
        ic = new InitialContext();
        cache = Collections.synchronizedMap(new HashMap<String, EJBHome>());
    } catch (NamingException ne) {
        throw new ServiceLocatorException(ne);
    }
}

static public ServiceLocator getInstance() {
    return me;
}

public EJBHome getRemoteHome(String jndiHomeName, Class<?> className) throws ServiceLocatorException {
    EJBHome home = null;
    try {
        if (cache.containsKey(jndiHomeName)) {
            home = (EJBHome) cache.get(jndiHomeName);
        } else {
            Object objref = ic.lookup(jndiHomeName);
            Object obj = PortableRemoteObject.narrow(objref, className);
            home = (EJBHome) obj;
            cache.put(jndiHomeName, home);
        }
    } catch (NamingException ne) {
        throw new ServiceLocatorException(ne);
    } catch (Exception e) {
        throw new ServiceLocatorException(e);
    }
    return home;
}

}

A: 

I don't know anything about EJB (so I don't know if the answer is any different), but normally when you invoke a long running task you just start a separate Thread.

This is easily done by using a SwingWorker.

camickr
camickr, thanks for your advise. I know about SwingWorker, but my remote method only return simple List of persons from database. EJB method operation is not expensive. expensive is invocation of EJB bean and JNDI lookup. I want to speed up lookup operation from swing client.
dimitri
Ignore my comment I thought you where worried about the GUI freezing during the remote call.
camickr
@camickr - Why would you answer a question in which you know nothing about the topic ?
Romain Hippeau
I already stated I misread the question. I saw the word "Swing" and thought the question was about "freezing" of the GUI during during the remote method call not about how to speed up the call.
camickr
A: 

Sun's Service Locator page provides an example implementation (see the Web-tier ServiceLocator) that avoids constructing unnecessary InitialContext and caches enterprise bean home interfaces.

Pascal Thivent
i red explanation, but this is for web tier.it will be good idea to place ServiceLocator into swing client side and obtain remote ejb ServiceLocator.getInstance().getRemoteHome(jndiName, Clazz) ?
dimitri
@dimitri And what is the difference between a Web Tier client and a Swing client? I suggest to experiment a bit if you have some doubts.
Pascal Thivent
I make test now and results are perfect. every next invocation on same EJB bean is not time expensive, because its EJBHome objects stored in ServiceLocator's cache.I have doubt about ServiceLocator in clients VM. every app user have its own ServiceLocator in my case.In web tier ServiceLocator instance is one for every one.
dimitri
I am confused. Maybe it is because my English language knowledge is bad (maybe programming language to) :))))
dimitri