Introduction: The recent project with the integration of 360 project interface, there is a feedback interface (that is, we receive the request, need to respond immediately, and also have an interface to push to their other calculations), the push process time, or two interfaces can not be returned at the same time, there are sequencing. And then I thought about putting my controller back in a moment to accept success, adding a new thread to do other time-consuming operations (thread pool configuration and parameter testing explained in step 5th).
1. Controller code is as follows:
@Autowired
Private Callthreaddemo worker;
@RequestMapping ("/bandbankconfirm2" Public void BandBankConfirm2 (String jsonstring) {SYSTEM.OUT.PRINTLN ( controller starts--------------" Here is the interface that needs to call the third party passed in the parameter String method = "Is.api.v3.order.bindcardfeedback" ; Map <string, object> map = new hashmap<> (); Map.put ( "Order_no", "254986512848973" "Bind_status", 1 "Reason", " controller End--------------"
Renderjson This method is jfinal, can be understood as @reponsebody need to return the operation of
Renderjson ("interface call complete");
}
Callthreaddemo Class Code:
@Component Public classCallthreaddemo {@AutowiredPrivateThreadpooltaskexecutor executor; @AutowiredPrivateservicetest servicetest; Public voidCallRong360 (FinalString method,FinalMap<string, object>map) { //This class is the abstract class I encapsulated, there is a public method, the specific code below theServiceparent Callrong360method =Newserviceparent () {@Override Public voidrun () {Try{Thread.Sleep (20000); } Catch(interruptedexception e) {e.printstacktrace (); } System.out.println ("Thread Start-------------");
Third-party public interfaces are called here Jsonobject result=Callbandbankmethod (method, map);
Call the service method here to implement your own business logic servicetest.insertuser ("111", "222222");
SYSTEM.OUT.PRINTLN (result); System.out.println ("Thread End-------------"); } }; Executor.execute (Callrong360method); System.out.println ("Currently active Threads:" +Executor.getactivecount ()); System.out.println ("Number of core threads:" +executor.getcorepoolsize ()); System.out.println ("Number of Bus threads:" +executor.getpoolsize ()); System.out.println ("Max thread pool number" +executor.getmaxpoolsize ()); System.out.println ("Thread processing Queue Length" +executor.getthreadpoolexecutor (). Getqueue (). Size ()); }
3, the encapsulated abstract class code is as follows:
Public Abstract classServiceparentImplementsRunnable { PublicJsonobject Callbandbankmethod (String method, map<string, object>map) {
This method is called the three-party interface, the common part// //The input parameters are as follows://map<string, object> Map = new hashmap<> ();//map.put ("Order_no", "254986512848973");//map.put ("Bind_status", 1);//map.put ("Reason", "" ");//net.sf.json.JSONObject ret = Callrong360method.callbandbankmethod ("Is.api.v3.order.bindcardfeedback", map); //// //the exception condition output parameter is null:Openapiclient openapiclient =Newopenapiclient (); Openapiclient.setmethod (method); for(Map.entry<string, object>Entry:map.entrySet ()) {Openapiclient.setfield (Entry.getkey (), String.valueof (Entry.getvalue ())); } net.sf.json.JSONObject ret=NULL; Try{ret=Openapiclient.execute (); } Catch(Exception e) {e.printstacktrace (); System.out.println ("Call Feedback Interface Exception---->" +e.getmessage ()); } returnret; }//defined here as an abstract method, creating an anonymous inner class or inheriting a class must implement the Public Abstract voidRun ();
4. The Servicetest interface method is as follows:
@Service Public class servicetest { @Autowired private bankmapper bankmapper; = {Exception. class }) publicvoid insertuser (string s, string s1) { Bankmapper.insertuser (S, s1); // this creates an exception and tests the transaction. Tested, transaction effective int i = 1/0; }}
Above four steps, the successful call controller method immediately return results, also implemented another thread to call the three-party interface return information, and implemented its own business logic.
5, then we continue to explain the thread pool corresponding configuration and parameter description, here we test to illustrate the role of parameters (Corepoolsize core thread number, maxpoolsize maximum number of threads, queuecapacity processing queue)
Threadpool.corepoolsize=5
threadpool.keepaliveseconds=200
threadpool.maxpoolsize=10
threadpool.queuecapacity=2
<!--thread pool-- class= "Org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor" > <property name= "corepoolsize" value= "${threadpool.corepoolsize}"/> <property name= " Keepaliveseconds "value=" ${threadpool.keepaliveseconds} "/> <property name=" maxpoolsize "value=" ${ Threadpool.maxpoolsize} "/> <property name=" queuecapacity "value=" ${threadpool.queuecapacity} "/> </bean> <!--configure task--> <task:annotation-driven executor= "Taskexecutor" in the bean XSD/ >
The log information for the test output is posted below: Controller starts--------------
current number of active threads:1Number of core threads:5Number of bus threads:1maximum number of thread pools 10 threads processing Queue Length 0controller end--------------
Controller Start--------------current number of active threads:2Number of core threads:5Number of bus threads:2maximum number of thread pools 10 threads processing Queue Length 0controller end--------------
Controller Start--------------current number of active threads:3Number of core threads:5Number of bus threads:3maximum number of thread pools 10 threads processing Queue Length 0controller end--------------
Controller Start--------------current number of active threads:4Number of core threads:5Number of bus threads:4maximum number of thread pools 10 threads processing Queue Length 0controller end--------------
Controller Start--------------current number of active threads:5Number of core threads:5Number of bus threads:5maximum number of thread pools 10 threads processing Queue Length 0controller end--------------
Controller Start--------------current number of active threads:5---------------"Because the number of active threads is 5 in the queue.Number of core threads:5Number of bus threads:5maximum number of thread pools 10 threads processing Queue Length 1-------------"The maximum number of core threads is reached hereCorepoolsize=5, starting into the processing queuequeuecapacity=2
controller End --------------
Controller Start--------------current number of active threads:5Number of core threads:5Number of bus threads:5maximum number of thread pools 10 threads processing Queue Length 2---------------continue into the queue and reach the maximum number of queues 2controller end--------------
Controller Start--------------current number of active threads:6-----------------"This is because the maximum number of queues has been reached, so continue to create threads to execute, until the last to the maximum number of threadsNumber of core threads:5Number of bus threads:6maximum number of thread pools 10 threads processing Queue Length 2controller end--------------
Controller Start--------------current number of active threads:7Number of core threads:5Number of bus threads:7maximum number of thread pools 10 threads processing Queue Length 2controller end--------------
Controller Start--------------current number of active threads:8Number of core threads:5Number of bus threads:8maximum number of thread pools 10 threads processing Queue Length 2controller end--------------
Controller Start--------------current number of active threads:9Number of core threads:5Number of bus threads:9maximum number of thread pools 10 threads processing Queue Length 2controller end--------------
Controller Start--------------current number of active threads:--------------"The number of active threads reaches the maximum number of thread poolsNumber of core threads:5Number of bus threads:10maximum number of thread pools 10 threads processing Queue Length 2controller end--------------
Controller Start--------------2018-07-01 20:23:19-----------------The call continues here because the maximum number of thread pools and the queue have reached the maximum value, throwing an exception[] [] [ WARN]-[thread:qtp1276504061-60]-[Org.springframework.web.servlet.handler.AbstractHandlerExceptionResolver.logException ()]: Handler execution resulted in exceptionorg.springframework.core.task.TaskRejectedException:Executor [[Email protected][running, pool Size= Ten, active threads = ten, queued tasks = 2, completed tasks = 0]] did not accept Task:com.fastx.cooperate.rong360.rong.s Ervice. Callthreaddemo$1@5fa6bf1 at Org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor.execute ( Threadpooltaskexecutor.java:245) at com.fastx.cooperate.rong360.rong.service.CallThreadDemo.callRong360 (Callthreaddemo.java:43)
Thus, by outputting the log, we can easily understand the role of each parameter.
Record the actual application of the thread pool in the project, and explain the configuration and parameter understanding of the thread pool.