| Sign In/My Account | View Cart |
How to Use JMS with PHP
Pages: 1, 2
This section describes how to install and configure all of the components so that PHP scripts will run under PHPMQ.
Download and unzip the MantaRay messaging layer.
Enable the RMI API in the MantaRay configuration:
enable_rmi_api to true in the default_config.params configuration file.create_rmi_registry to true in the default_config.params configuration file, unless you are using an external RMI registry.rmi_registry_port to the port that the RMI registry will listen to, in the default_config.params configuration file. rmi_registry_host_name to the IP address or hostname of the computer where the RMI registry is located (if the registry is local, use localhost). There are security considerations that need to be taken into account in order for the RMI API to work. Refer to jGuru: Remote Method Invocation: Introduction, and the security policy at Tom Valesky's "How to Create an RMI System."
Download and install PHP version 4.3.8.
Enable the PHP/Java extension by adding these lines in php.ini:
extension=php_java.dll
[Java]
java.class.path = "./extensions/php_java.jar;\
../mantaray/manta.jar;../phpmq/phpmq.jar"
java.home = "./j2sdk1.4.2_03/jre"
java.library = "./j2sdk1.4.2_03/jre/bin/client/jvm.dll"
java.library.path = "./extensions"
Editor's note: The java.class.path has been reformatted for this web page--it should all be on one line.
Most PHP scripts run over Apache and enable a dynamic user interface with an HTTP browser. Note that in order for the PHP/Java extension to work on Apache, you need to either run the PHP as a CGI or to run the PHP on a prefork-compiled Apache. To run PHP as a CGI on Apache, add these lines to the Apache httpd.conf configuration file:
ScriptAlias /php/ "./phpInstalledDirectory/"
AddType application/x-httpd-php .php
Action application/x-httpd-php "/php/php.exe"
Start MantaRay by running java org.mr.MantaStandAlone or by running standalone.bat.
To create an instance of the PHP messaging API, a PHP script should include messaging.php, like this:
<?php
include('./libraries/messaging.php');
$msg = new messaging('//localhost',10005);
...>
The parameters passed at instantiation are the host name and port number of the MantaRay RMI Registry.
After an instance of the messaging API has been created, developers can start working with the functions provided by the messaging API. These functions are discussed in the next section.
The PHPMQ messaging API includes the following functions:
function enqueue($userKey, $queueName, $message)
Sends $message (string) to a queue with the name $queueName (string). $userKey (string) is the identifier used by the messaging bus.
function dequeue($userKey, $queueName)
Returns a string message dequeued from the queue with the name $queueName (string). $userKey (string) is the identifier used by the messaging bus.
function getQueues()
Returns an array of strings that are the names of queues available on the message bus.
function getTopics()
Returns an array of strings that are the names of topics available on the message bus.
function subscribe($userKey $topicName, $messagesToCash)
Adds a listener to a given $topicName (string). The listener will receive up to $messagesToCash (number) messages. $userId (string) is the identifier used by the messaging bus; use this identifier to get messages with getMessageFromTopic(....).
function getMessageFromTopic($userKey, $topicName)
Gets the messages gathered by the listener on a given $topicName (string). $userKey (string) is the identifier used by the messaging bus used when invoking subscribe(...).
function publishMessage($userKey, $topicName, $msg)
Publishes a $msg (string) to a topic with name $topicName (string). $userKey (string) is the identifier used by the messaging bus.
In the PHPMQ download package, there are several example applications that use both queues and topics.
The ability of languages to cooperate is very important, but in many cases is still lacking or hard to implement. As a community, we should strive to always use the right tool for the task and ensure that these tools communicate with each other seamlessly.
PHPMQ is an example of how two languages can empower each other: using PHPMQ gives the PHP developer a window to the back-end world and gives the Java developer the freedom to use an easy and fast web development tool as his front-end tier (instead of the JSP/servlet approach that is more difficult to work with). With PHPMQ, you can have a long-lived Java application maintaining a session and keeping persistent data for the short-lived PHP scripts.
Amir Shevat is a senior software developer with eight years of experience in computing.
Return to ONJava.com.
Showing messages 1 through 7 of 7.
PersistentpublishMessage and enqueue methods. This will help you disable the persistency when you do not need it .
Why?The article is about extending the PHP to the backend enterprise Java world, a native JMS for PHP will not serve this purpose and will not give the PHP the abilities that are needed for this task.
I agree that the PHP/JAVA extension is costly (not to the extend you describe it but still costly). In the next release of PHPMQ I intend to implement the messaging interface over HTTP. This will eliminate the need for the PHP/JAVA extension.
Speaking of cost ... MantaRay is unique in that it uses a server-less, purely distributed architecture. You do not need a costly centralized broker (with load balancer and backups). Remove the centralized broker and you remove this single point of congestion and failure, you get better performance and a more durable system.
Having said that, PHPMQ is an opensource project and the PHPMQ messaging interface is well defined and decoupled from the implementation, you are more then welcome to contribute code connecting PHPMQ to any other JMS provider.
getMessageFromTopic block until the number of messages specified to subscribe have been picked up?
phpmqdequeue will return null if empty. In the real world there should be a default value (i.e. "./myAdd.gif") set when dequeue returns null.subscribe adds a listener that gathers messages up to, but no more than $messagesToCash. Calling the getMessageFromTopic($userKey, $topicName) returns all the messages gathered by that listener (empty array if non gathered).
Can I send persistent messages with PHPMQ?