Ejemplo n.º 1
0
 /**
  * Strategy: preCallback() -> callback() -> postCallback()
  *
  * @param RpcRequestMessage $request
  */
 protected final function onRequest(RpcRequestMessage $request)
 {
     // Reset possible previous values
     $this->currentRequest = null;
     try {
         $this->logger->notice('Message received!', ['body' => $request->getBody(), 'properties' => $request->getProperties()]);
         $this->currentRequest = $request;
         // Create an empty response
         $this->currentResponse = MessagesBuilder::emptyRpcResponse($this->getSerializer()->getSerializedContentType(), $this->currentRequest->getCorrelationId());
         //  Execute callback strategy
         $this->preCallback();
         $this->callback();
         $this->postCallback();
         // Send response
         $this->sendResponse($this->currentResponse);
         // Send ACK for request
         $this->connector->basicAck($request);
     } catch (RemoteProcedureException $e) {
         $this->logger->warning($e->getMessage());
         $responsePayload = RpcResponsePayload::create()->addError('Bad request! ' . $e->getMessage());
         $this->currentResponse->setPayload($responsePayload);
         // Send response
         $this->sendResponse($this->currentResponse);
         // Reject and drop conflictive message
         $this->connector->basicReject($request);
     } catch (MessageException $e) {
         $this->logger->warning($e->getMessage());
         $responsePayload = RpcResponsePayload::create()->addError('Bad request! ' . $e->getMessage());
         $this->currentResponse->setPayload($responsePayload);
         // Send response
         $this->sendResponse($this->currentResponse);
         // Reject and drop conflictive message
         $this->connector->basicReject($request);
     } catch (RpcExecutionException $e) {
         $this->logger->warning($e->getMessage());
         $responsePayload = RpcResponsePayload::create()->addError('Execution error! ' . $e->getMessage());
         $this->currentResponse->setPayload($responsePayload);
         // Send response
         $this->sendResponse($this->currentResponse);
         // Reject and drop conflictive message
         $this->connector->basicReject($request);
     } catch (\Exception $e) {
         $this->logger->error(sprintf('Unexpected error! [File: %s, Line: %s]: %s ', $e->getFile(), $e->getLine(), $e->getMessage()));
         // Reject and drop conflictive message
         $this->connector->basicReject($request);
     }
 }
Ejemplo n.º 2
0
 /**
  * @param RpcResponseMessage $response
  *
  * @throws MessageException
  */
 protected function onResponse(RpcResponseMessage $response)
 {
     $this->logger->notice('Message received!', ['body' => $response->getBody(), 'properties' => $response->getProperties()]);
     try {
         $this->rpcResponse = $response;
         if ($this->correlarionId != $this->rpcResponse->getCorrelationId()) {
             throw MessageException::wrongCorrelationId($this->rpcResponse->getCorrelationId(), $this->correlarionId);
         }
         // Send ack
         $this->connector->basicAck($response);
     } catch (\Exception $e) {
         // Create an error response and reject message
         $responsePayload = RpcResponsePayload::create()->addError($e->getMessage());
         $this->rpcResponse = MessagesBuilder::emptyRpcResponse($this->getSerializer()->getSerializedContentType(), $this->correlarionId);
         $this->rpcResponse->setPayload($responsePayload);
         $this->connector->basicReject($response, false);
     }
 }
Ejemplo n.º 3
0
 /**
  * @param MessageInterface $receivedMessage
  */
 protected final function onMessage(MessageInterface $receivedMessage)
 {
     try {
         $this->numberOfReceivedMessages++;
         $this->currentReceivedMessage = $receivedMessage;
         $this->logger->debug(sprintf('Processing message %s of %s...', $this->numberOfReceivedMessages, 0 == $this->numberOfMessagesToConsume ? 'unlimited' : $this->numberOfMessagesToConsume));
         // Process message
         $this->callback();
         // Send ACK
         $this->connector->basicAck($receivedMessage);
     } catch (\Exception $e) {
         $this->logger->error('Exception processing message!', ['exception' => ['code' => $e->getCode(), 'message' => $e->getMessage(), 'file' => $e->getFile(), 'line' => $e->getLine()]]);
         // Reject and requeue message if needed
         $this->connector->basicReject($receivedMessage, $this->requeueMessagesOnCallbackFails);
     }
     // Cancel consumption when limit reached ($this->numberOfMessagesToConsume)
     if (0 < $this->numberOfMessagesToConsume && $this->numberOfMessagesToConsume <= $this->numberOfReceivedMessages) {
         $this->logger->info(sprintf('Consumption limit reached! (limit: %s)', $this->numberOfMessagesToConsume));
         $this->connector->basicCancel($receivedMessage);
     }
 }