Free Online Courses for Software Developers - MrBool
× Please, log in to give us a feedback. Click here to login
×

You must be logged to download. Click here to login

×

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

×

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

How to use Enterprise Java Beans 3.x based application with Websphere 8.5 application server

In this article, we will learn about the process of creating EJB 3.x based application and deploying on Websphere 8.5 application server

We will be using eclipse for developing a sample EJB project which will be exported as an EAR file and deployed on Websphere Application Server. Testing will be done using a standalone Java client which will invoke the remote EJB interface and display the results. The sample EJB for this article is a simple stateless session bean that searches for the passed-in name in a default list and displays if that name exists in the list or not.

The complete process is carried out in following steps:

Step 1: Create an EJB based project from eclipse:

EJB project being created from eclipse

Figure 1: EJB project being created from eclipse

Add the following jar from Websphere Application Server in eclipse classpath (either copy the jar into your workspace or add it as an external JAR file) - <WAS_HOME>\AppServer\runtimes\com.ibm.ws.ejb.thinclient_8.5.0.jar

Step 2: In this step, we will create the EJB Remote Interface from eclipse.

Listing 1: Shows the code to perform step 2

package ejb31.test;

import javax.ejb.Remote;

/**
 * @author Administrator
 *
 */
@Remote
public interface ITestEJBRemoteInterface
{
   public boolean checkNames(String fsName);

}

In the above code, we have created a simple Remote Interface having checkNames() method which will be implemented in the stateless session bean.

Step 3: In this step, we will create a session bean which is stateless. Note the @Stateless annotation added to the bean. This bean holds a list of names in a list, performs a check if the passed in name is present in the list or not and returns true or false.

Listing 2: Shows the code to perform step 3

package ejb31.test;

import java.util.Arrays;
import java.util.List;

import javax.ejb.LocalBean;
import javax.ejb.Remote;
import javax.ejb.Stateless;

/**
 * Session Bean implementation class TestEJB
 */
@Stateless
public class TestEJB implements ITestEJBRemoteInterface {
   
   List<String> moListOfNames = Arrays.asList("Kevin","Jiten","Martina","Brian");

    /**
     * Default constructor. 
     */
    public TestEJB() 
    {
    }
    
   /**
    * Find if the passed name is present in the default list of names 
    * 
    * @return 
    */
   public boolean checkNames(String fsName)
    {
       boolean lboolNamePresent = false;
       
       if(fsName != null)
       {
          lboolNamePresent = moListOfNames.contains(fsName);
       }
       
       return lboolNamePresent;
    }

}

Step 4: ejb-jar.xml entry

Listing 3: Shows the code to perform step 4

<?xml version="1.0" encoding="UTF-8"?>
<ejb-jar xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:ejb="http://java.sun.com/xml/ns/javaee/ejb-jar_3_0.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/ejb-jar_3_1.xsd" version="3.1">
  <display-name> 
TestEJB3.1 </display-name>
</ejb-jar>

EJB display name is set as TestEJB3.1 as shown above.

Step 5: In this step, we will create another project to write the standalone java client.

Listing 4: Shows the code to perform step 5

package ejb31.test;

import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

import javax.ejb.EJB;
import javax.ejb.embeddable.EJBContainer;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

public class TestEJBClient
{


   /**
    * @param args
    */
   public static void main(String[] args)
   {
      
      Properties props = new Properties();

      props.put(Context.INITIAL_CONTEXT_FACTORY,
            "com.ibm.websphere.naming.WsnInitialContextFactory");

      // HOST_NAME – abc.com, BOOTSTRAP_ADDRESS PORT - 2809
      props.put(javax.naming.Context.PROVIDER_URL,
            "iiop://10.23.45.21:2809");     
     
      ITestEJBRemoteInterface loEJB =null;
      Object lobj;
      try
      {
         InitialContext ctx = new InitialContext(props);

         lobj = ctx.lookup("checkName");

         if (lobj instanceof ITestEJBRemoteInterface)
         {
            loEJB = (ITestEJBRemoteInterface) lobj;
         }
         
         String lsName = "Imran";

         // Invoke the Method using bean object ;
         System.out.println("Is "+ lsName + " present in the list:: "+loEJB.checkNames(lsName));

         System.out.println("EJB run successful");
      }
      catch (NamingException e)
      {
         // TODO Auto-generated catch block
         e.printStackTrace();
      }

   }

}

The above test client passes a name to the checkNames method of the stateless session bean and returns a result saying whether that name is present in the list or not.

We also need to create EJB stubs for the standalone client which can be done using createEJBStubs.bat file found under <WAS_HOME>/bin directory.

This bat file can be run against an EAR file or JAR file. In the current case, we export the EJB jar file (from the project that was created in Step 1) and run the following command:

> createEJBStubs.bat D:\temp\TestEJB3.1.jar
Processing the D:\temp\TestEJB3.1.jar input file.
Command Successful

If we look at the TestEJB3.1.jar file, new Stub class is added.

Newly generated TestEJB3.1.jar file

Figure 2: Newly generated TestEJB3.1.jar file

The TestEJB3.1.jar file needs to be added in classpath of standalone client project.

