예제 #1
0
 private IEnumerable<int> CreateTopicOnBroker(string topic, Broker broker)
 {
     var brokerAdminEndpoint = broker.BrokerInfo.AdminAddress.ToEndPoint();
     var adminRemotingClient = new SocketRemotingClient(brokerAdminEndpoint, _nameServerController.Setting.SocketSetting).Start();
     var requestData = _binarySerializer.Serialize(new CreateTopicRequest(topic));
     var remotingRequest = new RemotingRequest((int)BrokerRequestCode.CreateTopic, requestData);
     var remotingResponse = adminRemotingClient.InvokeSync(remotingRequest, 30000);
     if (remotingResponse.Code != ResponseCode.Success)
     {
         throw new Exception(string.Format("AutoCreateTopicOnBroker failed, errorMessage: {0}", Encoding.UTF8.GetString(remotingResponse.Body)));
     }
     adminRemotingClient.Shutdown();
     return _binarySerializer.Deserialize<IEnumerable<int>>(remotingResponse.Body);
 }
예제 #2
0
        static void StartSendMessageTest()
        {
            var serverIP = ConfigurationManager.AppSettings["ServerAddress"];
            var serverAddress = string.IsNullOrEmpty(serverIP) ? SocketUtils.GetLocalIPV4() : IPAddress.Parse(serverIP);
            var messageSize = int.Parse(ConfigurationManager.AppSettings["MessageSize"]);

            _message = new byte[messageSize];
            _mode = ConfigurationManager.AppSettings["Mode"];
            _messageCount = int.Parse(ConfigurationManager.AppSettings["MessageCount"]);

            _client = new SocketRemotingClient(new IPEndPoint(serverAddress, 5000)).Start();

            var sendAction = default(Action);

            if (_mode == "Oneway")
            {
                sendAction = () =>
                {
                    _client.InvokeOneway(new RemotingRequest(100, _message));
                    Interlocked.Increment(ref _sentCount);
                };
            }
            else if (_mode == "Sync")
            {
                sendAction = () =>
                {
                    _client.InvokeSync(new RemotingRequest(100, _message), 5000);
                    Interlocked.Increment(ref _sentCount);
                };
            }
            else if (_mode == "Async")
            {
                sendAction = () => _client.InvokeAsync(new RemotingRequest(100, _message), 100000).ContinueWith(t =>
                {
                    if (t.Exception != null)
                    {
                        _logger.Error(t.Exception);
                        return;
                    }
                    if (t.Result.Code <= 0)
                    {
                        _logger.Error(Encoding.UTF8.GetString(t.Result.Body));
                        return;
                    }
                    Interlocked.Increment(ref _sentCount);
                });
            }
            else if (_mode == "Callback")
            {
                _client.RegisterResponseHandler(100, new ResponseHandler());
                sendAction = () => _client.InvokeWithCallback(new RemotingRequest(100, _message));
            }

            Task.Factory.StartNew(() =>
            {
                for (var i = 0; i < _messageCount; i++)
                {
                    try
                    {
                        sendAction();
                    }
                    catch (Exception ex)
                    {
                        _logger.ErrorFormat("Send remoting request failed, errorMsg:{0}", ex.Message);
                        Thread.Sleep(3000);
                    }
                }
            });
        }
예제 #3
0
        static void StartSendMessageTest()
        {
            var serverIP = ConfigurationManager.AppSettings["ServerAddress"];
            var serverAddress = string.IsNullOrEmpty(serverIP) ? SocketUtils.GetLocalIPV4() : IPAddress.Parse(serverIP);
            var sendAction = default(Action);

            _client = new SocketRemotingClient(new IPEndPoint(serverAddress, 5000)).Start();

            if (_mode == "Oneway")
            {
                sendAction = () =>
                {
                    var request = new RemotingRequest(100, _message);
                    _client.InvokeOneway(request);
                    _performanceService.IncrementKeyCount(_performanceKey, (DateTime.Now - request.CreatedTime).TotalMilliseconds);
                };
            }
            else if (_mode == "Sync")
            {
                sendAction = () =>
                {
                    var request = new RemotingRequest(100, _message);
                    var response = _client.InvokeSync(request, 5000);
                    _performanceService.IncrementKeyCount(_performanceKey, (DateTime.Now - response.RequestTime).TotalMilliseconds);
                };
            }
            else if (_mode == "Async")
            {
                sendAction = () =>
                {
                    var request = new RemotingRequest(100, _message);
                    _client.InvokeAsync(request, 100000).ContinueWith(t =>
                    {
                        if (t.Exception != null)
                        {
                            _logger.Error(t.Exception);
                            return;
                        }
                        var response = t.Result;
                        if (response.ResponseCode <= 0)
                        {
                            _logger.Error(Encoding.UTF8.GetString(response.ResponseBody));
                            return;
                        }
                        _performanceService.IncrementKeyCount(_performanceKey, (DateTime.Now - response.RequestTime).TotalMilliseconds);
                    });
                };
            }
            else if (_mode == "Callback")
            {
                _client.RegisterResponseHandler(100, new ResponseHandler(_performanceService, _performanceKey));
                sendAction = () => _client.InvokeWithCallback(new RemotingRequest(100, _message));
            }

            Task.Factory.StartNew(() =>
            {
                for (var i = 0; i < _messageCount; i++)
                {
                    try
                    {
                        sendAction();
                    }
                    catch (Exception ex)
                    {
                        _logger.ErrorFormat("Send remotingRequest failed, errorMsg:{0}", ex.Message);
                        Thread.Sleep(3000);
                    }
                }
            });
        }