Commons

Home

About Us

Download

Information

Components Repository

Sandbox Components

Jakarta Community

Project Docs

Messenger : a web based JMS framework

  1. ¼Ò°³
  2. ¹®¼­
  3. ¿¹Á¦ ¼³Á¤
  4. ¿¹Á¦ ÄÚµå
  5. ¼³Á¤
  6. Messagelets


Introduction

Proposed by : James Strachan

Messenger´Â À¥¼­ºñ½º¿Í À¥¾îÇø®ÄÉÀÌ¼Ç ¾È¿¡¼­ JMS (Java Message Service)¸¦ ½±°Ô »ç¿ëÇÒ ¼ö ÀÖµµ·Ï ¸¸µå´Â JMSÇÁ·¹ÀÓ¿öÅ©ÀÌ´Ù.

¸Þ½ÅÀú´Â JMS APIÀÇ º¹À⼺ÀÇ ¸¹Àº ºÎºÐÀ» °£´ÜÇÑ facade APIµÚ·Î °¨Ãâ ¼ö ÀÖ°Ô ÇÑ´Ù.

´õÇÏ¿© ¸Þ½ÅÀú´Â XML ¼³Ä¡ ¼³Á¤À» Á¦°øÇÏ¿© ´ç½ÅÀÇ ¾îÇø®ÄÉÀÌ¼Ç ÄÚµå¾È¿¡ º¹ÀâÇÑ ¼³Ä¡ ¼³Á¤»çÇ×µé·Î ´ç½ÅÀÇ Äڵ尡 ¾îÁö·´Çô Áö´Â °ÍÀ» ÇÇÇϵµ·Ï ÇÑ´Ù.

¸Þ½ÅÀú´Â ¶ÇÇÑ MessageListenerµé,¶Ç´Â ¼­ºí¸´µé ¶Ç´Â JSP¸¦ °æÀ¯ÇÏ´Â JMS¸Þ¼¼ÁöµéÀ» ó¸®ÇÏ´Â ¾î¶² ¼­ºí¸´¿£Áø¿¡¼­µµ ¼³Ä¡µÉ¼ö ÀÖ´Â JMS±â¹Ý ÄÁÅ×À̳ÊÀÎ Messagelet ¿£ÁøÀ» Á¦°øÇÑ´Ù.


Documentation

ÃÖÁ¾ ¾ß°£ºô´õÀÇ JavaDoc ÀÌ ¿Â¶óÀÎ»ó¿¡ ÀÖ´Ù. ¶Ç´Â ´ç½ÅÀº nightly buildÀ» ´Ù¿î·Îµå¹ÞÀ» ¼ö ÀÖ´Ù.

¶ÇÇÑ »óÅ ¹®¼­°¡ ÀÖ´Ù. ¶Ç´Â Ãʱâ Á¦¾Èµµ ÀÖ´Ù.


Example Config

¿©±â¿¡ ¿¹Á¦Messenger.xml ¼³Ä¡¼³Á¤È­ÀÏÀÌ ÀÖ´Ù.

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

  <!-- this example Messenger XML config file should work with J2EE SDK -->

  <messenger name="topic">
    <jndi lookupName="TopicConnectionFactory">
      <property>
        <name>com.sun.jms.internal.java.naming.factory.initial</name>
        <value>com.sun.enterprise.naming.SerialInitContextFactory</value>
      </property>
    </jndi>
  </messenger>

  <messenger name="queue">
    <jndi lookupName="QueueConnectionFactory">
      <property>
        <name>com.sun.jms.internal.java.naming.factory.initial</name>
        <value>com.sun.enterprise.naming.SerialInitContextFactory</value>
      </property>
    </jndi>
  </messenger>

</manager>

ÀÌ°ÍÀº °¢°¢ topic°ú queue·Î ºÒ¸®´Â µÎ°³ÀÇ Ç¥ÁØ ¸Þ½ÅÀúµéÀ» ¸¸µé±â À§ÇØ J2EE SDK·Î ÀÛ¾÷ÇØ¾ß ÇÑ´Ù.

