2008. 8. 31. 18:32

BlazeDS 시작하기 / Getting started with BlazeDS - part 3

Reference : http://www.adobe.com/devnet/livecycle/articles/blazeds_gettingstarted_03.html

Building a messaging application
메시징 어플리케이션 만들기  

BlazeDS Message Service 퍼블리쉬/섭스크라이브 인프라 스트럭처를 제공합니다. 이 인프라스트럭처는 Flex 어플리케이션이 메시지를 퍼블리쉬하고 섭스크라이브 하게 하여, 실시간 데이터 푸쉬와 협력(collaborative) 어플리케이션 개발을 가능하게 합니다.

아래의 단계를 통해 BlazeDS Message Service 를 보여주는 간단한 채팅 어플리케이션을 만들 수 있습니다.

Step 1: Create the messaging destination
메시징 데스티네이션 생성

A messaging destination represents a topic of real time conversation that interested parties can subscribe (listen) to or contribute to by posting their own messages.

이 어플리케이션을 위한 간단한 채팅 데스티네이션 정의:

  1. blazeds-server 프로젝트에서, Flex 폴더에 있는 messaging-config.xml 을 여십시오.
  2. 아래와 같이 정의된 tutorial-chat 라는 데스티네이션을 추가하십시오:

    <destination id="tutorial-chat"/>

  3. Tomcat 을 다시 시작하십시오.

데스티네이션의 핵심 요소는 클라이언트와 서버 사이의 데이터 교환에 사용되는 채널입니다. BlazeDS를 사용할 때는, 메시징 데스티네이션은 일반적으로 스트리밍 채널이나 폴링 채널을 사용합니다.

  • Using a streaming channel, the server response is left open until the channel connection is closed, allowing the server to send incremental chunks of data to the client. Because HTTP connections are not duplex, a single streaming Action Message Format (AMF) or HTTP channel actually requires two browser HTTP connections in order to send data in both directions. One is needed for the streamed response from the server to the client that the channel hangs on to. A second transient connection, drawn from the browser pool, is needed only when data is sent to the server. This second transient connection is immediately released back to the browser’s connection pool after the data is sent.
  • A polling channel can be configured with a polling interval, or it can be set up to wait for data at the server-side if data is not immediately available (this approach is generally referred to as long polling). In either case, each poll response completes the request. Browser HTTP 1.1 connections are persistent by default, so the browser will likely recycle existing HTTP connections to send subsequent poll requests, which lowers the overhead for polling.

tutorial-chat 데스티네이션을 위해 명시적으로 채널을 정의할 필요는 없습니다. 데스티네이션 수준에서 채널을 명시하지 않으면 데스티네이션은 messaging-config.xml 파일의 가장 위에 정의되어 있는 기본(default) 채널을 사용합니다. 이 경우에, 클라이언트는 먼저 “my-streaming-amf” 채널을 사용하여 메시지 서비스에 연결하려고 할 것입니다. 만약 이 채널을 사용해서 서버에 연결하지 못하면, 클라이언트는 “my-polling-amf” 채널을 사용할 것입니다. 채널에 대한 설정은 services-config.xml 에서 변경할 수 있습니다.

Step 2: Create a Flex project
Flex 프로젝트 생성

  1. Eclipse 에서, File > New > Project… 를 선택하십시오.
  2. Flex Builder 아래의 Flex Project 를 선택하고, Next 를 클릭하십시오.
  3. 프로젝트 이름으로 “tutorial-chat” 을 입력하십시오.
  4. Use default location 이 선택되었는지 확인하십시오.
  5. 어플리케이션 종류에서 Web Application 을 선택하십시오.
  6. 어플리케이션 서버 종류로 J2EE 를 선택하십시오.
  7. Use remote object access service 를 선택하십시오.
  8. Create combined Java/Flex project using WTP 에 대한 선택을 해제하십시오.
  9. Next 를 클릭하십시오.
  10. LiveCycle Data Services 의 루트 폴더가 BlazeDS 웹 어플리케이션의 루트 폴더와 매치되는지 확인하십시오. 설정은 아래와 비슷하게 보여야 합니다 (you may need to adjust the exact folder name based on your own settings):

    Root Folder: C:\blazeds\tomcat\webapps\samples
    Root URL: http://localhost:8400/samples/
    Context Root: /samples

  11. Validate Configuration 을 클릭하고, Finish 를 클릭하십시오.

Step 3: Create the client application
클라이언트 어플리케이션 생성

새로 생성된 tutorial-chat 프로젝트에서, src 폴더에 있는 main.mxml 파일을 열고, 아래와 같이 어플리케이션을 구현하십시오:

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
creationComplete="consumer.subscribe()">

<mx:Script>
<![CDATA[import mx.messaging.events.MessageEvent;
import mx.messaging.messages.AsyncMessage;

private function send():void
{
var message:AsyncMessage = new AsyncMessage();
message.body.chatMessage = msg.text;
producer.send(message);
msg.text = "";
}

private function messageHandler(event:MessageEvent):void
{
log.text += event.message.body.chatMessage + "\n";
}

]]>
</mx:Script>

<mx:Producer id="producer" destination="tutorial-chat"/>
<mx:Consumer id="consumer" destination="tutorial-chat" message="messageHandler(event)"/>

<mx:Panel title="Chat" width="100%" height="100%">
<mx:TextArea id="log" width="100%" height="100%"/>
<mx:ControlBar>
<mx:TextInput id="msg" width="100%" enter="send()"/>
<mx:Button label="Send" click="send()"/>
</mx:ControlBar>
</mx:Panel>
</mx:Application>

 

Code highlights:
  • 클라이언트에서, BlazeDS Message Service API 는 퍼블리쉬와 섭스크라이브를 위해 사용되는 Producer 와 Consumer 라는 두 개의 클래스를 제공합니다. 
  • 데스티네이션에서 섭스크라이브 하기 위해, Consumer 클래스의 subscribe() 메쏘드를 사용하고 있습니다.
  • 메시지가 섭스크라이브 하려고 하는 데스티네이션으로 퍼블리쉬되면, Consumer 에서 메시지 이벤트가 시작됩니다.

이 예제에서, 메시지는 Flex 클라이언트에 의해 퍼블리쉬 됩니다. BlazeDS Message Service 는 BlazeDS 데스티네이션으로 메시지를 퍼블리쉬 할 수 있도록 서버의 컴포넌트를 허용하는 Java API 도 제공합니다. Flex 와 Java 어플리케이션 사이위 메시지 교환을 위환 세 번째 옵션은 Flex 클라이언트가 JMS 토픽으로 퍼블리쉬하고, 섭스크라이브 할 수 있게 데스티네이션을 Java Message Service (JMS) 토픽으로 매핑하는 것입니다.  

Step 4: Run the application
어플리케이션 실행

  1. 어플리케이션을 시작하기 위해 Eclipse 툴바에 있는 Run 아이콘을 클릭하십시오
  2. 채팅 어플리케이션의 두 번째 인스턴스를 실행하기 위해 다른 브라우저 창에서 같은 URL 을 여십시오
  3. 한 채팅 클라이언트에서 메시지를 입력하고 "Send"룰 클릭하십시오: 메시지가 두 번째 채팅 클라이언트에서 보일 것입니다