public function testCommitTransaction() { $this->assertTrue($this->Stomp->connect()); $this->assertTrue($this->Stomp->begin('my-id')); $this->assertTrue($this->Stomp->send('/queue/test', 'test 1', array('transaction' => 'my-id'))); $this->assertTrue($this->Stomp->commit('my-id')); $this->assertTrue($this->Stomp->subscribe('/queue/test')); $frame = $this->Stomp->readFrame(); $this->assertEquals('test 1', $frame->body, 'test 1 not received!'); $this->Stomp->ack($frame); }
/** * Tests Stomp->connect(), send(), and subscribe() - out of order. the messages should be received in FIFO order. */ public function testAsyncSub() { $this->assertTrue($this->Stomp->connect()); $this->assertTrue($this->Stomp->send('/queue/test', 'test 1')); $this->assertTrue($this->Stomp->send('/queue/test', 'test 2')); $this->assertTrue($this->Stomp->subscribe('/queue/test')); $frame = $this->Stomp->readFrame(); $this->assertEquals($frame->body, 'test 1', 'test 1 was not received!'); $this->Stomp->ack($frame); $frame = $this->Stomp->readFrame(); $this->assertEquals($frame->body, 'test 2', 'test 2 was not received!'); $this->Stomp->ack($frame); }
protected function consume() { $consumer2 = new Stomp($this->broker); $consumer2->sync = false; $consumer2->clientId = 'test'; $consumer2->setReadTimeout(1); $consumer2->connect('system', 'manager'); $consumer2->subscribe($this->topic, null, null, true); $frame = $consumer2->readFrame(); $this->assertEquals($frame->body, 'test message'); if ($frame != null) { $consumer2->ack($frame); } // yes, that's active mq! you must unsub two times... // http://mail-archives.apache.org/mod_mbox/activemq-dev/201205.mbox/raw/ // %3C634996273.21688.1336051731428.JavaMail.tomcat@hel.zones.apache.org%3E/ $consumer2->unsubscribe($this->topic); // that took me some time... $consumer2->unsubscribe($this->topic, null, null, true); $consumer2->disconnect(); }
$con->begin('tx2'); echo "Sent messages {\n"; for ($i = 1; $i < 5; $i++) { echo "\t{$i}\n"; $con->send('/queue/transactions', $i, array('transaction' => 'tx2')); } echo "}\n"; // they will be available for consumers after commit $con->commit('tx2'); // try to receive some messages $con->begin('tx3'); $messages = array(); for ($i = 1; $i < 3; $i++) { $msg = $con->readFrame(); array_push($messages, $msg); $con->ack($msg, 'tx3'); } // of we abort transaction, we will "rollback" out acks $con->abort('tx3'); $con->begin('tx4'); // so we need to ack received messages again // before we can receive more (prefetch = 1) if (count($messages) != 0) { foreach ($messages as $msg) { $con->ack($msg, 'tx4'); } } // now receive more messages for ($i = 1; $i < 3; $i++) { $msg = $con->readFrame(); $con->ack($msg, 'tx4');
* limitations under the License. */ // include a library use Stomp\Stomp; use Stomp\Message\Map; // make a connection $con = new Stomp('tcp://localhost:61613'); // connect $con->connect(); // send a message to the queue $body = array('city' => 'Belgrade', 'name' => 'Dejan'); $header = array(); $header['transformation'] = 'jms-map-json'; $mapMessage = new Map($body, $header); $con->send('/queue/test', $mapMessage); echo 'Sending array: '; print_r($body); $con->subscribe('/queue/test', array('transformation' => 'jms-map-json')); /** @var Map $msg */ $msg = $con->readFrame(); // extract if ($msg != null) { echo 'Received array: '; print_r($msg->map); // mark the message as received in the queue $con->ack($msg); } else { echo "Failed to receive a message\n"; } // disconnect $con->disconnect();
protected function consume() { $consumer2 = new Stomp($this->broker); $consumer2->sync = true; $consumer2->clientId = 'test'; $consumer2->setReadTimeout(1); $consumer2->connect($this->login, $this->password); $consumer2->subscribe($this->topic, array('persistent' => 'true')); $frame = $consumer2->readFrame(); $this->assertEquals($frame->body, 'test message'); if ($frame != null) { $consumer2->ack($frame); } $consumer2->disconnect(); }
echo "Disconnecting consumer\n"; // send a message while consumer is disconnected $producer->send('/topic/test', 'test-2'); echo "Message 'test-2' sent to topic\n"; // reconnect the durable consumer $consumer = new Stomp('tcp://localhost:61613'); $consumer->clientId = 'test'; $consumer->connect('admin', 'password'); $consumer->subscribe('/topic/test', null, true, true); echo "Reconnecting consumer\n"; // receive a message from the topic $msg = $consumer->readFrame(); // do what you want with the message if ($msg != null) { echo "Message '{$msg->body}' received from topic\n"; $consumer->ack($msg); } else { echo "Failed to receive a message\n"; } // disconnect if ($consumer->getProtocol() instanceof \Stomp\Protocol\ActiveMq) { // activeMq Way $consumer->unsubscribe('/topic/test'); $consumer->unsubscribe('/topic/test', null, true, true); } else { // default (apollo way) $consumer->unsubscribe('/topic/test', null, true, true); } // this message will never be seen, since no durable subscriber is present $producer->send('/topic/test', 'test-3'); $consumer->disconnect();