protected function subscribe() { $consumer = new Stomp($this->broker); $consumer->sync = true; $consumer->clientId = 'test'; $consumer->connect($this->login, $this->password); $consumer->subscribe($this->topic, array('persistent' => 'true')); $consumer->unsubscribe($this->topic); $consumer->disconnect(); }
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(); }
// 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(); $producer->disconnect();