TWX A good example of multi-threaded use

Source: Internet
Author: User

Using Newtonsoft.json;
Using System;
Using System.Collections.Generic;
Using System.Configuration;
Using System.Linq;
Using System.Text;
Using System.Threading;
Using TRS. EXPORT.BLL;
Using TRS. export.business;
Using TRS.Export.Common;
Using TRS. Export.constant;
Using TRS. export.entity;
Using TRS. Export.FrameEntity.Enums;
Using TRS. Export.frameprovider;
Using TRS. Export.Param.Bases;
Using TRS. Export.Param.Enums;
Using TRS. Export.Scheduler.Interfaces;

Namespace TRS. Export.Scheduler.Schedulers.Business
{
public class Businesstaobaoeventscheduler:ischeduler
{
Private readonly TBD_EVENTLOGBLL M_OBJEVENTLOGBLL = new TBD_EVENTLOGBLL ();

Private readonly taobaoeventbusiness m_objeventbusiness = new taobaoeventbusiness ();

Private readonly backuptablebusiness m_objbackupbusiness = new backuptablebusiness ();

Private readonly String m_httpresolve = configurationmanager.appsettings["Httpresolve"];

Private readonly String m_pcsreceivecodes = configurationmanager.appsettings["Pcsreceivecodes"];

Private Const int top = 1000;

Private Orderbyenum orderbyenum = ORDERBYENUM.ASC;

delegate void Asyncexecute (object obj);

public int Sleepinterval {get; set;}

Public string[] Args {get; set;}

Public Businesstaobaoeventscheduler ()
{
Sleepinterval = $;
}
//business.businesstaobaoeventscheduler| logistics_dispatched# suffix $0,1,2,3,4/5,6,7,8,9
public void Execute ()
{
if (Args = = NULL | | Args.length = = 0)
{
return;
}
//args=logistics_dispatched# suffix $0,1,2,3,4/5,6,7,8,9
//eventtype=logistics_dispatched# suffix $0,1,2,3,4/ 5,6,7,8,9
string[] EventType = args[0]. Split (' | ');
if (EventType = = NULL | | eventtype.length = = 0)
{
return;
}

if (eventtype.length = = 1)
{
if (eventtype[0]. IndexOf ("#") < 0)//eventtype sub-thread
{
Executebyeventtype (eventtype[0]);
}
Else if (eventtype[0]. IndexOf ("suffix") >-1)//To Eventtype+logid last split thread
{//array=0,1,2,3,4/5,6,7,8,9
string[] array = eventtype[0]. Substring (Eventtype[0]. IndexOf ("suffix") + 3). Split ('/');
for (int i = 0; i < array. Length; i++)
{
if (Staticconstant.async)
{
Asyncexecute ASYNC = new Asyncexecute (executebysuffix);

Async. BeginInvoke (new string[] {eventtype[0]. Split (' # ') [0], Array[i]}, AsyncCallback, async);
}
Else
{
Thread thread = new Thread (new Parameterizedthreadstart (Executebysuffix));
Thread. Name = string. Format ("Message parsing background job-{0}", Array[i]);
Thread. Start (new string[] {eventtype[0]. Split (' # ') [0], Array[i]});
}
}
}
Else if (eventtype[0]. IndexOf ("ground") >-1)//with Eventtype+destinationname sub-thread
{
string[] array = eventtype[0]. Substring (Eventtype[0]. IndexOf ("land") + 3). Split ('/');
for (int i = 0; i < array. Length; i++)
{
if (Staticconstant.async)
{
Asyncexecute ASYNC = new Asyncexecute (executebydestinationname);

Async. BeginInvoke (new string[] {eventtype[0]. Split (' # ') [0], Array[i]}, AsyncCallback, async);
}
Else
{
Thread thread = new Thread (new Parameterizedthreadstart (Executebydestinationname));
Thread. Name = string. Format ("Message parsing background job-{0}", Eventtype[0]. Split (' # ') [0] + "-" + array[i]);
Thread. Start (new string[] {eventtype[0]. Split (' # ') [0], Array[i]});
}
}
}
}
Else
{
for (int i = 0; i < eventtype.length; i++)//EventType thread
{
if (Staticconstant.async)
{
Asyncexecute async = new Asyncexecute (executebyeventtype);

Async. BeginInvoke (Eventtype[i], AsyncCallback, async);
}
Else
{
Thread thread = new Thread (new Parameterizedthreadstart (Executebyeventtype));
Thread. Name = string. Format ("Message parsing background job-{0}", Eventtype[i]);
Thread. Start (Eventtype[i]);
}
}
}

Console.readkey ();
}

public void AsyncCallback (IAsyncResult result)
{
Asyncexecute async = (Asyncexecute) result. asyncstate;

Async. EndInvoke (result);
}

public void Executebyeventtype (object obj)
{
while (true)
{
Try
{
String threadname = String. Format ("Message parsing background job-{0}", obj);

New Threadmonitorbusiness (). Callbackthread (ThreadName);

wherehelper<tbd_eventlog> where = new wherehelper<tbd_eventlog> ();
where. Apeendand (A = A.successflag = = 0 && a.actiontimes < 5);
where. Apeendand (A = A.eventtype = = obj. ToString ());

orderbyhelper<tbd_eventlog> = new Orderbyhelper<tbd_eventlog> (a = A.actiontime, orderByEnum);

list<tbd_eventlog> Lsteventlog = M_objeventlogbll.select (where, top, null, by-and-by);
if (Lsteventlog = = NULL | | lsteventlog.count = = 0)
{
Console.WriteLine ("Current thread: [{0}], no data, wait {1} seconds to continue ... {2} ", ThreadName, DateTime.Now);
Thread.Sleep (60 * 1000);

Continue
}

foreach (var item in Lsteventlog)
{
Execute (item);

Console.WriteLine ("Current thread: [{0}], wait {1} seconds to continue ... {2} ", ThreadName, 10/1000.00, DateTime.Now);
Thread.Sleep (10);
}

Console.WriteLine ("Current thread: [{0}], wait {1} seconds to continue ... {2} ", ThreadName, sleepinterval/1000, DateTime.Now);

Thread.Sleep (Sleepinterval);
}
catch (Exception ex)
{
New Emailbusiness (). Sendexceptionemail (ex);
}
}
}

public void Executebydestinationname (object obj)
{
while (true)
{
Try
{
string[] array = obj as string[];
if (array = = NULL | | array. Length < 2)
{
Break
}

String threadname = String. Format ("Message parsing background Job-{0}-{1}", Array[0], array[1]);

New Threadmonitorbusiness (). Callbackthread (ThreadName);

wherehelper<tbd_eventlog> where = new wherehelper<tbd_eventlog> ();
where. Apeendand (A = A.successflag = = 0 && a.actiontimes < 5);
if (array. Length > 0)
{
where. Apeendand (A = A.eventtype = = Array[0]);
}
if (array. Length > 1)
{
where. Apeendand (A = a.destinationname.in (array[1). Split (', ')));
}

orderbyhelper<tbd_eventlog> = new Orderbyhelper<tbd_eventlog> (a = A.actiontime, orderByEnum);

list<tbd_eventlog> Lsteventlog = M_objeventlogbll.select (where, top, null, by-and-by);
if (Lsteventlog = = NULL | | lsteventlog.count = = 0)
{
Console.WriteLine ("Current thread: [{0}], no data, wait {1} seconds to continue ... {2} ", ThreadName, DateTime.Now);
Thread.Sleep (60 * 1000);

Continue
}

foreach (var item in Lsteventlog)
{
Execute (item);

Console.WriteLine ("Current thread: [{0}], wait {1} seconds to continue ... {2} ", ThreadName, 10/1000.00, DateTime.Now);
Thread.Sleep (10);
}

Console.WriteLine ("Current thread: [{0}], wait {1} seconds to continue ... {2} ", ThreadName, sleepinterval/1000, DateTime.Now);

}
catch (Exception ex)
{
New Emailbusiness (). Sendexceptionemail (ex);
}
}
}

public void Executebysuffix (object obj)
{
while (true)
{
Try
{
string[] array = obj as string[];
if (array = = NULL | | array. Length < 2)
{
Break
}

String threadname = String. Format ("Message parsing background Job-{0}-{1}", Array[0], array[1]);

New Threadmonitorbusiness (). Callbackthread (ThreadName);

wherehelper<tbd_eventlog> where = new wherehelper<tbd_eventlog> ();
where. Apeendand (A = A.actiontime < DateTime.Now);
where. Apeendand (A = A.successflag = = 0 && a.actiontimes < 5);
if (array. Length > 0)
{
where. Apeendand (A = A.eventtype = = Array[0]);
}
if (array. Length > 1)
{

Small measures to prevent concurrent processing
where. Apeendand (A = A.logid.right (array[1). Split (', ') ));
}

orderbyhelper<tbd_eventlog> = new Orderbyhelper<tbd_eventlog> (a = A.actiontime, orderByEnum);

list<tbd_eventlog> Lsteventlog = M_objeventlogbll.select (where, top, null, by-and-by);
if (Lsteventlog = = NULL | | lsteventlog.count = = 0)
{
Console.WriteLine ("Current thread: [{0}], no data, wait {1} seconds to continue ... {2} ", ThreadName, DateTime.Now);
Thread.Sleep (60 * 1000);

Continue
}

foreach (var item in Lsteventlog)
{
Execute (item);

Console.WriteLine ("Current thread: [{0}], wait {1} seconds to continue ... {2} ", ThreadName, 10/1000.00, DateTime.Now);
Thread.Sleep (10);
}

Console.WriteLine ("Current thread: [{0}], wait {1} seconds to continue ... {2} ", ThreadName, sleepinterval/1000, DateTime.Now);

}
catch (Exception ex)
{
New Emailbusiness (). Sendexceptionemail (ex);
}
}
}

public void Execute (Tbd_eventlog EventLog)
{
System.Diagnostics.Stopwatch watch = new System.Diagnostics.Stopwatch ();
Watch. Start ();

bool PCs = false;
if (!m_pcsreceivecodes.isnullorempty ())
{
String[] codes = m_pcsreceivecodes.split (' | ');
foreach (var code in codes)
{
if (eventLog.EventContent.IndexOf (Code) >-1)
{
pcs = true;
}
}
}

if (PCS)
{
string eventcontent = Eventlog.eventcontent;
String destinationname = Stringhelper.getvaluebycutstr (ref eventcontent, "<consoWarehouseCode>", "</ Consowarehousecode> ", false, false);

Executesuccess (EventLog.LogID.Value, 1, destinationname);

Console.WriteLine ("Current Order is pcs:[{0}", wait {1} seconds to continue ... {2} ", Eventlog.tradeorderid, 000/1000.00, DateTime.Now);

Return
}
Else
{

}

Responseparam response = null;

if (!m_httpresolve.isnullorempty ())
{
string result = new Httphelper (). Execute (M_httpresolve, "{\" logid\ ":" + Eventlog.logid + ", \" appkey\ ": \" Trtaobaov02\ "}");
if (!result. IsNullOrEmpty ())
{
Response = jsonconvert.deserializeobject<responseparam> (result);
}
}
Else
{
Response = M_objeventbusiness.tabao_event (EVENTLOG);
}

Watch. Stop ();

if (response = = NULL | |!response.success)
{
if (Response.result = = "2" && eventLog.ActionTimes.Value > 3)
{
Executesuccess (EventLog.LogID.Value);
}
Else
{
Executefailure (EventLog.LogID.Value, EventLog.ActionTimes.Value, response.msg);
}

Console.WriteLine ("Trade order: {0} analysis failed! Time-consuming: {1} {2} {3} ", Eventlog.tradeorderid, watch. elapsedmilliseconds/1000.00, Response = = null? "": Response.msg, DateTime.Now);
}
Else
{
Executesuccess (EventLog.LogID.Value);

Console.WriteLine ("Trade order: {0} analysis succeeded! Time-consuming: {1} {2} {3} ", Eventlog.tradeorderid, watch. elapsedmilliseconds/1000.00, Response = = null? "": Response.msg, DateTime.Now);
}
}

public void Executefailure (long logid, int actiontimes, string exception)
{
Tbd_eventlog update = new Tbd_eventlog ();
Update. Logid = Logid;
Update. Actiontimes = actiontimes + 1;
Update. Exception = Exception;
Update. Actiontime = DateTime.Now;
if (Actiontimes >= 4)
{
Update. Successflag =-1;
}

M_objeventlogbll.update (Update);
}

public void executesuccess (long logid, int it = 0, string destinationname = null)
{
Tbd_eventlog update = new Tbd_eventlog ();
Update. Logid = Logid;
Update. Successflag = 1;
Update. Actiontime = DateTime.Now;
update.it = IT;
if (!destinationname.isnullorempty ())
{
Update. Destinationname = Destinationname;
}

M_objeventlogbll.update (Update);

Responseparam response = m_objbackupbusiness.backuptable (update);
if (response.success = = False)
{
Update = new Tbd_eventlog ();
Update. Logid = Logid;
Update. UpdateTime = DateTime.Now;
Update. Exception = response.msg?? "";

M_objeventlogbll.update (Update);
}
}
}
}

TWX A good example of multi-threaded use

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.