gpt4 book ai didi

java - 来自 Swing 的 EJB 调用

转载 作者:行者123 更新时间:2023-12-01 05:54:27 25 4
gpt4 key购买 nike

我在 GF V3.01 服务器上有应用程序,在 EJB 容器中有远程方法。当我从远程 Swing 应用程序调用远程方法时,进程需要很长时间才能执行。我阅读了有关 ServiceLocator 的内容,但找不到远程 Swing 应用程序的示例。请有人帮忙!给出一些加速远程方法调用的想法。

我创建了这个测试,如果这不是有效的方法,我会发表一些评论

/** Remote interface CountryManagerRemote */

@远程公共(public)接口(interface) CountryManagerRemote 扩展 EJBHome {

public String createCountry(Country country);

public String editCountry(Country country);

public List<Country> listAllCountry();

}

/** CountryManagerRemote 实现 */

@无状态公共(public)类 CountryManagerBean 实现 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

}

}

/** 远程挥杆应用代码 */公共(public)类 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");

}

}

}

/** 远程 ejb 的 ServiceLocator */

公共(public)类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;
}

}

最佳答案

我对 EJB 一无所知(所以我不知道答案是否有什么不同),但通常当您调用长时间运行的任务时,您只需启动一个单独的线程。

这可以通过使用 SwingWorker 轻松完成.

关于java - 来自 Swing 的 EJB 调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3541860/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com