Stomp Client

目录

简介

Represents a connection between PHP and a Stomp compliant Message Broker.

类摘要

Stomp

class Stomp {

/* 方法 */

public bool abort ( string $transaction_id [, array $headers ] )

public bool ack ( mixed $msg [, array $headers ] )

public bool begin ( string $transaction_id [, array $headers ] )

public bool commit ( string $transaction_id [, array $headers ] )

public __construct ([ string $broker = ini_get("stomp.default_broker_uri") [, string $username [, string $password [, array $headers ]]]] )

public bool __destruct ( void )

public string error ( void )

public array getReadTimeout ( void )

public string getSessionId ( void )

public bool hasFrame ( void )

public stompframe readFrame ([ string $class_name = "stompFrame" ] )

public bool send ( string $destination , mixed $msg [, array $headers ] )

public void setReadTimeout ( int $seconds [, int $microseconds ] )

public bool subscribe ( string $destination [, array $headers ] )

public bool unsubscribe ( string $destination [, array $headers ] )

}

Stomp::abort

stomp_abort

Rolls back a transaction in progress

说明

面向对象风格 (method):

public bool Stomp::abort ( string $transaction_id [, array $headers ] )

过程化风格:

bool stomp_abort ( resource $link , string $transaction_id [, array $headers ] )

Rolls back a transaction in progress.

参数

link
仅对过程化样式:由 stomp_connect 返回的 stomp 连接标识符。

transaction_id
The transaction to abort.

headers
关联数组包含附加的头信息(例如: receipt)。

返回值

成功时返回 TRUE, 或者在失败时返回 FALSE

注释

小贴士

Stomp is inherently asynchronous. Synchronous communication can be implemented adding a receipt header. This will cause methods to not return anything until the server has acknowledged receipt of the message or until read timeout was reached.

范例

示例 #1 面向对象风格

<?php

/* connection */
try {
    $stomp = new Stomp('tcp://localhost:61613');
} catch(StompException $e) {
    die('Connection failed: ' . $e->getMessage());
}

/* begin a transaction */
$stomp->begin('t1');

/* send a message to the queue */
$stomp->send('/queue/foo', 'bar', array('transaction' => 't1'));

/* rollback */
$stomp->abort('t1');

/* close connection */
unset($stomp);
?>

示例 #2 过程化风格

<?php

/* connection */
$link = stomp_connect('tcp://localhost:61613');

/* check connection */
if (!$link) {
    die('Connection failed: ' . stomp_connect_error());
}

/* begin a transaction */
stomp_begin($link, 't1');

/* send a message to the queue 'foo' */
stomp_send($link, '/queue/foo', 'bar', array('transaction' => 't1'));

/* rollback */
stomp_abort($link, 't1');

/* close connection */
stomp_close($link);

?>

Stomp::ack

stomp_ack

Acknowledges consumption of a message

说明

面向对象风格 (method):

public bool Stomp::ack ( mixed $msg [, array $headers ] )

过程化风格:

bool stomp_ack ( resource $link , mixed $msg [, array $headers ] )

Acknowledges consumption of a message from a subscription using client acknowledgment.

参数

link
仅对过程化样式:由 stomp_connect 返回的 stomp 连接标识符。

msg
The message/messageId to be acknowledged.

headers
关联数组包含附加的头信息(例如: receipt)。

返回值

成功时返回 TRUE, 或者在失败时返回 FALSE

注释

Note:

A transaction header may be specified, indicating that the message acknowledgment should be part of the named transaction.

小贴士

Stomp is inherently asynchronous. Synchronous communication can be implemented adding a receipt header. This will cause methods to not return anything until the server has acknowledged receipt of the message or until read timeout was reached.

范例

示例 #1 面向对象风格

<?php

$queue  = '/queue/foo';
$msg    = 'bar';

/* connection */
try {
    $stomp = new Stomp('tcp://localhost:61613');
} catch(StompException $e) {
    die('Connection failed: ' . $e->getMessage());
}

/* send a message to the queue 'foo' */
$stomp->send($queue, $msg);

/* subscribe to messages from the queue 'foo' */
$stomp->subscribe($queue);

/* read a frame */
$frame = $stomp->readFrame();

if ($frame->body === $msg) {
    /* acknowledge that the frame was received */
    $stomp->ack($frame);
}

/* remove the subscription */
$stomp->unsubscribe($queue);

