Exemplo n.º 1
0
 public function subscribe($exchange, $callback)
 {
     $this->stomp->subscribe($exchange);
     while ($frame = $this->stomp->readFrame()) {
         $callback($frame->body);
     }
 }
Exemplo n.º 2
0
 public function testReadFrameWithLeadingLineFeed()
 {
     $this->sut = new Stomp('tcp://localhost:61613');
     // Mock functions return values
     global $fusesourceStreamFunctionStubsBuffer;
     $fusesourceStreamFunctionStubsBuffer = array("MESSAGE\n\nbody", "\nMESSAGE\n\nbody");
     $this->sut->readFrame();
     $frame = $this->sut->readFrame();
     $this->assertSame('MESSAGE', $frame->command);
 }
Exemplo n.º 3
0
 private function connectSTOMP()
 {
     try {
         $stomp = new Stomp('tcp://' . $this->stompserver . ':' . $this->stompport);
         $stomp->clientId = $this->stompclientid;
         $stomp->connect();
     } catch (StompException $e) {
         $this->errors[] = $e->getMessage();
         return null;
     }
     return $stomp;
 }
Exemplo n.º 4
0
 /**
  * Tests Stomp->connect(), send() and subscribe() in order.
  */
 public function testSyncSub()
 {
     $this->assertTrue($this->Stomp->connect());
     $this->assertTrue($this->Stomp->subscribe('/queue/test'));
     $this->assertTrue($this->Stomp->send('/queue/test', 'test 1'));
     $this->assertTrue($this->Stomp->send('/queue/test', 'test 2'));
     $this->Stomp->setReadTimeout(5);
     $frame = $this->Stomp->readFrame();
     $this->assertEquals('test 1', $frame->body, 'test 1 not received!');
     $this->Stomp->ack($frame);
     $frame = $this->Stomp->readFrame();
     $this->assertEquals('test 2', $frame->body, 'test 2 not received!');
     $this->Stomp->ack($frame);
 }
Exemplo n.º 5
0
 /**
  * @return Stomp
  */
 public function getStomp()
 {
     if (!$this->stomp->isConnected()) {
         $this->stomp->connect(Arr::get($this->credentials, 'username', ''), Arr::get($this->credentials, 'password', ''));
     }
     return $this->stomp;
 }
Exemplo n.º 6
0
 /**
  * Tests Stomp->unsubscribe()
  */
 public function testUnsubscribe()
 {
     if (!$this->Stomp->isConnected()) {
         $this->Stomp->connect();
     }
     $this->Stomp->subscribe($this->queue);
     $this->assertTrue($this->Stomp->unsubscribe($this->queue));
 }
Exemplo n.º 7
0
 /**
  * Tests Stomp->connect()
  */
 public function testFailoverConnect()
 {
     $this->assertTrue($this->Stomp->connect());
 }
Exemplo n.º 8
0
 * 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.
 */
/*
 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 {
Exemplo n.º 9
0
 * 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.
 */
/*
 To successfully run this example, you must first start the broker with security enabled.
 You can do that by executing:
 $ ${ACTIVEMQ_HOME}/bin/activemq xbean:activemq-security.xml
 Then you can execute this example with:
 $ php security.php
*/
// include a library
use FuseSource\Stomp\Stomp;
use FuseSource\Stomp\Exception\StompException;
// make a connection
$con = new Stomp("tcp://localhost:61613");
// use sync operations
$con->sync = true;
// connect
try {
    $con->connect("dejan", "test");
} catch (StompException $e) {
    echo "dejan cannot connect\n";
    echo $e->getMessage() . "\n";
    echo $e->getDetails() . "\n\n\n";
}
$con->connect("guest", "password");
// send a message to the queue
try {
    $con->send("/queue/test", "test");
    echo "Guest sent message with body 'test'\n";
Exemplo n.º 10
0
 public function __destruct()
 {
     $this->client->disconnect();
 }
Exemplo n.º 11
0
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * 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\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: ";
Exemplo n.º 12
0
 /**
  * Connect to Stomp server, if not connected.
  *
  * @throws \FuseSource\Stomp\Exception\StompException
  */
 protected function connect()
 {
     if (!$this->stomp->isConnected()) {
         $this->stomp->connect(Arr::get($this->credentials, 'username', ''), Arr::get($this->credentials, 'password', ''));
     }
 }
Exemplo n.º 13
0
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * 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;
// 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";
Exemplo n.º 14
0
 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();
 }
Exemplo n.º 15
0
    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";
// 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");
Exemplo n.º 16
0
 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();
 }