2008. 8. 31. 23:30

[BlazeDS] Consumer 컴포넌트 / Working with Consumer components

Reference : http://livedocs.adobe.com/blazeds/1/blazeds_devguide/messaging_4.html#132296

MXML 이나 ActionScript 에서 Consumer 컴포넌트를 생성할 수 있습니다. 섭스크라이브를 하려면, Consumer.subscribe() 메쏘드를 호출하면 됩니다.

Consumer 컴포넌트의 message 와 fault 이벤트 핸들러를 명시할 수 있습니다. Consumer 컴포넌트는 메시지가 데스티네이션에 도착할 때 (and the message has been routed to a consumer subscribed to that destination) message 이벤트를 브로드캐스트 합니다. fault 이벤트는 Consumer 컴포넌트가 섭스크라이브되는 채널이 연결되지 않거나 섭스크립션 요청이 거절될 때 브로드캐스트 됩니다.

Consumer 클래스에 대한 정보는 Adobe LiveCycle ES ActionScript Reference 에서 볼 수 있습니다

Creating a Consumer component in MXML
MXML 에서 Consumer 컴포넌트 생성

MXML 에서 Consumer 컴포넌트를 생성하기 위해 <mx:Consumer> 태그를 사용합니다. 태그는 반드시 id 값을 갖고 있어야 합니다. 일반적으로 그 것은 서버의 services-config.xml 에 정의된 destination 을 나타냅니다.

아래의 코드는 데스티네이션과 acknowledge, fault 이벤트 핸들러가 명시된 <mx:Consumer> 태그를 보여줍니다:

<?xml version="1.0"?> <!-- ds\messaging\CreateConsumerMXML.mxml --> <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" creationComplete="logon();"> <mx:Script> <![CDATA[ import mx.messaging.*; import mx.messaging.messages.*; import mx.messaging.events.*; // 데스티네이션으로 섭스크라이브 private function logon():void { consumer.subscribe(); } // TextArea 컨트롤에 받은 메시지를 적음. private function messageHandler(event:MessageEvent):void { // 메시지 이벤트 처리. ta.text += event.message.body + "\n"; } private function faultHandler(event:MessageFaultEvent):void { // 메시지 폴트 이벤트 처리. } ]]> </mx:Script> <mx:Consumer id="consumer" destination="chat" message="messageHandler(event);" fault="faultHandler(event);"/> <mx:TextArea id="ta" width="100%" height="100%"/> </mx:Application>

컴포넌트의 unsubscribe() 메쏘드를 호출함으로써 Consumer 컴포넌트를 섭스크라이브 하지 않을 수 있습니다.

Creating a Consumer component in ActionScript
ActionScript 에서 Consumer 컴포넌트 생성

ActionScript 에서 Consumer 컴포넌트를 생성할 수 있습니다. 아래의 컴포넌트는 <mx:Script> 태그의 메쏘드에서 생성된 Consumer 컴포넌트를 보여줍니다:

<?xml version="1.0"?> <!-- ds\messaging\CreateConsumerAS.mxml --> <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" creationComplete="logon();"> <mx:Script> <![CDATA[ import mx.messaging.*; import mx.messaging.messages.*; import mx.messaging.events.*; // Consumer 타입의 변수 생성. private var consumer:Consumer; // Consumer 생성. private function logon():void { consumer = new Consumer(); consumer.destination = "chat"; consumer.addEventListener (MessageEvent.MESSAGE, messageHandler); consumer.addEventListener (MessageFaultEvent.FAULT, faultHandler); consumer.subscribe(); } // TextArea 컨트롤에 받은 메시지를 적음. private function messageHandler(event:MessageEvent):void { // 메시지 이벤트 처리. ta.text += event.message.body + "\n"; } private function faultHandler(event:MessageFaultEvent):void{ // 메시지 폴트 이벤트 처리. } ]]> </mx:Script> <mx:TextArea id="ta" width="100%" height="100%"/> </mx:Application>

Sending and receiving an object in a message
메시지에서 객체 주고 받기

메시지의 일부로 객체를 주고 받을 수 있습니다. 아래의 예제는 객체를 포함하고 있는 메시지르 주고 받습니다:

<?xml version="1.0"?> <!-- ds\messaging\SendObjectMessage.mxml --> <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" creationComplete="logon();"> <mx:Script> <![CDATA[ import mx.messaging.*; import mx.messaging.messages.*; import mx.messaging.events.*; // 데스티네이션으로 섭스크라이브. private function logon():void { consumer.subscribe(); } // TextInput 컨트롤에서 메시지 생성. private function sendMessage():void { var message:AsyncMessage = new AsyncMessage(); message.body = new Object(); message.body.uName = userName.text; message.body.uInput = input.text; message.body.theCollection = ['b','a',3,new Date()]; producer.send(message); } // TextArea 컨트롤에 받은 메시지 삽입. private function messageHandler(event:MessageEvent):void { // 메시지 이벤트 처리. ta.text = String(event.message.body.uName) + " ," + String(event.message.body.uInput); } ]]> </mx:Script> <mx:Producer id="producer" destination="chat"/> <mx:Consumer id="consumer" destination="chat" message="messageHandler(event);"/> <!-- 사용자 입력 처리. --> <mx:TextInput id="userName"/> <mx:TextInput id="input"/> <mx:Button label="Send" click="sendMessage();"/> <!-- 받은 메시지 표시. --> <mx:TextArea id="ta"/> </mx:Application>

Handling a network disconnection
네트워크 disconnection 처리

Use the Consumer.subscribed and Consumer.connected properties to monitor the status of the Consumer component. The connected property is set to true when the Flex client is connected to the server. The subscribed property is set to true when the Flex client is subscribed to a destination.

Two properties control the action of the Consumer component when the destination becomes unavailable, or the subscription to the destination fails:

  • resubscribeAttempts

    Specifies the number of times the component attempts to resubscribe to the server before dispatching a fault event. The component makes the specified number of attempts over each available channel. You can set the Channel.failoverURIs property to the URI of a computer to attempt to resubscribe to if the connection is lost. You typically use this property when operating in a clustered environment. For more information, see Clustering.

    A value of -1 specifies to continue indefinitely, and a value of 0 disables attempts.

  • resubscribeInterval

    Specifies the interval, in milliseconds, between attempts to resubscribe. Setting the value to 0 disables resubscription attempts.

Calling the receive method
receive 메쏘드 호출

Typically, you use a real-time polling or streaming channel with the Consumer component. In both cases, the Consumer receives messages from the server without having to initiate a request. For more information, see Channels.

You can use a non-real-time channel, such as an AMFChannel with polling-enabled set to false, with a Consumer component. In that case, call the Consumer.receive() method directly to initiate a request to the server to receive any queued messages. Before you call the receive() method, call the subscribe() method to subscribe to the destination. A fault event is broadcast if a failure occurs when the Consumer.receive() method is called.