±×·¡¼­ ¿ì¸®´Â ÀÚ¹ÙÄÚµå·ÎºÎÅÍ µÎ ¸Þ½ÅÀúµéÀ» ¾î¶»°Ô »ç¿ëÇÒ °ÍÀΰ¡?


Example Code

¿©±â ÅäÇÈ¿¡ ¸Þ¼¼Áö¸¦ º¸³»´Â ¾à°£ÀÇ ¿¹Á¦ Äڵ尡 ÀÖ´Ù.

 // get a Messenger and Destination Messenger messenger = MessengerManager.get(
"topic" ); Destination destination = messenger.getDestination( "CHAT.NEWBIES"
); // now lets send a message TextMessage message = messenger.createTextMessage(
"this is some text" ); messenger.send( destination, message ); 

¿©±â¿¡ ¸Þ¼¼Áö°¡ µµÂøÇÒ ¶§±îÁö ºí·ÏÅ·Çϸ鼭 Å¥¿¡ ¸Þ½ÃÁö¸¦ ¹Þ´Â ¾à°£ÀÇ Äڵ尡 ÀÖ´Ù.

 // get a Messenger and Destination Messenger messenger = MessengerManager.get(
"queue" ); Destination destination = messenger.getDestination( "REQUEST.BUILD"
); // now lets receive a message Message message = messenger.receive( destination
); 

°³º°ÀûÀÎ ¸Þ½ÅÀú °´Ã¼µéÀÌ ¾î¶»°Ô log4j°°Àº Åø¿¡ À¯»çÇÑ ¹æ¹ýÀ¸·Î ¸Þ½ÅÀú¸Å´ÏÀú µÚ¿¡ °¨Ãß¾îÁú ¼ö ÀÖ´ÂÁö¿¡ À¯ÀÇÇ϶ó.

¶ÇÇÑ ¸Þ½ÅÀú API°¡ MessageConsumer, MessageProducer, TopicPublisher, TopicSubscriber, QueueSender, QueueReceiveÀÇ »ç¿ë°ú ¿¬°á°ú ¼¼¼Ç°´Ã¼µéÀÇ °úÀ×»Ó¸¸ ¾Æ´Ï¶ó ÅäÇÈ°ú Å¥¼³Á¤ ÄÚµùÀÌ ÇÊ¿ä¾ø´Â °£´ÜÇÑ facadeÀÓÀ» À¯ÀÇÇ϶ó.


Configuration

µðÆúÆ®·Î ¸Þ½ÅÀú´Â, ±× ÀνºÅϽº°¡ ´ÙÀ½ÀÇ Äڵ带 °æÀ¯ÇÏ¿© ÃÄ´Ùº¸ÀÚ ¸»ÀÚ °ð Ŭ·¡½ºÆнº»ó¿¡ Messenger.xml ¶ó ºÒ¸®´Â XML¹®¼­¸¦ ãÀ» °ÍÀÌ´Ù.

 Messenger messenger = MessengerManager.get( "customer.orders"
); 

´ë½ÅÇÒ ¼ö ÀÖ´Â Á¢±ÙÀº ½Ã½ºÅÛ¼Ó¼ºÀ» ¸Þ½ÅÀú¼³Ä¡¼³Á¤ ¹®¼­¸¦ ÁöÀûÇÏ´Â org.apache.commons.messenger ·Î Á¤ÀÇÇÏ´Â °ÍÀÌ´Ù.¿¹¸¦ µé¾î,

 $ java -Dorg.apache.commons.messenger=http://localhost/config/Messenger.xml
MyApplication 

¼­ºí¸´È¯°æ¿¡¼­ ÀÌ°ÍÀº, ¼­ºí¸´ÃʱâÀÎÀÚµéÀ» »ç¿ëÇÏ´Â ¼­ºí¸´ ÃʱâÈ­ ¸Þ¼Òµå¾È¿¡¼­ ½Ì±ÛÅæ MessengerManager ¸¦ ¸íÈ®È÷ ¼³Á¤ÇÏ´Â ÁÁÀº ¾ÆÀ̵ð¾îÀÌ´Ù.¿©±â¿¡ ¿¹Á¦°¡ ÀÖ´Ù.

 public class MyServlet extends HttpServlet { public void init()
throws ServletException { // initialise the Messenger connections String url =
getInitParameter( "messenger" ); if ( url != null ) { MessengerManager.configure(
url ); } } } 


