Example #1
0
<?php

require_once '../init.php';
global $stompListen;
if ($stompListen != true) {
    exit;
}
$topics[] = '/topic/kills';
try {
    $stomp = new Stomp($stompServer, $stompUser, $stompPassword);
} catch (Exception $ex) {
    Util::out("Stomp error: " . $ex->getMessage());
    exit;
}
$stomp->setReadTimeout(1);
foreach ($topics as $topic) {
    $stomp->subscribe($topic, array('id' => 'zkb-' . $baseAddr, 'persistent' => 'true', 'ack' => 'client', 'prefetch-count' => 1));
}
$stompCount = 0;
$timer = new Timer();
while ($timer->stop() <= 59000) {
    $frame = $stomp->readFrame();
    if (!empty($frame)) {
        $killdata = json_decode($frame->body, true);
        $killID = (int) $killdata['killID'];
        if ($killID == 0) {
            continue;
        }
        $hash = $hash = Killmail::getCrestHash($killID, $killdata);
        $killdata['killID'] = (int) $killID;
        if (!$mdb->exists('crestmails', ['killID' => $killID, 'hash' => $hash])) {
 *
 * 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
require_once "Stomp.php";
// 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"));
Example #3
0
<?php

// include a library
require_once "Stomp.php";
// 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
 public function execute($parameters, $db)
 {
     global $stompServer, $stompUser, $stompPassword, $baseAddr;
     // Ensure the class exists
     if (!class_exists("Stomp")) {
         die("ERROR! Stomp not installed!  Check the README to learn how to install Stomp...\n");
     }
     // Build the topic from Admin's tracker list
     $adminID = $db->queryField("select id from zz_users where username = '******'", "id", array(), 0);
     $trackers = $db->query("select locker, content from zz_users_config where locker like 'tracker_%' and id = :id", array(":id" => $adminID), array(), 0);
     $topics = array();
     foreach ($trackers as $row) {
         $entityType = str_replace("tracker_", "", $row["locker"]);
         $entities = json_decode($row["content"], true);
         foreach ($entities as $entity) {
             $id = $entity["id"];
             $topic = "/topic/involved.{$entityType}.{$id}";
             $topics[] = $topic;
         }
     }
     if (sizeof($topics) == 0) {
         $topics[] = "/topic/kills";
     }
     try {
         $stomp = new Stomp($stompServer, $stompUser, $stompPassword);
         $stomp->setReadTimeout(1);
         foreach ($topics as $topic) {
             $stomp->subscribe($topic, array("id" => "zkb-" . $baseAddr, "persistent" => "true", "ack" => "client", "prefetch-count" => 1));
         }
         $stompCount = 0;
         $timer = new Timer();
         while ($timer->stop() < 60000) {
             $frame = $stomp->readFrame();
             if (!empty($frame)) {
                 $killdata = json_decode($frame->body, true);
                 if (!empty($killdata)) {
                     $killID = $killdata["killID"];
                     $count = $db->queryField("SELECT count(1) AS count FROM zz_killmails WHERE killID = :killID LIMIT 1", "count", array(":killID" => $killID), 0);
                     if ($count == 0) {
                         if ($killID > 0) {
                             $hash = Util::getKillHash(null, json_decode($frame->body));
                             $db->execute("INSERT IGNORE INTO zz_killmails (killID, hash, source, kill_json) values (:killID, :hash, :source, :json)", array("killID" => $killID, ":hash" => $hash, ":source" => "stompQueue", ":json" => json_encode($killdata)));
                             $stomp->ack($frame->headers["message-id"]);
                             $stompCount++;
                             continue;
                         } else {
                             $stomp->ack($frame->headers["message-id"]);
                             continue;
                         }
                     } else {
                         $stomp->ack($frame->headers["message-id"]);
                         continue;
                     }
                 }
             }
         }
         if ($stompCount > 0) {
             Log::log("StompReceive Ended - Received {$stompCount} kills");
         }
     } catch (Exception $ex) {
         $e = print_r($ex, true);
         Log::log("StompReceive ended with the error:\n{$e}\n");
     }
 }