Let's go inside

this blog is deprecated as a result of laziness.
posts - 59, comments - 2, trackbacks - 0, articles - 0

TrailBlazer第7天--MDB

Posted on 2006-07-27 14:24 Earth 阅读(213) 评论(0)  编辑  收藏 所属分类: JavaEE5/EJB3

The JMS clients send messages to message queues managed by the server (e.g., an email inbox can be a message queue). The message queues are monitored by a special kind of EJBs --Message Driven Beans (MDBs). The MDB processes the incoming messages and perform the services requested by the message. The MDBs are the end-point for JMS service request messages.
JMS客户端向消息队列(比如收件箱)发送消息。
消息队列(收件箱)由MDB监视,MDB处理进来的消息并提供消息所需要的服务

In this trail, the investment calculator application is refactored to use a Message Driven Bean. When you click on the "Calculate" button on the web form, the JSP page (calculator.jsp) sends a message containing the calculation parameters to a message queue. It then forwards to another JSP page (check.jsp) to periodically check whether the calculation result becomes available.
当你点击页面中的Calculate按钮时,JSP会把含有计算所需参数的信息发送到一个消息队列,然后页面跳转到check.jsp, check.jsp不断刷新自己以定期检查计算结果是否可用。

The message queue is monitored by a MDB, which retrieves the incoming message, parses its contents, and performs the calculation. After the calculation is done, the MDB saves the result to an application-wide shared cache. The result is then picked up and displayed by the result-monitoring JSP page (i.e., check.jsp).
该消息队列由一个MDB监控,当它检索到一条消息后,解析消息内容,然后进行计算,然后把计算 结果放在application中。最后这个结果被check.jsp捕获并显示。

1。监听器 The Message Driven Beans

@MessageDriven(activationConfig  =
{
  @ActivationConfigProperty(propertyName
= " destinationType " ,
    propertyValue
= " javax.jms.Queue " ),
  @ActivationConfigProperty(propertyName
= " destination " ,
    propertyValue
= " queue/mdb " )
})
public   class  CalculatorBean  implements  MessageListener {

  
public   void  onMessage (Message msg) {
    
try  {
      TextMessage tmsg 
=  (TextMessage) msg;
      Timestamp sent 
=
          
new  Timestamp(tmsg.getLongProperty( " sent " ));
      StringTokenizer st 
=
          
new  StringTokenizer(tmsg.getText(),  " , " );

      
int  start  =  Integer.parseInt(st.nextToken());
      
int  end  =  Integer.parseInt(st.nextToken());
      
double  growthrate  =  Double.parseDouble(st.nextToken());
      
double  saving  =  Double.parseDouble(st.nextToken());

      
double  result  =
          calculate (start, end, growthrate, saving);
      RecordManager.addRecord (sent, result);

    } 
catch  (Exception e) {
      e.printStackTrace ();
    }
  }
}

MDB用@MessageDriven标注
destinationType 表示所监听的消息的类型
destination 表示所监听的消息队列的名字(比如queue/mdb)
如果这个队列并不存在, JBoss会在deploy的时候自动创建一个,所以不需要XML配置文件!


2。在客户端的使用

  <% @ page  import = " trail.mdb.*, javax.naming.*, java.text.*,
                  javax.jms. * ,
                  java.sql.Timestamp
" %>
 
 
<%
   
if  ( " send " .equals(request.getParameter ( " action " ))) {
 
     QueueConnection cnn 
=   null ;
     QueueSender sender 
=   null ;
     QueueSession sess 
=   null ;
     Queue queue 
=   null ;
 
     
try  {
       InitialContext ctx 
=   new  InitialContext();
       queue 
=  (Queue) ctx.lookup( " queue/mdb " );
       QueueConnectionFactory factory 
=
           (QueueConnectionFactory) ctx.lookup(
" ConnectionFactory " );
       cnn 
=  factory.createQueueConnection();
       sess 
=  cnn.createQueueSession( false ,
                   QueueSession.AUTO_ACKNOWLEDGE);
 
     } 
catch  (Exception e) {
       e.printStackTrace ();
     }
 
     TextMessage msg 
=  sess.createTextMessage(
         request.getParameter (
" start " +   " , "   +
         request.getParameter (
" end " +   " , "   +
         request.getParameter (
" growthrate " +   " , "   +
         request.getParameter (
" saving " )
     );
     
//  The sent timestamp acts as the message's ID
      long  sent  =  System.currentTimeMillis();
     msg.setLongProperty(
" sent " , sent);
 
     sender 
=  sess.createSender(queue);
     sender.send(msg);
     
//  sess.commit ();
     sess.close ();
 
%>

To use the message driven bean, the client (i.e, JSP page calculator.jsp in this case) use the standard JMS API to obtain the target message queue to the MDB using the queue name (queue/mdb), and then send the message to the queue.

to be continued


只有注册用户登录后才能发表评论。


网站导航: