Our current application has a requirement to route all remote EJB calls used for batch jobs through an MDB to facilitate retries. As a result, I need to invoke the remote EJB through reflection.
When looking up the remote EJB3 interface, the returned value from the ctx.lookup(envProperties) call is actually a proxy object and not the real one which is expected. Thus, when looking up the method using clazz.getMethod(..) throws a NoSuchMethodException.
Below is my code.
The Remote Interface:
package my.interface.package; import java.io.Serializable; import java.util.Date; import java.util.List; public interface MyServiceBeanRemoteInterface extends Serializable { public List<Long> callBatchJob(Date batchDate, long batchId, boolean flag); }
Lookup code:
final Properties env = new Properties(); env.put("java.naming.factory.url.pkgs","org.jboss.ejb.client.naming"); env.put("remote.connections","default"); env.put("endpoint.name","client-endpoint"); env.put("remote.connection.default.host","localhost"); env.put("remote.connection.default.port","4447"); env.put("remote.connectionprovider.create.options.org.xnio.Options.SSL_ENABLED","false"); env.put("remote.connection.default.connect.options.org.xnio.Options.SASL_POLICY_NOANONYMOUS","false"); env.put("remote.connection.default.username","testuser"); env.put("remote.connection.default.password","testpassword"); Context ctx = new InitialContext(env); Object lookup = ctx.lookup( "ejb:myapp/business/MyServiceBean!my.interface.package.MyServiceBeanRemoteInterface"); Class<? extends Object> clazz = lookup.getClass(); /* Fails at below line */ Method method = clazz.getMethod("callBatchJob", Date.class, Long.class, Boolean.class); Object returnValue = method.invoke(lookup, new Date(), Long.valueOf(6), true);
How do I work around this? The implementation class can vary.