■ 클러스터 서버용 NATS 클라이언트를 사용하는 방법을 보여준다.
[TestCommon 프로젝트]
▶ ServerInfo.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 |
using System; using NATS.Client; namespace TestCommon { /// <summary> /// 서버 정보 /// </summary> public class ServerInfo : IDisposable { //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Private #region Field /// <summary> /// NATS 옵션 /// </summary> private Options options; /// <summary> /// NATS 연결 /// </summary> private IConnection connection = null; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Property ////////////////////////////////////////////////////////////////////////////////////////// Public #region NATS 옵션 - Options /// <summary> /// NATS 옵션 /// </summary> public Options Options { get { return this.options; } } #endregion #region NATS 연결 - Connection /// <summary> /// NATS 연결 /// </summary> public IConnection Connection { get { return this.connection; } } #endregion #region 서버 URL - URL /// <summary> /// 서버 URL /// </summary> public string URL { get { return this.options.Url; } } #endregion #region 사용자 ID - User /// <summary> /// 사용자 ID /// </summary> public string User { get { return this.options.User; } } #endregion #region 서버 URL 배열 - Servers /// <summary> /// 서버 URL 배열 /// </summary> public string[] Servers { get { return this.options.Servers; } } #endregion #region PING 주기 - PingInterval /// <summary> /// PING 주기 /// </summary> public int PingInterval { get { return this.options.PingInterval; } } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - ServerInfo(options) /// <summary> /// 생성자 /// </summary> /// <param name="options">NATS 옵션</param> public ServerInfo(Options options) { this.options = options; } #endregion #region 생성자 - ServerInfo(url, user, password, servers, pingInterval) /// <summary> /// 생성자 /// </summary> /// <param name="url">URL</param> /// <param name="user">사용자 ID</param> /// <param name="password">패스워드</param> /// <param name="servers">서버 주소 배열</param> /// <param name="pingInterval">PING 주기</param> public ServerInfo(string url, string user, string password, string[] servers = null, int pingInterval = 2000) { Options options = ConnectionFactory.GetDefaultOptions(); options.Url = url; options.User = user; options.Password = password; options.Servers = servers; options.PingInterval = pingInterval; this.options = options; } #endregion ////////////////////////////////////////////////////////////////////////////////////////// Private #region 생성자 - ServerInfo() /// <summary> /// 생성자 /// </summary> private ServerInfo() { } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Public #region 서버 연결하기 - ConnectServer() /// <summary> /// 서버 연결하기 /// </summary> public void ConnectServer() { this.connection = new ConnectionFactory().CreateConnection(this.options); } #endregion #region 서버 연결 끊기 - DisconnectServer() /// <summary> /// 서버 연결 끊기 /// </summary> public void DisconnectServer() { if(this.connection != null) { try { this.connection.Dispose(); } catch { } finally { this.connection = null; } } } #endregion #region 리소스 해제하기 - Dispose() /// <summary> /// 리소스 해제하기 /// </summary> public void Dispose() { DisconnectServer(); } #endregion } } |
▶ SubscriptionInfo.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 |
using System; using NATS.Client; namespace TestCommon { /// <summary> /// 구독 정보 /// </summary> public class SubscriptionInfo : IDisposable { //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Private #region Field /// <summary> /// 서브젝트 /// </summary> private string subject; /// <summary> /// 큐 /// </summary> private string queue; /// <summary> /// 키 /// </summary> private string key; /// <summary> /// 구독 /// </summary> private IAsyncSubscription subscription; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Property ////////////////////////////////////////////////////////////////////////////////////////// Public #region 서브젝트 - Subject /// <summary> /// 서브젝트 /// </summary> public string Subject { get { return this.subject; } } #endregion #region 큐 - Queue /// <summary> /// 큐 /// </summary> public string Queue { get { return this.queue; } } #endregion #region 키 - Key /// <summary> /// 키 /// </summary> public string Key { get { return this.key; } } #endregion #region 구독 - Subscription /// <summary> /// 구독 /// </summary> public IAsyncSubscription Subscription { get { return this.subscription; } } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - SubscriptionInfo(subject, queue) /// <summary> /// 생성자 /// </summary> /// <param name="subject">서브젝트</param> /// <param name="queue">큐</param> public SubscriptionInfo(string subject, string queue) { if(string.IsNullOrWhiteSpace(subject)) { throw new ArgumentException("subject가 null이거나 공백 문자열 입니다."); } this.subject = subject.Trim(); this.queue = string.IsNullOrWhiteSpace(queue) ? null : queue.Trim(); this.key = GetKey(this.subject, this.queue); this.subscription = null; } #endregion ////////////////////////////////////////////////////////////////////////////////////////// Private #region 생성자 - SubscriptionInfo() /// <summary> /// 생성자 /// </summary> private SubscriptionInfo() { } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Public #region 키 구하기 - GetKey(subject, queue) /// <summary> /// 키 구하기 /// </summary> /// <param name="subject">서브젝트</param> /// <param name="queue">큐</param> /// <returns>키</returns> public static string GetKey(string subject, string queue) { if(string.IsNullOrWhiteSpace(subject)) { throw new ArgumentException("subject가 null이거나 공백 문자열 입니다."); } string targetSubject = subject.Trim(); string targetQueue = string.IsNullOrWhiteSpace(queue) ? null : queue.Trim(); if(targetQueue == null) { return targetSubject; } else { return $"{targetSubject}_{targetQueue}"; } } #endregion ////////////////////////////////////////////////////////////////////////////////////////// Instance //////////////////////////////////////////////////////////////////////////////// Public #region 구독하기 - Subscribe(connection, receivedEventHandler) /// <summary> /// 구독하기 /// </summary> /// <param name="connection">NATS 연결</param> /// <param name="receivedEventHandler">수신 이벤트 핸들러</param> public void Subscribe(IConnection connection, EventHandler<MsgHandlerEventArgs> receivedEventHandler) { this.subscription = connection.SubscribeAsync(this.subject, this.queue, receivedEventHandler); } #endregion #region 구독 취소하기 - Unsubscribe() /// <summary> /// 구독 취소하기 /// </summary> public void Unsubscribe() { if(this.subscription != null) { try { if(this.subscription.IsValid) { try { this.subscription.Unsubscribe(); this.subscription.Dispose(); } catch { } } } finally { this.subscription = null; } } } #endregion #region 리소스 해제하기 - Dispose() /// <summary> /// 리소스 해제하기 /// </summary> public void Dispose() { Unsubscribe(); } #endregion } } |
▶ ReceivedEventArgs.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 |
using System; using NATS.Client; namespace TestCommon { /// <summary> /// 수신시 이벤트 인자 /// </summary> public class ReceivedEventArgs { //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Private #region Field /// <summary> /// 소스 이벤트 인자 /// </summary> private MsgHandlerEventArgs source; /// <summary> /// 응답 데이터 /// </summary> private byte[] replyData; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Property ////////////////////////////////////////////////////////////////////////////////////////// Public #region 서브젝트 - Subject /// <summary> /// 서브젝트 /// </summary> public string Subject { get { return this.source.Message.Subject; } } #endregion #region 데이터 - Data /// <summary> /// 데이터 /// </summary> public byte[] Data { get { return this.source.Message.Data; } } #endregion #region 수신 구독 - ArrivalSubcription /// <summary> /// 수신 구독 /// </summary> public ISubscription ArrivalSubcription { get { return this.source.Message.ArrivalSubcription; } } #endregion #region 응답 서브젝트 - Reply /// <summary> /// 응답 서브젝트 /// </summary> public string Reply { get { return this.source.Message.Reply; } } #endregion #region 응답 데이터 - ReplyData /// <summary> /// 응답 데이터 /// </summary> public byte[] ReplyData { get { return this.replyData; } set { this.replyData = value; } } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - ReceivedEventArgs(source) /// <summary> /// 생성자 /// </summary> /// <param name="source">소스 이벤트 인자</param> public ReceivedEventArgs(MsgHandlerEventArgs source) { if(source == null) { throw new ArgumentNullException("source가 null 입니다."); } this.source = source; this.replyData = null; } #endregion ////////////////////////////////////////////////////////////////////////////////////////// Private #region 생성자 - ReceivedEventArgs() /// <summary> /// 생성자 /// </summary> private ReceivedEventArgs() { } #endregion } } |
▶ NATSClient.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 |
using System; using System.Collections.Generic; using System.Text; using NATS.Client; namespace TestCommon { /// <summary> /// NATS 클라이언트 /// </summary> public class NATSClient : IDisposable { //////////////////////////////////////////////////////////////////////////////////////////////////// Event ////////////////////////////////////////////////////////////////////////////////////////// Public #region 서버 발견시 - ServerDiscovered /// <summary> /// 서버 발견시 /// </summary> public event EventHandler<ConnEventArgs> ServerDiscovered; #endregion #region 재연결시 - Reconnected /// <summary> /// 재연결시 /// </summary> public event EventHandler<ConnEventArgs> Reconnected; #endregion #region 연결 중단시 - Disconnected /// <summary> /// 연결 중단시 /// </summary> public event EventHandler<ConnEventArgs> Disconnected; #endregion #region 폐쇄시 - Closed /// <summary> /// 폐쇄시 /// </summary> public event EventHandler<ConnEventArgs> Closed; #endregion #region 에러시 - Error /// <summary> /// 에러시 /// </summary> public event EventHandler<ErrEventArgs> Error; #endregion #region 수신시 - MessageReceived /// <summary> /// 수신시 /// </summary> public event EventHandler<ReceivedEventArgs> Received; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Private #region Field /// <summary> /// 서버 정보 /// </summary> private ServerInfo serverInfo = null; /// <summary> /// 구독 정보 딕셔너리 /// </summary> private Dictionary<string, SubscriptionInfo> subscriptionInfoDictionary = null; /// <summary> /// 실행 여부 /// </summary> private bool inRunning = false; /// <summary> /// 동기 객체 /// </summary> private object syncObject; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Property ////////////////////////////////////////////////////////////////////////////////////////// Public #region 서버 정보 - ServerInfo /// <summary> /// 서버 정보 /// </summary> public ServerInfo ServerInfo { get { return this.serverInfo; } } #endregion #region 실행 여부 - IsRunning /// <summary> /// 실행 여부 /// </summary> public bool IsRunning { get { return this.inRunning; } } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - NATSClient(serverInfo, subscriptionInfoList) /// <summary> /// 생성자 /// </summary> /// <param name="serverInfo">서버 정보</param> /// <param name="subscriptionInfoList">구독 정보 리스트</param> public NATSClient(ServerInfo serverInfo, IList<SubscriptionInfo> subscriptionInfoList) { if(serverInfo == null) { throw new ArgumentNullException("serverInfo가 null 입니다."); } this.serverInfo = serverInfo; #region 구독 정보 딕셔너리를 설정한다. this.subscriptionInfoDictionary = new Dictionary<string, SubscriptionInfo>(); if(subscriptionInfoList != null) { foreach(SubscriptionInfo info in subscriptionInfoList) { if(!this.subscriptionInfoDictionary.ContainsKey(info.Key)) { this.subscriptionInfoDictionary.Add(info.Key, info); } } } #endregion this.syncObject = new object(); } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Public #region 시작하기 - Start() /// <summary> /// 시작하기 /// </summary> public void Start() { lock(this.syncObject) { if(this.inRunning) { return; } this.inRunning = true; try { Options options = this.serverInfo.Options; options.ServerDiscoveredEventHandler = (sender, e) => { FireServerDiscoveredEvent(e); }; options.ReconnectedEventHandler = (sender, e) => { FireReconnectedEvent(e); }; options.DisconnectedEventHandler = (sender, e) => { FireDisconnectedEvent(e); }; options.ClosedEventHandler = (sender, e) => { FireClosedEvent(e); }; options.AsyncErrorEventHandler = (sender, e) => { FireErrorEvent(e); }; this.serverInfo.ConnectServer(); foreach(KeyValuePair<string, SubscriptionInfo> keyValuePair in this.subscriptionInfoDictionary) { SubscriptionInfo info = keyValuePair.Value; info.Subscribe(this.serverInfo.Connection, (sender, e) => { FireReceivedEvent(e); }); } } catch(Exception exception) { this.inRunning = false; throw exception; } } } #endregion #region 중단하기 - Stop() /// <summary> /// 중단하기 /// </summary> public void Stop() { lock(this.syncObject) { if(!this.inRunning) { return; } this.inRunning = false; foreach(KeyValuePair<string, SubscriptionInfo> keyValuePair in this.subscriptionInfoDictionary) { SubscriptionInfo info = keyValuePair.Value; if(info != null) { info.Unsubscribe(); } } this.serverInfo.DisconnectServer(); Options options = this.serverInfo.Options; options.ServerDiscoveredEventHandler = null; options.ReconnectedEventHandler = null; options.DisconnectedEventHandler = null; options.ClosedEventHandler = null; options.AsyncErrorEventHandler = null; } } #endregion #region 메시지 송신하기 - SendMessage(subject, data) /// <summary> /// 메시지 송신하기 /// </summary> /// <param name="subject">수신 서브젝트</param> /// <param name="data">데이터</param> public void SendMessage(string subject, byte[] data) { if(this.serverInfo.Connection.State == ConnState.CLOSED || this.serverInfo.Connection.State == ConnState.DISCONNECTED) { throw new InvalidOperationException("메시지를 송신할 수 없습니다 : " + this.serverInfo.Connection.State.ToString()); } lock(this.syncObject) { if(string.IsNullOrWhiteSpace(subject)) { throw new ArgumentException("subject가 null이거나 공백 문자열 입니다."); } if(data == null || data.Length == 0) { throw new ArgumentException("data null이거나 길이가 0 입니다."); } int tryCount = 0; do { try { this.serverInfo.Connection.Publish(subject, data); this.serverInfo.Connection.Flush(); return; } catch(Exception exception) { if(tryCount >= 3) { throw exception; } } tryCount++; } while(true); } } #endregion #region 메시지 송신하기 - SendMessage(subject, message) /// <summary> /// 메시지 송신하기 /// </summary> /// <param name="subject">수신 서브젝트</param> /// <param name="message">송신 메시지</param> public void SendMessage(string subject, string message) { byte[] data = Encoding.UTF8.GetBytes(message); SendMessage(subject, data); } #endregion #region 메시지 요청하기 - RequestMessage(subject, data, timeout) /// <summary> /// 메시지 요청하기 /// </summary> /// <param name="subject">수신 서브젝트</param> /// <param name="data">송신 데이터</param> /// <param name="timeout">타임아웃 (디폴트 : 3초)</param> /// <returns>응답 데이터</returns> public byte[] RequestMessage(string subject, byte[] data, int timeout = 3000) { if(this.serverInfo.Connection.State == ConnState.CLOSED || this.serverInfo.Connection.State == ConnState.DISCONNECTED) { throw new InvalidOperationException("메시지를 요청할 수 없습니다 : " + this.serverInfo.Connection.State.ToString()); } lock(this.syncObject) { if(string.IsNullOrWhiteSpace(subject)) { throw new ArgumentException("subject가 null이거나 공백 문자열 입니다."); } if(data == null || data.Length == 0) { throw new ArgumentException("data가 null이거나 길이가 0 입니다."); } int tryCount = 0; do { try { Msg replyMessage = this.serverInfo.Connection.Request(subject, data, timeout); return replyMessage.Data; } catch(Exception exception) { if(tryCount >= 3) { throw exception; } } tryCount++; } while(true); } } #endregion #region 메시지 요청하기 - RequestMessage(subject, message, timeout) /// <summary> /// 메시지 요청하기 /// </summary> /// <param name="subject">수신 서브젝트</param> /// <param name="message">송신 메시지</param> /// <param name="timeout">타임아웃 (디폴트 : 3초)</param> /// <returns>응답 메시지</returns> public string RequestMessage(string subject, string message, int timeout = 3000) { byte[] data = Encoding.UTF8.GetBytes(message); byte[] replyData = RequestMessage(subject, data, timeout); string replyMessage = Encoding.UTF8.GetString(replyData); return replyMessage; } #endregion #region 구독하기 - Subscribe(subject, queue) /// <summary> /// 구독하기 /// </summary> /// <param name="subject">서브젝트</param> /// <param name="queue">큐</param> /// <returns>처리 결과</returns> public bool Subscribe(string subject, string queue) { lock(this.syncObject) { string key = SubscriptionInfo.GetKey(subject, queue); if(this.subscriptionInfoDictionary.ContainsKey(key)) { SubscriptionInfo info = this.subscriptionInfoDictionary[key]; if(info.Subscription == null) { info.Subscribe(this.serverInfo.Connection, (sender, e) => { FireReceivedEvent(e); }); return true; } else { return false; } } else { SubscriptionInfo info = new SubscriptionInfo(subject, queue); info.Subscribe(this.serverInfo.Connection, (sender, e) => { FireReceivedEvent(e); }); this.subscriptionInfoDictionary.Add(key, info); return true; } } } #endregion #region 구독 취소하기 - Unsubscribe(subject, queue) /// <summary> /// 구독 취소하기 /// </summary> /// <param name="subject">서브젝트</param> /// <param name="queue">큐</param> /// <returns>처리 결과</returns> public bool Unsubscribe(string subject, string queue) { lock(this.syncObject) { string key = SubscriptionInfo.GetKey(subject, queue); if(this.subscriptionInfoDictionary.ContainsKey(key)) { SubscriptionInfo info = this.subscriptionInfoDictionary[key]; if(info.Subscription == null) { return false; } else { info.Unsubscribe(); return false; } } else { return false; } } } #endregion #region 리소스 해제하기 - Dispose() /// <summary> /// 리소스 해제하기 /// </summary> public void Dispose() { Stop(); } #endregion ////////////////////////////////////////////////////////////////////////////////////////// Protected #region 서버 발견시 이벤트 발생시키기 - FireServerDiscoveredEvent(e) /// <summary> /// 서버 발견시 이벤트 발생시키기 /// </summary> /// <param name="e">이벤트 인자</param> protected void FireServerDiscoveredEvent(ConnEventArgs e) { ServerDiscovered?.Invoke(this, e); } #endregion #region 재연결시 이벤트 발생시키기 - FireReconnectedEvent(e) /// <summary> /// 재연결시 이벤트 발생시키기 /// </summary> /// <param name="e">이벤트 인자</param> protected void FireReconnectedEvent(ConnEventArgs e) { Reconnected?.Invoke(this, e); } #endregion #region 연결 중단시 이벤트 발생시키기 - FireDisconnectedEvent(e) /// <summary> /// 연결 중단시 이벤트 발생시키기 /// </summary> /// <param name="e">이벤트 인자</param> protected void FireDisconnectedEvent(ConnEventArgs e) { Disconnected?.Invoke(this, e); } #endregion #region 폐쇄시 이벤트 발생시키기 - FireClosedEvent(e) /// <summary> /// 폐쇄시 이벤트 발생시키기 /// </summary> /// <param name="e">NATS 연결</param> protected void FireClosedEvent(ConnEventArgs e) { Closed?.Invoke(this, e); } #endregion #region 에러 이벤트 발생시키기 - FireErrorEvent(e) /// <summary> /// 에러 이벤트 발생시키기 /// </summary> /// <param name="e">이벤트 인자</param> protected void FireErrorEvent(ErrEventArgs e) { Error?.Invoke(this, e); } #endregion #region 메시지 수신시 이벤트 발생시키기 - FireMessageReceivedEvent(source) /// <summary> /// 메시지 수신시 이벤트 발생시키기 /// </summary> /// <param name="source">소스 이벤트 인자</param> protected void FireReceivedEvent(MsgHandlerEventArgs source) { ReceivedEventArgs e = new ReceivedEventArgs(source); Received?.Invoke(this, e); if(e.Reply != null && e.ReplyData != null) { e.ArrivalSubcription.Connection.Publish(e.Reply, e.ReplyData); e.ArrivalSubcription.Connection.Flush(); } } #endregion } } |
[TestReceiver 프로젝트]
▶ Program.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 |
using System; using System.Text; using System.Timers; using NATS.Client; using TestCommon; namespace TestReceiver { /// <summary> /// 프로그램 /// </summary> class Program { //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Private #region Field /// <summary> /// 주 서버 정보 /// </summary> private static ServerInfo _primaryServerInfo = null; /// <summary> /// 보조 서버 정보 /// </summary> private static ServerInfo _secondaryServerInfo = null; /// <summary> /// 구독 정보 배열 /// </summary> private static SubscriptionInfo[] _subscriptionInfoArray = null; /// <summary> /// NATS 클라이언트 /// </summary> private static NATSClient _natsClient = null; /// <summary> /// 클러스터 네트워크 스위칭 허용 여부 /// </summary> private static bool _allowSwitchingClusterNetwork = true; /// <summary> /// 클러스터 네트워크 스위칭 타이머 /// </summary> private static Timer _switchingClusterNetworkTimer = null; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Private ////////////////////////////////////////////////////////////////////// Function #region NATS 클라이언트 서버 발견시 처리하기 - natsClient_ServerDiscovered(sender, e) /// <summary> /// NATS 클라이언트 서버 발견시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private static void natsClient_ServerDiscovered(object sender, ConnEventArgs e) { Console.WriteLine(""); Console.WriteLine("이벤트 : SERVER DISCOVERED"); Console.WriteLine("--------------------------------------------------"); WriteLog(e.Conn); Console.WriteLine("--------------------------------------------------"); } #endregion #region NATS 클라이언트 재연결시 처리하기 - natsClient_Reconnected(sender, e) /// <summary> /// NATS 클라이언트 재연결시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private static void natsClient_Reconnected(object sender, ConnEventArgs e) { Console.WriteLine(""); Console.WriteLine("이벤트 : RECONNECTED"); Console.WriteLine("--------------------------------------------------"); WriteLog(e.Conn); Console.WriteLine("--------------------------------------------------"); if(_allowSwitchingClusterNetwork) { if(e.Conn.State == ConnState.CONNECTED) { _switchingClusterNetworkTimer.Stop(); } } } #endregion #region NATS 클라이언트 연결 중단시 처리하기 - natsClient_Disconnected(sender, e) /// <summary> /// NATS 클라이언트 연결 중단시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private static void natsClient_Disconnected(object sender, ConnEventArgs e) { Console.WriteLine(""); Console.WriteLine("이벤트 : DISCONNECTED"); Console.WriteLine("--------------------------------------------------"); WriteLog(e.Conn); Console.WriteLine("--------------------------------------------------"); if(_allowSwitchingClusterNetwork) { if(e.Conn.State == ConnState.RECONNECTING) { _switchingClusterNetworkTimer.Start(); } else { _switchingClusterNetworkTimer.Stop(); } } } #endregion #region NATS 클라이언트 폐쇄시 처리하기 - natsClient_Closed(sender, e) /// <summary> /// NATS 클라이언트 폐쇄시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private static void natsClient_Closed(object sender, ConnEventArgs e) { Console.WriteLine(""); Console.WriteLine("이벤트 : CLOSED"); Console.WriteLine("--------------------------------------------------"); WriteLog(e.Conn); Console.WriteLine("--------------------------------------------------"); } #endregion #region NATS 클라이언트 에러시 처리하기 - natsClient_Error(sender, e) /// <summary> /// NATS 클라이언트 에러시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private static void natsClient_Error(object sender, ErrEventArgs e) { Console.WriteLine(""); Console.WriteLine("이벤트 : ERROR"); Console.WriteLine("--------------------------------------------------"); WriteLog(e.Conn); Console.WriteLine("--------------------------------------------------"); } #endregion #region NATS 클라이언트 수신시 처리하기 - natsClient_Received(sender, e) /// <summary> /// NATS 클라이언트 수신시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private static void natsClient_Received(object sender, ReceivedEventArgs e) { Console.WriteLine(""); Console.WriteLine("이벤트 : RECEIVED"); Console.WriteLine("--------------------------------------------------"); Console.WriteLine(" 서브젝트 : {0}", e.Subject ); Console.WriteLine(" 응답 서브젝트 : {0}", e.Reply ); Console.WriteLine(" 메시지 : {0}", Encoding.UTF8.GetString(e.Data)); Console.WriteLine("--------------------------------------------------"); if(!string.IsNullOrWhiteSpace(e.Reply)) { e.ReplyData = Encoding.UTF8.GetBytes("응답 메시지 : " + DateTime.Now.ToString()); } } #endregion #region 클러스터 네트워크 스위칭 타이머 경과시 처리하기 - switchingClusterNetworkTimer_Elapsed(sender, e) /// <summary> /// 클러스터 네트워크 스위칭 타이머 경과시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private static void switchingClusterNetworkTimer_Elapsed(object sender, ElapsedEventArgs e) { try { if(_natsClient.IsRunning) { _natsClient.Stop(); } ServerInfo serverInfo = null; if(_natsClient.ServerInfo == _primaryServerInfo) { serverInfo = _secondaryServerInfo; } else { serverInfo = _primaryServerInfo; } _natsClient = new NATSClient(serverInfo, _subscriptionInfoArray); _natsClient.ServerDiscovered += natsClient_ServerDiscovered; _natsClient.Reconnected += natsClient_Reconnected; _natsClient.Disconnected += natsClient_Disconnected; _natsClient.Closed += natsClient_Closed; _natsClient.Error += natsClient_Error; _natsClient.Received += natsClient_Received; _natsClient.Start(); Console.WriteLine(""); Console.WriteLine("SERVER CONNECTED"); Console.WriteLine("--------------------------------------------------"); WriteLog(_natsClient.ServerInfo.Connection); Console.WriteLine("--------------------------------------------------"); } finally { _switchingClusterNetworkTimer.Stop(); } } #endregion ////////////////////////////////////////////////////////////////////// Function #region 프로그램 시작하기 - Main() /// <summary> /// 프로그램 시작하기 /// </summary> private static void Main() { #region 주 서버 정보를 설정한다. _primaryServerInfo = new ServerInfo ( null, "user1", "test1234", new string[] { "127.0.0.1:10010", "127.0.0.1:10020" } ); #endregion #region 보조 서버 정보를 설정한다. _secondaryServerInfo = new ServerInfo ( null, "user1", "test1234", new string[] { "127.0.0.1:10030", "127.0.0.1:10040" } ); #endregion #region 구독 정보 배열을 설정한다. _subscriptionInfoArray = new SubscriptionInfo[] { new SubscriptionInfo("foo1", "aaa"), new SubscriptionInfo("foo2", null ) }; #endregion #region 클러스터 네트워크 스위칭 타이머를 설정한다. _switchingClusterNetworkTimer = new Timer(); _switchingClusterNetworkTimer.Interval = 1000 * 5; _switchingClusterNetworkTimer.Elapsed += switchingClusterNetworkTimer_Elapsed; #endregion #region NATS 클라이언트를 초기화한다. _natsClient = new NATSClient(_primaryServerInfo, _subscriptionInfoArray); _natsClient.ServerDiscovered += natsClient_ServerDiscovered; _natsClient.Reconnected += natsClient_Reconnected; _natsClient.Disconnected += natsClient_Disconnected; _natsClient.Closed += natsClient_Closed; _natsClient.Error += natsClient_Error; _natsClient.Received += natsClient_Received; #endregion Console.WriteLine("수신 메시지 클라인트를 초기화 했습니다."); #region NATS 클라이언트를 시작한다. try { _natsClient.Start(); } catch { #region NATS 클라이언트 시작시 에러가 발생하면 클러스터 네트워크를 변경해서 NATS 클라이언트를 시작한다. ServerInfo serverInfo = null; if(_natsClient.ServerInfo == _primaryServerInfo) { serverInfo = _secondaryServerInfo; } else { serverInfo = _primaryServerInfo; } _natsClient = new NATSClient(serverInfo, _subscriptionInfoArray); _natsClient.ServerDiscovered += natsClient_ServerDiscovered; _natsClient.Reconnected += natsClient_Reconnected; _natsClient.Disconnected += natsClient_Disconnected; _natsClient.Closed += natsClient_Closed; _natsClient.Error += natsClient_Error; _natsClient.Received += natsClient_Received; _natsClient.Start(); #endregion } #endregion #region 서버 연결 상태를 출력한다. Console.WriteLine(""); Console.WriteLine("SERVER CONNECTED"); Console.WriteLine("--------------------------------------------------"); WriteLog(_natsClient.ServerInfo.Connection); Console.WriteLine("--------------------------------------------------"); #endregion Console.WriteLine("수신 메시지 클라이언트를 시작했습니다."); Console.WriteLine("프로그램을 종료하기 위해 아무 키나 눌러 주시기 바랍니다."); Console.ReadKey(true); _natsClient.Stop(); Console.WriteLine("수신 메시지 클라이언트를 중단했습니다."); } #endregion #region 로그 작성하기 - WriteLog(connection) /// <summary> /// 로그 작성하기 /// </summary> /// <param name="connection">NATS 연결</param> private static void WriteLog(IConnection connection) { Console.WriteLine(" 연결 ID : {0}", connection.ConnectedId ); Console.WriteLine(" 연결 URL : {0}", connection.ConnectedUrl); Console.WriteLine(" 연결 상태 : {0}", connection.State ); Console.WriteLine(" 발견 서버" ); foreach(string server in connection.DiscoveredServers) { Console.WriteLine(" {0}", server); } } #endregion } } |
[TestSender 프로젝트]
▶ Program.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 |
using System; using System.Text; using System.Timers; using NATS.Client; using TestCommon; namespace TestSender { /// <summary> /// 프로그램 /// </summary> class Program { //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Private #region Field /// <summary> /// 주 서버 정보 /// </summary> private static ServerInfo _primaryServerInfo = null; /// <summary> /// 보조 서버 정보 /// </summary> private static ServerInfo _secondaryServerInfo = null; /// <summary> /// 구독 정보 배열 /// </summary> private static SubscriptionInfo[] _subscriptionInfoArray = null; /// <summary> /// NATS 클라이언트 /// </summary> private static NATSClient _natsClient = null; /// <summary> /// 클러스터 네트워크 스위칭 허용 여부 /// </summary> private static bool _allowSwitchingClusterNetwork = true; /// <summary> /// 클러스터 네트워크 스위칭 타이머 /// </summary> private static Timer _switchingClusterNetworkTimer = null; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Private ////////////////////////////////////////////////////////////////////// Function #region NATS 클라이언트 서버 발견시 처리하기 - natsClient_ServerDiscovered(sender, e) /// <summary> /// NATS 클라이언트 서버 발견시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private static void natsClient_ServerDiscovered(object sender, ConnEventArgs e) { Console.WriteLine(""); Console.WriteLine("이벤트 : SERVER DISCOVERED"); Console.WriteLine("--------------------------------------------------"); WriteLog(e.Conn); Console.WriteLine("--------------------------------------------------"); } #endregion #region NATS 클라이언트 재연결시 처리하기 - natsClient_Reconnected(sender, e) /// <summary> /// NATS 클라이언트 재연결시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private static void natsClient_Reconnected(object sender, ConnEventArgs e) { Console.WriteLine(""); Console.WriteLine("이벤트 : RECONNECTED"); Console.WriteLine("--------------------------------------------------"); WriteLog(e.Conn); Console.WriteLine("--------------------------------------------------"); if(_allowSwitchingClusterNetwork) { if(e.Conn.State == ConnState.CONNECTED) { _switchingClusterNetworkTimer.Stop(); } } } #endregion #region NATS 클라이언트 연결 중단시 처리하기 - natsClient_Disconnected(sender, e) /// <summary> /// NATS 클라이언트 연결 중단시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private static void natsClient_Disconnected(object sender, ConnEventArgs e) { Console.WriteLine(""); Console.WriteLine("이벤트 : DISCONNECTED"); Console.WriteLine("--------------------------------------------------"); WriteLog(e.Conn); Console.WriteLine("--------------------------------------------------"); if(_allowSwitchingClusterNetwork) { if(e.Conn.State == ConnState.RECONNECTING) { _switchingClusterNetworkTimer.Start(); } else { _switchingClusterNetworkTimer.Stop(); } } } #endregion #region NATS 클라이언트 폐쇄시 처리하기 - natsClient_Closed(sender, e) /// <summary> /// NATS 클라이언트 폐쇄시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private static void natsClient_Closed(object sender, ConnEventArgs e) { Console.WriteLine(""); Console.WriteLine("이벤트 : CLOSED"); Console.WriteLine("--------------------------------------------------"); WriteLog(e.Conn); Console.WriteLine("--------------------------------------------------"); } #endregion #region NATS 클라이언트 에러시 처리하기 - natsClient_Error(sender, e) /// <summary> /// NATS 클라이언트 에러시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private static void natsClient_Error(object sender, ErrEventArgs e) { Console.WriteLine(""); Console.WriteLine("이벤트 : ERROR"); Console.WriteLine("--------------------------------------------------"); WriteLog(e.Conn); Console.WriteLine("--------------------------------------------------"); } #endregion #region NATS 클라이언트 수신시 처리하기 - natsClient_Received(sender, e) /// <summary> /// NATS 클라이언트 수신시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private static void natsClient_Received(object sender, ReceivedEventArgs e) { Console.WriteLine(""); Console.WriteLine("이벤트 : RECEIVED"); Console.WriteLine("--------------------------------------------------"); Console.WriteLine(" 서브젝트 : {0}", e.Subject ); Console.WriteLine(" 응답 서브젝트 : {0}", e.Reply ); Console.WriteLine(" 메시지 : {0}", Encoding.UTF8.GetString(e.Data)); Console.WriteLine("--------------------------------------------------"); if(!string.IsNullOrWhiteSpace(e.Reply)) { e.ReplyData = Encoding.UTF8.GetBytes("응답 메시지 : " + DateTime.Now.ToString()); } } #endregion #region 클러스터 네트워크 스위칭 타이머 경과시 처리하기 - switchingClusterNetworkTimer_Elapsed(sender, e) /// <summary> /// 클러스터 네트워크 스위칭 타이머 경과시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private static void switchingClusterNetworkTimer_Elapsed(object sender, ElapsedEventArgs e) { try { if(_natsClient.IsRunning) { _natsClient.Stop(); } ServerInfo serverInfo = null; if(_natsClient.ServerInfo == _primaryServerInfo) { serverInfo = _secondaryServerInfo; } else { serverInfo = _primaryServerInfo; } _natsClient = new NATSClient(serverInfo, _subscriptionInfoArray); _natsClient.ServerDiscovered += natsClient_ServerDiscovered; _natsClient.Reconnected += natsClient_Reconnected; _natsClient.Disconnected += natsClient_Disconnected; _natsClient.Closed += natsClient_Closed; _natsClient.Error += natsClient_Error; _natsClient.Received += natsClient_Received; _natsClient.Start(); Console.WriteLine(""); Console.WriteLine("SERVER CONNECTED"); Console.WriteLine("--------------------------------------------------"); WriteLog(_natsClient.ServerInfo.Connection); Console.WriteLine("--------------------------------------------------"); } finally { _switchingClusterNetworkTimer.Stop(); } } #endregion ////////////////////////////////////////////////////////////////////// Function #region 프로그램 시작하기 - Main() /// <summary> /// 프로그램 시작하기 /// </summary> private static void Main() { #region 주 서버 정보를 설정한다. _primaryServerInfo = new ServerInfo ( null, "user1", "test1234", new string[] { "127.0.0.1:10010", "127.0.0.1:10020" } ); #endregion #region 보조 서버 정보를 설정한다. _secondaryServerInfo = new ServerInfo ( null, "user1", "test1234", new string[] { "127.0.0.1:10030", "127.0.0.1:10040" } ); #endregion #region 구독 정보 배열을 설정한다. _subscriptionInfoArray = null; #endregion #region 클러스터 네트워크 스위칭 타이머를 설정한다. _switchingClusterNetworkTimer = new Timer(); _switchingClusterNetworkTimer.Interval = 1000 * 5; _switchingClusterNetworkTimer.Elapsed += switchingClusterNetworkTimer_Elapsed; #endregion #region NATS 클라이언트를 초기화한다. _natsClient = new NATSClient(_primaryServerInfo, _subscriptionInfoArray); _natsClient.ServerDiscovered += natsClient_ServerDiscovered; _natsClient.Reconnected += natsClient_Reconnected; _natsClient.Disconnected += natsClient_Disconnected; _natsClient.Closed += natsClient_Closed; _natsClient.Error += natsClient_Error; _natsClient.Received += natsClient_Received; #endregion Console.WriteLine("송신 메시지 클라인트를 초기화 했습니다."); #region NATS 클라이언트를 시작한다. try { _natsClient.Start(); } catch { #region NATS 클라이언트 시작시 에러가 발생하면 클러스터 네트워크를 변경해서 NATS 클라이언트를 시작한다. ServerInfo serverInfo = null; if(_natsClient.ServerInfo == _primaryServerInfo) { serverInfo = _secondaryServerInfo; } else { serverInfo = _primaryServerInfo; } _natsClient = new NATSClient(serverInfo, _subscriptionInfoArray); _natsClient.ServerDiscovered += natsClient_ServerDiscovered; _natsClient.Reconnected += natsClient_Reconnected; _natsClient.Disconnected += natsClient_Disconnected; _natsClient.Closed += natsClient_Closed; _natsClient.Error += natsClient_Error; _natsClient.Received += natsClient_Received; _natsClient.Start(); #endregion } #endregion #region 서버 연결 상태를 출력한다. Console.WriteLine(""); Console.WriteLine("SERVER CONNECTED"); Console.WriteLine("--------------------------------------------------"); WriteLog(_natsClient.ServerInfo.Connection); Console.WriteLine("--------------------------------------------------"); #endregion Console.WriteLine("송신 메시지 클라이언트를 시작했습니다."); Console.WriteLine("메시지를 송신하기 위해 아무 키나 눌러 주시기 바랍니다."); Console.ReadKey(true); for(int i = 0; i < 500; i++) { string reply = _natsClient.RequestMessage("foo1", string.Format("테스트 메시지 : {0}", i + 1)); Console.WriteLine(reply); } Console.WriteLine("프로그램을 종료하기 위해 아무 키나 눌러 주시기 바랍니다."); Console.ReadKey(true); _natsClient.Stop(); Console.WriteLine("송신 메시지 클라이언트를 중단했습니다."); } #endregion #region 로그 작성하기 - WriteLog(connection) /// <summary> /// 로그 작성하기 /// </summary> /// <param name="connection">NATS 연결</param> private static void WriteLog(IConnection connection) { Console.WriteLine(" 연결 ID : {0}", connection.ConnectedId ); Console.WriteLine(" 연결 URL : {0}", connection.ConnectedUrl); Console.WriteLine(" 연결 상태 : {0}", connection.State ); Console.WriteLine(" 발견 서버" ); foreach(string server in connection.DiscoveredServers) { Console.WriteLine(" {0}", server); } } #endregion } } |
TestSolution.zip
NATS FOLDER.zip