+ All Categories
Home > Technology > Queue System and Zend\Queue implementation

Queue System and Zend\Queue implementation

Date post: 28-Nov-2014
Category:
Upload: gianluca-arbezzano
View: 731 times
Download: 2 times
Share this document with a friend
Description:
PugMi for Queue System and implementation in PHP with Zend\Queue
19
http:// twitter.com/ GianArb Queue system gianarb92@gmail .com Gianluca Arbezzano 1 Gianluca Arbezzano @ GianArb http://github.com/gianarb
Transcript
Page 1: Queue System and Zend\Queue implementation

http://twitter.com/GianArb 1

Queue system

[email protected] Gianluca Arbezzano

Gianluca Arbezzano@GianArbhttp://github.com/gianarb

Page 2: Queue System and Zend\Queue implementation

http://twitter.com/GianArbGianluca Arbezzano 2

TODO

• Send a mail• Upload big file• Imports• Messagging• …

[email protected]

Page 3: Queue System and Zend\Queue implementation

http://twitter.com/GianArbGianluca Arbezzano 3

Only one Role

[email protected]

You DON’T MAKE to user wait

Page 4: Queue System and Zend\Queue implementation

http://twitter.com/GianArbGianluca Arbezzano 4

The solution is…

[email protected]

Page 5: Queue System and Zend\Queue implementation

http://twitter.com/GianArbGianluca Arbezzano 5

Queue System

[email protected]

• Queue• Producer• Consumer• Message

Page 6: Queue System and Zend\Queue implementation

http://twitter.com/GianArbGianluca Arbezzano 6

Queue

[email protected]

ZendQueue\Queue

// Create an array queue adapter$adapter = new ArrayAdapter();

// Create a queue object$queue = new Queue('queue1', $adapter);

Adapter• Db• MongoDb• MongoCappedCollection• ArrayAdapter• TODO: Redis• TODO: RabbitMQ

Page 7: Queue System and Zend\Queue implementation

http://twitter.com/GianArbGianluca Arbezzano 7

Message Queue

Message queues provide an asynchronous communications protocol, meaning that the sender and receiver of the message do not need to interact with the message queue at the same time.

Cit. Wikipedia

[email protected]

ZendQueue\Message\Message implement Zend\Stdlib\Message

Page 8: Queue System and Zend\Queue implementation

http://twitter.com/GianArbGianluca Arbezzano 8

Producer• Send messages quickly

[email protected]

Consumer

• Receive messages• Usually it’s a worker that consumes one

message at time.

Page 9: Queue System and Zend\Queue implementation

http://twitter.com/GianArbGianluca Arbezzano 9

Queue System, when?

[email protected]

Registration Query Mail

• Time-shifts job processing when the “CPU” is less busy• Distributed computing• In a web app allows to run tasks in backgroundreducing the http request time

Page 10: Queue System and Zend\Queue implementation

http://twitter.com/GianArbGianluca Arbezzano 10

Workflow

[email protected]

Registration QuerySend

Message in queue

Send mail

Send mail

Send mail

Send mail

Send mailWORKER

Page 11: Queue System and Zend\Queue implementation

http://twitter.com/GianArbGianluca Arbezzano 11

Worker (old way)

use ZendQueue\Queue;

do {$messages = $queue->receive();//do some worksleep(1);

} while (…)

[email protected]

Page 12: Queue System and Zend\Queue implementation

http://twitter.com/GianArbGianluca Arbezzano 12

WORKERS!

[email protected]

Page 13: Queue System and Zend\Queue implementation

http://twitter.com/GianArbGianluca Arbezzano 13

Await

[email protected]

$queue->await()

Page 14: Queue System and Zend\Queue implementation

http://twitter.com/GianArbGianluca Arbezzano 14

Role

• Your adapter implement awaitMessage()• || your queue can emulate await

[email protected]

$adapter->awaitMessages(Queue $queue, $callback, ReceiveParameters $params = null)

Page 15: Queue System and Zend\Queue implementation

http://twitter.com/GianArbGianluca Arbezzano 15

*callback

[email protected]

Page 16: Queue System and Zend\Queue implementation

http://twitter.com/GianArbGianluca Arbezzano 16

Worker

use ZendQueue\Queue;use ZendQueue\QueueEvent;

$queue->getEventManager()->attach(QueueEvent::EVENT_RECEIVE, function (QueueEvent $e) use ($queue) { $message = $e->getMessages()->current(); //do some work $queue->delete($message);});

//Wait for incoming messages$queue->await();

[email protected]

Page 17: Queue System and Zend\Queue implementation

http://twitter.com/GianArbGianluca Arbezzano 17

Use case overview

• Short run jobs• Long run jobs• Schedule jobs (like cron jobs)

• Enterprise service bus

[email protected]

Page 19: Queue System and Zend\Queue implementation

http://twitter.com/GianArbGianluca Arbezzano 19

Ripa Club

[email protected]

Powered byhttp://github.com/ripaclub


Recommended