|
1 | 1 |
|
2 | 2 | using System;
|
| 3 | +using System.Threading; |
3 | 4 | using System.Threading.Tasks;
|
4 | 5 | using System.Timers;
|
5 |
| -using ServiceStack; |
6 | 6 | using ServiceStack.Redis;
|
7 | 7 | using ServiceStack.Text;
|
| 8 | +using Timer = System.Timers.Timer; |
8 | 9 |
|
9 | 10 | namespace TestRedisConnection
|
10 | 11 | {
|
| 12 | + public class Incr |
| 13 | + { |
| 14 | + public long Id { get; set; } |
| 15 | + } |
| 16 | + |
| 17 | + public class IncrResponse |
| 18 | + { |
| 19 | + public long Result { get; set; } |
| 20 | + } |
| 21 | + |
11 | 22 | class Program
|
12 | 23 | {
|
13 |
| - public static BasicRedisClientManager Manager { get; set; } |
| 24 | + private const string Channel = "longrunningtest"; |
| 25 | + private static DateTime StartedAt; |
| 26 | + |
| 27 | + private static long MessagesSent = 0; |
| 28 | + private static long HeartbeatsSent = 0; |
| 29 | + private static long HeartbeatsReceived = 0; |
| 30 | + private static long StartCount = 0; |
| 31 | + private static long StopCount = 0; |
| 32 | + private static long DisposeCount = 0; |
| 33 | + private static long ErrorCount = 0; |
| 34 | + private static long FailoverCount = 0; |
| 35 | + private static long UnSubscribeCount = 0; |
| 36 | + |
| 37 | + public static RedisManagerPool Manager { get; set; } |
| 38 | + public static RedisPubSubServer PubSubServer { get; set; } |
| 39 | + |
14 | 40 | static void Main(string[] args)
|
15 | 41 | {
|
16 |
| - Manager = new BasicRedisClientManager("localhost"); |
| 42 | + Manager = new RedisManagerPool("10.0.0.9"); |
| 43 | + StartedAt = DateTime.UtcNow; |
17 | 44 |
|
18 |
| - var q = new System.Timers.Timer { Interval = 2 }; |
19 |
| - q.Elapsed += CheckConnection; |
| 45 | + var q = new Timer { Interval = 1000 }; |
| 46 | + q.Elapsed += OnInterval; |
20 | 47 | q.Enabled = true;
|
21 | 48 |
|
22 |
| - if ("q" == Console.ReadLine()) |
23 |
| - return; |
| 49 | + using (PubSubServer = new RedisPubSubServer(Manager, Channel) |
| 50 | + { |
| 51 | + OnStart = () => |
| 52 | + { |
| 53 | + Console.WriteLine("OnStart: #" + Interlocked.Increment(ref StartCount)); |
| 54 | + }, |
| 55 | + OnHeartbeatSent = () => |
| 56 | + { |
| 57 | + Console.WriteLine("OnHeartbeatSent: #" + Interlocked.Increment(ref HeartbeatsSent)); |
| 58 | + }, |
| 59 | + OnHeartbeatReceived = () => |
| 60 | + { |
| 61 | + Console.WriteLine("OnHeartbeatReceived: #" + Interlocked.Increment(ref HeartbeatsReceived)); |
| 62 | + }, |
| 63 | + OnMessage = (channel, msg) => |
| 64 | + { |
| 65 | + Console.WriteLine("OnMessage: @" + channel + ": " + msg); |
| 66 | + }, |
| 67 | + OnStop = () => |
| 68 | + { |
| 69 | + Console.WriteLine("OnStop: #" + Interlocked.Increment(ref StopCount)); |
| 70 | + }, |
| 71 | + OnError = ex => |
| 72 | + { |
| 73 | + Console.WriteLine("OnError: #" + Interlocked.Increment(ref ErrorCount) + " ERROR: " + ex); |
| 74 | + }, |
| 75 | + OnFailover = server => |
| 76 | + { |
| 77 | + Console.WriteLine("OnFailover: #" + Interlocked.Increment(ref FailoverCount)); |
| 78 | + }, |
| 79 | + OnDispose = () => |
| 80 | + { |
| 81 | + Console.WriteLine("OnDispose: #" + Interlocked.Increment(ref DisposeCount)); |
| 82 | + }, |
| 83 | + OnUnSubscribe = channel => |
| 84 | + { |
| 85 | + Console.WriteLine("OnUnSubscribe: #" + Interlocked.Increment(ref UnSubscribeCount) + " channel: " + channel); |
| 86 | + }, |
| 87 | + }) |
| 88 | + { |
| 89 | + Console.WriteLine("PubSubServer StartedAt: " + StartedAt.ToLongTimeString()); |
| 90 | + PubSubServer.Start(); |
| 91 | + |
| 92 | + "Press Enter to Quit...".Print(); |
| 93 | + Console.ReadLine(); |
| 94 | + Console.WriteLine("PubSubServer EndedAt: " + DateTime.UtcNow.ToLongTimeString()); |
| 95 | + Console.WriteLine("PubSubServer TimeTaken: " + (DateTime.UtcNow - StartedAt).TotalSeconds + "s"); |
| 96 | + } |
24 | 97 | }
|
25 | 98 |
|
26 |
| - private static void CheckConnection(object sender, ElapsedEventArgs e) |
| 99 | + private static void OnInterval(object sender, ElapsedEventArgs e) |
27 | 100 | {
|
28 |
| - Task.Factory.StartNew(CheckThisConnection); |
| 101 | + Task.Factory.StartNew(PublishMessage); |
29 | 102 | }
|
30 | 103 |
|
31 |
| - private static void CheckThisConnection() |
| 104 | + private static void PublishMessage() |
32 | 105 | {
|
33 | 106 | try
|
34 | 107 | {
|
35 |
| - "CheckThisConnection()...".Print(); |
36 |
| - using (var redisClient = Manager.GetClient()) |
| 108 | + var message = "MSG: #" + Interlocked.Increment(ref MessagesSent); |
| 109 | + Console.WriteLine("PublishMessage(): " + message); |
| 110 | + using (var redis = Manager.GetClient()) |
37 | 111 | {
|
38 |
| - using (var trans = redisClient.CreateTransaction()) |
39 |
| - { |
40 |
| - trans.QueueCommand( |
41 |
| - r => r.SetEntryInHash("Test", "Price", "123")); |
42 |
| - trans.QueueCommand( |
43 |
| - r => r.SetEntryInHash("Test2", "Price", "123")); |
44 |
| - trans.QueueCommand( |
45 |
| - r => r.SetEntryInHash("Test3", "Price", "123")); |
46 |
| - trans.QueueCommand( |
47 |
| - r => r.SetEntryInHash("Test4", "Price", "123")); |
48 |
| - trans.QueueCommand( |
49 |
| - r => r.SetEntryInHash("Test5", "Price", "123")); |
50 |
| - trans.QueueCommand( |
51 |
| - r => r.SetEntryInHash("Test6", "Price", "123")); |
52 |
| - trans.QueueCommand( |
53 |
| - r => r.SetEntryInHash("Test7", "Price", "123")); |
54 |
| - trans.QueueCommand( |
55 |
| - r => r.SetEntryInHash("Test8", "Price", "123")); |
56 |
| - trans.QueueCommand( |
57 |
| - r => r.SetEntryInHash("Test9", "Price", "123")); |
58 |
| - trans.QueueCommand( |
59 |
| - r => r.SetEntryInHash("Test10", "Price", "123")); |
60 |
| - trans.QueueCommand( |
61 |
| - r => r.SetEntryInHash("Test11", "Price", "123")); |
62 |
| - trans.QueueCommand( |
63 |
| - r => r.SetEntryInHash("Test12", "Price", "123")); |
64 |
| - trans.QueueCommand( |
65 |
| - r => r.SetEntryInHash("Test13", "Price", "123")); |
66 |
| - trans.QueueCommand( |
67 |
| - r => r.SetEntryInHash("Test14", "Price", "123")); |
68 |
| - trans.QueueCommand( |
69 |
| - r => r.SetEntryInHash("Test15", "Price", "123")); |
70 |
| - trans.QueueCommand( |
71 |
| - r => r.SetEntryInHash("Test16", "Price", "123")); |
72 |
| - trans.QueueCommand( |
73 |
| - r => r.SetEntryInHash("Test17", "Price", "123")); |
74 |
| - trans.QueueCommand( |
75 |
| - r => r.SetEntryInHash("Test18", "Price", "123")); |
76 |
| - trans.QueueCommand( |
77 |
| - r => r.SetEntryInHash("Test19", "Price", "123")); |
78 |
| - trans.Commit(); |
79 |
| - } |
| 112 | + redis.PublishMessage(Channel, message); |
80 | 113 | }
|
81 | 114 | }
|
82 | 115 | catch (Exception ex)
|
83 | 116 | {
|
84 |
| - "ERROR: {0}".Print(ex.ToString()); |
| 117 | + Console.WriteLine("ERROR PublishMessage: " + ex); |
85 | 118 | }
|
86 | 119 | }
|
87 | 120 | }
|
|
0 commit comments