mercredi 10 mai 2017

Dynamically invoke remote EJB3 method using reflection

I am using the approach described in this question.

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.

Remote Interface Class

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:swapp/Business-1/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.

P.S. I am calling this from inside and MDB.





Aucun commentaire:

Enregistrer un commentaire