Reputation: 23
How to inject a parameter in the client of an Ejb? Something like that:
final Hashtable<String, String> jndiProperties = new Hashtable<String, String>();
jndiProperties.put("java.naming.factory.initial", "org.ow2.carol.jndi.spi.MultiOrbInitialContextFactory");
jndiProperties.put("java.naming.factory.url.pkgs", "org.ow2.jonas.naming");
jndiProperties.put("java.naming.provider.url", "rmi://localhost:1099");
final Context context = new InitialContext(jndiProperties);
Object obj = context.lookup("MyEjbTest");
context.addToEnvironment("user", new Object());
In the server side, using an Interceptor get the parameter injected by the client:
public Object intercept(InvocationContext ctx) throws Exception {
Object o = ctx.getContextData().get("user");
if (o != null) {
LOG.info("Exists " + o.toString());
return ctx.proceed();
} else {
return null;
}
}
The parameter user is never injected in the context and in the server side o is always null. Is there any way to handle that?
Upvotes: 1
Views: 1668
Reputation: 33956
No, there is no standard way to implicitly pass data to an EJB from a client. You must explicitly pass data to the EJB via a method argument.
If you're using RMI-IIOP, then you could write your own interceptor to transfer context data to the server and then store it in a thread local. If you're using WebSphere Application Server, you could use application context work areas (this was attempted to be standardized by JSR 149, but it was not deemed portable enough). These options are likely too niche or too cumbersome, so you're likely better off just explicitly passing the data via a method argument.
A complete example for sending additional context data using RMI-IIOP is quite extensive, but the general steps are:
byte[]
to be added to the ServiceContext.Upvotes: 1