/** * 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); }
/** * Tests Stomp->send() */ public function testSend() { if (!$this->Stomp->isConnected()) { $this->Stomp->connect(); } $this->assertTrue($this->Stomp->send($this->queue, 'testSend')); $this->Stomp->subscribe($this->queue); $frame = $this->Stomp->readFrame(); $this->assertEquals('testSend', $frame->body, 'Body of test frame does not match sent message'); $this->Stomp->ack($frame); $this->Stomp->unsubscribe($this->queue); }
/** * {@inheritdoc} */ public function transport(SourceInterface $source) { if (!$this->client->isConnected()) { $this->client->connect(); $this->client->subscribe((string) $source); } $message = $this->client->readFrame(); $table = $this->getTableFactory()->create(); if ($message != NULL) { $this->client->ack($message); } if (!$message) { return $table; } $row = $table->getNewRow(); $row->set('command', $message->command); if (!empty($message->headers)) { foreach ($message->headers as $key => $value) { $row->set($key, $value); } } $row->set('body', $message->body); return $table; }
*/ /* To successfully run this example, you must first start the broker with stomp+ssl enabled. You can do that by executing: $ ${ACTIVEMQ_HOME}/bin/activemq xbean:activemq-connectivity.xml Then you can execute this example with: $ php connectivity.php */ // include a library use FuseSource\Stomp\Stomp; // make a connection $con = new Stomp("failover://(tcp://localhost:61614,ssl://localhost:61612)?randomize=false"); // connect $con->connect(); // send a message to the queue $con->send("/queue/test", "test"); echo "Sent message with body 'test'\n"; // subscribe to the queue $con->subscribe("/queue/test"); // receive a message from the queue $msg = $con->readFrame(); // do what you want with the message if ($msg != null) { echo "Received message with body '{$msg->body}'\n"; // mark the message as received in the queue $con->ack($msg); } else { echo "Failed to receive a message\n"; } // disconnect $con->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");
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); $frame = $consumer2->readFrame(); $this->assertEquals($frame->body, "test message"); if ($frame != null) { $consumer2->ack($frame); } $consumer2->disconnect(); }
} else { echo "Failed to receive a message\n"; } sleep(1); // disconnect durable consumer $consumer->unsubscribe("/topic/test"); $consumer->disconnect(); echo "Disconnecting consumer\n"; // send a message while consumer is disconnected // note: only persistent messages will be redelivered to the durable consumer $producer->send("/topic/test", "test1", array('persistent' => 'true')); echo "Message 'test1' sent to topic\n"; // reconnect the durable consumer $consumer = new Stomp("tcp://localhost:61613"); $consumer->clientId = "test"; $consumer->connect(); $consumer->subscribe("/topic/test"); 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 $consumer->unsubscribe("/topic/test"); $consumer->disconnect(); $producer->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(); }