示例#1
0
 public JsonAmqpRpcServer(string hostName, string queueName, IJsonRpcHandler handler)
 {
     _log = LogManager.GetCurrentClassLogger();
     _log.Info("AMQP RPC server starting: host = {0}; queue = {1}", hostName, queueName);
     _messaging = AmqpRpc.CreateMessaging(AmqpRpc.CreateConnector(hostName), queueName);
     _handler   = handler;
 }
示例#2
0
        private void Reply(IReceivedMessage message, JsonTransportResponse response)
        {
            var reply   = message.CreateReply();
            var headers = AmqpRpc.CreateHeaders(null, response.Status);

            reply.Properties.Headers = headers;
            _log.Debug("Sending reply: {0}", response.Body);
            reply.Body = Json.Serialize(response.Body);
            reply.From = null;
            _messaging.Send(reply);
        }
示例#3
0
        public JsonAmqpRpcClient(string hostName, string target)
        {
            _target = target;
            var connector = AmqpRpc.CreateConnector(hostName);

            _messaging                = Factory.CreateMessaging();
            _messaging.Connector      = connector;
            _messaging.SetupReceiver += channel =>
            {
                _replyTo             = channel.QueueDeclare();
                _messaging.QueueName = _replyTo;
            };
            _messaging.QueueName = "DUMMY_QUEUE_NAME";
            _messaging.Init();
        }
示例#4
0
        /// <summary>
        /// Call a remote procedure with a specified timeout
        /// </summary>
        /// <param name="endpoint">The endpoint to which to route the request</param>
        /// <param name="body">The request body</param>
        /// <param name="receiveTimeout">Timeout in ms, after which a null response is returned</param>
        /// <returns>The response</returns>
        public JContainer Call(string endpoint, JContainer body, int receiveTimeout)
        {
            var message = _messaging.CreateMessage();

            message.To                 = _target;
            message.ReplyTo            = _replyTo;
            message.Body               = Json.Serialize(body);
            message.Properties.Headers = AmqpRpc.CreateHeaders(endpoint, null);
            _messaging.Send(message);

            var reply = _messaging.Receive(receiveTimeout);

            if (AmqpRpc.GetStatusCode(reply) != 200)
            {
                throw new AmqpRpcError(Encoding.UTF8.GetString(reply.Body));
            }
            // TODO handle null reply (timeout)

            return(Json.Deserialize(reply.Body));
        }