Top-up, withdrawal function involves multithreading, queue problems

Source: Internet
Author: User

Use double dry third party to realize recharge, withdraw function.

Ideas

Configure a listener to initialize a queue when the project starts, to open a thread (the Run method checks the queue for values, some to execute the business, and no threads to wait). Recharge, withdraw after successful callback method, populate queue, Wake waiting thread to execute business method.

Implement

Configuring a listener in Web. xml

<listener>
<listener-class>com.wjz.listener.mywebconfigcontextlistener</listener-class>< /listener>

Mywebconfigcontextlistener Listener

 PackageCom.wjz.listener;ImportJavax.servlet.ServletContext;Importjavax.servlet.ServletContextEvent;ImportJavax.servlet.ServletContextListener;ImportOrg.springframework.context.ApplicationContext;Importorg.springframework.web.context.support.WebApplicationContextUtils;ImportCom.wjz.queue.MyCashJobQueue;ImportCom.wjz.service.MyAccountService;ImportCom.wjz.service.MyUserService; Public classMywebconfigcontextlistenerImplementsServletcontextlistener { Public voidcontextinitialized (Servletcontextevent sce) {ServletContext ServletContext=Sce.getservletcontext (); ApplicationContext Context=Webapplicationcontextutils.getrequiredwebapplicationcontext (ServletContext); Myuserservice UserService= (Myuserservice) context.getbean ("UserService"); Myaccountservice Accountservice= (Myaccountservice) context.getbean ("Accountservice"); //initializing the business processing queueMycashjobqueue.init (UserService, Accountservice); }}

Mycashjobqueue function Queue

 PackageCom.wjz.queue;ImportJava.util.Queue;ImportJava.util.concurrent.ConcurrentLinkedQueue;ImportCom.wjz.model.MyCashModel;ImportCom.wjz.service.MyAccountService;ImportCom.wjz.service.MyUserService;ImportCom.wjz.task.MyTask;ImportCom.wjz.task.impl.MyCashTask;/** * @authorWJZ*/ Public classMycashjobqueue<t>{@SuppressWarnings ({"Rawtypes", "unchecked" })    Privatequeue<t> queue =NewConcurrentlinkedqueue (); Privatemytask Task;  Publicmycashjobqueue (mytask Task) { This. Task =task; }         Public StaticMycashjobqueue<mycashmodel> Cash_queue =NULL;
   //Initialize a task object to inject into the function queue, the Task object thread Public Static voidInit (myuserservice userservice, Myaccountservice accountservice) {cash_queue=NewMycashjobqueue<mycashmodel> (NewMycashtask (UserService, Accountservice)); } Public intsize () {returnqueue.size (); } Public synchronizedT Peek () {return(T) Queue.peek (); } Public synchronizedT-remove (T model) {returnQueue.remove (); } //To populate the queue with values, wake up waiting threads
Synchronizing an instance object of Mycashjobqueue Public synchronized voidOffer (T model) {if(!Queue.contains (model)) {Queue.offer (model);
        //sync content synchronized(Task.getlock ()) {Task.execute (); } } }}

MyTask Task Interface

 PackageCom.wjz.task; Public InterfaceMyTask { Public Static FinalString cash_status = "Cash_status";  Public voidexecute ();  Public voidDorun ();  Public voiddowait ();  Public voidStop ();  PublicObject Getlock (); }

Recharge, withdrawal function realization class

 PackageCom.wjz.task.impl;ImportCom.wjz.model.MyCashModel;ImportCom.wjz.queue.MyCashJobQueue;ImportCom.wjz.service.MyAccountService;ImportCom.wjz.service.MyUserService;ImportCom.wjz.task.MyAbstractTask;ImportCom.wjz.task.MyTask; Public classMycashtaskextendsMyabstracttask {PrivateMyuserservice UserService; PrivateMyaccountservice Accountservice; @Override Public voidDorun () { while(Mycashjobqueue.cash_queue! =NULL&& MyCashJobQueue.CASH_QUEUE.size () > 0) {            //remove a task from a queueMycashmodel Cashmodel =MyCashJobQueue.CASH_QUEUE.peek (); Try {                if(Cashmodel! =NULL) {                    //Business ProcessingAccountservice.dorechargetask (); }            } Catch(Exception e) {}finally {                //remove a task from a queueMyCashJobQueue.CASH_QUEUE.remove (Cashmodel); }         }            }         PublicMycashtask (Myuserservice userservice, Myaccountservice accountservice) {Super();  This. UserService =UserService;  This. Accountservice =Accountservice; } @Override PublicObject Getlock () {returnMytask.cash_status; }}

Top-up, withdrawal function involves multithreading, queue problems

Related Article

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.