<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    Jhonney的專欄

       ----人見人愛
    隨筆 - 49, 文章 - 1, 評論 - 23, 引用 - 0
    數據加載中……

    Message-Driven POJOs

    http://jroller.com/page/habuma/20050715#message_driven_pojos

    Message-Driven POJOs

    In my preparation for a presentation I'll be giving next month, I have been tinkering with Message-driven POJOs as proposed by James Strachan and as implemented in ActiveMQ.

    Message-driven POJOs offer MDB-like functionality to simple JavaBeans. I have found message-driven POJOs to be a compelling alternative to MDBs, especially if you're already a big fan of Spring and are using it to develop your applications. Message-driven POJOs can take advantage of all of the features provided by Spring (such as dependency injection and AOP).

    Although message-driven POJOs aren't exactly new news, I had a hard time finding much information about them online. ActiveMQ's own documentation isn't too bad, but left me with several unanswered questions. As a service to everyone out there who may be searching for more info on message-driven POJOs, I'm writing this article to document what I've learned. I hope you find it useful and maybe it will fill in some of the gaps for you.

    What you'll need

    To get started, here's a summary of the JARs that I used when developing and running the examples:

    • activemq-core-3.1-SNAPSHOT.jar
    • activemq-container-3.1-SNAPSHOT.jar
    • activemq-ra-3.1-SNAPSHOT.jar
    • commons-logging-1.0.3.jar
    • concurrent-1.3.4.jar
    • geronimo-spec-j2ee-connector-1.5-rc4.jar (or equivalent J2EE JAR)
    • geronimo-spec-j2ee-management-1.0-rc4.jar (or equivalent J2EE JAR)
    • geronimo-spec-jms-1.1-rc4.jar (or equivalent J2EE JAR)
    • geronimo-spec-jta-1.0.1B-rc4.jar (or equivalent J2EE JAR)
    • log4j-1.2.9.jar
    • spring.jar (version 1.2.2)

    The biggest challenge you'll face when developing message-driven POJOs is pulling together all of these JARs. To make things easier on you, I've made everything you'll need (including the example code) available here.

    Your mileage may vary if you choose to use different versions of JARs than what I've listed above. One important note, however, is that in my examples, I'm using the shorthand XML available in Spring 1.2, so if you use an older version of Spring, you'll need to tweak the XML appropriately.

    Writing the message-driven POJO

    Writing a message-driven POJO isn't dramatically different than writing a message-driven EJB. The major difference is that you don't have to implement javax.ejb.MessageDrivenBean, which means that you also don't have to implement the EJB lifecycle methods mandated by that interface. (If you need to hook into a POJO's lifecycle, you can use Spring's init-method and destroy-method attributes when declaring the bean.)

    In the interest of keeping the focus on how to develop message-driven POJOs, my example will be as simple as possible. In the grand tradition of all simple examples, here's a "Hello World" message-driven POJO:

      package com.habuma.mdpojo;

      import javax.jms.JMSException;
      import javax.jms.Message;
      import javax.jms.MessageListener;

      public class HelloBean implements MessageListener {
        public void onMessage(Message msg) {
          try {
            String name = msg.getStringProperty("name");
            if(name == null) {
              name = "World";
            }

            System.out.println("Hello " + name + "!");
          } catch (JMSException e) {
            // handle exception?
          }
        }
      }

    Notice that (as with MDBs) message-driven POJOs still must implement javax.jms.MessageListener. Even though James Strachan refers to these as POJOs, I don't consider this a pure POJO because in order to work it must implement a platform/framework-specific interface. But the MessageListener interface isn't too much of a burden to bear and it's still lighterweight than MDBs, so I guess I can't complain too much.

    The onMessage() method should be self-explanatory. When a message is dispatched to this bean, this method is called. Inside of the method, we extract the "name" property from the message and use it to display the traditional greeting.

    Listening for messages

    The next thing to do is to declare HelloBean as a bean in a Spring context and associate it with a message queue. Declaring the bean itself is simple enough (in hello-mdpojo.xml):

      <bean id="helloBean" class="com.habuma.mdpojo.HelloBean"/>

    There's really nothing special about this bean. In a real application, however, your message-driven POJO would probably be more interesting and be injected with references to DAOs, service-layer beans, or some other beans that it collaborates with to do its job. It may also be proxied by some Spring aspects for declarative transactions, security, etc.

    To associate "helloBean" with a message queue, we'll need to use ActiveMQ's JCAContainer. The JCAContainer associates itself with a specific ActiveMQ server and acts as a factory to produce connectors to that server. It is declared in Spring as a <bean>:

      <bean id="activeMQContainer" class="org.activemq.jca.JCAContainer">
        <property name="workManager">
          <bean id="workManager" class="org.activemq.work.SpringWorkManager"/>
        </property>

        <property name="resourceAdapter">
          <bean id="activeMQResourceAdapter"
              class="org.activemq.ra.ActiveMQResourceAdapter">
            <property name="serverUrl" value="tcp://localhost:61616"/>
          </bean>
        </property>
      </bean>

    For the most part, this chunk of XML is boiler-plate code to set up a JCAContainer to interact with the message queue. The good news is that you'll only need one JCAContainer, regardless of how many of your POJOs will be message-driven.

    The only thing of interest is the value assigned to the "serverUrl" property of the "activeMQResourceAdapter" inner-bean. This property tells the container how to connect to your ActiveMQ server--in this case, via TCP on port 61616 of the localhost (where ActiveMQ will be listening by default.)

    At this point we have a JCAContainer that is associated with the ActiveMQ server and we have a POJO that is ready and willing to accept messages. All that's left is to connect the POJO to the JCAContainer. The following <bean> does the trick:

      <bean id="HelloMDP"
          factory-method="addConnector"
          factory-bean="activeMQContainer">
        <property name="activationSpec">
          <bean class="org.activemq.ra.ActiveMQActivationSpec">
            <property name="destination" value="Hello.Queue"/>
            <property name="destinationType" value="javax.jms.Queue"/>
          </bean>
        </property>
        <property name="ref" value="helloBean" />
      </bean>

    This bean is where the proverbial "rubber meets the road". Using Spring's factory-method feature, this bean declaration leans on the JCAContainer's addConnector() method to create a connector to associate the "helloBean" with the ActiveMQ server. Specifically, the "activationSpec" property tells the JCAContainer to set up a connector that listens to the queue named "Hello.Queue" and the "ref" property tells it to send messages from that queue to the bean named "helloBean".

    Bringing this message-driven POJO to life requires two things. First, you'll need to fire up an ActiveMQ server. In the bin directory of the ActiveMQ distribution, you'll find either an activemq.bat (for Windows) or an activemq script (for UNIX). Running this script will start up an ActiveMQ server instance listening on port 61616.

    The next thing you'll need is a simple application that loads the Spring application context. The following main() method should suffice (I'll leave the class you place this in up to you):

      public static void main(String[] args) {
        new FileSystemXmlApplicationContext("hello-mdpojo.xml");
      }

    If all goes well, running this program will result in a HelloBean POJO that is patiently awaiting a message to process. So, let's send it a message.

    Sending a message

    There are a variety of ways to send a JMS message, but for the purposes of this example, I'm going to use Spring's JmsTemplate (as described in chapter 7 of Spring in Action). The following declaration of a JmsTemplate is what I used to test the HelloBean (in mq-client.xml):

      <bean id="jmsTemplate"
          class="org.springframework.jms.core.JmsTemplate">
        <property name="defaultDestinationName" value="Hello.Queue"/>
        <property name="connectionFactory" ref="connectionFactory"/>
      </bean>

    The "defaultDestinationName" property tells the template the name of the message queue to attach to. The "connectionFactory" property tells the template how to connect. I could've used Spring's JndiObjectFactoryBean to pull a connection factory from JNDI (as I described in SiA) or any implementation of javax.jms.ConnectionFactory. But so that I don't have to mess with JNDI for this simple example, let's use ActiveMQ's ActiveMQConnectionFactory:

      <bean id="connectionFactory"
          class="org.activemq.ActiveMQConnectionFactory">
        <property name="brokerURL" value="tcp://localhost:61616" />
      </bean>

    Now we just need to use the "jmsTemplate" bean to send a message. In a real application, you might inject "jmsTemplate" into a property of another bean to use. But for simplicity's sake we'll just access the bean directly from a main() method (again, I'll leave the choice of a class up to you):

      public static void main(String[] args) {
        ApplicationContext ctx = new
            FileSystemXmlApplicationContext("mq-client.xml");

        JmsTemplate template = (JmsTemplate) ctx.getBean("jmsTemplate");
        template.send(
          new MessageCreator() {
            public Message createMessage(Session session)
                throws JMSException {
              MapMessage message = session.createMapMessage();
                message.setStringProperty("name", "Craig");
              return message;
            }
          }
        );
      }

    Run this program in a separate window from where the application that runs the JCAContainer. If things are going your way, you should see "Hello Craig!" in the window running the JCAContainer app. Congratulations, you've just invoked a message-driven POJO.

    So what?

    What's the big deal? Why are message-driven POJOs better than MDBs? Well, I never actually claimed any superiority of message-driven POJOs over MDBs. But here are some things to consider:

    • MDBs must be run within an EJB container. Depending on the architecture of your app, this may be an excessive requirement, especially if you aren't use any other EJBs and do not require the features of a full-blown EJB container. Message-driven POJOs, on the other hand, can run anywhere, even (as shown here) in a simple main() method. The only requirement is an ActiveMQ message queue.
    • MDBs require that you implement the lifecycle methods of javax.ejb.MessageDrivenBean. Often these lifecycle methods aren't needed and are left as empty implementations. This isn't a real problem, except that it's simply unnecessary.
    • Although it may not be apparent from the simple HelloBean example, message-driven POJOs can take full advantage of the dependency injection and AOP support offered by Spring (including Spring's support for declarative transactions and Acegi's support for declarative security). In short, you can do anything with the POJO that you can do with any other bean in a Spring context.
    • The XML used to declare a message-driven POJO is slightly more verbose then for an MDB. You should, however, keep in mind that you'll only need to declare one JCAContainer bean, regardless of how many message-driven POJOs your application has.

    One thing that I've not yet considered is how EJB 3 MDBs compare the message-driven POJOs. Honestly, I've not taken the time to perform this comparison. Perhaps this is something I'll do soon. If you have any insight in this subject, I'd be quite interested in hearing it.

    Message-driven POJOs are just a small portion of a talk that I'll be presenting at the LoneStar Software Symposium in Austin, TX next month. If you're in the Austin area, I hope to see you there.

    posted on 2007-06-20 02:25 Jhonney 閱讀(337) 評論(0)  編輯  收藏 所屬分類: 轉載區

    主站蜘蛛池模板: 免费不卡在线观看AV| 青青青视频免费观看| 99在线观看视频免费| 久久精品国产精品亚洲蜜月| 99久久免费国产特黄| 在线观看午夜亚洲一区| WWW国产成人免费观看视频| 亚洲片国产一区一级在线观看 | 在线精品亚洲一区二区| 国国内清清草原免费视频99| 四虎影视在线看免费观看| 亚洲成AV人在线观看网址| jizz日本免费| 亚洲AV中文无码字幕色三| 午夜视频在线免费观看| 亚洲天堂中文资源| 免费无码肉片在线观看| MM1313亚洲精品无码久久| 免费va在线观看| 国产美女视频免费观看的网站| 亚洲无码在线播放| 亚洲成人免费在线| 亚洲熟妇AV日韩熟妇在线| 免费观看国产精品| 国产做国产爱免费视频| 中文字幕亚洲精品| 思思99re66在线精品免费观看| 久久水蜜桃亚洲AV无码精品 | 精品久久久久久久免费加勒比| 美女视频黄视大全视频免费的| 亚洲精品国产精品国自产观看| 久久伊人免费视频| 亚洲字幕AV一区二区三区四区| 亚洲av无码乱码在线观看野外| 两个人看的www免费视频| 亚洲一卡二卡三卡四卡无卡麻豆 | 免费无码中文字幕A级毛片| 亚洲中文字幕久久无码| 中文字幕亚洲一区| 久久久久国色AV免费看图片| 91av免费在线视频|