Ian P. Christian's Personal Blog Random witterings from pookey

16Mar/090

STOMPing with PHP – intergration with ActiveMQ

In a previous post, "Playing with ActiveMQ using Maven", I showed how to produce and consume simple messages using Java. In this short post I show how to access these messages from PHP using STOMP.

Assuming you are continuing from my previous article - you will need to modify your ActiveMQ configuration file to enable STOMP access. Only one additional line is required, but I include the full config below.

<?xml version="1.0"?>
<beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:amq="http://activemq.apache.org/schema/core"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://www.springframework.org/schema/beans
  http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
  http://activemq.apache.org/schema/core
  http://activemq.apache.org/schema/core/activemq-core.xsd
  ">
  <broker xmlns="http://activemq.apache.org/schema/core" brokerName="localhost" dataDirectory="./data">
    <!-- The transport connectors ActiveMQ will listen to -->
    <transportConnectors>
      <transportConnector name="openwire" uri="tcp://localhost:61616"/>
      <transportConnector name="stomp" uri="stomp://localhost:61613"/>
    </transportConnectors>
  </broker>
</beans>

Restart your activeMQ (see previous post). Now we need to get the STOMP library - this is available here: http://stomp.codehaus.org/PHP. Here's an example script I've made which monitors a queue and consumes each message as it arrives.

<?php
 
 
// modify the include path for stomp (ug!)
set_include_path(get_include_path() . PATH_SEPARATOR . dirname(__FILE__) . '/Stomp/');
require_once('Stomp/Stomp.php');
 
$conn = new Stomp("tcp://localhost:61613");
 
echo "connecting....";
$conn->connect();
echo " done!\n";
$conn->subscribe('/queue/test');
 
while (1)
{
  if (($msg = $conn->readFrame()) !== false)
  {
    echo (string)$msg;
    $conn->ack($msg);
  }
}

You should see the output as follows:

connecting.... done!
MESSAGE
message-id: ID:fork-60632-1237222786629-0:0:1:1:100
destination: /queue/test
timestamp: 1237222787006
expires: 0
priority: 4
 
Hello World!
Filed under: geek, php Leave a comment
Comments (0) Trackbacks (0)

No comments yet.


Leave a comment


No trackbacks yet.