Thursday, March 20, 2014

Create Simple Stateless session bean with EJB 3.x


You can create a simple java application project with maven. then you need to create the business interface and implementation beans for the EJB.

 Then the application will looks like as below.


 pom.xml

    4.0.0
    com.chathurangaonline.ejb.samples
    ejb-business-interface
    jar
    1.0
    ejb-business-interface
    http://maven.apache.org
    

        
            junit
            junit
            3.8.1
            test
        

        
            org.testng
            testng
            6.8.8
        

        
            javax
            javaee-api
            7.0
        

        
            org.jboss
            jboss-remote-naming
            1.0.7.Final
        

        
            org.jboss.xnio
            xnio-nio
            3.0.3.GA
        

    

    
        
            
                maven-compiler-plugin
                
                    1.7
                    1.7
                
            
        
    




SalaryCalculator.java (Business Interface)


package com.chathurangaonline.ejb.samples;

import javax.ejb.Remote;

@Remote
public interface SalaryCalculator {

    Double getAnnualIncrementOnBasicSalary(Double basicSalary);
}




@Remote annotation will be used because, i am planing to run the client in a separate JVM from the JVM where the EJB deployed and runs.




SalaryCalculatorImpl.java (Implementation Bean)


package com.chathurangaonline.ejb.samples;

import javax.ejb.Stateless;

@Stateless
public class SalaryCalculatorImpl implements SalaryCalculator{

    @Override
    public Double getAnnualIncrementOnBasicSalary(Double basicSalary) {
        if(basicSalary > 0 && basicSalary <= 35000){
            return basicSalary*0.2;
        }
        else if(basicSalary >35000){
            return  basicSalary*0.1;
        }
        else{
            throw new IllegalArgumentException();
        }
    }
}

this will be a stateless session bean.



SalaryCalculatorImplTest.java (TestNg Unit tests)


package com.chathurangaonline.ejb.samples;

import org.testng.Assert;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

public class SalaryCalculatorImplTest {

    private static SalaryCalculator salaryCalculator;

    @Test(expectedExceptions = IllegalArgumentException.class)
    public void testGetAnnualIncrementOnBasicSalaryWithNull() throws Exception {

       salaryCalculator.getAnnualIncrementOnBasicSalary(null);
    }

    @Test(expectedExceptions = IllegalArgumentException.class)
    public void testGetAnnualIncrementOnBasicSalaryWithNegativeValue() throws Exception{

     salaryCalculator.getAnnualIncrementOnBasicSalary(-90000D);
    }

    @Test(expectedExceptions = IllegalArgumentException.class)
    public void testGetAnnualIncrementOnBasicSalaryWithZero() throws Exception{

        salaryCalculator.getAnnualIncrementOnBasicSalary(0d);
    }

    @Test
    public void testGetAnnualIncrementOnBasicSalary(){

       double increment = salaryCalculator.getAnnualIncrementOnBasicSalary(50000d);
        System.out.println(" increment ["+increment+"]");
        Assert.assertTrue(increment>0);
    }

    @BeforeClass
    private void setUp(){
        if(salaryCalculator == null){
             salaryCalculator = new SalaryCalculatorImpl();
        }
    }
} 
 




Once this is developed, this can be compiled and packaged as a jar file.
then it can be deployed in any  EJB container (like Glassfish, Jboss etc...).
once it is deployed, check the log in the EJB continer to get the required JNDI
configurations for the EJB being deployed. those will be required when the
client is developed.

 we will look at in the next post, how to develop EJB cline tapplication for the EJB deployed in different EJB containers.


Thanks
Chathuranga Tennakoon
http://www.chathurangaonline.com/




No comments:

Post a Comment