EJB client calling the same EJB in different servers within a loop
mleite Sep 13, 2019 4:30 PMHello, there...
My scenario is quite simple, but I can not figure out how to make it work.
Recently we have migrated from JBoss 5.1 to Wildfly 11. The situation I am going to explain used to work within JBoss 5.1, but is not working within Wildfly anymore.
I have a list of servers where resides a @Remote EJB and I need to connect to each of these servers and call this EJB. However, I can only see the connection being performed at the first server of the list. All other calls are made to that server, regardless I create new contexts and lookups.
Looks like the InitialContext is being "reused".
Here are some code snippets
public interface RemoteEJB { String call(); }
@Stateless @Remote public class RemoteEJBImpl implements RemoteEJB { public String call() { String hostname = null; try { hostname = InetAddress.getLocalHost().getHostName(); } catch (UnknownHostException e) { System.err.println(e.getMessage()); } return hostname; } }
@Stateless public class MainLocalEJB { private static final String REMOTE_EJB = "RemoteEJBImpl!com.test.service.RemoteEJB"; public List executeRemoteBeansInLoop() { List result = new ArrayList(); String[] remoteAddresses = {"192.168.55.10","192.168.55.20"}; Arrays.stream(remoteAddresses).forEach(remoteAddress -> { result.add(this.lookupRemoteService(remoteAddress, "8080").call()); }); return result; } private RemoteEJB lookupRemoteService(String remoteAddress, String remotePort) { RemoteEJB bean = null; try { Context cContext = this.getContext(remoteAddress, remotePort); bean = (RemoteEJB) cContext.lookup(this.getJNDIName(REMOTE_EJB)); } catch (Exception e) { e.printStackTrace(); } return bean; } private Context getContext(String remoteAddress) throws NamingException { Properties env = new Properties(); env.put(Context.INITIAL_CONTEXT_FACTORY,"org.wildfly.naming.client.WildFlyInitialContextFactory"); env.put(Context.PROVIDER_URL, this.getURL(remoteAddress); return new InitialContext(env); } private String getURL(String remoteAddress, String remotePort) { StringBuilder url = new StringBuilder(); url.append("remote+http://"); url.append(remoteAddress); url.append(":8080"); return url.toString(); } private String getJNDIName(String beanName) { return this.getJNDINamePrefix() + beanName; } private String getJNDINamePrefix() { return "ejb:connectivity-test-remote-ear/connectivity-test-remote/"; } }
Could you give me any help with that?
Thanks in advance,
Marcelo