/** * @param Event|string $event * @param array $params if an Event is provided for $event, $params are merged into (overwriting) $event's params * @throws \SoPhp\PubSub\Exception\InvalidEventException */ public function publish($event, $params = array()) { $eventObj = $this->buildEvent($event, $params); if (!$this->message) { $this->message = new AMQPMessage($eventObj->toJson(), array('content_type' => 'text/plain', 'delivery_mode' => 2)); } else { $this->message->setBody($eventObj->toJson()); } $this->getChannel()->basic_publish($this->message, $this->getExchangeDescriptor()->getName()); }
/** * @param AMQPReader $propertyReader * @param AMQPReader $contentReader * @return \PhpAmqpLib\Message\AMQPMessage */ protected function createMessage($propertyReader, $contentReader) { $bodyChunks = array(); $bodyReceivedBytes = 0; $message = new AMQPMessage(); $message->load_properties($propertyReader)->setBodySize($contentReader->read_longlong()); while (bccomp($message->getBodySize(), $bodyReceivedBytes, 0) == 1) { list($frame_type, $payload) = $this->next_frame(); $this->validate_body_frame($frame_type); $bodyReceivedBytes = bcadd($bodyReceivedBytes, mb_strlen($payload, 'ASCII'), 0); if (is_int($this->maxBodySize) && $bodyReceivedBytes > $this->maxBodySize) { $message->setIsTruncated(true); continue; } $bodyChunks[] = $payload; } $message->setBody(implode('', $bodyChunks)); $messageEncoding = $message->getContentEncoding(); if ($this->auto_decode && !empty($messageEncoding)) { try { // Where does the decode() method come from if body is a string? $decodedBody = $message->getBody()->decode($messageEncoding); $message->setBody($decodedBody); } catch (\Exception $e) { $this->debug->debug_msg('Ignoring body decoding exception: ' . $e->getMessage()); } } return $message; }
echo " getting channel... \n"; $ch = $connection->channel(); if ($ch->is_open !== true) { echo " no channel object"; } // create the exchange and bind to the attributes queue // TODO: need to have the exchange name come from rabbit $ch->exchange_declare('attributeExchange', $type = "direct", false, false, $auto_delete = true); $ch->queue_bind('attributes', 'attributeExchange', 'attribute'); // create a new message that we will reuse for all the values in the request echo " setting up new message... \n"; $body = "place holder"; $properties = array('content_type' => 'text/plain', 'delivery_mode' => 2); $msg = new AMQPMessage($body, $properties); // todo: would like to set data limit here, more research needed //$bytes = 150; //$msg->setBodySizeLimit($bytes); // loop through each json object and publish the value to rabbit foreach ($obj['data'] as $array) { $body = '{"key":"' . $array['key'] . '", "value":"' . $array['value'] . '"}'; $msg->setBody($body); echo " sending message\n"; $ch->basic_publish($msg, 'attributeExchange', 'attribute'); } // todo: need to find out why connection closing was failing, and try to modularize the connection, possibly pooling? //echo " closing channel and connection.\n"; //$ch->close(); //$connection->close(); ?>
/** * Update the AMQP message (e.g.: before rescheduling) with new config. */ public function updateAMQPMessage() { $this->amqpMessage->setBody(json_encode($this->config)); }
/** * @param AMQPReader $propertyReader * @param AMQPReader $contentReader * @return \PhpAmqpLib\Message\AMQPMessage */ protected function createMessage($propertyReader, $contentReader) { $bodyChunks = array(); $bodyReceivedBytes = 0; $message = new AMQPMessage(); $message->load_properties($propertyReader)->setBodySize($contentReader->read_longlong()); while (bccomp($message->getBodySize(), $bodyReceivedBytes, 0) == 1) { list($frame_type, $payload) = $this->next_frame(); $this->validate_body_frame($frame_type); $bodyReceivedBytes = bcadd($bodyReceivedBytes, mb_strlen($payload, 'ASCII'), 0); if (is_int($this->maxBodySize) && $bodyReceivedBytes > $this->maxBodySize) { $message->setIsTruncated(true); continue; } $bodyChunks[] = $payload; } $message->setBody(implode('', $bodyChunks)); return $message; }