|
||||||||||
|
Chapter 9 Examples
Download the Chapter 9 code View the code examples 9.6 9.7 9.8 9.9 Example 9.1 Abstract DAOFactory Class
// Abstract class DAO Factory
public abstract class DAOFactory {
// List of DAO types supported by the factory
public static final int CLOUDSCAPE = 1;
public static final int ORACLE = 2;
public static final int SYBASE = 3;
...
// There will be a method for each DAO that can be
// created. The concrete factories will have to
// implement these methods.
public abstract CustomerDAO getCustomerDAO();
public abstract AccountDAO getAccountDAO();
public abstract OrderDAO getOrderDAO();
...
public static DAOFactory getDAOFactory(
int whichFactory) {
switch (whichFactory) {
case CLOUDSCAPE:
return new CloudscapeDAOFactory();
case ORACLE :
return new OracleDAOFactory();
case SYBASE :
return new SybaseDAOFactory();
...
default :
return null;
}
}
}
Example 9.2 Concrete DAOFactory Implementation for Cloudscape
// Cloudscape concrete DAO Factory implementation
import java.sql.*;
public class CloudscapeDAOFactory extends DAOFactory {
public static final String DRIVER=
"COM.cloudscape.core.RmiJdbcDriver";
public static final String DBURL=
"jdbc:cloudscape:rmi://localhost:1099/CoreJ2EEDB";
// method to create Cloudscape connections
public static Connection createConnection() {
// Use DRIVER and DBURL to create a connection
// Recommend connection pool implementation/usage
}
public CustomerDAO getCustomerDAO() {
// CloudscapeCustomerDAO implements CustomerDAO
return new CloudscapeCustomerDAO();
}
public AccountDAO getAccountDAO() {
// CloudscapeAccountDAO implements AccountDAO
return new CloudscapeAccountDAO();
}
public OrderDAO getOrderDAO() {
// CloudscapeOrderDAO implements OrderDAO
return new CloudscapeOrderDAO();
}
...
}
Example 9.3 Base DAO Interface for Customer
// Interface that all CustomerDAOs must support
public interface CustomerDAO {
public int insertCustomer(...);
public boolean deleteCustomer(...);
public Customer findCustomer(...);
public boolean updateCustomer(...);
public RowSet selectCustomersRS(...);
public Collection selectCustomersVO(...);
...
}
Example 9.4 Cloudscape DAO Implementation for Customer
// CloudscapeCustomerDAO implementation of the
// CustomerDAO interface. This class can contain all
// Cloudscape specific code and SQL statements.
// The client is thus shielded from knowing
// these implementation details.
import java.sql.*;
public class CloudscapeCustomerDAO implements
CustomerDAO {
public CloudscapeCustomerDAO() {
// initialization
}
// The following methods can use
// CloudscapeDAOFactory.createConnection()
// to get a connection as required
public int insertCustomer(...) {
// Implement insert customer here.
// Return newly created customer number
// or a -1 on error
}
public boolean deleteCustomer(...) {
// Implement delete customer here
// Return true on success, false on failure
}
public Customer findCustomer(...) {
// Implement find a customer here using supplied
// argument values as search criteria
// Return a value object if found,
// return null on error or if not found
}
public boolean updateCustomer(...) {
// implement update record here using data
// from the customerData value object
// Return true on success, false on failure or
// error
}
public RowSet selectCustomersRS(...) {
// implement search customers here using the
// supplied criteria.
// Return a RowSet.
}
public Collection selectCustomersVO(...) {
// implement search customers here using the
// supplied criteria.
// Alternatively, implement to return a Collection
// of value objects.
}
...
}
Example 9.5 Customer Value Object
public class Customer implements java.io.Serializable {
// member variables
int CustomerNumber;
String name;
String streetAddress;
String city;
...
// getter and setter methods...
...
}
Example 9.6 Using a DAO and DAO Factory Client Code
...
// create the required DAO Factory
DAOFactory cloudscapeFactory =
DAOFactory.getDAOFactory(DAOFactory.DAOCLOUDSCAPE);
// Create a DAO
CustomerDAO custDAO =
cloudscapeFactory.getCustomerDAO();
// create a new customer
int newCustNo = custDAO.insertCustomer(...);
// Find a customer object. Get the value object.
Customer cust = custDAO.findCustomer(...);
// modify the values in the value object.
cust.setAddress(...);
cust.setEmail(...);
// update the customer object using the DAO
custDAO.updateCustomer(cust);
// delete a customer object
custDAO.deleteCustomer(...);
// select all customers in the same city
Customer criteria=new Customer();
criteria.setCity("New York");
Collection customersList =
custDAO.selectCustomersVO(criteria);
// returns customersList - collection of Customer
// value objects. iterate through this collection to
// get values.
...
Example 9.7 Order Service Activator
public class OrderServiceActivator implements
javax.jms.MessageListener{
// Queue session and receiver: see JMS API for
// details
private QueueSession orderQueueSession;
private QueueReceiver orderQueueReceiver;
// Note: values should come from property files or
// environment instead of hard coding.
private String connFactoryName =
"PendingOrdersQueueFactory";
private String queueName = "PendingOrders";
// use a service locator to locate administered
// JMS components such as a Queue or a Queue
// Connection factory
private JMSServiceLocator serviceLocator;
public OrderServiceActivator(String connFactoryName,
String queueName) {
super();
this.connFactoryName = connFactoryName;
this.queueName = queueName;
startListener();
}
private void startListener() {
try {
serviceLocator = new JMSServiceLocator
(connFactoryName);
qConnFactory =
serviceLocator.getQueueConnectionFactory();
qConn = qConnFactory.createQueueConnection();
// See JMS API for method usage and arguments
orderQueueSession = qConn.createQueueSession (...);
Queue ordersQueue =
serviceLocator.getQueue(queueName);
orderQueueReceiver =
orderQueueSession.createReceiver(ordersQueue);
orderQueueReceiver.setMessageListener(this);
}
catch (JMSException excp) {
// handle error
}
}
// The JMS API specifies the onMessage method in the
// javax.jms.MessageListener interface.
// This method is asynchronously invoked
// when a message arrives on the Queue being
// listened to by the ServiceActivator.
// See JMS Specification and API for more details.
public void onMessage(Message msg) {
try {
// parse Message msg. See JMS API for Message.
...
// Invoke business method on an enterprise
// bean using the bean's business delegate.
// OrderProcessorDelegate is the business
// delegate for OrderProcessor Session bean.
// See Business Delegate pattern for details.
OrderProcessorDelegate orderProcDeleg =
new OrderProcessorDelegate();
// Use data values from the parsed message to
// invoke business method on bean via delegate
orderProcDeleg.fulfillOrder(...);
// send any acknowledgement here...
}
catch (JMSException jmsexcp) {
// Handle JMSExceptions, if any
}
catch (Exception excp) {
// Handle any other exceptions
}
}
public void close() {
try {
// cleanup before closing
orderQueueReceiver.setMessageListener (null);
orderQueueSession.close();
}
catch(Exception excp) {
// Handle exception - Failure to close
}
}
}
Example 9.8 Session Facade as Client for Service Activator
// imports...
public class OrderDispatcherFacade
implements javax.ejb.SessionBean {
...
// business method to create new Order
public int createOrder(...) throws OrderException {
// create new business order entity bean
...
// successfully created Order. send Order to
// asynchronous backend processing
OrderSender orderSender = new OrderSender();
orderSender.sendOrder(order);
// close the sender, if done...
orderSender.close();
// other processing
...
}
}
Example 9.9 OrderSender: Used to Dispatch Orders to Queue
// imports...
public class OrderSender {
// Queue session and sender: see JMS API for details
private QueueSession orderQueueSession;
private QueueSender orderQueueSender;
// These values could come from some property files
private String connFactoryName =
"PendingOrdersQueueFactory";
private String queueName = "PendingOrders";
// use a service locator to locate administered
// JMS components such as a Queue or a Queue.
// Connection factory
private JMSServiceLocator serviceLocator;
...
// method to initialize and create queue sender
private void createSender() {
try {
// using ServiceLocator and getting Queue
// Connection Factory is similar to the
// Service Activator code.
serviceLocator = new JMSServiceLocator
(connFactoryName);
qConnFactory =
serviceLocator.getQueueConnectionFactory();
qConn = qConnFactory.createQueueConnection();
// See JMS API for method usage and arguments
orderQueueSession = qConn.createQueueSession
(...);
Queue ordersQueue =
serviceLocator.getQueue(queueName);
orderQueueSender =
orderQueueSession.createSender(ordersQueue);
catch(Exception excp) {
// Handle exception - Failure to create sender
}
}
// method to dispatch order to fulfillment service
// for asynchronous processing
public void sendOrder(Order newOrder) {
// create a new Message to send Order object
ObjectMessage objMessage =
queueSession.createObjectMessage(order);
// set object message properties and delivery
// mode as required.
// See JMS API for ObjectMessage
// Set the Order into the object message
objMessage.setObject(order);
// send the message to the Queue
orderQueueSender.send(objMessage);
...
} catch (Exception e) {
// Handle exceptions
}
...
}
...
public void close() {
try {
// cleanup before closing
orderQueueReceiver.setMessageListener (null);
orderQueueSession.close();
}
catch(Exception excp) {
// Handle exception - Failure to close
}
}
}
|
|||||||||