/* close connection */
unset($stomp);

?>

示例 #2 过程化风格

<?php

$queue  = '/queue/foo';
$msg    = 'bar';

/* connection */
$link = stomp_connect('ssl://localhost:61612');

/* check connection */
if (!$link) {
    die('Connection failed: ' . stomp_connect_error());
}

/* begin a transaction */
stomp_begin($link, 't1');

/* send a message to the queue 'foo' */
stomp_send($link, $queue, $msg, array('transaction' => 't1'));

/* commit a transaction */
stomp_commit($link, 't1');

/* subscribe to messages from the queue 'foo' */
stomp_subscribe($link, $queue);

/* read a frame */
$frame = stomp_read_frame($link);

if ($frame['body'] === $msg) {
    /* acknowledge that the frame was received */
    stomp_ack($link, $frame['headers']['message-id']);
}

/* remove the subscription */
stomp_unsubscribe($link, $queue);

/* close connection */
stomp_close($link);

?>

Stomp::begin

stomp_begin

Starts a transaction

说明

面向对象风格 (method):

public bool Stomp::begin ( string $transaction_id [, array $headers ] )

过程化风格:

bool stomp_begin ( resource $link , string $transaction_id [, array $headers ] )

Starts a transaction.

参数

link
仅对过程化样式:由 stomp_connect 返回的 stomp 连接标识符。

transaction_id
The transaction id.

headers
关联数组包含附加的头信息(例如: receipt)。

返回值

成功时返回 TRUE, 或者在失败时返回 FALSE

注释

小贴士

Stomp is inherently asynchronous. Synchronous communication can be implemented adding a receipt header. This will cause methods to not return anything until the server has acknowledged receipt of the message or until read timeout was reached.

范例

See stomp_commit or stomp_abort.

Stomp::commit

stomp_commit

Commits a transaction in progress

说明

面向对象风格 (method):

public bool Stomp::commit ( string $transaction_id [, array $headers ] )

过程化风格:

bool stomp_commit ( resource $link , string $transaction_id [, array $headers ] )

Commits a transaction in progress.

参数

link
仅对过程化样式:由 stomp_connect 返回的 stomp 连接标识符。

transaction_id
The transaction id.

headers
关联数组包含附加的头信息(例如: receipt)。

返回值

成功时返回 TRUE, 或者在失败时返回 FALSE

注释

小贴士

Stomp is inherently asynchronous. Synchronous communication can be implemented adding a receipt header. This will cause methods to not return anything until the server has acknowledged receipt of the message or until read timeout was reached.

范例

示例 #1 面向对象风格

<?php

/* connection */
try {
    $stomp = new Stomp('tcp://localhost:61613');
} catch(StompException $e) {
    die('Connection failed: ' . $e->getMessage());
}

/* begin a transaction */
$stomp->begin('t1');

/* send a message to the queue */
$stomp->send('/queue/foo', 'bar', array('transaction' => 't1'));

/* commit */
$stomp->commit('t1');

/* close connection */
unset($stomp);

?>

示例 #2 过程化风格

<?php

/* connection */
$link = stomp_connect('tcp://localhost:61613');

/* check connection */
if (!$link) {
    die('Connection failed: ' . stomp_connect_error());
}

/* begin a transaction */
stomp_begin($link, 't1');

/* send a message to the queue 'foo' */
stomp_send($link, '/queue/foo', 'bar', array('transaction' => 't1'));

/* commit */
stomp_commit($link, 't1');

/* close connection */
stomp_close($link);

?>

Stomp::__construct

stomp_connect

打开一个连接

说明

面向对象风格 (constructor):

public Stomp::__construct ([ string $broker = ini_get("stomp.default_broker_uri") [, string $username [, string $password [, array $headers ]]]] )

过程化风格:

resource stomp_connect ([ string $broker = ini_get("stomp.default_broker_uri") [, string $username [, string $password [, array $headers ]]]] )

打开一个到兼容stomp通讯协议的消息代理服务器的连接.

参数

broker
代理服务器的统一资源标识符(URI)。

username
用户名.

password
密码.

headers
关联数组包含附加的头信息(例如: receipt)。

返回值

Note:

A transaction header may be specified, indicating that the message acknowledgment should be part of the named transaction.

更新日志

版本说明
1.0.1增加了headers 参数

范例

示例 #1 面向对象风格

<?php

/* connection */
try {
    $stomp = new Stomp('tcp://localhost:61613');
} catch(StompException $e) {
    die('Connection failed: ' . $e->getMessage());
}

/* close connection */
unset($stomp);

?>

示例 #2 过程化风格

<?php

/* connection */
$link = stomp_connect('ssl://localhost:61612');

/* check connection */
if (!$link) {
    die('Connection failed: ' . stomp_connect_error());
}

/* close connection */
stomp_close($link);

?>

Stomp::__destruct

stomp_close

Closes stomp connection

说明

面向对象风格 (destructor):

public bool Stomp::__destruct ( void )

过程化风格:

bool stomp_close ( resource $link )

Closes a previously opened connection.

参数

link
仅对过程化样式:由 stomp_connect 返回的 stomp 连接标识符。

返回值

成功时返回 TRUE, 或者在失败时返回 FALSE

范例

See stomp_connect.

Stomp::error

stomp_error

Gets the last stomp error

说明

面向对象风格 (method):

public string Stomp::error ( void )

过程化风格:

string stomp_error ( resource $link )

Gets the last stomp error.

参数

link
仅对过程化样式:由 stomp_connect 返回的 stomp 连接标识符。

返回值

Returns an error string or FALSE if no error occurred.

范例

示例 #1 面向对象风格

<?php

/* connection */
try {
    $stomp = new Stomp('tcp://localhost:61613');
} catch(StompException $e) {
    die('Connection failed: ' . $e->getMessage());
}

var_dump($stomp->error());

if (!$stomp->abort('unknown-transaction', array('receipt' => 'foo'))) {
    var_dump($stomp->error());
}

/* close connection */
unset($stomp);

?>

以上例程的输出类似于:

bool(false)
string(43) "Invalid transaction id: unknown-transaction"

示例 #2 过程化风格

<?php

/* connection */
$link = stomp_connect('ssl://localhost:61612');

/* check connection */
if (!$link) {
    die('Connection failed: ' . stomp_connect_error());
}

var_dump(stomp_error($link));

if (!stomp_abort($link, 'unknown-transaction', array('receipt' => 'foo'))) {
    var_dump(stomp_error($link));
}

/* close connection */
stomp_close($link);

?>

以上例程的输出类似于:

bool(false)
string(43) "Invalid transaction id: unknown-transaction"

Stomp::getReadTimeout

stomp_get_read_timeout

Gets read timeout

说明

面向对象风格 (method):

public array Stomp::getReadTimeout ( void )

过程化风格:

array stomp_get_read_timeout ( resource $link )

Gets read timeout

参数

link
仅对过程化样式:由 stomp_connect 返回的 stomp 连接标识符。

返回值

Returns an array with 2 elements: sec and usec.

范例

示例 #1 面向对象风格

<?php

/* connection */
try {
    $stomp = new Stomp('tcp://localhost:61613');
} catch(StompException $e) {
    die('Connection failed: ' . $e->getMessage());
}

var_dump($stomp->getReadTimeout());

/* close connection */
unset($stomp);

?>

以上例程的输出类似于:

array(2) {
  ["sec"]=>
  int(2)
  ["usec"]=>
  int(0)
}

示例 #2 过程化风格

<?php

/* connection */
$link = stomp_connect('ssl://localhost:61612');

/* check connection */
if (!$link) {
    die('Connection failed: ' . stomp_connect_error());
}

var_dump(stomp_get_read_timeout($link));

/* close connection */
stomp_close($link);

?>

以上例程的输出类似于:

array(2) {
  ["sec"]=>
  int(2)
  ["usec"]=>
  int(0)
}

Stomp::getSessionId

stomp_get_session_id

Gets the current stomp session ID

说明

面向对象风格 (method):

public string Stomp::getSessionId ( void )

过程化风格:

string stomp_get_session_id ( resource $link )

Gets the current stomp session ID.

参数

link
仅对过程化样式:由 stomp_connect 返回的 stomp 连接标识符。

返回值

string session id on success 或者在失败时返回 FALSE.

范例

示例 #1 面向对象风格

<?php

/* connection */
try {
    $stomp = new Stomp('tcp://localhost:61613');
} catch(StompException $e) {
    die('Connection failed: ' . $e->getMessage());
}

var_dump($stomp->getSessionId());

/* close connection */
unset($stomp);

?>

以上例程的输出类似于:

string(35) "ID:php.net-52873-1257291895530-4:14"

示例 #2 过程化风格

<?php

/* connection */
$link = stomp_connect('ssl://localhost:61612');

/* check connection */
if (!$link) {
    die('Connection failed: ' . stomp_connect_error());
}

var_dump(stomp_get_session_id($link));

/* close connection */
stomp_close($link);

?>

以上例程的输出类似于:

string(35) "ID:php.net-52873-1257291895530-4:14"

Stomp::hasFrame

stomp_has_frame

Indicates whether or not there is a frame ready to read

说明

面向对象风格 (method):

public bool Stomp::hasFrame ( void )

过程化风格:

bool stomp_has_frame ( resource $link )

Indicates whether or not there is a frame ready to read.

参数

link
仅对过程化样式:由 stomp_connect 返回的 stomp 连接标识符。

返回值

Returns TRUE if a frame is ready to read, or FALSE otherwise.

Stomp::readFrame

stomp_read_frame

Reads the next frame

说明

面向对象风格 (method):

public stompframe Stomp::readFrame ([ string $class_name = "stompFrame" ] )

过程化风格:

array stomp_read_frame ( resource $link )

Reads the next frame. It is possible to instantiate an object of a specific class, and pass parameters to that class's constructor.

参数

link
仅对过程化样式:由 stomp_connect 返回的 stomp 连接标识符。

class_name
The name of the class to instantiate. If not specified, a stompFrame object is returned.

返回值

Note:

A transaction header may be specified, indicating that the message acknowledgment should be part of the named transaction.

更新日志

版本说明
Stomp 0.4.0class_name parameter was added.

范例

示例 #1 面向对象风格

<?php

/* connection */
try {
    $stomp = new Stomp('tcp://localhost:61613');
} catch(StompException $e) {
    die('Connection failed: ' . $e->getMessage());
}

/* subscribe to messages from the queue 'foo' */
$stomp->subscribe('/queue/foo');

/* read a frame */
var_dump($stomp->readFrame());

/* close connection */
unset($stomp);

?>

以上例程的输出类似于:

object(StompFrame)#2 (3) {
  ["command"]=>
  string(7) "MESSAGE"
  ["headers"]=>
  array(5) {
    ["message-id"]=>
    string(41) "ID:php.net-55293-1257226743606-4:2:-1:1:1"
    ["destination"]=>
    string(10) "/queue/foo"
    ["timestamp"]=>
    string(13) "1257226805828"
    ["expires"]=>
    string(1) "0"
    ["priority"]=>
    string(1) "0"
  }
  ["body"]=>
  string(3) "bar"
}

示例 #2 过程化风格

<?php

/* connection */
$link = stomp_connect('ssl://localhost:61612');

/* check connection */
if (!$link) {
    die('Connection failed: ' . stomp_connect_error());
}

/* subscribe to messages from the queue 'foo' */
stomp_subscribe($link, '/queue/foo');

/* read a frame */
$frame = stomp_read_frame($link);

/* close connection */
stomp_close($link);

?>

以上例程的输出类似于:

array(3) {
  ["command"]=>
  string(7) "MESSAGE"
  ["body"]=>
  string(3) "bar"
  ["headers"]=>
  array(6) {
    ["transaction"]=>
    string(2) "t1"
    ["message-id"]=>
    string(41) "ID:php.net-55293-1257226743606-4:3:-1:1:1"
    ["destination"]=>
    string(10) "/queue/foo"
    ["timestamp"]=>
    string(13) "1257227037059"
    ["expires"]=>
    string(1) "0"
    ["priority"]=>
    string(1) "0"
  }
}

Stomp::send

stomp_send

Sends a message

说明

面向对象风格 (method):

public bool Stomp::send ( string $destination , mixed $msg [, array $headers ] )

过程化风格:

bool stomp_send ( resource $link , string $destination , mixed $msg [, array $headers ] )

Sends a message to the Message Broker.

参数

link
仅对过程化样式:由 stomp_connect 返回的 stomp 连接标识符。

destination
Where to send the message

msg
Message to send.

headers
关联数组包含附加的头信息(例如: receipt)。

返回值

成功时返回 TRUE, 或者在失败时返回 FALSE

注释

Note:

