SkillFox Bank Ltd is currently using
manual transaction system, which they want to convert into Internet enabled
computerized system.
As the new system is
Internet enabled, their software consultant has suggested using Java, EJB &
related technologies for developing this distributed system.
Considering the above scenario,
develop a component using EJB to calculate the interest for any type of account
(like Savings, Current etc). All above-mentioned types of accounts will
reuse this bean.
Hints
1. Interest (Remote Interface)
This interface defines the `Remote'
interface for the `Interest' EJB. Its single method is the only method exposed to
the outside world. The class InterestBean implements this method.
2. InterestHome (Home
Interface)
This interface
defines the `home' interface for the `Interest' EJB. It creates an instance of
the `InterestBean' class on the server, and returns a remote reference to an
Interest interface on the client.
3. InterestBean (Bean class)
This class contains the implementation for
the `calculateCompoundInterest' method exposed by this Bean.
This method
calculates the compound interest on the sum `principle', with interest rate per
period `rate' over `periods' time periods. This method also prints a message to
standard output.
4. InterestClient (Client)
Client will
call the “calculateCompoundInterest” method by passing necessary arguments.
5. Create META-INF folder in
current working directory. Create ejb-jar.xml
and jboss.xml files and save in META-INF
folder.
Complete Solution of Case Study
Interest (Remote Interface)
import javax.ejb.EJBObject;
import java.rmi.RemoteException;
public interface Interest extends EJBObject
{
public double calculateCompoundInterest(double principle, double rate,
double periods) throws RemoteException;
}
InterestHome
(Home Interface)
import java.io.Serializable;
import java.rmi.RemoteException;
import javax.ejb.CreateException;
import javax.ejb.EJBHome;
public interface InterestHome extends EJBHome
{
Interest create() throws
RemoteException, CreateException;
}
InterestBean
(Bean class)
import java.rmi.RemoteException;
import javax.ejb.SessionBean;
import javax.ejb.SessionContext;
public class InterestBean implements SessionBean
{
public double calculateCompoundInterest(double principle, double rate,
double periods)
{
System.out.println
("Someone called `calculateCompoundInterest!'");
return principle *
Math.pow(1+rate, periods) - principle;
}
/**
Empty method body
*/
public InterestBean() {}
/**
Empty method body
*/
public void ejbCreate() {}
/**
Empty method body
*/
public void ejbRemove() {}
/**
Empty method body
*/
public void ejbActivate() {}
/**
Empty method body
*/
public void ejbPassivate() {}
/**
Empty method body
*/
public void setSessionContext(SessionContext sc) {}
}
InterestClient
(Client)
import javax.ejb.*;
import javax.naming.InitialContext;
import javax.rmi.PortableRemoteObject;
import java.util.Properties;
class InterestClient
{
public static void main(String[] args)
{
System.setProperty("java.naming.factory.initial","org.jnp.interfaces.
NamingContextFactory");
System.setProperty("java.naming.provider.url","192.168.21.75:1099");
System.setProperty("java.naming.factory.url.pkgs","org.jboss.naming");
try
{
/* system properties for JNDI
initialization Get the naming context
*/
InitialContext jndiContext = new InitialContext();
System.out.println("Context printed");
Object ref =
jndiContext.lookup("interest/Interest");
System.out.println("Got reference");
// Get a reference from this to the Bean's Home interface
InterestHome home = (InterestHome)
PortableRemoteObject.narrow (ref, InterestHome.class);
// Create an Interest object from the Home interface
Interest interest = home.create();
// call the calculateCompoundInterest() method to do the calculation
System.out.println("Interest
on 1000 units, at 10% per period, compounded over 2 periods is:");
System.out.println(interest.calculateCompoundInterest (1000, 0.10, 2));
}
catch(Exception e)
{
System.out.println(e.toString());
}
}
}
META-INF
( It consist of 2 XML files i.e. jboss.xml & ejb-jar.xml)
<?xml version="1.0" encoding="UTF-8"?>
<ejb-jar>
<description>jBoss test
application </description>
<display-name>Test</display-name>
<enterprise-beans>
<session>
<ejb-name>Interest</ejb-name>
<home>InterestHome</home>
<remote>Interest</remote>
<ejb-class>InterestBean</ejb-class>
<session-type>Stateless</session-type>
<transaction-type>Bean</transaction-type>
</session>
</enterprise-beans>
</ejb-jar>
<?xml version="1.0" encoding="UTF-8"?>
<jboss>
<enterprise-beans>
<session>
<ejb-name>Interest</ejb-name>
<jndi-name>interest/Interest</jndi-name>
</session>
</enterprise-beans>
</jboss>
|