Note: createEJBStubs.bat file needs to be run only if the client environment is one of the following:

  1. Simple Java SE VM ( applicable in the current case )
  2. Websphere Application Server without the EJB 3.0 Feature Pack applied
  3. Any non-Websphere application server environment like Tomcat, Weblogic,etc.

Step 6: Below is the complete project as seen in eclipse

Complete web project as seen from eclipse

Figure 3: Complete web project as seen from eclipse

Step 7: Export the EJB project as an EAR file from eclipse and deploy it on WAS 8.5

JNDI binding set for the deployed EJB 3.x application in Websphere Application Server

Figure 4: JNDI binding set for the deployed EJB 3.x application in Websphere Application Server

The JNDI Name is used in the Test client for context lookup as shown below:

         InitialContext ctx = new InitialContext(props);

         lobj = ctx.lookup("checkName");

Let’s try to understand the Provider URL used for making connection to the Websphere application:

iiop://10.23.45.21:2809

iiop://<hostname:port>
Where
hostname – host name or ip of machine where Websphere application server is installed
port – BOOTSTRAP_ADDRESS port under Ports for the server where this application is deployed

 
Bootstrap Adress

Figure 5: Bootstrap Adress

Step 8: In this step, we will invoke remote EJB from the client that was created in Step 4.

Listing 5: Shows the output after running the standalone client as a java application

JSAS1480I: Security is not enabled because the ConfigURL property file is not set.
Is Imran present in the list:: false
EJB run successful

Step 9: Let’s modify the client to send a name that is present in the list and see what happens

Listing 6: Shows the part of Test Client which is modified. One of the inputs is changed to a String

      String lsName = "Kevin";

Listing 7: Shows the output after running the Client as a java application

JSAS1480I: Security is not enabled because the ConfigURL property file is not set.
Is Kevin present in the list:: true
EJB run successful
 

Suppose, we give incorrect JNDI name for lookup as follows :

Listing 8: Shows the JNDI lookup name change

InitialContext ctx = new InitialContext(props);

lobj = ctx.lookup("Name");        

Listing 9: Shows the output after running the Client as a java application

JSAS1480I: Security is not enabled because the ConfigURL property file is not set.
javax.naming.NameNotFoundException: Context: <CellName>/nodes/<NodeName>/servers/server1, name: Name: First component in name Name not found. [Root exception is org.omg.CosNaming.NamingContextPackage.NotFound: IDL:omg.org/CosNaming/NamingContext/NotFound:1.0]
	at com.ibm.ws.naming.jndicos.CNContextImpl.mapNotFoundException(CNContextImpl.java:4563)
	at com.ibm.ws.naming.jndicos.CNContextImpl.doLookup(CNContextImpl.java:1821)
	at com.ibm.ws.naming.jndicos.CNContextImpl.doLookup(CNContextImpl.java:1776)
	at com.ibm.ws.naming.jndicos.CNContextImpl.lookupExt(CNContextImpl.java:1433)
	at com.ibm.ws.naming.jndicos.CNContextImpl.lookup(CNContextImpl.java:615)
	at com.ibm.ws.naming.util.WsnInitCtx.lookup(WsnInitCtx.java:165)
	at com.ibm.ws.naming.util.WsnInitCtx.lookup(WsnInitCtx.java:179)
	at javax.naming.InitialContext.lookup(InitialContext.java:436)
	at ejb31.test.TestEJBClient.main(TestEJBClient.java:38)
Caused by: org.omg.CosNaming.NamingContextPackage.NotFound: IDL:omg.org/CosNaming/NamingContext/NotFound:1.0
	at org.omg.CosNaming.NamingContextPackage.NotFoundHelper.read(NotFoundHelper.java:95)
	at com.ibm.WsnOptimizedNaming._NamingContextStub.resolve_complete_info(_NamingContextStub.java:506)
	at com.ibm.ws.naming.jndicos.CNContextImpl$2.run(CNContextImpl.java:2957)
	at com.ibm.ws.naming.jndicos.CNContextImpl$2.run(CNContextImpl.java:2953)
	at com.ibm.ws.naming.util.CommonHelpers.retry(CommonHelpers.java:801)
	at com.ibm.ws.naming.jndicos.CNContextImpl.cosResolve(CNContextImpl.java:2951)
	at com.ibm.ws.naming.jndicos.CNContextImpl.doLookup(CNContextImpl.java:1817)
	... 7 more

As we can see from above, we get NameNotFoundException if we perform incorrect JNDI name lookup. This is the most frequent error which we come across while performing context lookup.

Conclusion

Finally, in this article, we have learnt invoking a remote EJB 3.x application deployed on Websphere Application Server from a standalone java client.

Also read



I have done my bachelor in Computer Science and I am well versed with programming languages such as JAVA, C#, html and done courses on Android development.

What did you think of this post?
Services
[Close]
To have full access to this post (or download the associated files) you must have MrBool Credits.

  See the prices for this post in Mr.Bool Credits System below:

Individually – in this case the price for this post is US$ 0,00 (Buy it now)
in this case you will buy only this video by paying the full price with no discount.

Package of 10 credits - in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download few videos. In this plan you will receive a discount of 50% in each video. Subscribe for this package!

Package of 50 credits – in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download several videos. In this plan you will receive a discount of 83% in each video. Subscribe for this package!


> More info about MrBool Credits
[Close]
You must be logged to download.

Click here to login