public function test_emptyRpcResponse_buildsProperRpcResponse() { $contentType = MessageInterface::CONTENT_TYPE_PLAIN_TEXT; $correlationId = 123; $message = MessagesBuilder::emptyRpcResponse($contentType, $correlationId); $this->assertTrue($message instanceof RpcResponseMessage); $this->assertEquals($correlationId, $message->getCorrelationId()); $this->assertEquals($contentType, $message->getContentType()); $this->assertFalse($message->getPayload()->hasErrors()); $this->assertEquals('', $message->getPayload()->getResponse()); $this->assertEquals(RpcResponsePayload::STATUS_CODE_UNDEFINED, $message->getPayload()->getStatus()); }
/** * 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); } }
/** * @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); } }