简体   繁体   中英

Stomp / ActiveMQ in PHP theory - how to emulate Java's MDBs?

How does one go about creating an equivalent of a Message-Driven Bean in PHP?

So, I understand that it is possible to send() messages to ActiveMQ via Stomp protocol, and also that it is possible to connect() to ActiveMQ and then to readFrame() when there is a message available in the queue in ActiveMQ.

What I don't understand is how does one solve the problem of asynchronous processing of received messages from the queue.

If you have one thread that is constantly waiting on the queue (and blocking) until something is in it, does that mean that you can only process one message at a time in PHP?

In Java EE MDBs you don't worry about threads, since the app server instantiates MDBs when they are needed, but I don't understand how to create a high-throughput application in PHP which listens on an ActiveMQ Queue.

Any ideas?

It's not really clear in which sense you refer to PHP, PHP as in the language or PHP as in runtime used with a web server?

However, I would create a separate php script and start it separately from any script invoked from a web application. Such as when the server boots up and has a loop that reads STOMP messages, just like you describe.

How would you get it to communicate with the web application then? Simply process and store the content of the messages somehow in the web application database. This is essentially how JavaEE MDBs work as well, but there is a container to handle starting of MDB threads, as you have notice.

About threading: you could fork processes in PHP, although threading is not really an option. look at this example: http://us.php.net/pcntl_fork

// fork a php script into two processes. Then make each process
if (pcntl_fork() == -1) {
 die('Forking failed');
} 
// This is run twice in different processes, one main process and one child.
run_message_listener_loop(); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM