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