Adding multithreading on the basis of spring task scheduling
Three different ways:
(1) using the Opensymphony Quartz Scheduler
(2) using the JDK Timer support class
(3) Springtaskexecutor abstract
Spring Container Configuration
<!--Receive Data - <!--Asynchronous thread pool - <BeanID= "ThreadPool"class= "Org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor"> <!--number of core threads - < Propertyname= "Corepoolsize"value= "Ten" /> <!--Maximum number of threads - < Propertyname= "Maxpoolsize"value= "+" /> <!--Maximum Queue Length >=mainexecutor.maxsize - < Propertyname= "Queuecapacity"value= "+" /> <!--the thread pool maintains idle time allowed by threads - < Propertyname= "Keepaliveseconds"value= "+" /> <!--processing policy for the thread pool to reject tasks (wireless threads available) - < Propertyname= "Rejectedexecutionhandler"> <Beanclass= "Java.util.concurrent.threadpoolexecutor$callerrunspolicy" /> </ Property> </Bean> <BeanID= "Collectsalesorderexecutor"class= "Com.fts.internal.CollectSalesOrderExecutor"> < Propertyname= "ThreadPool"ref= "ThreadPool" /> < Propertyname= "DataSource"ref= "DataSource" /> </Bean> <BeanID= "Springscheduleexecutortask"class= "Org.springframework.scheduling.concurrent.ScheduledExecutorTask"> < Propertyname= "Runnable"ref= "Collectsalesorderexecutor" /> <!--The container starts executing after 10 seconds of loading - < Propertyname= "Delay"value= "10000" /> <!--30 seconds per task interval - < Propertyname= "Period"value= "30000" /> </Bean> <BeanID= "Springscheduledexecutorfactorybean"class= "Org.springframework.scheduling.concurrent.ScheduledExecutorFactoryBean"> < Propertyname= "Scheduledexecutortasks" > <List> <refBean= "Springscheduleexecutortask" /> </List>
</property>
</bean>
Java Background call
Collectsalesorderexecutor.java
Public classCollectsalesorderexecutorextendsTimerTask {//inject threadpooltaskexecutor into the main thread PrivateThreadpooltaskexecutor ThreadPool; Privatejdbctemplate template; Public voidSetthreadpool (Threadpooltaskexecutor threadPool) { This. ThreadPool =ThreadPool; } //Inject data source Public voidSetdatasource (DataSource DataSource) { This. Template =NewJdbcTemplate (DataSource); } @Override Public voidrun () {System.out.format ("Start executing%s ...%n",NewDate ()); @SuppressWarnings ("Unchecked") //get a list of devicesList<equipment> IPList = Template.query ("Select e.* from Equipment E", Parameterizedbeanpropertyrowmapper.newinstance (equipment.class)); if(IPList! =NULL) { for(equipment equipment:iplist) {Try { //perform data acquisition and database storage to individual devicesThreadpool.execute (NewCollectsalesordertask (Template,equipment.getip ())); } Catch(Exception ex) {ex.printstacktrace (); } } } }}
Collectsalesordertask.java
Public classCollectsalesordertaskImplementsRunnable {PrivateString IP; Privatejdbctemplate template; Publiccollectsalesordertask (jdbctemplate template, String IP) { This. Template =template; This. IP =IP; } @Override Public voidrun () {//Connecting DevicesSystem.out.format ("Execute acquisition data%s ...%n", IP); //Receiving device Datalist<report> list = Jhscalecommunicationutils.getdevicesales ( This. IP); //Save Local Database if(List! =NULL&&!list.isempty ()) Storesalesorder (list); }}
Attention:
Encountered a problem processing, that is, the PC as a server use, may not shut down for a long time, the next day will report the following error:
caused by:com.mysql.jdbc.CommunicationsException:Communications link failure due to underlying exception:
Cause: MySQL server default "Wait_timeout" is 8 hours "is the default value is 28,800 seconds", that is, a connection idle for more than 8 hours, MySQL will automatically disconnect the connection, The popular saying is that a connection is not active within 8 hours and will automatically disconnect the connection
Supplemental---Spring Multi-threaded Task Scheduler