Tutorial : How to invoke an EJB from Java Application Client using WildFly and Eclipse

As defined in the tutorial above, an EJB is a re-usable and managed component deployed on J2EE Application Server such as WildFly. This tutorial is aimed to illustrate how to invoke the functionalities of an already deployed Enterprise Java Bean Tutorial.

1. Importing JAR file for EJB

To invoke the methods of an EJB remotely, we need to have the RemoteInterfaces within our new project. The easy way for doing it get the JAR file for the deployed EJB using the steps below.

In the example, we used the MyAdditionEJB project developed in [ the tutorial above ]

1 Right click on the MyAdditionEJB project and select Export –> Export as shown :

2 Select EJB –> EJB Jar File as shown:

3 Click Browse to select a place to store the JAR file ( Ex, your Desktop or C: drive …). Click Finish once done.

2. Developing EJB Java Client

1 If you want, within the same Eclipse running the EJB, Create a new Java Project, Click on New–>Other->Java Project, Click Next

2 Type in the name of the project as : MyCalculator then click Finish

3 If you are asked with a dialog window to Open Associated Perspective, Click NO

4 Right click on the project : MyCalculator, select New then choose Class, choose the name as : StartClac and click Finish

5 Copy the following code for the StartClac class:

    import java.util.*;
    import javax.naming.*;
    import com.logic.*;
    import javax.ejb.*;
    public class StartCalc {
    	 * @param args
    	public static void main(String[] args) {           
    			System.out.println("Hello From Java!");
    		    Properties props = new Properties();
    	        InitialContext context = new InitialContext(props);
    	        String appName = "";        	 
    	        String moduleName = "MyAdditionEJB";
    	        String distinctName = "";        	 
    	        String beanName = Addition.class.getSimpleName();        	 
    	        String interfaceName = AdditionRemote.class.getName();
    	        String name = "ejb:" + appName + "/" + moduleName + "/" +  distinctName    + "/" + beanName + "!" + interfaceName;
    	        AdditionRemote bean = (AdditionRemote)context.lookup(name);
    	        int result=bean.add(4,6);
    	        System.out.println("Result computed by EJB is :"+result); 	        
    		}catch(Exception e){

6 There are a number of errors ! we will correct all of them easily 🙂

The Addition class and AdditionRemote interface are not recognized

7 Right click on the project: MyCalculator and select Build Path then click Configure Build Path

8 Click On Libraries, then Click : Add External Jars to add TWO JAR files.

  • MyAdditionEJB.jar : The EJB Jar file exported from the first section of this tutorial.

  • jboss-client.jar : It is the JBoss client Jar file which is found under the bin/client folder where you have installed/extracted JBoss.

9 Right click on the class StartCalc under the project MyCalculator and select Run as then choose Java Application

MAKE SURE that the WildFly Server is running and MyAdditionEJB is deployed as shown below

10 if you run the Client Application, you would get this error !

WARN: Could not register a EJB receiver for connection to localhost:4447
java.net.ConnectException: Connection refused: no further information

java.lang.IllegalStateException: EJBCLIENT000025: No EJB receiver available for handling [appName:, moduleName:MyAdditionEJB, distinctName:] combination for invocation context org.jboss.ejb.client.EJBClientInvocationContext@49993335
at org.jboss.ejb.client.EJBClientContext.requireEJBReceiver(EJBClientContext.java:798)

11 We need to create the JBoss Properties file. Right Click on the src folder, select New and choose Other
12 Under General, select File and click Next
13 Type in the name for the file as : jboss-ejb-client.properties and click Finish

14 Type in the following text inside the property file jboss-ejb-client.properties :


15 Run now the client Application and would shall see the following messages :

Hello From Java!
Dec 10, 2016 12:21:42 PM org.jboss.ejb.client.EJBClient <clinit>
INFO: JBoss EJB Client version 2.1.4.Final
Dec 10, 2016 12:21:42 PM org.xnio.Xnio <clinit>
INFO: XNIO version 3.4.0.Final
Dec 10, 2016 12:21:42 PM org.xnio.nio.NioXnio <clinit>
INFO: XNIO NIO Implementation Version 3.4.0.Final
Dec 10, 2016 12:21:43 PM org.jboss.remoting3.EndpointImpl <clinit>
INFO: JBoss Remoting version 4.0.21.Final
Dec 10, 2016 12:21:44 PM org.jboss.ejb.client.remoting.VersionReceiver handleMessage
INFO: EJBCLIENT000017: Received server version 2 and marshalling strategies [river]
Dec 10, 2016 12:21:44 PM org.jboss.ejb.client.remoting.RemotingConnectionEJBReceiver associate
INFO: EJBCLIENT000013: Successful version handshake completed for receiver context EJBReceiverContext{clientContext=org.jboss.ejb.client.EJBClientContext@7b49cea0, receiver=Remoting connection EJB receiver [connection=org.jboss.ejb.client.remoting.ConnectionPool$PooledConnection@887af79,channel=jboss.ejb,nodename=sam-win]} on channel Channel ID c70cba3e (outbound) of Remoting connection 5e853265 to localhost/ of endpoint "config-based-ejb-client-endpoint" <67205a84>
Result computed by EJB is :10

This is the structure for the project: