| Flex 2 Developer's Guide > Data Access and Interconnectivity > Using Flex Messaging > Working with Producer components | |||
You can create Producer components in MXML or ActionScript. To send a message, you create an AsyncMessage object and then call a Producer component's send() method to send the message to a destination.
You can also specify acknowledge and fault event handlers for a Producer component. An acknowledge event is broadcast when a destination successfully receives a message that a Producer component sends. A fault event is dispatched when a destination cannot successfully process a message due to a connection-, server-, or application-level failure.
A Producer component can send messages to destinations that use the Flex Message Service with no message adapters or to destinations that use message adapters. A message adapter is server-side code that acts as a conduit between the Flex Message Service and other messaging systems. For example, the Java Message Service (JMS) adapter enables Producer components to send messages to JMS topics. On the client side, the APIs for non-adapter and adapter-enabled destinations are identical. The definition for a particular destination in the Flex services configuration file determines what happens when a Producer component sends messages to the destination or a Consumer component subscribes to the destination. For information about configuring destinations, see Configuring the Message Service.
For reference information about the Producer class, see Adobe Flex 2 Language Reference.
You use the <mx:Producer> tag to create a Producer component in MXML. The tag must contain an id value. It should also specify a destination that is defined in the server-side services-config.xml file.
The following code shows an <mx:Producer> tag that specifies a destination and acknowledge and fault event handlers:
<?xml version="1.0"?>
<!-- fds\messaging\CreateProducerMXML.mxml -->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml">
<mx:Script>
<![CDATA[
import mx.rpc.events.FaultEvent;
import mx.messaging.*;
import mx.messaging.messages.*;
import mx.messaging.events.*;
private function messageHandler(event:MessageEvent):void {
// Handle message event.
}
private function acknowledgeHandler(event:MessageAckEvent):void{
// Handle message event.
}
private function faultHandler(event:MessageFaultEvent):void {
// Handle fault event.
}
]]>
</mx:Script>
<mx:Producer id="producer"
destination="ChatTopicJMS"
acknowledge="acknowledgeHandler(event)"
fault="faultHandler(event)"/>
</mx:Application>
You can create a Producer component in an ActionScript method. The following code shows a Producer component that is created in a method in an <mx:Script> tag. The import statements import the classes required to create a Producer component, create Message objects, add event listeners, and create message handlers.
<?xml version="1.0"?>
<!-- fds\messaging\CreateProducerAS.mxml -->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml">
<mx:Script>
<![CDATA[
import mx.messaging.*;
import mx.messaging.messages.*;
import mx.messaging.events.*;
private var producer:Producer;
private function acknowledgeHandler(event:MessageAckEvent):void{
// Handle message event.
}
private function faultHandler(event:MessageFaultEvent):void{
// Handle fault event.
}
private function logon():void {
producer = new Producer();
producer.destination = "ChatTopicJMS";
producer.addEventListener(MessageAckEvent.ACKNOWLEDGE, acknowledgeHandler);
producer.addEventListener(MessageFaultEvent.FAULT, faultHandler);
}
]]>
</mx:Script>
</mx:Application>
To send a message from a Producer component to a destination, you create an mx.messaging.messages.AsyncMessage object, populate the body of the AsyncMessage object, and then call the component's send() method. You can create text messages and messages that contain objects.
The following code creates a message, populates the body of the message with text, and sends the message by calling a Producer component's send() method:
<?xml version="1.0"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml">
<mx:Script>
<![CDATA[
import mx.messaging.*;
import mx.messaging.messages.*;
import mx.messaging.events.*;
private var producer:Producer;
private function sendMessage():void {
var message:AsyncMessage = new AsyncMessage();
message.body = userName.text + ": " + input.text;
producer.send(message);
}
]]>
</mx:Script>
<mx:TextInput id="userName"/>
<mx:TextInput id="input"/>
<mx:Button label="Send" click="sendMessage();"/>
</mx:Application>
The following code creates a message, populates the body of the message with an object, and sends the message by calling a Producer component's send() method:
Revised: 2/12/07. Replaced incorrect example.
<?xml version="1.0"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"> <mx:Script>
<![CDATA[
import mx.messaging.*;
import mx.messaging.messages.*;
import mx.messaging.events.*;
private var producer:Producer;
private function sendMessage():void {
var message:AsyncMessage = new AsyncMessage();
message.body=new Object();
message.body.prop1 = "abc";
message.body.prop2 = "abc";
message.body.theCollection=[’b’,’a’,3,new Date()];
producer.send(message);
}
]]>
</mx:Script>
</mx:Application>
You can include extra information for a message in the form of message headers. You can send strings and numbers in message headers. The headers of a message are contained in an associative array where the key is the header name. The headers property of a message class lets you add headers for a specific message instance.
The following code adds a message header called prop1 and sets its value:
<?xml version="1.0"?>
<!-- fds\messaging\SendMessageHeader.mxml -->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml">
<mx:Script>
<![CDATA[
import mx.messaging.*;
import mx.messaging.messages.*;
import mx.messaging.events.*;
private var producer:Producer;
private function sendMessage():void {
var message:AsyncMessage = new AsyncMessage();
message.headers = new Array();
message.headers["prop1"] = 5;
message.body =input.text;
producer.send(message);
}
]]>
</mx:Script>
<mx:TextInput id="input"/>
</mx:Application>
You can use a Consumer component's selector property to filter the messages that the component receives, based on message header values. For more information, see Filtering messages with a message selector.
|
NOTE |
|
Do not start message header names with the text JMS or DS. These prefixes are reserved. |
A Producer component sends a message once. If the delivery of a message that a Producer component sends is in doubt, the Producer component dispatches a fault event, which indicates that it never received an acknowledgment for the specified message. When the event is dispatched, the handler code can then make a decision to abort or attempt to resend the faulted message. Two events can trigger a fault that indicated delivery is in doubt. It can be triggered when the value of the requestTimeout property is exceeded or the underlying message channel becomes disconnected before the acknowledgment message is received. The fault handler code can detect this scenario by inspecting the faultCode property of the associated ErrorMessage for the ErrorMessage.MESSAGE_DELIVERY_IN_DOUBT code.
Flex 2.01