public function sync($exchange, $payload) { $this->stomp->send($exchange, $payload); $this->stomp->subscribe("{$exchange}/response"); $frame = $this->stomp->readFrame(); $body = $frame->body; //... }
/** * Broadcast the given event. * * @param array $channels * @param string $event * @param array $payload * @return void */ public function broadcast(array $channels, $event, array $payload = []) { $this->connect(); $payload = json_encode(['event' => $event, 'data' => $payload]); foreach ($channels as $channel) { $this->stomp->send($channel, $payload); } }
/** * 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); }
*/ /* 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();
* See the License for the specific language governing permissions and * limitations under the License. */ // include a library use FuseSource\Stomp\Stomp; use FuseSource\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')); $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();
* See the License for the specific language governing permissions and * limitations under the License. */ // include a library use FuseSource\Stomp\Stomp; // make a connection $con = new Stomp("tcp://localhost:61613"); // connect $con->connect(); $con->setReadTimeout(1); // subscribe to the queue $con->subscribe("/queue/transactions", array('ack' => 'client', 'activemq.prefetchSize' => 1)); // try to send some messages $con->begin("tx1"); for ($i = 1; $i < 3; $i++) { $con->send("/queue/transactions", $i, array("transaction" => "tx1")); } // if we abort transaction, messages will not be sent $con->abort("tx1"); // now send some messages for real $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");
protected function produce() { $producer = new Stomp($this->broker); $producer->sync = false; $producer->connect("system", "manager"); $producer->send($this->topic, "test message", array('persistent' => 'true')); $producer->disconnect(); }
use FuseSource\Stomp\Stomp; // create a producer $producer = new Stomp("tcp://localhost:61613"); // create a consumer $consumer = new Stomp("tcp://localhost:61613"); $consumer->setReadTimeout(1); // set clientId on a consumer to make it durable $consumer->clientId = "test"; // connect $producer->connect(); $consumer->connect(); // subscribe to the topic $consumer->subscribe("/topic/test"); sleep(1); // send a message to the topic $producer->send("/topic/test", "test", array('persistent' => 'true')); echo "Message 'test' sent to topic\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"; } sleep(1); // disconnect durable consumer $consumer->unsubscribe("/topic/test"); $consumer->disconnect(); echo "Disconnecting consumer\n";
* distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ // include a library use FuseSource\Stomp\Stomp; use FuseSource\Stomp\Message\Bytes; // make a connection $con = new Stomp("tcp://localhost:61613"); // connect $con->connect(); // send a message to the queue $body = "test"; $bytesMessage = new Bytes($body); $con->send("/queue/test", $bytesMessage); echo "Sending message: "; print_r($body . "\n"); $con->subscribe("/queue/test"); $msg = $con->readFrame(); // extract if ($msg != null) { echo "Received message: "; print_r($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();
protected function produce() { $producer = new Stomp($this->broker); $producer->sync = true; $producer->connect($this->login, $this->password); $producer->send($this->topic, "test message", array('persistent' => 'true')); $producer->disconnect(); }