private function sendMessageToAM($destination, $message) { require_once 'Stomp.php'; require_once 'Stomp/Message/Map.php'; $con = new Stomp(Service::ACTIVE_MQ); $conn->sync = false; $con->connect(); $header = array(); $header['transformation'] = 'jms-map-json'; $mapMessage = new StompMessageMap($message, $header); $result = $con->send($destination, $mapMessage, array('persistent' => 'true')); $con->disconnect(); if (!$result) { $this->log("sendMessageToAM", "send message to activemq failure,\r\n destination: {$destination},\r\n mapMessage: " . print_r($mapMessage, true) . "\r\n <br>"); } return $result; }
/** * Send a meesage to ActiveMQ * * @param array $data message to send * @param string $queue name of the queue * @param string $scheme type of the connection: tcp, udp * @param string $host host of the ActiveMQ * @param int $port port of the ActiveMQ * @return true|false depends on the message is sent correctly */ public function send($data = null, $queue, $scheme, $host, $port) { if ($data) { try { $url = $scheme . "://" . $host . ":" . $port; $con = new Stomp($url); $message = '[' . json_encode($data) . ']'; $con->connect(); $con->send($queue, json_encode($data)); $con->disconnect(); return true; } catch (Exception $e) { return false; } } else { return false; } }
<?php require_once 'Stomp.php'; $stomp = new Stomp("tcp://localhost:61613"); $stomp->connect('system', 'manager'); $stomp->subscribe("/topic/STOCKS.JAVA"); $stomp->subscribe("/topic/STOCKS.IONA"); $i = 0; while ($i++ < 100) { $frame = $stomp->readFrame(); $xml = new SimpleXMLElement($frame->body); echo $xml->attributes()->name . "\t" . number_format($xml->price, 2) . "\t" . number_format($xml->offer, 2) . "\t" . ($xml->up == "true" ? "up" : "down") . "\n"; $stomp->ack($frame); } $stomp->disconnect();
/** * Hook to send transaction information to ActiveMQ server * TODO: Seriously. Parameterize sendStomp. I hated this when there were only * two of them, and now I've made another one. * THE ONLY THING this does differently, is use a different queue, and set the * correlation-id if one is set in the transaction array. * @global string $wgStompServer ActiveMQ server name. * @global string $wgLimboStompQueueName Name of the destination queue for * 'limbo' transactions. * @param array $transaction Key-value array of staged and ready donation data. * @return bool Just returns true all the time. Presumably an indication that * nothing exploded big enough to kill the whole thing. */ function sendLimboSTOMP($transaction) { global $wgStompServer, $wgLimboStompQueueName, $wgCCLimboStompQueueName; if ($transaction['payment_method'] === 'cc') { $queueName = isset($wgCCLimboStompQueueName) ? $wgCCLimboStompQueueName : 'cc-limbo'; } else { $queueName = isset($wgLimboStompQueueName) ? $wgLimboStompQueueName : 'limbo'; } // include a library require_once "Stomp.php"; $properties = array('persistent' => 'true', 'correlation-id' => $transaction['correlation-id'], 'payment_method' => $transaction['payment_method']); if (array_key_exists('antimessage', $transaction)) { $message = ''; $properties['antimessage'] = 'true'; } else { $message = json_encode(createQueueMessage($transaction)); } // make a connection $con = new Stomp($wgStompServer); // connect $con->connect(); // send a message to the queue $result = $con->send("/queue/{$queueName}", $message, $properties); if (!$result) { wfDebugLog('activemq_stomp', 'Send to Q failed for this message: ' . $message); } $con->disconnect(); return true; }
/* 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 require_once "Stomp.php"; // 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();
} 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();
public function dealSkuStock() { require __DOCROOT__ . '/Stomp.php'; require __DOCROOT__ . '/Stomp/Message/Map.php'; $consumer = new Stomp($this->conf['service']['activeMq']); $consumer->clientId = "inventorySkuStock"; $consumer->connect(); $consumer->subscribe($this->conf['topic']['skuStock'], array('transformation' => 'jms-map-json')); $msg = $consumer->readFrame(); if ($msg != null) { $consumer->ack($msg); if ($msg->map['stock'] <= 0 && $msg->map['operate'] == "+") { $sql = "select status from sku_status_history where sku = '" . $msg->map['sku'] . "' order by id desc limit 0,1"; $result = mysql_query($sql, $this->conn); $row = mysql_fetch_assoc($result); $this->log("dealSkuStock", print_r($msg->map, true) . "<br>"); $this->updateCustomFieldValueBySku($msg->map['sku'], $this->conf['fieldArray']['skuStatus'], $this->conf['skuStatus'][str_replace(" ", "_", $row['status'])]); } } else { echo date("Y-m-d H:i:s") . " no message\n"; } $consumer->disconnect(); }
public function dealSkuOutOfStockMessage() { require_once __DOCROOT__ . '/Stomp.php'; require_once __DOCROOT__ . '/Stomp/Message/Map.php'; $consumer = new Stomp($this->config['service']['activeMQ']); $consumer->clientId = "eBayListingSkuOutOfStock"; $consumer->connect(); $consumer->subscribe($this->config['topic']['skuOutOfStock'], array('transformation' => 'jms-map-json')); //for($i=0; $i<6; $i++){ $msg = $consumer->readFrame(); if ($msg != null) { //echo "Message '$msg->body' received from queue\n"; //print_r($msg->map); $consumer->ack($msg); $sku_array = explode(",", $msg->map['sku']); foreach ($sku_array as $sku) { $sql = "update template set status = 3 where SKU = '" . $sku . "' and status = 6"; echo $sql . "\n"; $result = mysql_query($sql, Cron::$database_connect); } } else { echo date("Y-m-d H:i:s") . " no message\n"; } //sleep(1); //} $consumer->disconnect(); }
public function update($old, $new){ try { if ($new !== null) { // Update or Create $method = "request"; $tmpFilename = $new->getIcs($userId, $method, false); } elseif ($old !== null) { // Delete $method = "cancel"; $tmpFilename = $old->getIcs($userId, $method, false); } else { throw new OBM_ObserverException(__('$old and $new cannot be null at the same time in OBM_EventStompObserver')); } $contentOfTmpFilename = fread(fopen($tmpFilename, "r"), filesize($tmpFilename)); $stomp = new Stomp("tcp://".$GLOBALS['stomp_host'].":".$GLOBALS['stomp_port']); $stomp->connect(); $stomp->send("jms.topic.eventChanges", $contentOfTmpFilename, array('persistent'=>'true')); $stomp->disconnect(); } catch (StompException $e) { throw new OBM_ObserverException(__("An Exception was thrown during OBM_EventStompObserver->update " . $e->getMessage() . " -")); } }
function send_bean(&$bean, $queue_name) { global $sugar_config; $stomp_url = "tcp://localhost:40001"; if (!empty($sugar_config['stomp_url'])) { $stomp_url = $sugar_config['stomp_url']; } // make a connection $con = new Stomp($stomp_url); // connect $con->connect(); // send a message to the queue $con->send($queue_name, $this->serialize_bean($bean)); // disconnect $con->disconnect(); }