My first learning objective on EJB is to establish a connection to the server and then return a value, say
5063, to the client. Thus, I have the following piece on server side:
@Remote
public interface AuthenticationRemote {
public int status();
}
@Stateless
@LocalBean
public class Authentication implements AuthenticationRemote {
/**
* Default constructor.
*/
public Authentication() {
}
@Override
public int status() {
return 5063;
}
}
The project name for the pieces above,
ejb2, will be the context path used by a client to identify the remote path of a specific method call. When the piece is deployed to the server, following log would be seen:
java:global/ejb2/Authentication!org.huahsin.AuthenticationRemote
java:app/ejb2/Authentication!org.huahsin.AuthenticationRemote
java:module/Authentication!org.huahsin.AuthenticationRemote
java:jboss/exported/ejb2/Authentication!org.huahsin.AuthenticationRemote
java:global/ejb2/Authentication!org.huahsin.Authentication
java:app/ejb2/Authentication!org.huahsin.Authentication
java:module/Authentication!org.huahsin.Authentication
On the client side, as in following piece, is a separate project which will establish a connection to the above piece:
public class EjbClient {
public static void main(String args[]) throws NamingException {
Properties props = new Properties();
props.put("java.naming.factory.url.pkgs", "org.jboss.ejb.client.naming");
props.put("java.naming.factory.initial", "org.jboss.naming.remote.client.InitialContextFactory");
props.put("java.naming.provider.url", "remote://127.0.0.1:4447");
props.put("jboss.naming.client.ejb.context", "true");
props.put("jboss.naming.client.connect.options.org.xnio.Options.SASL_POLICY_NOPLAINTEXT","false");
InitialContext context = new InitialContext(props);
String appName = "";
String moduleName = "ejb2";
String distinctName = "";
String beanName = Authentication.class.getSimpleName();
String interfaceName = AuthenticationRemote.class.getName();
String name = "ejb:" + appName + "/" + moduleName + "/" + distinctName + "/" + beanName + "!" + interfaceName;
AuthenticationRemote bean = (AuthenticationRemote) context.lookup(name);
int result = bean.status();
}
}
I was so unlucky that I was failed to establish a connection to server. End I up I get this:
Exception in thread "main" javax.naming.NamingException: Failed to create remoting connection [Root exception is java.lang.RuntimeException: Operation failed with status WAITING]
at org.jboss.naming.remote.client.ClientUtil.namingException(ClientUtil.java:36)
at org.jboss.naming.remote.client.InitialContextFactory.getInitialContext(InitialContextFactory.java:121)
at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:684)
at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:307)
at javax.naming.InitialContext.init(InitialContext.java:242)
at javax.naming.InitialContext.(InitialContext.java:216)
at org.huahsin68.EjbClient.main(EjbClient.java:21)
Caused by: java.lang.RuntimeException: Operation failed with status WAITING
at org.jboss.naming.remote.protocol.IoFutureHelper.get(IoFutureHelper.java:89)
at org.jboss.naming.remote.client.NamingStoreCache.getRemoteNamingStore(NamingStoreCache.java:56)
at org.jboss.naming.remote.client.InitialContextFactory.getOrCreateCachedNamingStore(InitialContextFactory.java:166)
at org.jboss.naming.remote.client.InitialContextFactory.getOrCreateNamingStore(InitialContextFactory.java:139)
at org.jboss.naming.remote.client.InitialContextFactory.getInitialContext(InitialContextFactory.java:104)
... 5 more
But later I found out I forgot to turn on my server. Finally, the
result variable is returning the value of 5063.