RabbitMq.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using MassTransit;
  5. using MassTransit.Util;
  6. using System.Data;
  7. using Microsoft.Extensions.Configuration;
  8. using Dapper;
  9. namespace WK_KLI_LAPS_COMMONTOOL_Service.App_Data
  10. {
  11. public class RabbitMq
  12. {
  13. public bool SendMq(string Tranid, string UserId, string AppType, string StageType, string Enviroment)
  14. {
  15. string ServiceAddress = "";
  16. string RabbitMQHost = "";
  17. string userName = "";
  18. string password = "";
  19. // BLDatamanipulation db = new BLDatamanipulation();
  20. using (IDbConnection dbConnection = ConnectionBase.Connection)
  21. {
  22. dbConnection.Open();
  23. DynamicParameters param = new DynamicParameters();
  24. param.Add("p_tranid", dbType: DbType.Int32, value: Tranid, direction: ParameterDirection.Input);
  25. param.Add("p_lotid", dbType: DbType.Int32, value: UserId, direction: ParameterDirection.Input);
  26. param.Add("p_option", dbType: DbType.String, value: "P", direction: ParameterDirection.Input);
  27. param.Add("p_option", dbType: DbType.String, value: AppType, direction: ParameterDirection.Input);
  28. var Checkinapp = dbConnection.Query<object>("public.spusrlottranupdatev1", param, commandType: CommandType.StoredProcedure).ToList();
  29. //var Checkinapp = db.GetDataTable(db_, "select * from usr_lot_tran_update_v1(" + Tranid + ", " + UserId + ", '" + "P" + "', '" + AppType + "') as dd", null);
  30. //--uncomment and check start------
  31. //if (Checkinapp.Rows.Count > 0)
  32. //{
  33. // for (var item = 0; item <= Checkinapp.Rows.Count - 1; item++)
  34. // {
  35. // checkinprocess obj = JsonConvert.DeserializeObject<checkinprocess>(Checkinapp.Rows[item][0].ToString());
  36. // if (obj.ipaddress != null)
  37. // {
  38. // ServiceAddress = obj.ipaddress + "" + "/" + obj.root_path;
  39. // RabbitMQHost = obj.ipaddress;
  40. // userName = obj.username;
  41. // password = obj.password;
  42. // StageType = obj.stagetype;
  43. // }
  44. // else
  45. // {
  46. // return false;
  47. // }
  48. // }
  49. //}
  50. //--uncomment and check end------
  51. if (StageType != "server")
  52. return false;
  53. }
  54. //ConfigureLogger();
  55. // Log4NetLogger.Use();
  56. IBusControl busControl = CreateBus(RabbitMQHost, userName, password);
  57. TaskUtil.Await(() => busControl.StartAsync());
  58. try
  59. {
  60. IRequestClient<ISimpleRequest, ISimpleResponse> client = CreateRequestClient(busControl, ServiceAddress);
  61. var response = client.Request(new SimpleRequest(DateTime.Now.ToString(), Tranid, UserId, AppType, StageType, Enviroment, null, ""));
  62. System.Threading.Thread.Sleep(500);
  63. //client = null;
  64. }
  65. catch (Exception ex)
  66. {
  67. return false;
  68. }
  69. finally
  70. {
  71. busControl.Stop();
  72. }
  73. return true;
  74. }
  75. public bool SendMqMail(string Tranid, string JobWorkflowid, string flag, string curtranid, string userid)
  76. {
  77. string ServiceAddress = "";
  78. string RabbitMQHost = "";
  79. string userName = "";
  80. string password = "";
  81. ServiceAddress = Startup.Configuration.GetValue<string>("AppSettings:mailqueue_path") + "" + "/" + Startup.Configuration.GetValue<string>("AppSettings:mailqueue"); //obj.ipaddress + "" + " /" + obj.root_path;
  82. RabbitMQHost = Startup.Configuration.GetValue<string>("AppSettings:mailqueue_path"); //obj.ipaddress;
  83. userName = "admin"; //obj.username;
  84. password = "admin"; //obj.password;
  85. //ConfigureLogger();
  86. // Log4NetLogger.Use();
  87. IBusControl busControl = CreateBus(RabbitMQHost, userName, password);
  88. TaskUtil.Await(() => busControl.StartAsync());
  89. try
  90. {
  91. //p.TransId => tranid
  92. //p.AppType => jobworkflowid
  93. //p.StageType => sflag
  94. //p.Enviroment => curtranid
  95. //p.UserId => userid_
  96. DynamicParams dynamicparams = new DynamicParams()
  97. {
  98. jobworkflowid = JobWorkflowid,
  99. nexttranid = curtranid
  100. };
  101. List<DynamicParams> dynamicparams_ = new List<DynamicParams>();
  102. dynamicparams_.Add(dynamicparams);
  103. IRequestClient<ISimpleRequest, ISimpleResponse> client = CreateRequestClient(busControl, ServiceAddress);
  104. var response = client.Request(new SimpleRequest(DateTime.Now.ToString(), Tranid, userid, "", "", Startup.Configuration.GetValue<string>("AppSettings:envirnmentname"), dynamicparams, flag));
  105. System.Threading.Thread.Sleep(500);
  106. //client = null;
  107. }
  108. catch (Exception ex)
  109. {
  110. return false;
  111. }
  112. finally
  113. {
  114. busControl.Stop();
  115. }
  116. return true;
  117. }
  118. public IRequestClient<ISimpleRequest, ISimpleResponse> CreateRequestClient(IBusControl busControl, string ServiceAddress)
  119. {
  120. var serviceAddress = new Uri(ServiceAddress);
  121. IRequestClient<ISimpleRequest, ISimpleResponse> client =
  122. busControl.CreateRequestClient<ISimpleRequest, ISimpleResponse>(serviceAddress, TimeSpan.FromSeconds(10));
  123. return client;
  124. }
  125. public IBusControl CreateBus(string RabbitMQHost, string userName, string password)
  126. {
  127. return Bus.Factory.CreateUsingRabbitMq(x => x.Host(new Uri(RabbitMQHost), h =>
  128. {
  129. h.Username(userName);
  130. h.Password(password);
  131. }));
  132. }
  133. static void ConfigureLogger()
  134. {
  135. // const string logConfig = @"<?xml version=""1.0"" encoding=""utf-8"" ?>
  136. //<log4net>
  137. // <root>
  138. // <level value=""INFO"" />
  139. // <appender-ref ref=""console"" />
  140. // </root>
  141. // <appender name=""console"" type=""log4net.Appender.ColoredConsoleAppender"">
  142. // <layout type=""log4net.Layout.PatternLayout"">
  143. // <conversionPattern value=""%m%n"" />
  144. // </layout>
  145. // </appender>
  146. //</log4net>";
  147. // using (var stream = new MemoryStream(Encoding.UTF8.GetBytes(logConfig)))
  148. // {
  149. // XmlConfigurator.Configure(stream);
  150. // }
  151. }
  152. }
  153. }
  154. public interface ISimpleResponse
  155. {
  156. string CusomerName { get; }
  157. }
  158. public interface ISimpleRequest
  159. {
  160. DateTime Timestamp { get; }
  161. string CustomerId { get; }
  162. string TransId { get; set; }
  163. string UserId { get; set; }
  164. string AppType { get; set; }
  165. string StageType { get; set; }
  166. string Enviroment { get; set; }
  167. object DynamicList { get; set; }
  168. string Flag { get; set; }
  169. }
  170. class SimpleRequest
  171. : ISimpleRequest
  172. {
  173. readonly string _customerId;
  174. readonly string _TransId;
  175. readonly string _UserId;
  176. readonly string _AppType;
  177. readonly string _StageType;
  178. readonly string _Enviroment;
  179. readonly DateTime _timestamp;
  180. readonly object _DynamicList;
  181. readonly string _Flag;
  182. public SimpleRequest(string customerId, string Tranid, string UserId, string AppType, string StageType, string Enviroment, object DynamicList, string Flag)
  183. {
  184. _customerId = customerId;
  185. _TransId = Tranid;
  186. _UserId = UserId;
  187. _AppType = AppType;
  188. _StageType = StageType;
  189. _Enviroment = Enviroment;
  190. _timestamp = DateTime.UtcNow;
  191. _DynamicList = DynamicList;
  192. _Flag = Flag;
  193. }
  194. public DateTime Timestamp
  195. {
  196. get { return _timestamp; }
  197. }
  198. public string TransId
  199. {
  200. get { return _TransId; }
  201. }
  202. public string CustomerId
  203. {
  204. get { return _customerId; }
  205. }
  206. public string UserId
  207. {
  208. get { return _UserId; }
  209. }
  210. public string AppType
  211. {
  212. get { return _AppType; }
  213. }
  214. public string StageType
  215. {
  216. get { return _StageType; }
  217. }
  218. public string Enviroment
  219. {
  220. get { return _Enviroment; }
  221. }
  222. public object DynamicList
  223. {
  224. get { return _DynamicList; }
  225. }
  226. public string Flag
  227. {
  228. get { return _Flag; }
  229. }
  230. string ISimpleRequest.TransId
  231. {
  232. get
  233. {
  234. throw new NotImplementedException();
  235. }
  236. set
  237. {
  238. throw new NotImplementedException();
  239. }
  240. }
  241. string ISimpleRequest.UserId
  242. {
  243. get
  244. {
  245. throw new NotImplementedException();
  246. }
  247. set
  248. {
  249. throw new NotImplementedException();
  250. }
  251. }
  252. string ISimpleRequest.AppType
  253. {
  254. get
  255. {
  256. throw new NotImplementedException();
  257. }
  258. set
  259. {
  260. throw new NotImplementedException();
  261. }
  262. }
  263. string ISimpleRequest.StageType
  264. {
  265. get
  266. {
  267. throw new NotImplementedException();
  268. }
  269. set
  270. {
  271. throw new NotImplementedException();
  272. }
  273. }
  274. string ISimpleRequest.Enviroment
  275. {
  276. get
  277. {
  278. throw new NotImplementedException();
  279. }
  280. set
  281. {
  282. throw new NotImplementedException();
  283. }
  284. }
  285. object ISimpleRequest.DynamicList
  286. {
  287. get
  288. {
  289. throw new NotImplementedException();
  290. }
  291. set
  292. {
  293. throw new NotImplementedException();
  294. }
  295. }
  296. string ISimpleRequest.Flag
  297. {
  298. get
  299. {
  300. throw new NotImplementedException();
  301. }
  302. set
  303. {
  304. throw new NotImplementedException();
  305. }
  306. }
  307. }
  308. public class checkinprocess
  309. {
  310. public Int32? jobworkflowid { get; set; }
  311. public Int32? isexe { get; set; }
  312. public Int32? isservice { get; set; }
  313. public Int32? applicationserverid { get; set; }
  314. public string paramss { get; set; }
  315. public string ipaddress { get; set; }
  316. public string appname { get; set; }
  317. public string stagetype { get; set; }
  318. public string file_movement { get; set; }
  319. public string root_path { get; set; }
  320. public Int32? stageorder { get; set; }
  321. public string checkout_app { get; set; }
  322. public Int32? appid { get; set; }
  323. public string username { get; set; }
  324. public string password { get; set; }
  325. }
  326. public class DynamicParams
  327. {
  328. public string jobworkflowid { get; set; }
  329. public string nexttranid { get; set; }
  330. }