google Analytics

Wednesday, July 28, 2010

EJB3 Stateless Bean Example

A stateful session bean automatically saves bean state between client invocations
without your having to write any additional code,Amazon. In contrast,
stateless session beans do not maintain any state and model application services
that can be completed in a single client invocation. You could build stateless
session beans for implementing business processes such as charging a credit card
or checking customer credit history

for example

import javax.ejb.Stateless;

@Stateless
public class CalculatorBean implements CalculatorRemote, CalculatorLocal
{
public int add(int x, int y)
{
return x + y;
}

public int subtract(int x, int y)
{
return x - y;
}
}


@Local
public interface CalculatorLocal extends Calculator
{
}

@Remote
public interface CalculatorRemote extends Calculator
{

}


public interface Calculator
{
int add(int x, int y);

int subtract(int x, int y);
}


public class Client
{
public static void main(String[] args) throws Exception
{
InitialContext ctx = new InitialContext();
Calculator calculator = (Calculator) ctx.lookup("CalculatorBean/remote");

System.out.println("1 + 1 = " + calculator.add(1, 1));
System.out.println("1 - 1 = " + calculator.subtract(1, 1));
}
}

No comments:

Post a Comment