TrailBlazer第8天--Message Driven POJOs
Posted on 2006-07-27 15:17 Earth 閱讀(245) 評論(0) 編輯 收藏 所屬分類: JavaEE5/EJB3一、定義服務接口,并用@Producer標注
Calculator.java?
package
?trail.mdpojo;
? import ?org.jboss.annotation.ejb.Producer;
?@Producer
? public ? interface ?Calculator?{
?
??? public ? void ?doCalculation?( long ?sent,? int ?start,? int ?end,
?????????????????????????????? double ?growthrate,
?????????????????????????????? double ?saving);?
?}?
? import ?org.jboss.annotation.ejb.Producer;
?@Producer
? public ? interface ?Calculator?{
?
??? public ? void ?doCalculation?( long ?sent,? int ?start,? int ?end,
?????????????????????????????? double ?growthrate,
?????????????????????????????? double ?saving);?
?}?
二、定義服務實現,并用@Consumer標注,同MDB一樣,如果所監聽的消息隊列不存在,JBoss會自動創建一個,不需要任何XML配置文件
MdpojoCalculator.java?
?
package
?trail.mdpojo;
? import ?org.jboss.annotation.ejb.Consumer;
? import ?javax.ejb. * ;
? import ?java.sql.Timestamp;
?
?@Consumer(activationConfig? =
?{
???@ActivationConfigProperty(propertyName = " destinationType " ,
?????propertyValue = " javax.jms.Queue " ),
???@ActivationConfigProperty(propertyName = " destination " ,
?????propertyValue = " queue/mdpojo " )
?})
? public ? class ?MdpojoCalculator? implements ?Calculator?{
?
??? public ? void ?doCalculation?( long ?sent,? int ?start,? int ?end,? double ?growthrate,?
double ?saving)?{
?
????? double ?result? = ?calculate?(start,?end,?growthrate,?saving);
?????RecordManager.addRecord?( new ?Timestamp(sent),?result);
?
?????System.out.println?( " The?MD?POJO?is?invoked " );
???}
??? //
?}??
? import ?org.jboss.annotation.ejb.Consumer;
? import ?javax.ejb. * ;
? import ?java.sql.Timestamp;
?
?@Consumer(activationConfig? =
?{
???@ActivationConfigProperty(propertyName = " destinationType " ,
?????propertyValue = " javax.jms.Queue " ),
???@ActivationConfigProperty(propertyName = " destination " ,
?????propertyValue = " queue/mdpojo " )
?})
? public ? class ?MdpojoCalculator? implements ?Calculator?{
?
??? public ? void ?doCalculation?( long ?sent,? int ?start,? int ?end,? double ?growthrate,?
double ?saving)?{
?
????? double ?result? = ?calculate?(start,?end,?growthrate,?saving);
?????RecordManager.addRecord?( new ?Timestamp(sent),?result);
?
?????System.out.println?( " The?MD?POJO?is?invoked " );
???}
??? //

?}??
三、Message Driven POJOs的使用
???????如果要使用MDPOJO,你首先需要從JNDI中查找@Producer接口所對應的stub object,
這個stub object實現了Producer接口的同時還實現了ProducerObject接口。你需要用ProducerObject.getProducerManager()得到一個ProducerManager然后調用manager.connect().它會創建一個JMS連接。接下來就可以使用@Producer中定義的服務了。
calculator.jsp?
?
<%
@?page?
import
=
"
trail.mdpojo.*,?javax.naming.*,?java.text.*,
??????????????????org.jboss.ejb3.mdb. * " %>
?
? <%
??? if ?( " send " .equals(request.getParameter?( " action " )))?{
?
????? int ?start? = ?Integer.parseInt(request.getParameter?( " start " ));
????? int ?end? = ?Integer.parseInt(request.getParameter?( " end " ));
????? double ?growthrate? = ?Double.parseDouble(request.getParameter?( " growthrate " ));
????? double ?saving? = ?Double.parseDouble(request.getParameter?( " saving " ));
?
????? // ?The?sent?timestamp?acts?as?the?message's?ID
????? long ?sent? = ?System.currentTimeMillis();
?
?????Calculator?cal? = ? null ;
?????ProducerManager?manager? = ? null ;
????? try ?{
?
???????InitialContext?ctx? = ? new ?InitialContext();
???????cal? = ?(Calculator)?ctx.lookup(
???????????????????Calculator. class .getName());
??????? // ?cal?=?(Calculator)?ctx.lookup(
??????? // ?????????????"EJB3Trail/MdpojoCalculator/remote");
??????ProducerObject?po? = ?(ProducerObject)?cal;
???????manager? = ?po.getProducerManager();
?
?????}? catch ?(Exception?e)?{
???????e.printStackTrace?();
?????}
?
?????manager.connect();? // ?internally?create?a?JMS?connection
????? try ?{
???????cal.doCalculation(sent,?start,?end,
?????????????????????????growthrate,?saving);
?????}? finally ?{
???????manager.close();? // ?clean?up?the?JMS?connection
?????}
? %> ?
??????????????????org.jboss.ejb3.mdb. * " %>
?
? <%
??? if ?( " send " .equals(request.getParameter?( " action " )))?{
?
????? int ?start? = ?Integer.parseInt(request.getParameter?( " start " ));
????? int ?end? = ?Integer.parseInt(request.getParameter?( " end " ));
????? double ?growthrate? = ?Double.parseDouble(request.getParameter?( " growthrate " ));
????? double ?saving? = ?Double.parseDouble(request.getParameter?( " saving " ));
?
????? // ?The?sent?timestamp?acts?as?the?message's?ID
????? long ?sent? = ?System.currentTimeMillis();
?
?????Calculator?cal? = ? null ;
?????ProducerManager?manager? = ? null ;
????? try ?{
?
???????InitialContext?ctx? = ? new ?InitialContext();
???????cal? = ?(Calculator)?ctx.lookup(
???????????????????Calculator. class .getName());
??????? // ?cal?=?(Calculator)?ctx.lookup(
??????? // ?????????????"EJB3Trail/MdpojoCalculator/remote");
??????ProducerObject?po? = ?(ProducerObject)?cal;
???????manager? = ?po.getProducerManager();
?
?????}? catch ?(Exception?e)?{
???????e.printStackTrace?();
?????}
?
?????manager.connect();? // ?internally?create?a?JMS?connection
????? try ?{
???????cal.doCalculation(sent,?start,?end,
?????????????????????????growthrate,?saving);
?????}? finally ?{
???????manager.close();? // ?clean?up?the?JMS?connection
?????}
? %> ?
?