如何在OC4J 10g (10.1.3)? 中配置使用MQ JCA 1.5 资源

来源:互联网 发布:js点击事件的几种写法 编辑:程序博客网 时间:2024/05/17 22:58

 

First Created: 09-15-2006
作者:  Sandeep Bangera

  • 介绍
  • 先决条件
    • 你应该知道什么
    • 软件需求
    • 记号
  • 编译程序
    • 预览
    • 更新 ActiveMQ rar 可发布的OC4J
    • Inbound Communication 配置 MDB 接受从ActiveMQ来的消息
    • Outbound Communication 配置 Session Bean  发送消息到ActiveMQ
  • 运行程序
    • 运行程序
    • 运行程序
    • 启动OC4J 实例
    • 启动 ActiveMQ 实例
    • 启动 ActiveMQ 实例
    • 产生,编译,发布,运行应用程序
    • 运行应用程序
    • 简化后的步骤
  • 总结

 

介绍

Demo application演示了如何配置和发布ActiveMQ JCA 1.5 资源适配器到OC4J 10g 10.1.3. 此资源适配器允许在OC4J中使用ActiveMQ

 

Demoapplication演示你如何做:
Outbound Communication:  配置StatelessSession Bean 发送消息到一个ActiveMQqueue

Inbound Communication:  配置一个Message Driven Bean接受从ActiveMQ发送的消息。

先决条件

你应该知道什么

  • 你应该熟悉这些技术: JMS, MDB, EJB;
  • 链接到 OC4J Howto's on OTN

软件需求

  • Oracle Containers for J2EE 10g (10.1.3), standalone , 随带一个以配置好的Web站点(比如http-web-site)
  • Sun's JDK 1.5 版或以上
  • Apache Ant 1.6.2 或以上以编译程序
  • ActiveMQ  4.0.1或以上
  • 任何 HTML 浏览器,如Mozilla, Microsoft Internet Explorer, Netscape, .

记号

  • %ORACLE_HOME% - OC4J 的安装目录
  • %JAVA_HOME% - JDK的安装目录
  • %ACTIVEMQ_HOME% - ActiveMQ的安装目录
  • %HOWTO_HOME% - Demo的解压目录

编译程序

此程序的Javadoc位于 %HOWTO_HOME%/doc/javadoc/ 目录.
配置文件位于 %HOWTO_HOME%/etc目录, 包括部署描述文件比如application.xml.

预览

Howto Architecture

上图展示了例子中的消息的流向,一个独立的JMS客户程序产生JMS文本消息并把它放到queue.inbound队列里。这些inbound消息被MDB消费,此MDB然后调用Stateless EJBStatelessEJBMDB中取出输入然后发布一个简单的响应消息到queue.outbound队列中。

 

更新 ActiveMQrar 可发布的OC4J

