Implementation
This section describes how OTN developers used message-driven
beans (MDBs) to implement an alert system in the FBS 10g. The FBS 10g also uses
MDBs to manage trade notifications, but because the implementation is essentially
the same, it is not covered here. The Design section
describes how OTN developers decided where to use MDBs.
There's relatively little Java code to write. The key is to
get the right information into the right configuration files. Here are the major
implementation tasks:
Writing the MDB
Implementation Class
The Java class for an MDB must implement the MessageDrivenBean
and MessageListener interfaces. They key method is onMessage,
which is invoked by the container. The following listing shows the implementation
of oracle.otnsample.ibfbs.admin.ejb.AlertQueue.
public void onMessage(javax.jms.Message message) { try { // Initialize context for lookup Context ctx = new InitialContext(); // Lookup for the mailservice bean in the JNDI tree MailServiceHome mailServiceHome = (MailServiceHome) ctx.lookup("MailService"); // Get an instance of mailservice MailService mailService = mailServiceHome.create(); TextMessage txtMessage = (TextMessage) message; // Based on the Alert Mode, send alert mail to either Email id or // Mobile mail box if (txtMessage.getStringProperty("alertMode").equals("M")) { mailService.sendMail(txtMessage.getStringProperty("mobile"), "alerts@fbs.com", "Stock Price Alert", getTextMsg( txtMessage.getStringProperty("accountNumber"), txtMessage.getStringProperty("name"), txtMessage.getStringProperty("symbol"), txtMessage.getStringProperty("minlimit"), txtMessage.getStringProperty("maxlimit"), txtMessage.getFloatProperty("rate"))); } else { mailService.sendMail(txtMessage.getStringProperty("email"), "alerts@fbs.com", "Stock Price Alert", getHTMLMsg( txtMessage.getStringProperty("accountNumber"), txtMessage.getStringProperty("name"), txtMessage.getStringProperty("symbol"), txtMessage.getStringProperty("minlimit"), txtMessage.getStringProperty("maxlimit"), txtMessage.getFloatProperty("rate"))); } } catch (Throwable ex) { // Trap errors ex.printStackTrace(); } }
Configuring ejb-jar.xml
The following listings come from the configuration file <fbs_home>\ejb\src\META-INF\ejb-jar.xml.
The first listing below names the MDB, identifies the class that implements
it, and specifies a destination class for associated messages.
<message-driven> <ejb-name>AlertMessageBean</ejb-name> <ejb-class>oracle.otnsamples.ibfbs.admin.ejb.AlertQueue</ejb-class> <transaction-type>Container</transaction-type> <message-driven-destination> <destination-type>javax.jms.Queue</destination-type> </message-driven-destination> </message-driven>
The following listing defines the ExchangeQueue message-driven
bean.
<message-driven>
<ejb-name>ExchangeMessageBean</ejb-name>
<ejb-class>oracle.otnsamples.ibfbs.admin.ejb.ExchangeQueue</ejb-class>
<transaction-type>Container</transaction-type>
<message-driven-destination>
<destination-type>javax.jms.Queue</destination-type>
</message-driven-destination>
</message-driven>
Configuring orion-ejb-jar.xml
The EJB 2.1 specification does not address the mapping of
an MDB to a destination (queue), so OTN developers used orion-ejb-jar.xml
to do the mapping.The following listing from <fbs_home>\ejb\src\META-INF\orion-ejb-jar.xml
specifies MDB deployment information.
<message-driven-deployment name="AlertMessageBean" destination-location="jms/AlertQueue" connection-factory-location="jms/AlertQueueConnectionFactory" />
Creating OC4J Queues
Edit <OC4J_HOME>/j2ee/home/config/jms.xml
and add the following tags within the <jms-server> tag.
<queue name="Exchange Queue" location="jms/ExchangeQueue">
<description>FBS Exchage Queue</description>
</queue>
<queue name="Alert Queue" location="jms/AlertQueue">
<description>FBS Alert queue</description>
</queue>
<queue-connection-factory name="jms/ExchangeQueueConnectionFactory"
location="jms/ExchangeQueueConnectionFactory"/>
<queue-connection-factory name="jms/AlertQueueConnectionFactory"
location="jms/AlertQueueConnectionFactory"/>
Sending Messages to the Queues
In the FBS 10g, Exchange Trading Operations (Buy Stocks, Sell
Stocks) as well as Alert processing are carried out using MDBs. This document
has described the implementation of AlertsBean. You can also view the source
file ExchangeQueue.java
to see how FBS 10g uses MDBs to handle real-time asynchronous tasks such as
trading operations.
You may also find the following snippet from TradeManagementSessionFacadeBean.java
interesting.
// Lazy loading of QueueConn factory and Q conn
if(exchageQConnfactory == null || exchangeQueue == null) {
InitialContext ic = new InitialContext();
exchageQConnfactory = (QueueConnectionFactory)ic.lookup(
(String)connHash.get("EXCHQCONNFACTORY"));
exchangeQueue = (Queue)ic.lookup((String)connHash.get("EXCHQUEUE"));
}
// Create Queue Connection
qconn = exchageQConnfactory.createQueueConnection();
qconn.start();
session = qconn.createQueueSession(false, 1);
// Create a message and set the trade details
Message message = session.createMessage();
message.setStringProperty("toAddress", userEmail);
message.setStringProperty("symbol", symbol);
message.setStringProperty("tradeDate", tradeDt);
message.setStringProperty("tradeType", tradeType);
message.setStringProperty("qty", quantity.toString());
message.setJMSExpiration(18000L);
// Send the message
session.createSender(exchangeQueue).send(message);
|