public SendResult Send(Message message, object routingKey) { Ensure.NotNull(message, "message"); EnsureProduceMQClient(); var currentRoutingKey = GetStringRoutingKey(routingKey); var queueIds = GetTopicQueueIds(message.Topic); var queueId = _queueSelector.SelectQueueId(queueIds, message, currentRoutingKey); if (queueId < 0) { throw new Exception(string.Format("No available routing queue for topic [{0}].", message.Topic)); } string strExchange = message.Topic + "." + queueId; string strRoutingKey = message.Topic + "." + queueId; var queueMessage = new QueueMessage(message.Topic, message.Code, message.Body, queueId, DateTime.Now, DateTime.Now, DateTime.Now, currentRoutingKey, 0); var data = _binarySerializer.Serialize(queueMessage); //var request = new SendMessageRequest { Message = message, QueueId = queueId, RoutingKey = routingKey.ToString() }; //var data = MessageUtils.EncodeSendMessageRequest(request); _channel.BasicPublish(strExchange, strRoutingKey, _basicProp, data); return(new SendResult(SendStatus.Success)); }
public SendResult Send(Message message, object arg) { var queueCount = GetTopicQueueCount(message.Topic); if (queueCount == 0) { throw new Exception(string.Format("No available queue for topic [{0}], producerId:{1}.", message.Topic, Id)); } var queueId = _queueSelector.SelectQueueId(queueCount, message, arg); var remotingRequest = BuildSendMessageRequest(message, queueId); var remotingResponse = _remotingClient.InvokeSync(remotingRequest, Setting.SendMessageTimeoutMilliseconds); var response = _binarySerializer.Deserialize <SendMessageResponse>(remotingResponse.Body); var sendStatus = SendStatus.Success; //TODO, figure from remotingResponse.Code; return(new SendResult(sendStatus, response.MessageOffset, response.MessageQueue, response.QueueOffset)); }
public async Task <SendResult> SendAsync(Message message, object routingKey, int timeoutMilliseconds = 30000) { Ensure.NotNull(message, "message"); var currentRoutingKey = GetStringRoutingKey(routingKey); var queueIds = GetTopicQueueIds(message.Topic); var queueId = _queueSelector.SelectQueueId(queueIds, message, currentRoutingKey); if (queueId < 0) { throw new Exception(string.Format("No available routing queue for topic [{0}].", message.Topic)); } var remotingRequest = BuildSendMessageRequest(message, queueId, currentRoutingKey); var remotingResponse = await _remotingClient.InvokeAsync(remotingRequest, timeoutMilliseconds).ConfigureAwait(false); if (remotingResponse == null) { return(new SendResult(SendStatus.Timeout, string.Format("Send message timeout, message: {0}, routingKey: {1}, timeoutMilliseconds: {2}", message, routingKey, timeoutMilliseconds))); } if (remotingResponse.Code == (int)ResponseCode.Success) { var response = Encoding.UTF8.GetString(remotingResponse.Body).Split(':'); return(new SendResult(SendStatus.Success, response[2], long.Parse(response[0]), new MessageQueue(message.Topic, queueId), long.Parse(response[1]))); } else { return(new SendResult(SendStatus.Failed, Encoding.UTF8.GetString(remotingResponse.Body))); } }