JNDI Lookup of EJB from stand alone client on JBoss AS 7.1
I followed the examples explained in the following JBoss official links:
https://docs.jboss.org/author/display/AS71/EJB+invocations+from+a+remote+client+using+JNDI
https://docs.jboss.org/author/display/AS71/Remote+EJB+invocations+via+JNDI+-+EJB+client+API+or+remote-naming+project
This is my lookup method:
But when i ran the client, i kept getting the following exception:
javax javaee-api 6.0 provided
https://docs.jboss.org/author/display/AS71/EJB+invocations+from+a+remote+client+using+JNDI
https://docs.jboss.org/author/display/AS71/Remote+EJB+invocations+via+JNDI+-+EJB+client+API+or+remote-naming+project
This is my lookup method:
private static Hello lookupRemoteStatelessCalculator() throws NamingException {
final Hashtable jndiProperties = new Hashtable();
jndiProperties.put("jboss.naming.client.ejb.context", true);
jndiProperties.put(Context.URL_PKG_PREFIXES,
"org.jboss.ejb.client.naming");
jndiProperties.put(Context.INITIAL_CONTEXT_FACTORY, "org.jboss.naming.remote.client.InitialContextFactory");
jndiProperties.put(Context.PROVIDER_URL,"remote://localhost:4447");
// username
jndiProperties.put(Context.SECURITY_PRINCIPAL, "admin");
// password
jndiProperties.put(Context.SECURITY_CREDENTIALS, "password");
final Context context = new InitialContext(jndiProperties);
// The app name is the application name of the deployed EJBs. This is
// typically the ear name
// without the .ear suffix. However, the application name could be
// overridden in the application.xml of the
// EJB deployment on the server.
// Since we haven't deployed the application as a .ear, the app name for
// us will be an empty string
final String appName = "";
// This is the module name of the deployed EJBs on the server. This is
// typically the jar name of the
// EJB deployment, without the .jar suffix, but can be overridden via
// the ejb-jar.xml
// In this example, we have deployed the EJBs in a
// jboss-as-ejb-remote-app.jar, so the module name is
// jboss-as-ejb-remote-app
final String moduleName = "jbosslookup";
// AS7 allows each deployment to have an (optional) distinct name. We
// haven't specified a distinct name for
// our EJB deployment, so this is an empty string
final String distinctName = "";
// The EJB name which by default is the simple class name of the bean
// implementation class
final String beanName = HelloBean.class.getSimpleName();
// the remote view fully qualified class name
final String viewClassName = Hello.class.getName();
// let's do the lookup
System.out.println("ejb:" + appName + "/"
+ moduleName + "/" + distinctName + "/" + beanName + "!"
+ viewClassName);
return (Hello) context.lookup("ejb:" + appName + "/"
+ moduleName + "/" + distinctName + "/" + beanName + "!"
+ viewClassName);
}
You need to have username and password set up on the server as application user. This username and password credential is used by the client when creating the initial context. This information along with other information is passed as Properties argument into the initial context constructor.
And then, you need to have the jboss-client.jar in the classpath of the client.
But when i ran the client, i kept getting the following exception:
Exception in thread "main" java.lang.ClassFormatError: Absent Code attribute in method that is not native or abstract in class file javax/ejb/NoSuchEJBException
It turned out that i need to replace the dependency:
With the following JBoss implementation dependency:
<dependency>
<groupId>org.jboss.spec</groupId>
<artifactId>jboss-javaee-6.0</artifactId>
<version>3.0.2.Final</version>
<type>pom</type>
<scope>provided</scope>
</dependency>
But it was not enough. JBoss wants me to specify the Xalan dependency too.
<dependency>
<groupId>xalan</groupId>
<artifactId>xalan</artifactId>
<version>2.7.1</version>
<scope>provided</scope>
</dependency>
Java EE dependency does not have implementation in maven repo, that`s what caused the exception. You also need implementation specific dependency when doing unit tests. I read that this issue is solved in Java EE 7 but i haven`t tested this yet.