remote ejb with context data.
jimmy.pannier Jun 13, 2016 11:07 AMHi,
I need to create ejb client to remote an ejb but with data in context.
So i have a method to construct a ejb client.
/**
* Get service needed authentication
*
* @param userContext
* @return
*/
public T getService(final String fullUserId, final String applicationName) {
try {
final Properties ejbClientContextProps = new Properties();
ejbClientContextProps.put("org.jboss.ejb.client.scoped.context", "true");
final Context initialContext = new InitialContext(ejbClientContextProps);
// find the EJB client context which is scoped to this JNDI context
final EJBClientContext scopedEJBClientContext = (EJBClientContext) initialContext.lookup("ejb:/EJBClientContext");
// register the interceptor against the scoped EJB client context
scopedEJBClientContext.registerInterceptor(0x99999, new ProxyInterceptor(fullUserId, applicationName));
return (T) initialContext.lookup(jndiName);
}
catch (NamingException e) {
LOGGER.warn("Service non disponible : " + jndiName + ", " + e);
}
return null;
}
The interceptor fills data in context
public class ProxyInterceptor implements EJBClientInterceptor {
public static final String FULL_USER_ID = "fullUserId";
public static final String APPLICATION_NAME = "applicationName";
private final String fullUserId;
private final String applicationName;
/**
*
*/
public ProxyInterceptor(final String fullUserId, final String applicationName) {
this.fullUserId = fullUserId;
this.applicationName = applicationName;
}
/*
* (non-Javadoc)
* @see org.jboss.ejb.client.EJBClientInterceptor#handleInvocation(org.jboss.ejb.client.EJBClientInvocationContext)
*/
@Override
public void handleInvocation(final EJBClientInvocationContext ctx) throws Exception {
ctx.getContextData().put(FULL_USER_ID, fullUserId);
ctx.getContextData().put(APPLICATION_NAME, applicationName);
ctx.sendRequest();
}
/*
* (non-Javadoc)
* @see org.jboss.ejb.client.EJBClientInterceptor#handleInvocationResult(org.jboss.ejb.client.EJBClientInvocationContext)
*/
@Override
public Object handleInvocationResult(EJBClientInvocationContext ctx) throws Exception {
return ctx.getResult();
}
}
And in the implementation of the ejb, i try to get data via an annotation
@Resource
private EJBContext context;
/*
* (non-Javadoc)
* @see com.inovelan.cloud.user.api.server.service.IUserEjbService#getUserContext()
*/
@Override
public String getUserContext() {
return (String) context.getContextData().get(ProxyInterceptor.FULL_USER_ID);
}
The problem is that the method getUserContext() returns null.
1) why does it returns null ?
2) Is there another way to pass data than using the property org.jboss.ejb.client.scoped.context ? (because context must always manually closed by the developper --> Scoped EJB client contexts - WildFly 9 - Project Documentation Editor)