Thursday 21 May 2009

2-How to Develop Message-Driven Beans

Files located in the package src/mdbex:
FILE : OrderARSHome.java
package mdbex;

import java.util.*;
import java.rmi.*;
import javax.ejb.*;

public interface OrderARSHome extends EJBHome {
/* Create methods */
public OrderARS create(String studentId,
String status, double amount) throws CreateException, RemoteException;
/* Finder methods */
public OrderARS findByPrimaryKey(String key)
throws FinderException, RemoteException;
public Collection findByStatus(String status)
throws FinderException, RemoteException;
/* Home methods */
public double getTotalAmountOfAllOrders()
throws FinderException, RemoteException;
}

FILE: OrderARS.java
package mdbex;

import java.rmi.*;
import javax.ejb.*;

public interface OrderARS extends EJBObject {
public String getOrderId()
throws RemoteException;
public String getStudentId()
throws RemoteException;
public void setStudentId(String studentId)
throws RemoteException;
public double getAmount()
throws RemoteException;
public void setAmount(double amount)
throws RemoteException;
public java.sql.Timestamp getOrderDate()
throws RemoteException;
public void setOrderDate(java.sql.Timestamp date)
throws RemoteException;
public String getStatus()
throws RemoteException;
public void setStatus(String status)
throws RemoteException;
}

FILE: OrderARSEJB.java
package mdbex;

import java.util.*;
import java.io.*;
import java.rmi.*;
import javax.naming.*;
import javax.ejb.*;

public abstract class OrderARSEJB implements EntityBean
{
protected EntityContext ctx;
public abstract String getOrderId();
public abstract void setOrderId(String orderId);
public abstract String getStudentId();
public abstract void setStudentId(String studentid);
public abstract java.sql.Timestamp getOrderDate();
public abstract void setOrderDate(java.sql.Timestamp timestamp);
public abstract String getStatus();
public abstract void setStatus(String status);
public abstract double getAmount();
public abstract void setAmount(double amount);
/* Callback methods */
public void setEntityContext(EntityContext ctx) {
print("setEntityContext called");
this.ctx = ctx;
}
public void unsetEntityContext() {
print("unsetEntityContext called.\n");
this.ctx = null;
}
public void ejbActivate() {
print("ejbActivate() called.\n");
}
public void ejbPassivate() {
print("ejbPassivate() called.\n");
}
public void ejbStore() {
print("ejbStore() called.\n");
}
public void ejbLoad() {
print("ejbLoad() called.\n");
}
public void ejbRemove() throws RemoveException {
print("ejbRemove() called.\n");
}
public String ejbCreate( String studentId,
String status, double amount) throws CreateException {
print("ejbCreate() called.\n");
String orderId = getUniqueId();
setOrderId(orderId);
setStudentId(studentId);
setStatus(status);
setAmount(amount);
setOrderDate(new java.sql.Timestamp(System.currentTimeMillis()));
return null;
}
public void ejbPostCreate(String studentId,
String courseId, double amount) throws CreateException {
print("ejbPostCreate() called.\n");
}
/* Home methods */
public double ejbHomeGetTotalAmountOfAllOrders()
throws FinderException {
double totalAmount = 0.0;
Collection col = ejbSelectAllOrderAmounts();
Enumeration amounts=Collections.enumeration(col);
while( amounts.hasMoreElements() ) {
Double amount= (Double)amounts.nextElement();
totalAmount += amount.doubleValue();
}
return totalAmount;
}
/* select methods. */
public abstract Collection ejbSelectAllOrderAmounts()
throws FinderException ;

void print(String s) {
System.out.println(s);
}
String getUniqueId(){
return new Long(System.currentTimeMillis()).toString();
}
}