(EJB Client Context) Multi-user EJB invocation
a.d.jbpm Apr 7, 2013 9:49 AMHi,
I am trying to connect 2 users from my multi-threaded EJB client application using the JBoss 7 recommended solution for invoking ejb methods (https://docs.jboss.org/author/display/AS71/EJB+invocations+from+a+remote+client+using+JNDI).
Each user will create a ContextSelector, then he calls EJBClientContext.setSelector with the newly created context as argument. Finally he lookups a remote service to call a business method.
The problem occurs in a multithreaded context : the last user calling EJBClientContext.setSelector will appear as the principal caller for all other calls.
Any help on this issue would be very appreciated.
Following I provide you my client code and EJB code.
- Here is my client:
private static ContextSelector<EJBClientContext> createContextSelector(String userName, String password){
Properties clientProps = new Properties();
clientProps.put("remote.connectionprovider.create.options.org.xnio.Options.SSL_ENABLED", "false");
clientProps.put("remote.connections", "default");
clientProps.put("remote.connection.default.host", "localhost");
clientProps.put("remote.connection.default.port", "4447");
clientProps.put("remote.connection.default.connect.options.org.xnio.Options.SASL_POLICY_NOANONYMOUS", "false");
clientProps.put("remote.connection.default.username", userName);
clientProps.put("remote.connection.default.password", password);
clientProps.put("remote.connection.default.connect.options.org.xnio.Options.SASL_DISALLOWED_MECHANISMS", "JBOSS-LOCAL-USER");
EJBClientConfiguration ecc = new PropertiesBasedEJBClientConfiguration(clientProps);
ContextSelector<EJBClientContext> selector = new ConfigBasedEJBClientContextSelector(ecc);
return selector;
}
public static void main(String[] args) throws InterruptedException {
final String beanName = "ejb:/com.howto.jboss.examples//SecureBean!com.howto.jboss.examples.SecureBeanRemote";
//User1 thread
new Thread(new Runnable() {
public void run() {
try {
final ContextSelector<EJBClientContext> userContext = createContextSelector("user1", "password");
EJBClientContext.setSelector(userContext);
Properties props = new Properties();
props.put(Context.URL_PKG_PREFIXES, "org.jboss.ejb.client.naming");
Context context = new InitialContext(props);
SecureBeanRemote remoteService = (SecureBeanRemote) context.lookup(beanName);
String caller = remoteService.getCaller();
System.out.println("user1 context: " + caller);
} catch (Exception e) {
e.printStackTrace();
}
}
}).start();
//User2 thread
new Thread(new Runnable() {
public void run() {
try {
final ContextSelector<EJBClientContext> userContext = createContextSelector("user2", "password");
EJBClientContext.setSelector(userContext);
Properties props = new Properties();
props.put(Context.URL_PKG_PREFIXES, "org.jboss.ejb.client.naming");
Context context = new InitialContext(props);
SecureBeanRemote remoteService = (SecureBeanRemote) context.lookup(beanName);
String caller = remoteService.getCaller();
System.out.println("user2 context: " + caller);
} catch (Exception e) {
e.printStackTrace();
}
}
}).start();
}
- My EJB is only returing caller principal name:
@Stateless(mappedName = "SecureBean")
@SecurityDomain("other")
public class SecureBean implements SecureBeanRemote {
@Resource
private SessionContext sessionContext;
public SecureBean() {
}
@PermitAll
@Override
public String getCaller() {
return sessionContext.getCallerPrincipal().getName();
}
}