Messagelets

¸Þ½ÅÀú ÇÁ·ÎÁ§Æ®´Â Tomcat4.0 µîµîÀÇ ¾î´À ¼­ºí¸´¿£Áø¿¡¼­µµ ´Þ¸®´Â JMS±â¹Ý ÄÁÅ×À̳ÊÀÎ Messagelet¿£ÁøÀ» Á¦°øÇÑ´Ù. Messagelet ¿£ÁøÀº JMS ¸Þ¼¼Áö¸®½º³Êµé, ¼­ºí¸´µé ¶Ç´Â ½ÉÁö¾î JSP±îÁöµµ »ç¿ëÇÏ´Â ¹æ´ëÇÑ ¹æ¹ýÀ¸·Î JMS ¸Þ¼¼ÁöµéÀ» ó¸®Çϱâ À§ÇÑ °£´ÜÇÑ ÇÁ·¹ÀÓ¿öÅ©¸¦ Á¦°øÇÑ´Ù.

Messagelet ÄÁÅ×À̳ʸ¦ ¼³Ä¡Çϱâ À§ÇÏ¿©you need to add the ManagerServlet in a web application giving it an XML configuration file describing all the various JMS connections and an XML configuration file describing all the subscriptions.

Here are example connections and subscriptions XML configuration files. There now follows the section you need to add to your web.xml configuration file to deploy the Messagelet Manager Servlet.


  <servlet>
    <servlet-name>managerServlet</servlet-name>
    <servlet-class>org.apache.commons.messagelet.ManagerServlet</servlet-class>
    <init-param>
      <param-name>connections</param-name>
      <param-value>/WEB-INF/Messenger.xml</param-value>
    </init-param>
    <init-param>
      <param-name>subscriptions</param-name>
      <param-value>/WEB-INF/subscriptions.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>

Once you've done the above and the web application is started the Messagelet engine will subscribe to the various JMS subscriptions and then dispatch JMS messages to the various MessageListener objects, Servlets or JSP pages.

There are a variety of ways in which you can process JMS messages depending on your requirements.

  • A MessageListener is a standard JMS listener of messages.
  • A MessageDrivenObject is-a JMS MessageListener which has extra servlet-based lifecycle methods just like a Servlet. This allows an MDO to know when its being initialised and when its being destroryed and so do some resource management (such as creating or closing database connections etc). Also on initialisation the MDO gets access to the ServletContext so that it can read some initialization parameters from the web application or perform web-app level logging and so on.
  • A MessengerMDO is-a MessageDrivenObject but also provides a number of helper methods such as access to the Messenger to which its listening, so that responses can be sent back to the originator of the message, as well as access to the ServletContext and some logging helper methods.
  • A Servlet can be any GenericServlet or HttpServlet. If the JMS message that is being dispatched is a TextMessage then the body of the message is available via the ServletRequest.getReader() method, in the normal Servlet way. Any output written to the ServletResponse.getWriter() will be converted into a TextMessage and sent back to the originator. All servlets and JSP pages have access to the originating JMS message and Messenger objects via the "message" and "messenger" request attributes respectively.
  • A Messagelet is a JMS specific Servlet, just like a HttpServlet is a HTTP specific Servlet. It provides access to a Messagelet specific MessageletRequest and MessageletResponse to allow access to the JMS Message and the Messenger for sending replies.
  • A JSP page can be any piece of JSP, for example the standard JSP tag library can be used to perform JavaScript, XPath, XSLT or SQL operations on the incoming message. The request scope "message" and "messenger" attributes can be used to access the originating JMS Message and Messenger objects.

There are some examples of an MDO, Servlet and Messagelet here or you can see example JSP here



Copyright © 1999-2002, Apache Software Foundation