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>

이것은 각각 topicqueue로 불리는 두개의 표준 메신저들을 만들기 위해 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