From: http://www.riastep.com /? P = 101
Http://www.riastep.com /? Cat = 6
In Flex, there are two transmission modes (Request/response) and (publish/subscribe ).
Producer (producer)/consumer (consumer), producer and consumer use messaging to communicate, obviously using the publishing/subscription mechanism. The producer is used to publish messages, and the consumer is used to subscribe to consumers. For example, a client subscribes to a message from the server. When the message changes, the client that subscribes to the message will receive the updated message and update it instantly. Therefore, we can make a lot of applications with real-time interaction requirements, such as chat rooms, Im, meeting rooms, online stocks, etc...
Message Publishing component-producer
Common attributes:
- ID-producer Instance name.
- Destination-the target of the message service. This value should match the target entry in the messaging-config.xml file.
- Channelset-provides access to the Message channel class configured for the producer.
- Autoconnect-if it is
true
, Then the producer will be called for the first timesend()
Method is automatically connected to its target. Iffalse
, Must be explicitly calledconnect()
Method to establish a connection with the target. By default, this attribute istrue
But applications that need to operate in offline mode may set this attributefalse
To preventsend()
Method.
- Connected-read-only attribute. Indicates whether the producer is connected to its target through its channelset.
- Reconnectattempts-Number of reconnection attempts when a connection to the target is lost or the target to which the connection is closed.
- Reconnectinterval-Number of milliseconds between retries.
- Requesttimeout-sets the request timeout (in seconds) for sent messages. If no confirmation, response, or error is received from the remote target before the timeout is reached, an error is reported on the client.
- Subtopic-access to the subtopic of the remote target.
Common Methods:
connect():void--
Connect the producer to its target. When establishing a connection,connected
The property is changedtrue。
Disconnect (): void -- disconnect the producer from its remote target. This method does not wait until the unfinished network operation is completed. Calldisconnect()
Method, the producer reports that it is not connected and will not receive any uncompleted message confirmation or error. If you are trying to reconnect, disconnecting will stop the attempt to automatically reconnect.
Send (message: iMessage): void -- send a specified message to the target. If you use producer to publish/subscribe to message transmission, unless you use a custom message type and the message target on the server is configured to process custom messages, otherwise, only asyncmessage messages can be sent.
Common events:
- Fault-schedule a fault event when a message error occurs.
- Acknowledge-messageresponder schedules the confirmation result of the sent message.
- Channelconnect-scheduling when establishing a connection with the channel.
- Channeldisconnect-scheduling when disconnected from the channel.
- Channelfault-scheduling when an error occurs in the channel used.
After a connection is established between the producer and disconnect, the connected attribute is set to true.Public Function send (message: iMessage): The connected attribute is automatically checked before the void method sends a message. After the producer sends a message using the send () method, the acknowledge event is automatically scheduled when the message is sent successfully. If the message fails to be sent, the fault event is automatically scheduled. We can process the event in the event listening method. If the client and severs lose connection for some reason, the producer automatically tries to connect to the server according to the reconnectattempts and reconnectinterval settings. If reconnectattempts is set to-1, the producer will try to reconnect to the server without limit. If reconnectattempts is set to 0, no operation will be attempted to reconnect to the server.
Message subscription and receiving component-consumer
Common attributes:
- ID-consumer Instance name.
- Destination-the target of the message service. This value should match the target entry in the messaging-config.xml file.
- Channelset-provides access to the Message channel class configured for consumer.
- Connected-read-only attribute. Indicates whether the consumer has connected to the target through its channelset.
- Subscribed-read-only attribute. Indicates whether the consumer is currently in the subscription status.
- Reconnectattempts-Number of reconnection attempts when a connection to the target is lost or the target to which the connection is closed.
- Reconnectinterval-Number of milliseconds between retries.
- Requesttimeout-sets the request timeout (in seconds) for sent messages. If no confirmation, response, or error is received from the remote target before the timeout is reached, an error is reported on the client.
- Subtopic-access to the subtopic of the remote target.
- Selector-string type. The consumer selector.
Common Methods:
subscribe(clientId:String = null):void
-Subscribe to a remote target.
- Unsubscribe (preservedurable: Boolean = false): void-Unsubscribe from a remote destination. For persistent JMS subscriptions, this will destroy the persistent subscription on the JMS server.
- Receive ()-Requests all messages queued for this consumer on the server. This method should be used only for consumers subscribed through non-real-time and non-polling channels. If you have not subscribed to consumer, this method is not available.
- Disconnect ()-disconnect a consumer from its remote target. This method should be called on the consumer that is no longer needed by the application after the subscription is canceled. This method does not wait for unfinished network operations to complete or send a message to the server to cancel the subscription. After disconnect () is called, consumer will report that it is in the disconnected unsubscribe status because it will not receive any other messages before re-establishing the connection and re-subscription. If you are attempting to subscribe again, disconnecting will stop the attempt to automatically subscribe again.
Common events:
- Message-consumer is scheduled to receive messages.
- Fault-schedule a fault event when a message error occurs.
- Channelconnect-scheduling when establishing a connection with the channel.
- Channeldisconnect-scheduling when disconnected from the channel.
- Channelfault-scheduling when an error occurs in the channel used.
After a connection is established between the consumer and the target referred to by disconnect, the connected attribute is set to true, and the subscribed attribute is set to true after messages are successfully subscribed using the subscribe () method. When a message is sent to the client,Consumer automatically schedules the message event. We can handle it in the message event listening method.
If the client and severs lose connection for some reason, the producer automatically tries to connect to the server according to the reconnectattempts and reconnectinterval settings. If reconnectattempts is set to-1, the producer will try to reconnect to the server without limit. If reconnectattempts is set to 0, no operation will be attempted to reconnect to the server.Consumer uses its
Unsubscribe () method.
Other materials: http://www.javaeye.com/topic/549037