我在 Apache TomEE+ 上部署了 2 个 webapp。在第一个 webapp 中,我创建了一个单例 EJB 类,声明如下:
@Singleton
@Remote
@Startup
@Lock(READ)
public class SolverRegistryBean implements SolverRegistry
{
// ...
从 openejb.log 我可以看到,单例可用:
2011-12-05 20:43:22,696 - INFO - Jndi(name=SolverRegistryBeanRemote) --> Ejb(deployment-id=SolverRegistryBean)
2011-12-05 20:43:22,696 - INFO - Jndi(name=global/localhost/SolverCore-1.0/SolverRegistryBean!grid.solver.SolverRegistry) --> Ejb(deployment-id=SolverRegistryBean)
2011-12-05 20:43:22,696 - INFO - Jndi(name=global/localhost/SolverCore-1.0/SolverRegistryBean) --> Ejb(deployment-id=SolverRegistryBean)
2011-12-05 20:43:22,750 - INFO - Started Ejb(deployment-id=SolverRegistryBean, ejb-name=SolverRegistryBean, container=My Singleton Container)
从我的另一个应用程序中,我想访问那个单例 bean,所以我这样做:
Properties p = new Properties();
p.put(Context.INITIAL_CONTEXT_FACTORY,
"org.apache.openejb.client.RemoteInitialContextFactory");
p.put("java.naming.provider.url", "http://127.0.0.1:8080/openejb/ejb");
try {
ctx = new InitialContext(p);
SolverRegistry reg = (SolverRegistry)ctx.lookup(
"openejb:global/global/localhost/SolverCore-1.0/SolverRegistryBean");
但后来我得到 ClassCastException
java.lang.ClassCastException: $Proxy94 cannot be cast to grid.solver.SolverRegistry
我检查了这个 $Proxy94 类是否实现了接口:grid.solver.SolverRegistry、org.apache.openejb.core.ivm.IntraVmProxy和org.apache.openejb.BeanContext$Removable。
为什么这不起作用?