I have used eclipse to create a EJB project and have created 2 classes as below.
package com.abhijit.ejbs;
import java.util.List;
import javax.ejb.Remote;
@Remote
public interface TestEJBRemote {
public void addElement(int a);
public void removeElement(int a);
public List getElements();
}
the actual EJB as below -
package com.abhijit.ejbs;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.LocalBean;
import javax.ejb.Stateful;
/**
* Session Bean implementation class TesEJB
*/
@Stateful
@LocalBean
public class TestEJB implements TestEJBRemote {
List <Integer> myList = new ArrayList<>();
/**
* Default constructor.
*/
public TestEJB() {
// TODO Auto-generated constructor stub
}
@Override
public void addElement(int a) {
myList.add(a);
}
@Override
public void removeElement(int a) {
myList.remove(a);
}
@Override
public List getElements() {
return myList;
}
}
This EJB is then deployed on the JBoss AS 7 and it runs wihtout any errors..I see this message in the console -
**java:global/EJB1/TestEJB!com.abhijit.ejbs.TestEJBRemote
java:app/EJB1/TestEJB!com.abhijit.ejbs.TestEJBRemote
java:module/TestEJB!com.abhijit.ejbs.TestEJBRemote
java:jboss/exported/EJB1/TestEJB!com.abhijit.ejbs.TestEJBRemote
java:global/EJB1/TestEJB!com.abhijit.ejbs.TestEJB
java:app/EJB1/TestEJB!com.abhijit.ejbs.TestEJB
java:module/TestEJB!com.abhijit.ejbs.TestEJB**
Now , I am creating a 2nd Eclipse Java project and writing a simple java remote client code as below --
package com;
import java.util.Hashtable;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import com.abhijit.ejbs.TestEJB;
import com.abhijit.ejbs.TestEJBRemote;
public class EJBClient {
public static void main(String[] args) {
TestEJBRemote values;
try {
System.out.println("----");
final Hashtable<String, String> jndiProperties = new Hashtable();
jndiProperties.put(Context.URL_PKG_PREFIXES, "org.jboss.ejb.client.naming");
//tried with rmi: also .. not working....
jndiProperties.put(Context.PROVIDER_URL, "ejb://localhost:1099");
InitialContext ic = new InitialContext(jndiProperties);
values = (TestEJBRemote)ic.lookup("java:global/EJB1/TestEJB!com.abhijit.ejbs.TestEJB");
System.out.println("---" + values.getElements());
}
catch(Exception e) {
e.printStackTrace();
}
}
Tried with various combinations in the lookup() but when I run this java client code which would run in a separate JVM, its not working ..I am getting this error -
javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file: java.naming.factory.initial
at javax.naming.spi.NamingManager.getInitialContext(Unknown Source)
at javax.naming.InitialContext.getDefaultInitCtx(Unknown Source)
at javax.naming.InitialContext.getURLOrDefaultInitCtx(Unknown Source)
at javax.naming.InitialContext.lookup(Unknown Source)
at com.EJBClient.main(EJBClient.java:25)
I am sure it must be something to do with the jndi parameters..But I am not getting what it is...
This solution works for Wildfly 8, 9 and 10.
1. Add required dependency
wildfly-ejb-client-bomYou need to add the dependency
wildfly-ejb-client-bomto your project.In case you are using maven you can add the following dependency to your
pom.Info use other versions for wildfly 8 (
8.2.1.Final) or 9 (9.0.2.Final).2. Create a
jboss-ejb-client.propertiesAdd to your
resourcesfolder ajboss-ejb-client.propertiesand add the following content.3. Create your initial context
You need to create the initial context as follows.
4. Lookup remote business interface
Finally you can do a lookup for your stateful session bean.
If you want to do a lookup for a stateless bean you have to remove
?stateful.Unfortunately, the latest wildfly docs are a mess, and they don't provide a working solution. But you can find a working hello world example under ejb-remote: Remote EJB Client Example.