zoukankan      html  css  js  c++  java
  • DriverMessageBean配置详解

    0

    环境介绍:JBoss 4 + EJB 2.1.

    第一,配置Destination.

      在${jboss安装目录}/server/default/deploy/jms下,新建一个service文件,如my-service.xml。在其中配置destination. JBoss该目录下已有jbossmq-destinations-service.xml,可以参照此进行配置 。如:

    配置Queue示例:

    <mbean code="org.jboss.mq.server.jmx.Queue"
      name="jboss.mq.destination:service=Queue,name=testQueue">
        <depends optional-attribute-name="DestinationManager">jboss.mq:service=DestinationManager</depends>
        <depends optional-attribute-name="SecurityManager">jboss.mq:service=SecurityManager</depends>
        <attribute name="MessageCounterHistoryDayLimit">-1</attribute>
        <attribute name="SecurityConf">
          <security>
            <role name="guest" read="true" write="true"/>
            <role name="publisher" read="true" write="true" create="false"/>
            <role name="noacc" read="false" write="false" create="false"/>
          </security>
        </attribute>
      </mbean>

    配置Topic示例:

      <mbean code="org.jboss.mq.server.jmx.Topic"
      name="jboss.mq.destination:service=Topic,name=testTopic">
        <depends optional-attribute-name="DestinationManager">jboss.mq:service=DestinationManager</depends>
        <depends optional-attribute-name="SecurityManager">jboss.mq:service=SecurityManager</depends>
        <attribute name="SecurityConf">
          <security>
            <role name="guest" read="true" write="true"/>
            <role name="publisher" read="true" write="true" create="false"/>
            <role name="durpublisher" read="true" write="true" create="true"/>
          </security>
        </attribute>
      </mbean>

    第二步:开发MDB.

    如:

    public class MyTestMdb
         implements javax.ejb.MessageDrivenBean,
                  javax.jms.MessageListener
    {


    public void ejbCreate() {
     // TODO Auto-generated method stub
     super.ejbCreate();
    }

    public MessageDrivenContext getMessageDrivenContext() {
     // TODO Auto-generated method stub
     return super.getMessageDrivenContext();
    }

    public void onMessage(Message arg0) {
     // TODO Auto-generated method stub
     //Process messages here

    }
       public void setMessageDrivenContext(javax.ejb.MessageDrivenContext ctx)
       {
          super.setMessageDrivenContext(ctx);
       }

       public void ejbRemove()
       {
          super.ejbRemove();
       }

    }

    第三步:配置ejb-jar.xml。可以用xdoclet自动生成。ejb-jar.xml放到ejb源码/META-INF下。

    主体大致如下:

          <message-driven id="MessageDriven_1">
             <description><![CDATA[<!-- begin-xdoclet-definition -->]]></description>

             <ejb-name>MyTestMdb</ejb-name>

             <ejb-class>org.beyondsoul.mdb.MyTestMdb</ejb-class>

             <messaging-type>javax.jms.MessageListener</messaging-type>
             <transaction-type>Container</transaction-type>
             <message-destination-type>javax.jms.Queue</message-destination-type>
             <activation-config>
               <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>

          </message-driven>

    第四步:配置供应商专有配置文件jboss.xml。jboss.xml放到ejb源码/META-INF下。在上一步的标准配置里,是没有涉及Destination的配置的,这个配置在这一步完成。

      <message-driven>
       <ejb-name>MyTestMdb</ejb-name>
       <destination-jndi-name>queue/testQueue</destination-jndi-name>
      </message-driven>

    至此,配置完成。需要多注意上述粗体部分。   这一句<destination-jndi-name>queue/testQueue</destination-jndi-name>,前一部分粗体是queue。当Destination是Queue类型时,就用“queue”,是Topic时,用“topic”;后一部分粗体是上述配置的Destination名称。

    需要说明的是,此处并没有配置Connection Factory,比如象BEA Weblogic的配置那样。JBoss 里 Connection Factory 的名称已经内置为“ConnectionFactory”了;

    **************************
    方法二


    由于Connection Factory以及Destination都是EJB容器供应商专门提供,这些信息不在ejb-jar.xml里配置,所以在移植上带来很多方便。当然,有时也可考虑用resource-ref 在ejb-jar.xml里进行处理。如下所示:

    先在jboss.xml里如是配置:

    <?xml version="1.0"?>
    <jboss>
    <enterprise-beans>
    <message-driven>
    <ejb-name>TextMDB</ejb-name>
    <destination-jndi-name>queue/testQueue</destination-jndi-name>
    <resource-ref>
       <res-ref-name>jms/QCF</res-ref-name>
           <jndi-name>ConnectionFactory</jndi-name>
       </resource-ref>
    </message-driven>
    </enterprise-beans>
    </jboss>

    与此同时,保持同步,在ejb-jar.xml里如是配置:

    <message-driven id="MessageDriven_1">
             <description><![CDATA[<!-- begin-xdoclet-definition -->]]></description>

             <ejb-name>MyTestMdb</ejb-name>

             <ejb-class>org.beyondsoul.mdb.MyTestMdb</ejb-class>

             <messaging-type>javax.jms.MessageListener</messaging-type>
             <transaction-type>Container</transaction-type>
             <message-destination-type>javax.jms.Queue</message-destination-type>
             <activation-config>
               <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>
            
             <resource-ref>
                <res-ref-name>jms/QCF</res-ref-name>
                <res-type>javax.jms.QueueConnectionFactory</res-type>
                <res-auth>Container</res-auth>
             </resource-ref>

          </message-driven>

    最后再补充说一下,消息发送端的代码示例:

     //Get InitialContext
            Hashtable env = new Hashtable();
            env.put(Context.INITIAL_CONTEXT_FACTORY,
                    "org.jnp.interfaces.NamingContextFactory");
            env.put(Context.PROVIDER_URL, "jnp://localhost:1099/");
            env.put(Context.URL_PKG_PREFIXES,
                            "org.jboss.naming:org.jnp.interfaces");
            InitialContext iniCtx = new InitialContext(env);

     //Get a connection, find the destination, then send messages
            QueueConnectionFactory qcf = (QueueConnectionFactory) iniCtx.lookup("ConnectionFactory");;
            conn = qcf.createQueueConnection();
            que = (Queue) iniCtx.lookup("queue/testQueue");
            session = conn.createQueueSession(false, QueueSession.AUTO_ACKNOWLEDGE);
            conn.start();

            QueueSender send = session.createSender(que);
            for (int i = 0; i < 10; i++) {
                TextMessage tm = session.createTextMessage(text + i);
                send.send(tm);
            }
     //post process
            send.close();
            conn.stop();
            session.close();
            conn.close();     

    如:my-service.xml

    <?xml version="1.0" encoding="UTF-8"?>

    <!-- $Id: jbossmq-destinations-service.xml,v 1.4.6.1 2004/11/16 04:32:39 ejort Exp $ -->

    <!--
       | This file defines the default Queues and Topics that JBossMQ
       | ships with.  The default Queues and Topics are used by the
       | JBoss test suite and by the sample jms programs.
       |
       | You can add other destinations to this file, or you can create other
       | *-service.xml files to contain your application's destinations.
     -->

    <server>
    <mbean code="org.jboss.mq.server.jmx.Queue"
      name="jboss.mq.destination:service=Queue,name=testQueue">
        <depends optional-attribute-name="DestinationManager">jboss.mq:service=DestinationManager</depends>
        <depends optional-attribute-name="SecurityManager">jboss.mq:service=SecurityManager</depends>
        <attribute name="MessageCounterHistoryDayLimit">-1</attribute>
        <attribute name="SecurityConf">
          <security>
            <role name="guest" read="true" write="true"/>
            <role name="publisher" read="true" write="true" create="false"/>
            <role name="noacc" read="false" write="false" create="false"/>
          </security>
        </attribute>
      </mbean>

    <mbean code="org.jboss.mq.server.jmx.Topic"
      name="jboss.mq.destination:service=Topic,name=testTopic">
        <depends optional-attribute-name="DestinationManager">jboss.mq:service=DestinationManager</depends>
        <depends optional-attribute-name="SecurityManager">jboss.mq:service=SecurityManager</depends>
        <attribute name="SecurityConf">
          <security>
            <role name="guest" read="true" write="true"/>
            <role name="publisher" read="true" write="true" create="false"/>
            <role name="durpublisher" read="true" write="true" create="true"/>
          </security>
        </attribute>
      </mbean>

    </server> 

  • 相关阅读:
    【APUE】Chapter15 Interprocess Communication
    【APUE】Chapter14 Advanced I/O
    【APUE】Chapter5 Standard I/O Library
    【APUE】Chapter4 File and Directories
    【APUE】Chapter3 File I/O
    【APUE】Chapter1 UNIX System Overview
    【APUE】Chapter13 Daemon Processes
    【APUE】Chapter10 Signals
    Hive之数据类型
    Hive之内置函数
  • 原文地址:https://www.cnblogs.com/pocter/p/3684534.html
Copyright © 2011-2022 走看看