Esempio n. 1
0
        private string SendMessage(StompRequest stompRequest)
        {
            var token = GetToken();

            var frame = new StompFrame(StompCommands.Send);

            frame.Headers.Add(StompHeader.ReplyTo, stompRequest.ReplayTo);
            frame.Headers.Add(StompHeader.ContentType, "application/json");
            frame.Headers.Add(StompHeader.UserId, token);
            frame.Headers.Add(StompHeader.CorrelationId, stompRequest.CorrelationId);
            frame.Headers.Add(StompHeader.Destination, stompRequest.Destination);

            if (!string.IsNullOrWhiteSpace(stompRequest.AppId))
            {
                frame.Headers.Add(StompHeader.AppId, stompRequest.AppId);
            }

            if (!string.IsNullOrWhiteSpace(stompRequest.Body))
            {
                frame.Body = stompRequest.Body;
                frame.Headers.Add(StompHeader.ContentLength, frame.Body.ToUTF8Bytes().Length.ToString());
            }

            // only one send at a time
            lock (_lockSend)
            {
                CheckConnection(token);
                _stomp.SendFrame(frame);
            }

            string message   = null;
            var    endWaitAt = DateTime.Now.AddSeconds(_configuration.MessageTimeoutSec);

            while (message == null && DateTime.Now < endWaitAt)
            {
                SecucardTrace.Info("Waiting for Message with correlationId={0}", stompRequest.CorrelationId);
                message = PullMessage(stompRequest.CorrelationId, _configuration.MaxMessageAgeSec);
                Thread.Sleep(500);
            }
            if (message == null)
            {
                throw new MessageTimeoutException("No answer for " + stompRequest.CorrelationId + " received within " +
                                                  _configuration.MessageTimeoutSec + "s.");
            }

            return(message);
        }