快上网专注成都网站设计 成都网站制作 成都网站建设
成都网站建设公司服务热线:028-86922220

网站建设知识

十年网站开发经验 + 多家企业客户 + 靠谱的建站团队

量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决

MessageDrivenBeansTutorial(新增的EJB类型,绝对精彩!!!!!)(转)

Message Driven Beans Tutorial(新增的EJB类型,绝对精彩!!!!!) (转)[@more@]

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



What is a Message Driven Bean?

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.






Message processing before (above) and after (below) Message Driven Beans.






Structure of an MDB

  • It has no home or remote interfaces, and is only a bean class.

  • It resembles a stateless session bean - that is, it has short-lived instances and does not retain state for a client.

  • A client interacts with the MDB in the same way it interacts with a JMS application or JMS server.

  • Through the MDB, the EJB 2.0 Container sets itself up as a listener for asynchronous invocation and directly invokes the bean (no interfaces), which then behaves like an enterprise bean.

  • All instances of a particular MDB type are equivalent as they are not directly visible to the client and maintain no conversational state. This means that the Container can pool instances to enhance scalability.


Lifecycle of an MDB

The EJB Container performs several tasks at the beginning of the life cycle of the MDB:

  • Creates a message consumer (a QueueReceiver or TopicSubscriber) to receive the messages

  • Associates the bean with a destination and connection factory at deployment

  • Registers the message listener and the message acknowledgement mode

The 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

Writing an MDB involves the following tasks:

  • Implement the javax.ejb.MessageDrivenBean and javax.jms.MessageListener interfaces in the MDB class.

  • Provide an implementation of the business logic inside the onMessage().

  • Provide a setMessageDrivenContext() method that associates the bean with its environment.

  • Provide an ejbCreate() method that returns void and takes no arguments. This method may be blank.

  • Provide an ejbRemove() method implementation. This method may be blank, unless certain resources need to be acquired before the bean goes out of SCOpe.

Sample application

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.


Sample application components

Client StockServer.java Acts as a Server and displays fluctuations in stock prices.
Destinations
StockMarket A topic where stock price details are published to StockServer BuyQueue A queue on which BuyAgent MDB listens for stocks under buy advise SellQueue A queue on which SellAgent MDB listens for stocks under sell advise
Message Driven Beans
BuyAgentMDB.java Invoked by messages received from BuyQueue and in turn updates the topic StockMarket SellAgentMDB.java Invoked by messages received from SellQueue and in turn updates the topic StockMarket SubscriberBean.java sends client's buy/sell actions to BuyQueue or SellQueue on the message server