Introducción a Zend Framework

 Aprendiendo Zend Framework

Apéndice

 Referencia de Zend Framework


  • Zend_Gdata
  • Zend_Http
  • Zend_InfoCard
  • Zend_Json
  • Zend_Layout
  • Zend_Ldap
  • Zend_Loader
  • Zend_Locale
  • Zend_Log
  • Zend_Mail
  • Zend_Markup
  • Zend_Measure
  • Zend_Memory
  • Zend_Mime
  • Zend_Navigation
  • Zend_Oauth
  • Zend_OpenId
  • Zend_Paginator
  • Zend_Pdf
  • Zend_ProgressBar
  • Zend_Queue
  • Zend_Reflection
  • Zend_Registry
  • Zend_Rest

  • Zend_Search_Lucene
  • Zend_Serializer
  • Zend_Server
  • Zend_Service
  • Zend_Session
  • Zend_Soap
  • Zend_Tag
  • Zend_Test
  • Zend_Text
  • Zend_TimeSync
  • Zend_Tool
  • Zend_Tool_Framework
  • Zend_Tool_Project
  • Zend_Translate
  • Zend_Uri
  • Zend_Validate
  • Zend_Version
  • Zend_View
  • Zend_Wildfire
  • Zend_XmlRpc
  • ZendX_Console_Process_Unix
  • ZendX_JQuery
  • Traducción al 26.9% - Actualizado el 2011-11-16 - Revisión 24249 - Versión ZF 1.11.x

    19.2. Queue Service Introduction

    The QueueService implements access to message queues available as local or remote services. The simple queues that QueueService supports implement a messaging pattern that enables different processes to exchange messages in a reliable and scalable way. One common use case for such message queues is job dispatching, in which a frontend web server adds a complex job to a queue for a backend worker to do the expensive processing. The frontend web server can then return the page without waiting for the work to be completed.

    The interface Zend_Cloud_QueueService_Adapter defines the methods which concrete queue service adapters must implement. The following adapters are shipped with the Simple Cloud API:

    19.2.1. Instantiating and Configuring QueueService Adapters

    To instantiate a QueueService adapter, use the static method Zend_Cloud_QueueService_Factory::getAdapter(), which accepts either an array or a Zend_Config object. Three parameters apply to all adapters, while the remaining parameters are adapter-specific properties; these adapter-specific properties often contain access details.

    The general parameters are as follows:

    • queue_adapter specifies the concrete adapter class;

    • message_class specifies the class to use to represent queue messages; defaults to Zend_Cloud_QueueService_Message; and

    • messageset_class specifies the class to use to represent collections of queue messages; defaults to Zend_Cloud_QueueService_MesageSet.

    Ejemplo 19.17. Instantiating an Amazon SQS adapter via the factory

    $queues Zend_Cloud_QueueService_Factory::getAdapter(array(
        
    Zend_Cloud_QueueService_Factory::QUEUE_ADAPTER_KEY  => 'Zend_Cloud_QueueService_Adapter_Sqs',
        
    Zend_Cloud_QueueService_Adapter_Sqs::AWS_ACCESS_KEY => $amazonKey,
        
    Zend_Cloud_QueueService_Adapter_Sqs::AWS_SECRET_KEY => $amazonSecret,
    ));

    19.2.1.1. Service-Specific Options

    Tabla 19.4. Zend_Cloud_QueueService_Adapter_Sqs Options

    Option key Description Used in Required Default
    aws_accesskey Your Amazon AWS access key Constructor Yes None
    aws_secretkey Your Amazon AWS secret key Constructor Yes None
    http_adapter HTTP adapter to use in all access operations Constructor No Zend_Http_Client_Adapter_Socket
    http_adapter HTTP adapter to use in all access operations Constructor No Zend_Http_Client_Adapter_Socket
    visibility_timeout Message visibility timeout receiveMessages() No 60

    Tabla 19.5. Zend_Cloud_QueueService_Adapter_WindowsAzure Options

    Option key Description Used in Required Default
    storage_accountname Windows Azure account name Constructor Yes None
    storage_accountkey Windows Azure account key Constructor Yes None
    storage_host Windows Azure access host Constructor No queue.core.windows.net
    storage_proxy_host Proxy hostname Constructor No None
    storage_proxy_port Proxy port Constructor No 8080
    storage_proxy_credentials Proxy credentials Constructor No None
    http_adapter HTTP adapter to use in all access operations Constructor No Zend_Http_Client_Adapter_Socket
    visibility_timeout Message visibility timeout receiveMessages() No 60
    prefix Filter the results to only queue names beginning with given prefix listQueues() No None
    max_results Limit queue list to certain number of results listQueues() No 5,000
    ttl Set time-to-live for message sendMessage() No 7 days

    Tabla 19.6. Zend_Cloud_QueueService_Adapter_ZendQueue Options

    Option key Description Used in Required Default
    adapter Concrete Zend_Queue adapter to use. See the Zend_Queue documentation for supported adapters and their options. Constructor No Filesystem
    timeout Visibility timeout for messages createQueue(), receiveMessages() No 30

    19.2.2. Basic concepts

    Every queue service typically offers one or more queues. Each queue can store zero or more messages. A process can send a message to a queue, and another process can remove it. Usually processes remove the oldest message in the queue, observing a first in, first out (FIFO) queue-style interface.

    19.2.3. Exceptions

    If some error occurs inside the storage service, a Zend_Cloud_QueueService_Exception is thrown. If the exception was caused by underlying service driver, you can use the getClientException() method to retrieve the original exception.

    Since different cloud providers implement different sets of services, some adapters do not implement certain features. In this case, the Zend_Cloud_OperationNotAvailableException exception is thrown.

    19.2.4. Create a queue

    The createQueue() method creates a message queue with the given name. It returns a queue identifier, the format of which is service-dependent. Some services return a URL for the queue identifier, while others return a GUID to use in future operations.

    Ejemplo 19.18. Creating a queue

    $queueId $queues->createQueue('my-queue');

    19.2.5. Delete a queue

    The deleteQueue() method removes the queue from the service. You must use the identifier received from createQueue() when calling deleteQueue().

    Ejemplo 19.19. Deleting a queue

    $queueId $queues->createQueue('my-queue');

    // ... do stuff ...

    $queues->deleteQueue($queueId);

    [Nota] Nota

    Deleting a queue can take significant time for some services. Typically, you cannot re-create a queue with the same name until the original queue is fully removed.

    19.2.6. List queues

    To retrieve the list of all queues in the system, use the listQueues() method.

    Ejemplo 19.20. Listing queues

    $names $queues->listQueues();
    foreach (
    $names as $name) {
        echo 
    "Found queue $name\n";
    }

    19.2.7. Set queue metadata

    In some services, you can associate a set of key-value pairs with the queue as queue metadata. To set queue metadata, use the storeQueueMetadata() method:

    Ejemplo 19.21. Setting queue metadata

    $queues->storeQueueMetadata($queueId, array(
        
    'purpose'       => 'Operations',
        
    'administrator' => 'joe@example.com',
    ));

    19.2.8. Fetch queue metadata

    To retrieve queue metadata, use the fetchQueueMetadata() method.

    Ejemplo 19.22. Fetching queue metadata

    $metadata $queues->fetchQueueMetadata($queueId);
    foreac h($metadata as $key => $value) {
        echo 
    "Metadata $key$value\n";
    }

    19.2.9. Send a message

    To add a message to a queue, use the sendMessage() method. The message is passed as an unstructured string.

    Ejemplo 19.23. Sending a message

    $queues->sendMessage($queueId"Hello world!");

    19.2.10. Receive a message

    To receive one or more messages from the queue, use the receiveMessages() method. This method returns a Zend_Cloud_QueueService_MessageSet instance by default, unless configured otherwise. Each element of the MessageSet is an instance of Zend_Cloud_QueueService_Message by default, unless configuired otherwise.

    Ejemplo 19.24. Receiving a message

    // Get the first message
    $messages $queues->receiveMessages($queueId);
    if (
    count($messages)) {
        foreach (
    $messages as $message) {
            echo 
    "Message: " $message->getBody();
            break;
        }
    }

    // Get two messages
    $messages $queues->receiveMessages($queueId2);

    When a message is received, it is not visible to other clients. It is not deleted from the queue, however, until the client that has received the message calls the deleteMessage() method. If it is not deleted during the specfied visibility timeout, it will become visible to all other clients again. In other words, all clients will be able to retrieve the message with the receiveMessages() method if the visibility timeout is exceeded.

    19.2.11. Delete a message

    In order to delete the message from the queue, use the deleteMessage() method. This method deletes the specified message.

    Ejemplo 19.25. Deleting a message

    // process and delete $max messages
    $messages $queues->receiveMessages($queueId$max);
    if (
    count($messages)) {
        foreach (
    $messages as $message) {
            
    process($message);
            
    $queues->deleteMessage($queueId$message);
        }
    }

    19.2.12. Accessing concrete adapters

    Sometimes it is necessary to retrieve the concrete adapter for the service that the Queue API is working with. This can be achieved by using the getAdapter() method.

    [Nota] Nota

    Accessing the underlying adapter breaks portability among services, so it should be reserved for exceptional circumstances only.

    Ejemplo 19.26. Using a concrete adapter

    // send the message directly with the SQS client library
    $sqs $queues->getAdapter();
    $sqs->sendMessage("myQueue""hello!");

    digg delicious meneame google twitter technorati facebook

    Comentarios

    Loading...