标签:
[Serializable] public class Order { public string OrderID { get; set; } public int MemberID { get; set; } public string GoodsID { get; set; } public DateTime SubmitTime { get; set; } }
public class BaseQueue: IDisposable { protected MessageQueue queue; public BaseQueue(string queuePath) { queue = new MessageQueue(queuePath); } /// <summary> /// 出队列 /// </summary> public virtual object Receive() { try { using (Message message = queue.Receive()) { message.Formatter = queue.Formatter; return message; } } catch (MessageQueueException mqex) { throw; } } /// <summary> /// 入队列 /// </summary> public virtual void Send(object msg) { queue.Send(msg); } public void Dispose() { queue.Dispose(); } }
public class OrderQueue : BaseQueue { // Path example - FormatName:DIRECT=OS:MyMachineName\Private$\OrderQueueName private static readonly string queuePath = ".\\private$\\orderqueue"; public OrderQueue() : base(queuePath) { // 可以设置序列化格式 queue.Formatter = new BinaryMessageFormatter(); } /// <summary> /// 从消息队列出一个订单对象,先入先出 /// </summary> public Order Receive() { object obj = base.Receive(); return (Order)((Message)obj).Body; } /// <summary> /// 把一个订单对象压入消息队列 /// </summary> public void Send(Order orderMessage) { base.Send(orderMessage); } }
Order or = new Order(); or.OrderID = DateTime.Now.ToString("yyyyMMddHHmmss"); or.MemberID = int.Parse(TextBox2.Text.Trim()); or.GoodsID = DropDownList1.SelectedItem.Text.ToString(); or.SubmitTime = DateTime.Now; try { OrderQueue orQ = new OrderQueue(); orQ.Send(or); Label1.Text = "提交成功!"; } catch (Exception ex) { Label1.Text = "提交失败!详细原因:" + ex.Message; }
private static int batchSize = 4;//int.Parse(ConfigurationManager.AppSettings["BatchSize"]); private static int threadCount = 1;//int.Parse(ConfigurationManager.AppSettings["ThreadCount"]); Thread[] workerThreads = new Thread[threadCount]; private static int totalOrdersProcessed = 0; public Form1() { InitializeComponent(); } //启动线程 private void button1_Click(object sender, EventArgs e) { Thread workTicketThread; for (int i = 0; i < threadCount; i++) { workTicketThread = new Thread(new ThreadStart(ProcessOrders)); // Make this a background thread, so it will terminate when the main thread/process is de-activated workTicketThread.IsBackground = true; workTicketThread.SetApartmentState(ApartmentState.STA); // Start the Work workTicketThread.Start(); workerThreads[i] = workTicketThread; } MessageBox.Show("starting..."); } //结束线程 private void button2_Click(object sender, EventArgs e) { //abort all threads for (int i = 0; i < workerThreads.Length; i++) { workerThreads[i].Abort(); } MessageBox.Show(totalOrdersProcessed + " Orders processed."); } /// <summary> /// Process a batch of asynchronous orders from the queue and submit them to the database within a transaction /// </summary> private static void ProcessOrders() { OrderQueue orQueue = new OrderQueue(); while (true) { int processedItems = 0; ArrayList queueOrders = new ArrayList(); for (int j = 0; j < batchSize; j++) { try { queueOrders.Add(orQueue.Receive()); } catch (Exception) { j = batchSize; } } //process the queued orders for (int k = 0; k < queueOrders.Count; k++) { //orQueue.Insert((Order)queueOrders[k]); PETSHOP是插入到数据库方法,以下改为写到日志文件 Order o = (Order)queueOrders[k]; MyLog.WriteLog("订单号:" + o.OrderID + " 商品ID:" + o.GoodsID); processedItems++; totalOrdersProcessed++; } MessageBox.Show("(Thread Id " + Thread.CurrentThread.ManagedThreadId + ") batch finished, " + processedItems + " items"); } }
标签:
原文地址:http://www.cnblogs.com/kavilee/p/4433021.html