A transaction header may be specified, indicating that the message acknowledgment should be part of the named transaction.

小贴士

Stomp is inherently asynchronous. Synchronous communication can be implemented adding a receipt header. This will cause methods to not return anything until the server has acknowledged receipt of the message or until read timeout was reached.

范例

See stomp_ack.

Stomp::setReadTimeout

stomp_set_read_timeout

Sets read timeout

说明

面向对象风格 (method):

public void Stomp::setReadTimeout ( int $seconds [, int $microseconds ] )

过程化风格:

void stomp_set_read_timeout ( resource $link , int $seconds [, int $microseconds ] )

Sets read timeout.

参数

link
仅对过程化样式:由 stomp_connect 返回的 stomp 连接标识符。

seconds
The seconds part of the timeout to be set.

microseconds
The microseconds part of the timeout to be set.

范例

示例 #1 面向对象风格

<?php

/* connection */
try {
    $stomp = new Stomp('tcp://localhost:61613');
} catch(StompException $e) {
    die('Connection failed: ' . $e->getMessage());
}

$stomp->setReadTimeout(10);
    
/* close connection */
unset($stomp);

?>

示例 #2 过程化风格

<?php

/* connection */
$link = stomp_connect('ssl://localhost:61612');

/* check connection */
if (!$link) {
    die('Connection failed: ' . stomp_connect_error());
}

stomp_set_read_timeout($link, 10);
    
/* close connection */
stomp_close($link);

?>

Stomp::subscribe

stomp_subscribe

Registers to listen to a given destination

说明

面向对象风格 (method):

public bool Stomp::subscribe ( string $destination [, array $headers ] )

过程化风格:

bool stomp_subscribe ( resource $link , string $destination [, array $headers ] )

Registers to listen to a given destination.

参数

link
仅对过程化样式:由 stomp_connect 返回的 stomp 连接标识符。

destination
Destination to subscribe to.

headers
关联数组包含附加的头信息(例如: receipt)。

返回值

成功时返回 TRUE, 或者在失败时返回 FALSE

注释

小贴士

Stomp is inherently asynchronous. Synchronous communication can be implemented adding a receipt header. This will cause methods to not return anything until the server has acknowledged receipt of the message or until read timeout was reached.

范例

See stomp_ack.

Stomp::unsubscribe

stomp_unsubscribe

Removes an existing subscription

说明

面向对象风格 (method):

public bool Stomp::unsubscribe ( string $destination [, array $headers ] )

过程化风格:

bool stomp_unsubscribe ( resource $link , string $destination [, array $headers ] )

Removes an existing subscription.

参数

link
仅对过程化样式:由 stomp_connect 返回的 stomp 连接标识符。

destination
Subscription to remove.

headers
关联数组包含附加的头信息(例如: receipt)。

返回值

成功时返回 TRUE, 或者在失败时返回 FALSE

注释

小贴士

Stomp is inherently asynchronous. Synchronous communication can be implemented adding a receipt header. This will cause methods to not return anything until the server has acknowledged receipt of the message or until read timeout was reached.

范例

See stomp_ack.

简介

Represents a message which was sent or received from a Stomp compliant Message Broker.

类摘要

StompFrame

class StompFrame {

/* 属性 */

public $command ;

public $headers ;

public $body ;

/* 方法 */

__construct ([ string $command [, array $headers [, string $body ]]] )

}

属性

command
Frame command.

headers
Frame headers (array).

body
Frame body.

StompFrame::__construct

Constructor

说明

StompFrame::__construct ([ string $command [, array $headers [, string $body ]]] )

Constructor.

参数

command
Frame command

headers
Frame headers (array).

body
Frame body.

简介

Represents an error raised by the stomp extension. See Exceptions for more information about Exceptions in PHP.

类摘要

StompException

class StompException extends Exception {

/* 继承的方法 */

final public string Exception::getMessage ( void )

final public Throwable Exception::getPrevious ( void )

final public int Exception::getCode ( void )

final public string Exception::getFile ( void )

final public int Exception::getLine ( void )

final public array Exception::getTrace ( void )

final public string Exception::getTraceAsString ( void )

public string Exception::__toString ( void )

final private void Exception::__clone ( void )

/* 方法 */

public string getDetails ( void )

}

StompException::getDetails

Get exception details

说明

public string StompException::getDetails ( void )

Get exception details.

返回值

string containing the error details.