activemq+jboss+ejb

 

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是

           但是在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.   
  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.   
  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.       
  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.       
  4.     <resource-adapter-name>activemq-ra.rarresource-adapter-name>  
  5.       
  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.                   
  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.                   
  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>  
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章