hk2000c技術專欄

          技術源于哲學,哲學來源于生活 關心生活,關注健康,關心他人

            BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理 ::
            111 隨筆 :: 1 文章 :: 28 評論 :: 0 Trackbacks
          關鍵字:   ActiveMQ    

          ActiveMQ 實踐之路(四) ActiveMQ 4.x +JBoss 4.x MDP實戰篇

                在<<ActiveMQ 實踐之路(三) ActiveMQ 4.x +JBoss 4.x 整合篇 >>里面我們比較詳細的講解了ActiveMQ與JBoss的整合,
          既然選擇了JBoss,那么項目里面或多或少都會使用到EJB,下面我們就詳細地介紹如何在ActiveMQ 4.x+JBOSS 4.x環境下開發
          Message Driven Bean,并且與使用jbossMQ在配置上作了比較詳細地比較。這里的OrderMessage 僅僅是一個自動生成的Message Driven Bean,在onMessage方法里面,作日志輸入。

           一. 配置ejb-jar.xml
                
                  1.  ejb-jar.xml 不能使用XML DTD,需要使用XML Schema(XSD)
                      
          很多朋友可能使用XDoclet來生成ejb-jar.xml,我這里直接使用XDoclet生成的ejb-jar.xml是

          <!--CTYPE ejb-jar PUBLIC "-//Sun Microsystems, Inc.//DTD Enterprise JavaBeans 2.0//EN" "http://java.sun.com/dtd/ejb-jar_2_0.dt-->

                     但是在ActiveMQ+JBoss配置中間需要使用新的XML Schema才能完成對ra的定義,如下.

          xml 代碼
          1. <ejb-jar xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/ejb-jar_2_1.xsd" version="2.1">  

           

          2. ejb-jar.xml 直接使用JBossMQ的Message DriverBean 和使用ActiveMQ RA配置的區別

                  (1) .使用JBossMQ的MessageDrivenBean的 ejb-jar.xml配置

          xml 代碼
          1. <!-- Message Driven Beans -->  
          2. <message-driven>  
          3.     <description>  
          4.           
          5.     description>  
          6.     <display-name>Name for OrderMessagedisplay-name>  
          7.     <ejb-name>OrderMessageejb-name>  
          8.     <ejb-class>com.mostone.ejb.OrderMessageejb-class>  
          9.     <transaction-type>Containertransaction-type>  
          10.     <acknowledge-mode>Auto-acknowledgeacknowledge-mode>  
          11.     <message-driven-destination>  
          12.         <destination-type>javax.jms.Queuedestination-type>  
          13.     message-driven-destination>  
          14. message-driven>  

               (2). 使用ActiveMQ RA配置的MessageDrivenBean的ejb-jar.xml配置

          xml 代碼
          1. <!-- Message Driven Beans -->  
          2. <message-driven>  
          3.     <description>  
          4.           
          5.     description>  
          6.     <display-name>Name for OrderMessagedisplay-name>  
          7.     <ejb-name>OrderMessageejb-name>  
          8.     <ejb-class>com.mostone.ejb.OrderMessageejb-class>  
          9.     <transaction-type>Containertransaction-type>  
          10.     <acknowledge-mode>Auto-acknowledgeacknowledge-mode>  
          11.     <!--使用了activetion-config-->  
          12.     <activation-config>  
          13.         <activation-config-property>  
          14.             <activation-config-property-name>destinationactivation-config-property-name>  
          15.             <activation-config-property-value>queue.outboundactivation-config-property-value>  
          16.         activation-config-property>  
          17.         <activation-config-property>  
          18.             <activation-config-property-name>destinationTypeactivation-config-property-name>  
          19.             <activation-config-property-value>javax.jms.Queueactivation-config-property-value>  
          20.         activation-config-property>  
          21.     activation-config>  
          22. message-driven>  

                  其中destination,destinationType是ra.xml里面提供的配置屬性,(這里官方的文檔是Destination,DestinationType, 而實際上activemq-ra.rar里面的ra.xml是destination,destinationType,注意大小寫區別)

          . 配置jboss.xml
                     大部分配置都是在jboss.xml里面.
                   1.使用JBossMQ 和使用ActiveMQ RA配置Message Driven Bean的區別
                     1.) 使用JBossMQ 的配置


          xml 代碼
          1. <message-driven>  
          2.     <ejb-name>OrderMessageejb-name>  
          3.     <destination-jndi-name>queue/testQueuedestination-jndi-name>  
          4. message-driven>  

                   2.) 使用ActiveMQ RA的配置

          xml 代碼
          1. <message-driven>  
          2.     <ejb-name>ActiveMQMDPejb-name>  
          3.     <!-- 使用了resource-adapter-name -->  
          4.     <resource-adapter-name>activemq-ra.rarresource-adapter-name>  
          5.     <!-- 這里的configuration-name 需要和后面container-name 的名字相同 -->  
          6.     <configuration-name>ActiveMQ Message Driven Beanconfiguration-name>  
          7. message-driven>  

                  2. jboss.xml 配置invoker proxy和container 支持ActiveMQ RA

          xml 代碼
          1. <invoker-proxy-bindings>  
          2.     <invoker-proxy-binding>  
          3.         <name>activemq-message-driven-beanname>  
          4.         <invoker-mbean>defaultinvoker-mbean>  
          5.         <proxy-factory>org.jboss.ejb.plugins.inflow.JBossMessageEndpointFactoryproxy-factory>  
          6.         <proxy-factory-config>  
          7.             <endpoint-interceptors>  
          8.                 <interceptor>org.jboss.proxy.ClientMethodInterceptorinterceptor>  
          9.                 <interceptor>org.jboss.ejb.plugins.inflow.MessageEndpointInterceptorinterceptor>  
          10.                 <interceptor>org.jboss.proxy.TransactionInterceptorinterceptor>  
          11.                 <interceptor>org.jboss.invocation.InvokerInterceptorinterceptor>  
          12.             endpoint-interceptors>  
          13.         proxy-factory-config>  
          14.     invoker-proxy-binding>  
          15. invoker-proxy-bindings>  

          MessageDrivenBean的container配置,這里的必須和上面的相同 才能起作用.


           

          xml 代碼
          1. <container-configurations>  
          2.     <container-configuration>  
          3.         <container-name>ActiveMQ Message Driven Beancontainer-name>  
          4.         <call-logging>falsecall-logging>  
          5.         <invoker-proxy-binding-name>activemq-message-driven-beaninvoker-proxy-binding-name>  
          6.         <container-interceptors>  
          7.             <interceptor>org.jboss.ejb.plugins.ProxyFactoryFinderInterceptorinterceptor>  
          8.                 <interceptor>org.jboss.ejb.plugins.LogInterceptorinterceptor>  
          9.                 <interceptor>org.jboss.ejb.plugins.RunAsSecurityInterceptorinterceptor>  
          10.                 <!-- CMT -->  
          11.                 <interceptor transaction="Container">org.jboss.ejb.plugins.TxInterceptorCMTinterceptor>  
          12.                 <interceptor transaction="Container">org.jboss.ejb.plugins.CallValidationInterceptorinterceptor>  
          13.                 <interceptor transaction="Container" metricsEnabled="true">org.jboss.ejb.plugins.MetricsInterceptorinterceptor>  
          14.                 <interceptor transaction="Container">org.jboss.ejb.plugins.MessageDrivenInstanceInterceptorinterceptor>  
          15.                 <!-- BMT -->  
          16.                 <interceptor transaction="Bean">org.jboss.ejb.plugins.MessageDrivenInstanceInterceptorinterceptor>  
          17.                 <interceptor transaction="Bean">org.jboss.ejb.plugins.MessageDrivenTxInterceptorBMTinterceptor>  
          18.                 <interceptor transaction="Bean">org.jboss.ejb.plugins.CallValidationInterceptorinterceptor>  
          19.                 <interceptor transaction="Bean" metricsEnabled="true">org.jboss.ejb.plugins.MetricsInterceptorinterceptor>  
          20.                 <interceptor>org.jboss.resource.connectionmanager.CachedConnectionInterceptorinterceptor>  
          21.         container-interceptors>  
          22.         <instance-pool>org.jboss.ejb.plugins.MessageDrivenInstancePoolinstance-pool>  
          23.         <instance-cache>instance-cache>  
          24.         <persistence-manager>persistence-manager>  
          25.         <container-pool-conf>  
          26.                 <MaximumSize>100MaximumSize>  
          27.         container-pool-conf>  
          28.         container-configuration>  
          29. container-configurations>  

          以上就是ActiveMQ+JBoss InBound 的配置

          三.在Servlet中通過發送消息,驗證上面的Message Driven Bean

               為了驗證這個MessageDrivenBean能夠正常工作,我使用一個很簡單的servlet向這個queue發送消息,前一篇的activemq-ds.xml 已經提供在啟動的時候綁定了JNDI activemq/QueueConnectionFactory,activemq/queue/outbound,我們直接使用就行了,

          java 代碼
          1. try {   
          2.         initialContext = new InitialContext();   
          3.         QueueConnectionFactory connectionFactory = (QueueConnectionFactory) initialContext   
          4.                 .lookup("java:activemq/QueueConnectionFactory");   
          5.            Connection con=connectionFactory.createConnection();   
          6.         Session session =  con.createSession(false, Session.AUTO_ACKNOWLEDGE);   
          7.   
          8.         Queue queue = (Queue) initialContext.lookup("activemq/queue/outbound");   
          9.         MessageProducer producer = session.createProducer(queue);   
          10.         TextMessage txtMessage = session.createTextMessage();   
          11.         txtMessage.setText("A");   
          12.         producer.send(txtMessage);   
          13.         producer.close();   
          14.         session.close();   
          15.         con.close();   
          16.            
          17.     } catch (NamingException e) {   
          18.         // TODO Auto-generated catch block   
          19.         e.printStackTrace();   
          20.     } catch (JMSException e) {   
          21.         // TODO Auto-generated catch block   
          22.         e.printStackTrace();   
          23.     }   

          四.關于durable方式訂閱topic的補充說明
               使用durable方式,你需要在ejb-jar.xml中額外的配置,subscriptionDurability,clientId,subscriptionName

          xml 代碼
          1. <activation-config>  
          2.     <activation-config-property>  
          3.         ......   
          4.         <activation-config-property>  
          5.             <activation-config-property-name>subscriptionDurabilityactivation-config-property-name>  
          6.             <activation-config-property-value>Durableactivation-config-property-value>  
          7.         activation-config-property>  
          8.         <activation-config-property>  
          9.             <activation-config-property-name>clientIdactivation-config-property-name>  
          10.             <activation-config-property-value>fooactivation-config-property-value>  
          11.         activation-config-property>  
          12.         <activation-config-property>  
          13.             <activation-config-property-name>subscriptionNameactivation-config-property-name>  
          14.             <activation-config-property-value>baractivation-config-property-value>  
          15.         activation-config-property>  
          16.         ......   
          17.     activation-config-property>  
          18. activation-config>  

           

          ok 這樣就可以使用durable方式訂閱topic了。

          參考文檔:
                  JBoss Integration
                 ActiveMQ Inbound Communication
              ActiveMQ Outbound Communication


          posted on 2007-11-16 17:10 hk2000c 閱讀(561) 評論(0)  編輯  收藏 所屬分類: JMS
          主站蜘蛛池模板: 曲麻莱县| 黄梅县| 桐城市| 阿荣旗| 龙陵县| 新平| 安陆市| 六盘水市| 凤翔县| 山东| 和林格尔县| 临猗县| 柳林县| 波密县| 龙山县| 元江| 潼关县| 巩义市| 台东县| 通城县| 车险| 乐业县| 云林县| 绩溪县| 江口县| 陵川县| 竹溪县| 龙陵县| 舟曲县| 巴林左旗| 灵丘县| 长泰县| 横峰县| 梧州市| 黔西县| 黄梅县| 旌德县| 南宫市| 姚安县| 卢氏县| 开原市|