[英]EJB call from swing
我在GF V3.01服务器上有应用程序,在EJB容器中有远程方法。 当我从远程swing应用程序进程调用远程方法时,需要很长时间才能执行。 我阅读了有关ServiceLocator的信息,但找不到远程Swing应用程序的示例。 有人请帮忙! 给一些想法,以加快远程方法的调用。
如果此方法无效,我将创建此测试并提出一些意见
/** Remote interface CountryManagerRemote */
@Remote公共接口CountryManagerRemote扩展了EJBHome {
public String createCountry(Country country);
public String editCountry(Country country);
public List<Country> listAllCountry();
}
/ ** CountryManagerRemote实现* /
@Stateless公共类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
}
}
/ **远程swing应用程序代码* /公共类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 * /
公共类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可以很容易地做到这一点。
Sun的Service Locator页面提供了一个示例实现(请参见Web层ServiceLocator
),该实现避免了构造不必要的InitialContext
并缓存了企业bean home接口。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.