ActiveMQ rar 不能直接发布到OC4J.需做如下的小修改:

  • 解压原始的ActiveMQ 资源适配文件%ACTIVEMQ_HOME%/lib/optional/activemq-ra-4.0.1.rar  到,比如C:/temp
  • 将在%HOWTO_HOME%/etc/connector 目录下的oc4j-ra.xml 到一解压目录,比如C:/temp/META-INF
  • 修改 ra.xml 别且注释掉嵌入式broker的使用
    <!--
       <config-property-value>xbean:broker-config.xml</config-property-value>
    -->
    <!-  
        <config-property-value>vm://localhost</config-property-value>
    -->
  • 移除所有除主ra.jar,即activemq-ra-4.0.1.jar,的.jar文件。ra需要的jar文件将被发布为对应用程序可用的共享库。
  • 修改后,创建一个新的rar
    jar -cvfm C:/temp/activemq-ra.rar META-INF/MANIFEST.MF META-INF/* activemq-ra-4.0.1.jar broker-config.xml
  • 拷贝新的 activemq-ra.rar%HOWTO_HOME%/activemq-lib

注意: 这些步骤在当前的ActiveMQ 4.0.1 howto中已经修改. 如果你有用更新的ActiveMQ,你需要在最新的ActiveMQ RAR 中作以上修改.

  • 为所有需要的ActiveMQ资源适配器创建一个共享库并将它发布到OC4J中。幸运的是,activeMQ.jar已经包含了所有ActiveMQ需要的库文件,因此,我们只需要exportjar为共享库

      <oracle:publishSharedLibrary
                deployerUri="deployer:oc4j:${oc4j.host}:${oc4j.admin.port}"
                 userId="${oc4j.admin.user}"
                 password="${oc4j.admin.password}"
                 libraryName="${shared.library.name}"
                 libraryVersion="4.0.1"
                logFile="${log.dir}/publish-library.log">
                 <oracle:uploadCodeSourcepath="${activemq.home}/${activemq.core.jar}" />
                 <!-- You may need to add these files to the sharedlibrary if you are interested
                       in ActiveMQpersistence features.
                       In adddition you will need to modify broker-config.xml inra.jar to use spring properties -->
                 <!--
                    <oracle:uploadCodeSourcepath="${activemq.home}/lib/optional/spring-1.2.6.jar" />
                    <oracle:uploadCodeSourcepath="${activemq.home}/lib/optional/xbean-spring-2.2.jar" />
                 -->
          </oracle:publishSharedLibrary>

  • orion-application.xmlImport 共享库

     <imported-shared-libraries>
         <import-shared-libraryname="activemq.rar" max-version="4.0.1"/>
      </imported-shared-libraries>

Inbound Communication – 配置 MDB 接受从ActiveMQ来的消息

Message Driven Bean

ActiveMQMessageDrivenEJBBeanejb-jar.xml中的部署描述如下:

    <message-driven>
      <description>MessageDriven Bean</description>
     <display-name>ActiveMQMessageDrivenEJB</display-name>
     <ejb-name>ActiveMQMessageDrivenEJB</ejb-name>
      <ejb-class>com.oracle.howto.activemq.ActiveMQMessageDrivenEJBBean</ejb-class>
     <messaging-type>javax.jms.MessageListener</messaging-type>
     <transaction-type>Container</transaction-type>
      <activation-config>
           <activation-config-property>
              <activation-config-property-name>destination</activation-config-property-name>
              <activation-config-property-value>queue.inbound</activation-config-property-value>
           </activation-config-property>
           <activation-config-property>
              <activation-config-property-name>destinationType</activation-config-property-name>
              <activation-config-property-value>javax.jms.Queue</activation-config-property-value>
           </activation-config-property>
           <activation-config-property>
              <activation-config-property-name>acknowledgeMode</activation-config-property-name>
              <activation-config-property-value>Auto-acknowledge</activation-config-property-value>
           </activation-config-property>
           <activation-config-property>
              <activation-config-property-name>messageSelector</activation-config-property-name>
              <activation-config-property-value/>
           </activation-config-property>
      </activation-config>
    </message-driven>

activation-config-property 用于将MDB连接到JCA  destination属性值只能是与ActiveMQ相关的特定值,并且只能是MDB监听的队列的物理名。

Connector

上面定义的两个activation configuration与下面的定义在ra.xml中的元素相关:

       <inbound-resourceadapter>
           <messageadapter>
               <messagelistener>
                   <messagelistener-type>javax.jms.MessageListener</messagelistener-type>
                   <activationspec>
                       <activationspec-class>org.apache.activemq.ra.ActiveMQActivationSpec</activationspec-class>
                       
                       <required-config-property>
                           <config-property-name>destination</config-property-name>
                       </required-config-property>
                       <required-config-property>
                           <config-property-name>destinationType</config-property-name>
                       </required-config-property>
                       
                   </activationspec>
               </messagelistener>
           </messageadapter>
        </inbound-resourceadapter>

Additional  Glue Steps

用实际的目的JNDI信息更新orion-ejb-jar.xml.  它用于连接inbound JMS DestinationMDB
       <message-driven-deployment name="ActiveMQMessageDrivenEJB"resource-adapter="activemq-rar"
                   connection-factory-location="activeMQ/QueueConnectionFactory"destination-location="activeMQJMS/MyQ">
       </message-driven-deployment>

ejb.-jar.xml中更新onMessage 需要的transaction 信息
      <container-transaction>
        <method>
           <ejb-name>ActiveMQMessageDrivenEJB</ejb-name>
           <method-name>onMessage</method-name>
           <method-params>
              <method-param>javax.jms.Message</method-param>
           </method-params>
        </method>
        <trans-attribute>Required</trans-attribute>
      </container-transaction>

Outbound Communication – 配置 Session Bean  发送消息到ActiveMQ

Stateless Session Bean

  • ejb-jar.xmlActiveMQEJBBean的描述如下:

        <resource-ref>
       <res-ref-name>jms/QCF</res-ref-name>
       <res-type>javax.jms.QueueConnectionFactory</res-type>
       <res-auth>Container</res-auth>
       <res-sharing-scope>Unshareable</res-sharing-scope>
      </resource-ref>
      <resource-env-ref>
       <resource-env-ref-name>jms/QUEUE</resource-env-ref-name>
       <resource-env-ref-type>javax.jms.Queue</resource-env-ref-type>
      </resource-env-ref>

jms/QCF ActiveMQEJBBean 来查找javax.jms.QueueConnectionFactoryJNDI 名字,我们将配置它来指向ActiveMQ QueueConnectionFactory.

  • orion-ejb-jar.xml 中连接实际JNDI名字与上面相似
            <session-deployment name="ActiveMQEJB">
                <resource-ref-mapping     name="jms/QCF"  location="activeMQ/QueueConnectionFactory" />     
                <resource-env-ref-mapping name="jms/QUEUE" location="activeMQJMS/MyQ" />
            </session-deployment>

The Connector

上面定义的resource-ref defined 将用来连接在ra.xml中的如下元素
       <outbound-resourceadapter>
           <connection-definition>
               <managedconnectionfactory-class>org.apache.activemq.ra.ActiveMQManagedConnectionFactory</managedconnectionfactory-class>
               <connectionfactory-interface>javax.jms.ConnectionFactory</connectionfactory-interface>
               <connectionfactory-impl-class>org.apache.activemq.ra.ActiveMQConnectionFactory</connectionfactory-impl-class>
               <connection-interface>javax.jms.Connection</connection-interface>
               <connection-impl-class>org.apache.activemq.ra.ManagedConnectionProxy</connection-impl-class>
           </connection-definition>

        <adminobject>
           <adminobject-interface>javax.jms.Queue</adminobject-interface>
           <adminobject-class>org.apache.activemq.command.ActiveMQQueue</adminobject-class>
           <config-property>
               <config-property-name>PhysicalName</config-property-name>
               <config-property-type>java.lang.String</config-property-type>
           </config-property>
       </adminobject>

Glue Code

下面的片段用以连接Outbound队列.  下面的片段位于 oc4j-connectors.xml 

    <connectorname="activemq-rar" path="activemq-ra.rar" >
        <adminobject-configlocation="activeMQJMS/MyQ">
           <adminobject-class>org.apache.activemq.command.ActiveMQQueue</adminobject-class>
            <config-propertyname="PhysicalName" value="queue.outbound"/>
        </adminobject-config>
        <adminobject-configlocation="activeMQJMS/MyT">
           <adminobject-class>org.apache.activemq.command.ActiveMQTopic</adminobject-class>
            <config-propertyname="PhysicalName" value="topic.outbound"/>
        </adminobject-config>
    </connector>

连接工厂在oc4j-ra.xml

    <connector-factorylocation="activeMQ/QueueConnectionFactory" connector-name="activemq-rar">
       <connection-pooling use="none">
       </connection-pooling>
        <security-configuse="none">
       </security-config>
       <connectionfactory-interface>javax.jms.QueueConnectionFactory</connectionfactory-interface>
    </connector-factory>

运行程序

下面指示如何在OracleContainers for J2EE 10g (10.1.3). 中运行此演示程序

检查例子的文件目录

  • how-to-use-activemq-JCA-RA/src/java包含例子的所有java代码.
    • client/ 
      • ActiveMQApp.java - JMS 客户:发送消息到ActiveMQ的队列中
    • ejb/
      • ActiveMQEJB.java - ActiveMQ Stateless EJB 远程接口.
      • ActiveMQEJBHome.java - ActiveMQ Stateless EJB home 接口
      • ActiveMQEJBBean.java - ActiveMQ Stateless EJB 实现.
      • ActiveMQEJBLocal.java - ActiveMQ Stateless EJB 本地 接口.
      • ActiveMQEJBLocalHome.java - ActiveMQ Stateless EJB 本地 home 接口.
      • ActiveMQMessageDrivenEJBBean.java -  ActiveMQ MDB:监听 ActiveMQ inbound队列
  • how-to-use-activemq-JCA-RA/lib/
    • active-mq.ear 可发布的J2EE应用程序文件.
    • active-mq-client.jar – JMS客户端 测试 jar 文件
  • how-to-use-activemq-JCA-RA/etc – 包含所有必须的应用程序描述符
    • connector/
      • oc4j-ra.xml  - OC4J 特定的rar 描述文件
    • ear/
      • application.xml
      • oc4j-connectors.xml
      • orion-application.xml
    • ejb/
      • ejb-jar.xml
      • orion-ejb-jar.xml
  • how-to-use-activemq-JCA-RA/doc
    • how-to-use-activemq-JCA-RA.html 此文档.
    • javadoc所有源文件的javadoc
  • how-to-use-activemq-JCA-RA/build.xml主要的Ant build 文件
  • how-to-use-activemq-JCA-RA/common.xml build.xml使用
  • how-to-use-activemq-JCA-RA/ant-oracle.properties - build.xml使用
  • how-to-use-activemq-JCA-RA/activemq-lib
    • activemq-ra-4.0.1.rar.orig ActiveMQ 发布  %ACTIVEMQ_HOME%/lib/optional的原始ActiveMQ Resource Adapter
    • activemq-ra.rar -  The modified ActiveMQ resource adapter  ready to be deployed to OC4J.
  • how-to-use-activemq-JCA-RA/build – build中使用的临时目录
  • how-to-use-activemq-JCA-RA/log存放 build/deploy 日志的临时目录
  • how-to-use-activemq-JCA-RA/lib存放能够发布的程序包(., ear, war, rar, jar files)

配置环境

请检查并确保下面的属性在howto.properties(位于例子的根目录)文件中正确配置 (注意一些属性依赖于定义的环境变量,如果这些环境变量已经被设置,这些属性就不需要修改). 如果不要,根据你的环境修改如下变量:

  • oracle.home - oracle安装的根目录缺省设置为ORACLE_HOME环境变量.
  • java.home -  JDK 安装的根目录缺省设置为JAVA_HOME 环境变量.
  • oc4j.host – OC4J实例运行的主机名缺省设置为localhost.
  • oc4j.http.port - OC4J HTTP监听的端口缺省设置为 8888.
  • oc4j.admin.port  - OC4J administration 进程监听的端口,. 缺省设置为 23791.
  • oc4j.admin.user - OC4J administrator的名字缺省设置为 "admin".
  • oc4j.admin.password - OC4J administrator口令缺省设置为 "welcome".

         ActiveMQ 特定的属性 

  • activemq.port - ActiveMQ administration进程监听的端口缺省设置为 61616.
  • activemq.core.jar缺省的activeMQ jar名字,包含所有ActiveMQ要求的, ActiveMQ 4.0.1 中,此 jar文件在%ACTIVEMQ_HOME%/incubator-activemq-4.0.1.jar中可以被找到. 4.0.1而言,缺省设置为incubator-activemq-4.0.1.jar
  • activemq.home – ActiveMQ安装的根目录缺省设置为 ACTIVEMQ_HOME 环境变量.

另外,请确保与OC4J ant发布包联系的ant命令在你的执行目录中 (%OC4J_HOME%/ant/bin).

启动OC4J 实例

打开一新的console窗口. 启动一 OC4J10.1.3实例:

              %OC4J_HOME%/bin/oc4jstart

注意oc4j命令 要求JAVA_HOME 环境变量,此变量指向安装好的JDK

 

启动ActiveMQ 实例

打开一新的console窗口.启动一 ActiveMQ 10.1.3 实例:

  •  
    • %ACTIVEMQ_HOME%/bin/activemq

注意activemq命令 要求JAVA_HOME 环境变量,此变量指向安装好的JDK

产生,编译,发布,运行应用程序

打开一新的console窗口,设置ORACLE_HOME,ACTIVEMQ_HOME, JAVA_HOME环境变量。在%HOWTO_HOME%目录键入下面的命令以运行应用程序

  • ant

现在,在%HOWTO_HOME%/lib目录,你应该创建了active-mq.ear.

如果编译成功,此命令会试图发布应用程序。它将首先测试OC4J是否运行。发布后,它将运行测试程序。测试程序将执行 JMSclientJMS client会发布消息带MDB(通过ActiveMQ的资源适配器) 监听的ActiveMQinbound队列中

你将在键入ant命令的console中看到如下消息:
" Sent message: Hello world to ActiveMQ inbound queue!"

转到启动OC4Jconsole中,你就看到 MDB Stateless EJB产生的输出:

"Inside MDB, received from inboundqueue: Hello world to ActiveMQ inbound queue!
Message Received from MDB inside Session Bean: Hello world to ActiveMQ inboundqueue!
Sending Message from Stateless EJB to ActiveMQ outbound queue: Hello world toActiveMQ inbound queue!"

为验证StatelessEJB发送到ActiveMQ outbound队列“queue.outbound”是否被ActiveMQ成功接收,我们将使用jconsole(JDK15缺省自带的JMX浏览器)

.

  • %JAVA_HOME/bin/jconsole. 现在,你将看到一个连接到代理的Jconsole窗口,其中列有ActiveMQ进程。选择并点击此连接

Jconsole Connect Image

  • Jconsole中转到MBeans. 

AtiveMQ MBean

  • 现在,你可以通过浏览操作的”Operations”浏览queue.outbound 队列
    ActiveMQ browse

  • 点击browse按钮,就可以看到从Stateless Session Bean发送到ActiveMQ outbound队列的消息,请滚到到最后面看文本消息

 

注:你也可以单独发布应用程序。 确保 已经设置%ORACLE_HOME%环境变量 ,从%HOWTO_HOME% 目录,键入命令:

  • ant deploy

如果你只有兴趣运行客户端,请键入命令:

  • ant run

 

简化后的步骤:

你需要四个console窗口运行此测试

  • 在第一个console窗口,运行 %OC4J_HOME%/bin/oc4j start启动OC4J
  • 在第二个console 窗口,运行%ACTIVEMQ_HOME%/bin/activemq 启动ActiveMQ
  • 在第三个console 窗口,设置ORACLE_HOME, ACTIVEMQ_HOME, JAVA_HOME 环境变量,到 %HOWTO_HOME% 目录并键入ant命令。请确保%ORACLE_HOME/ant/binPATH环境变量中
  • 在第四个console 窗口, 键入jconsole,你可以用jconsole来验证收到的outbound消息

总结

通过此文档,你应该可以:

  • 知道OC4J 10g (10.1.3) 完全支持JCA 1.5

学到在OC4J中如何配置和使用ActiveMQ的资源适配器进行inboundoutbound通讯。

 

原创粉丝点击