Home » Enterprise Java » ejb3 » Stateless Session Bean » Simple Stateless Session Bean example

About Ilias Tsagklis

Ilias Tsagklis
Ilias is a software developer turned online entrepreneur. He is co-founder and Executive Editor at Java Code Geeks.

Simple Stateless Session Bean example

With this example we are going to demonstrate how to create a simple Stateless Session Bean. A stateless session bean does not maintain state with the client. When a client invokes the methods of a stateless bean, the bean’s instance variables may contain a state specific to that client, but only for the duration of the invocation. When the method is finished, the client-specific state should not be retained. Clients may, however, change the state of instance variables in pooled stateless beans, and this state is held over to the next invocation of the pooled stateless bean. Except during method invocation, all instances of a stateless bean are equivalent, allowing the EJB container to assign an instance to any client. That is, the state of a stateless session bean should apply accross all clients.

The EJB implementation class.

The CalculatorService is an EJB implementation class that is a stateless session bean that implements a few methods. It is annotated with the javax.ejb.Stateless annotation. The class must have a default public constructor.

package com.javacodegeeks.snippets.enterprise;

import javax.ejb.Stateless;

@Stateless
public class CalculatorService implements CalculatorServiceLocal, CalculatorServiceRemote {

    public CalculatorService() {
    }

    @Override
    public long add(long i, long j) {

  return (i + j);
    }

    @Override
    public double divide(long i, long j) {

  return ((double)i / j);
    }

    @Override
    public long multiply(long i, long j) {

  return (i * j);
    }

    @Override
    public long subtract(long i, long j) {

  return (i - j);
    }
    
}

The EJB local interface (suitable for in VM communication)

The local interface defines the bean’s business and life cycle methods. To build an enterprise bean that allows only local access, you must annotate the business interface of the enterprise bean as a @Local interface.

package com.javacodegeeks.snippets.enterprise;

import javax.ejb.Local;

@Local
public interface CalculatorServiceLocal {

    public long add(long i, long j);
    public long subtract(long i, long j);
    public long multiply(long i, long j);
    public double divide(long i, long j);
    
}

The EJB remote interface (suitable for intra VM communication)

A remote client of an enterprise bean can run on a different machine and a different Java virtual machine (JVM) than the enterprise bean it accesses. (It is not required to run on a different JVM.) To create an enterprise bean that allows remote access, you must decorate the business interface of the enterprise bean with the @Remote annotation:

package com.javacodegeeks.snippets.enterprise;

import javax.ejb.Remote;

@Remote
public interface CalculatorServiceRemote {

    public long add(long i, long j);
    public long subtract(long i, long j);
    public long multiply(long i, long j);
    public double divide(long i, long j);
    
}

The application.xml file describing the modules in the .ear archive

<?xml version="1.0" encoding="UTF-8"?>
<application xmlns="http://java.sun.com/xml/ns/j2ee" 


 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  


 xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/application_1_4.xsd"


 version="1.4">
  <display-name>Calculator Application</display-name>

  <module>
     <ejb>calculatorServiceEJB3.jar</ejb>
  </module>

</application>

The structure of the .ear archive

calculatorApp.ear
 |
 |_calculatorServiceEJB3.jar
 |   |_com
 |   |  |_javacodegeeks
 |   |     |_snippets
 |   |
  |_enterprise
 |   |
     |_CalculatorService.class
 |   |
     |_CalculatorServiceLocal.class
 |   |
     |_CalculatorServiceRemote.class
 |   |_META-INF
 |
 |_META-INF
    |_application.xml

Run the application using a client

In CalculatorServiceClient we connect to JBoss naming service running on local host and on default port 1099. We use the Context to set the configuration for the JBoss server, such as Context.INITIAL_CONTEXT_FACTORY, Context.PROVIDER_URL and Context.URL_PKG_PREFIXES. We get the bean using the lookup(Name name) method of Context to invoke its methods.

package com.javacodegeeks.snippets.enterprise;

import java.util.Hashtable;

import javax.naming.Context;
import javax.naming.InitialContext;


public class CalculatorServiceClient {

    public static void main(String[] a) throws Exception {


  /*

   * Connecting to JBoss naming service running on local host and on

   * default port 1099 the environment that should be created is like the

   * one shown below :

   */

  Hashtable env = new Hashtable();

  env.put(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory");

  env.put(Context.PROVIDER_URL, "jnp://localhost:1099");

  env.put(Context.URL_PKG_PREFIXES, "org.jboss.naming:org.jnp.interfaces");

  

  Context ctx = new InitialContext(env);

  

  CalculatorServiceRemote calculatorService = (CalculatorServiceRemote) ctx.lookup("calculatorApp/CalculatorService/remote");

  

  long i = 10;

  long j = 3;

  

  System.out.println(i+"+"+j+"="+calculatorService.add(i, j));

  System.out.println(i+"-"+j+"="+calculatorService.subtract(i, j));

  System.out.println(i+"*"+j+"="+calculatorService.multiply(i, j));

  System.out.println(i+"/"+j+"="+calculatorService.divide(i, j));

    }

}

Output:

10+3=13
10-3=7
10*3=30
10/3=3.3333333333333335

 
This was an example of how to create a simple Stateless Session Bean.

(No Ratings Yet)
1 Comment Views Tweet it!

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you our best selling eBooks for FREE!

 

1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design

 

and many more ....

 

Receive Java & Developer job alerts in your Area

 

1
Leave a Reply

avatar
1 Comment threads
0 Thread replies
1 Followers
 
Most reacted comment
Hottest comment thread
1 Comment authors
Jose Recent comment authors
  Subscribe  
newest oldest most voted
Notify of
Jose
Guest
Jose

In class CalculatorServiceClient, shouldn’t it be an import to CalculatorServiceRemote class? How should it be if it is in another project?
Thanks!