forked from bluedoctor/MSF-DistTransExample
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
200 lines (174 loc) · 8.02 KB
/
Program.cs
File metadata and controls
200 lines (174 loc) · 8.02 KB
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
using DistTransDto;
using DistTransServices;
using PWMIS.EnterpriseFramework.Service.Basic;
using PWMIS.EnterpriseFramework.Service.Client;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DistTransClient
{
class Program
{
static void Main(string[] args)
{
DistTrans3PCState state ;//= DistTrans3PCState.Rep_Yes_1PC;
if (typeof(DistTrans3PCState).IsEnum)
{
state = (DistTrans3PCState)Enum.Parse(typeof(DistTrans3PCState), "Rep_Yes_1PC");
}
Proxy client = new Proxy();
client.ErrorMessage += client_ErrorMessage;
Console.Write("请输入服务器的主机名或者IP地址(默认 127.0.0.1):");
string host = Console.ReadLine();
if (string.IsNullOrEmpty(host))
host = "127.0.0.1";
Console.WriteLine("服务地址:{0}", host);
Console.Write("请输入服务的端口号(默认 8888),如果是订单服务,请输入 12308:");
string port = Console.ReadLine();
if (string.IsNullOrEmpty(port))
port = "8888";
Console.WriteLine("服务端口号:{0}", port);
client.ServiceBaseUri = string.Format("net.tcp://{0}:{1}", host, port);
Console.WriteLine("当前客户端代理的服务基础地址是:{0}", client.ServiceBaseUri);
Console.WriteLine();
Console.WriteLine("MSF 分布式事务 模式调用示例:");
//TestSample(client);
TestCreateOrder(client);
Console.ReadLine();
}
private static void TestCreateOrder(Proxy client)
{
List<BuyProductDto> buyProducts = new List<BuyProductDto>();
buyProducts.Add(new BuyProductDto() { ProductId=1, BuyNumber=3});
buyProducts.Add(new BuyProductDto() { ProductId =2, BuyNumber = 1 });
int orderId = 8000;
int userId = 100;
ServiceRequest request = new ServiceRequest();
request.ServiceName = "OrderService";
request.MethodName = "CreateOrder";
request.Parameters = new object[] { orderId,userId, buyProducts };
bool result=client.RequestServiceAsync<bool>(request).Result;
if(result)
Console.WriteLine("创建订单成功,订单号:{0}",orderId);
else
Console.WriteLine("创建订单失败,订单号:{0}", orderId);
Console.WriteLine("------开始并发下单测试,按任意键继续---------");
Console.ReadLine();
System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();
sw.Start();
int taskCount = 10;
List<Task> tasks = new List<Task>();
for (int i = 1; i <= taskCount; i++)
{
Proxy client1 = new Proxy();
client1.ServiceBaseUri = client.ServiceBaseUri;
ServiceRequest request1 = new ServiceRequest();
request1.ServiceName = "OrderService";
request1.MethodName = "CreateOrder";
request1.Parameters = new object[] { orderId+i, userId, buyProducts };
var task = client1.RequestServiceAsync<bool>(request1);
tasks.Add(task);
Console.WriteLine("添加第 {0}个任务.",i);
}
Console.WriteLine("{0} 个订单请求任务创建完成,开始等待所有任务执行完成!",taskCount);
Task.WaitAll(tasks.ToArray());
Console.WriteLine("所有任务执行完成!");
sw.Stop();
Console.WriteLine("总耗时:{0}(s),TPS:{1}",sw.Elapsed.TotalSeconds,(double)taskCount /sw.Elapsed.TotalSeconds);
}
private static void TestSample(Proxy client)
{
/*
ServiceRequest request = new ServiceRequest();
request.ServiceName = "DTCService";
request.MethodName = "RegisterTransaction";
request.Parameters = new object[] { "1234567890" };
client.Subscribe<string, DistTrans3PCState, DistTrans3PCState>(request,
s =>
{
Console.WriteLine("ServerMessage:{0}", s);
client.SendTextMessage("client ....");
},
para => {
Console.WriteLine("服务器回调消息:{0}",para);
Console.Write("事务是否执行成功?(yes/no)");
string repMsg1 = Console.ReadLine();
if (repMsg1 == "yes")
return DistTrans3PCState.Rep_Yes_1PC;
else
return DistTrans3PCState.Rep_No_1PC;
}
);
*/
ServiceRequest request = new ServiceRequest();
request.ServiceName = "DemoService";
request.MethodName = "TransactionTest";
request.Parameters = new object[] { 3, 2 };
/*
Task<bool> task = client.RequestServiceAsync<bool, string, string>(request,
s =>
{
Console.WriteLine("接收到服务器指令:{0}", s);
Console.WriteLine("即将回复服务器,请输入回复内容(yes/no/错误信息),也可以直接关闭本进程。");
string rep = Console.ReadLine();
if (s == "CanCommit")
{
Console.WriteLine("回复结果:是否可以提交:{0}", rep);
}
else if (s == "DoCommit")
{
Console.WriteLine("回复结果:提交是否成功:{0}", rep);
}
Console.WriteLine();
return rep;
}
);
try
{
task.Wait();
Console.WriteLine("服务访问完成,结果:{0}", task.Result);
}
catch (Exception ex)
{
Console.WriteLine("访问服务错误:{0}",ex.Message);
}
*/
client.RequestService<bool, string, string>(request.ServiceUrl, PWMIS.EnterpriseFramework.Common.DataType.Text,
r =>
{
Console.WriteLine("服务访问完成,结果:{0}", r);
},
s =>
{
Console.WriteLine("接收到服务器指令:{0}", s);
Console.WriteLine("即将回复服务器,请输入回复内容(yes/no/错误信息),也可以直接关闭本进程。");
string rep = Console.ReadLine();
if (s == "CanCommit")
{
Console.WriteLine("回复结果:是否可以提交:{0}", rep);
}
else if (s == "DoCommit")
{
Console.WriteLine("回复结果:提交是否成功:{0}", rep);
}
Console.WriteLine();
return rep;
});
System.Threading.Thread.Sleep(1000 * 60 * 60);
string repMsg = "ok";
while (repMsg != "")
{
Console.Write("回复服务器(输入为空,则退出):>>");
repMsg = Console.ReadLine();
client.SendTextMessage(repMsg);
}
}
static void client_ErrorMessage(object sender, MessageSubscriber.MessageEventArgs e)
{
//如果是分布式事务,这里应该回滚
Console.WriteLine("请求服务器错误:{0}", e.MessageText);
}
}
}