我想从 Java 桌面应用程序调用部署在 JBoss 7 中的无状态会话 Bean。
我创建了 InitialContext 实例,如果我在同一个线程中查找并调用 bean 方法(以下代码中的 bean1),一切正常。
但是,如果我在另一个线程中从 InitialContext 获取 bean 代理,则当我从 bean(以下代码中的 bean2)调用方法时,程序会引发异常。
我使用的部分源代码:
public class Tester1 implements Runnable
{
InitialContext ctx;
static String beanAddr = "MobileSubscribersService_war_exploded/SubscriberEJB!com.persianswitch.ussd.ISubscriberBeanRemote";
public void doIt() throws NamingException
{
Properties p = new Properties();
p.put(Context.INITIAL_CONTEXT_FACTORY,"org.jboss.naming.remote.client.InitialContextFactory");
p.put(Context.PROVIDER_URL, "remote://localhost:4447");
p.put(Context.SECURITY_PRINCIPAL, "user");
p.put(Context.SECURITY_CREDENTIALS, "pass");
p.put("jboss.naming.client.ejb.context", true);
ctx = new InitialContext(p);
ISubscriberBeanRemote bean1 = (ISubscriberBeanRemote)ctx.lookup(beanAddr);
int res = bean1.getLanguageByMobileNo("12345"); // this line runs good
Thread th = new Thread(this);
th.start();
}
@Override
public void run() {
try {
ISubscriberBeanRemote bean2 = (ISubscriberBeanRemote)ctx.lookup(beanAddr);
int res = bean2.getLanguageByMobileNo("12345"); // Throws Exception Here
} catch (NamingException e) {
}
}
}
异常内容:
Exception in thread "Thread-2" java.lang.IllegalStateException: No EJB receiver available for handling [appName:,modulename:MobileSubscribersService_war_exploded,distinctname:] combination for invocation context org.jboss.ejb.client.EJBClientInvocationContext@3337cdec
at org.jboss.ejb.client.EJBClientContext.requireEJBReceiver(EJBClientContext.java:584)
at org.jboss.ejb.client.ReceiverInterceptor.handleInvocation(ReceiverInterceptor.java:119)
at org.jboss.ejb.client.EJBClientInvocationContext.sendRequest(EJBClientInvocationContext.java:181)
at org.jboss.ejb.client.EJBInvocationHandler.doInvoke(EJBInvocationHandler.java:136)
at org.jboss.ejb.client.EJBInvocationHandler.doInvoke(EJBInvocationHandler.java:121)
at org.jboss.ejb.client.EJBInvocationHandler.invoke(EJBInvocationHandler.java:104)
at com.sun.proxy.$Proxy0.getLanguageByMobileNo(Unknown Source)
at com.persianswitch.Tester1.run(Tester1.java:52)
at java.lang.Thread.run(Thread.java:722)
我想在多个线程中重用一个 InitialContext 以提高性能。
我的代码有什么错误吗?有什么我必须知道的规则吗?