大橙子网站建设,新征程启航
为企业提供网站建设、域名注册、服务器等服务
The following tutorial illustrates how a Message Driven Bean is written and deployed in an Enterprise JavaBeansTM 2.0 Container. The Mdb component is invoked by an inbound message from a Java client. This function is demonstrated with a sample application run on Pramati Server 3.0 (Alpha). The Server ships with Pramati Message Server 1.0 and can be DOWNLOADed from www.pramati.com. The application sources are freely downloadable to get a better understanding of how MDB components are written and work.
创新互联主要从事网站建设、网站设计、网页设计、企业做网站、公司建网站等业务。立足成都服务平度,十年网站建设经验,价格优惠、服务专业,欢迎来电咨询建站服务:18980820575
A message driven bean is a stateless, server-side, transaction-aware component that is driven by a Java message (javax.JMS.message
). It is invoked by the EJB Container when a message is received from a JMS Queue or Topic. It acts as a simple message listener.
A Java client, an enterprise bean, a Java ServerPagesTM (JSP) component, or a non-J2EE application may send the message. The client sending the message to the destination need not be aware of the MDBs deployed in the EJB Container. However, the message must confoRM to JMS specifications.
Before MDBs were introduced, JMS described a classical approach to implement asynchronous method invocation. The approach used an external Java program that acted as the listener, and on receiving a message, invoked a session bean method.
However, in this approach the message was received outside the application server and was thus not part of a transaction in the EJB Server. MDB solves this problem.
The EJB Container performs several tasks at the beginning of the life cycle of the MDB:
QueueReceiver
or TopicSubscriber
) to receive the messagesThe lifecycle of an MDB depends on the lifespan of the EJB Server in which it is deployed. As MDBs are stateless, bean instances are typically pooled by the EJB Server and retrieved by the Container when a message becomes available on the topic or queue.
Writing an MDB involves the following tasks:
javax.ejb.MessageDrivenBean
and javax.jms.MessageListener
interfaces in the MDB class.onMessage()
.setMessageDrivenContext()
method that associates the bean with its environment.ejbCreate()
method that returns void and takes no arguments. This method may be blank.ejbRemove()
method implementation. This method may be blank, unless certain resources need to be acquired before the bean goes out of SCOpe. The tutorial uses a sample application, StockTrader
, to illustrate the writing of an MDB. The sample uses a simple message driven bean buyAgentMDB
that is contacted by a client which wishes to buy shares. The client looks up the BuyQueue
and implements the javax.jms.MessageListener
. It provides a private method buy()
that takes two arguments: a double value that holds the price and a string (stockSymbol
) that holds the scrip symbol.