简体   繁体   English

Java EE | ejb3 | 运行时调度

[英]java ee | ejb3 | runtime dispatch

I would like to call an ejb3 at runtime. 我想在运行时调用ejb3。 The name of the ejb and the method name will only be available at runtime, so I cannot include any remote interfaces at compile time. ejb的名称和方法的名称仅在运行时可用,因此在编译时不能包含任何远程接口。

String bean = 'some/Bean';
String meth = 'doStuff';

//lookup the bean
Object remoteInterface = (Object) new InitialContext().lookup(bean);

//search the method ..
// foreach (methods)
// if method == meth, method.invoke(bean);

the beans should be distributed accross multiple application servers, and all beans are to be called remotely. 这些bean应该分布在多个应用程序服务器上,并且所有bean都将被远程调用。

Any hints? 有什么提示吗? specifically i do not want: 特别是我不想

  1. dependency injection 依赖注入
  2. inclusion of appliation specific ejb interfaces in the dispatcher (above) 在调度程序中包含特定于应用程序的ejb接口(如上所述)
  3. webservices, thats like throwing out processing power for nothing, all the xml crap webservices,那就像浪费处理能力,所有的xml废话

Is it possible to load an ejb3 remote interface over the network (if yes, how?), so I could cache the interface in some hashmap or something. 是否可以通过网络加载ejb3远程接口(如果是,如何?),所以我可以将接口缓存在某种哈希图中。

I have a solution with a remote dispatcher bean, which I can include in the above main dispatcher, which does essentially the same, but just relays the call to a local ejb (which I can lookup how? naming lookup fails). 我有一个使用远程调度程序bean的解决方案,该解决方案可以包含在上面的主调度程序中,它的功能基本相同,但是只是将调用中继到本地ejb(我可以查找该方法吗?命名查找失败)。 Given the remote dispatcher bean, I can use dependency injection. 给定远程调度程序bean,我可以使用依赖项注入。

thanks for any help 谢谢你的帮助

(netbeans and glassfish btw) (顺便说一句,网豆和玻璃鱼)

ejb3 calls use RMI. ejb3调用使用RMI。 RMI supports remote class loading, so i'd suggest looking into that. RMI支持远程类加载,因此建议您对此进行研究。

also, JMX mbeans support fully untyped, remote invocations. 同样,JMX mbeans支持完全无类型的远程调用。 so, if you could use mbeans instead of session beans, that could work. 因此,如果您可以使用mbeans代替会话bean,那可能会起作用。 (JBoss, for instance, supports ejb3-like mbeans with some custom annotations). (例如,JBoss支持带有一些自定义注释的类似ejb3的mbean)。

lastly, many app servers support CORBA invocations, and CORBA supports untyped method invocations. 最后,许多应用程序服务器支持CORBA调用,而CORBA支持无类型的方法调用。

You might be able to use java.rmi.server.RMIClassLoader for the remote class loading. 您可能可以使用java.rmi.server.RMIClassLoader进行远程类加载。 You'll also need to load any classes that the remote service returns or throws. 您还需要加载远程服务返回或抛出的所有类。

It cannot be done. 无法完成。 You will always get a "class not found" exception. 您将始终收到“找不到类”异常。 That is in my opinion the biggest disadvantage of EJB/Java. 我认为这是EJB / Java的最大缺点。 You loose some of the dynamcis, because the meta-language features are limited. 由于元语言功能有限,因此您失去了一些活力。

EJB3 supports RMI/IIOP, but not RMI/JRMP (standard RMI) so dynamic class loading is not supported.You loose some Java generality, but gain other features like being able to communicate about transactions and security. EJB3支持RMI / IIOP,但不支持RMI / JRMP(标准RMI),因此不支持动态类加载。您松开了一些Java通用性,但获得了诸如能够就事务和安全性进行通信之类的其他功能。

You need to use reflection for this, and it is very simple to do. 为此,您需要使用反射,这非常简单。 Assuming that you're looking for a void method called meth : 假设您正在寻找一个称为meth的void方法:

Object ejb = ctx.lookup(bean);
for (Method m : ejb.getClass().getMethods()) {
    if (m.getName().equals(meth) && m.getParameterTypes().length == 0) {
        m.invoke(service);
    }
}

If you're looking for a specific method signature just modify the m.getParameterTypes() test accordingly, eg for a method with a single String parameter you can try: 如果您正在寻找特定的方法签名,只需相应地修改m.getParameterTypes()测试,例如,对于具有单个String参数的方法,您可以尝试:

Arrays.equals(m.getParameterTypes(), new Class[]{String.class})

And then pass an Object[] array with the actual arguments to the m.invoke() call: 然后将带有实际参数的Object []数组传递给m.invoke()调用:

m.invoke(service, new Object[]{"arg0"})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM