首页 » phpcode » PHP通过Stomp协议与,ActiveMQ,通信所遇到的问题

PHP通过Stomp协议与,ActiveMQ,通信所遇到的问题

 

1.php安装stomp扩展,http://pecl.php.net/package/stomp,选择对应的版本~~!

2.因要java那边需要jms-map-json的头的消息头

客户端send.php编码:


<?php
//http://www.lancehendrix.com/techdocs/incubation/Talking%20Stomp%20to%20ActiveMQ.html
$st = new SStomp();
$body = array(
    'map' => array(
        'entry' => array(
            array('string' => array('contentName', 'Partner Resources')),
            array('string' => array('contentHREF', '123')),
            array('string' => array('contentId', '10034')),
            array('string' => array('author', 'Lance Hendrix')),
            array('string' => array('authorEmail', 'lance@lancehendrix.com')),
            array('string' => array('content', 'These are the partners we should be using...')),
            array('string' => array('contentType', 'blog')),
            array(array('string' => 'uid', 'int' => 1234))
        )
    )
);
$s = $st->send($body);
?>
SStomp.php
<?php

/**
 * Basic text stomp message
 *
 * @package Stomp
 * @author Dejan Bosanac <dejan@nighttale.net>
 * @version $Revision: 23 $
 */
class Stomp_Message extends StompFrame
{
    public function __construct ($body, $headers = null)
    {
        //$this->headers['amq-msg-type'] = 'MapMessage';
        $this->body = json_encode($body);
        //$this->headers = array_merge($this->headers,$headers);
        $this->headers = $headers;
        $this->command = "SEND";
    }
}

class SStomp{

    public $broker = 'tcp://192.168.1.121:61613';
    public $queue  = 'quene';
    public $msg    = 'bar';

    public function send($msg=array()){
        try {
            //http://pecl.php.net/package/stomp
            $con = new Stomp($this->broker,'admin','admin');
            // send a message to the queue
            //$con->send($this->queue, $msg);
            $header = array(
                'transformation' => 'jms-map-json',
                'amq-msg-type' => 'MapMessage',
                //'DeliveryMode' => 'PERSISTENT',
            );
            $mapMessage = new Stomp_Message($msg, $header);

            $f = $con->send($this->queue, $mapMessage,array());
            $s = $con->subscribe($this->queue);
            // receive a message from the queue
            $msg = $con->readFrame();
            print_r($msg);
            $con->disconnect();

        } catch (Exception $e) {
            var_dump($e->getMessage());
        }
    }
}

原文链接:PHP通过Stomp协议与,ActiveMQ,通信所遇到的问题,转载请注明来源!

1