在spring任務調度的基礎上增加多線程
三種方式:
(1)使用OpenSymphony Quartz 調度器
(2)使用JDK Timer支持類
(3)SpringTaskExecutor抽象
spring 容器配置
<!-- 接收數(shù)據(jù) --> <!-- 異步線程池 --> <bean id="threadPool" class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor"> <!-- 核心線程數(shù) --> <property name="corePoolSize" value="10" /> <!-- 最大線程數(shù) --> <property name="maxPoolSize" value="100" /> <!-- 隊列最大長度 >=mainExecutor.maxSize --> <property name="queueCapacity" value="1000" /> <!-- 線程池維護線程所允許的空閑時間 --> <property name="keepAliveSeconds" value="300" /> <!-- 線程池對拒絕任務(無線程可用)的處理策略 --> <property name="rejectedExecutionHandler"> <bean class="java.util.concurrent.ThreadPoolExecutor$CallerRunsPolicy" /> </property> </bean> <bean id="collectSalesOrderExecutor" class="com.fts.internal.CollectSalesOrderExecutor"> <property name="threadPool" ref="threadPool" /> <property name="dataSource" ref="dataSource" /> </bean> <bean id="springScheduleExecutorTask" class="org.springframework.scheduling.concurrent.ScheduledExecutorTask"> <property name="runnable" ref="collectSalesOrderExecutor" /> <!-- 容器加載10秒后開始執(zhí)行 --> <property name="delay" value="10000" /> <!-- 每次任務間隔 30秒--> <property name="period" value="30000" /> </bean> <bean id="springScheduledExecutorFactoryBean" class="org.springframework.scheduling.concurrent.ScheduledExecutorFactoryBean"> <property name="scheduledExecutorTasks" > <list> <ref bean="springScheduleExecutorTask" /> </list> </property> </bean>
java后臺調用
collectSalesOrderExecutor.java
public class CollectSalesOrderExecutor extends TimerTask { //注入ThreadPoolTaskExecutor 到主線程中 private ThreadPoolTaskExecutor threadPool; private JdbcTemplate template; public void setThreadPool(ThreadPoolTaskExecutor threadPool) { this.threadPool = threadPool; } //注入數(shù)據(jù)源 public void setDataSource(DataSource dataSource) { this.template = new JdbcTemplate(dataSource); } @Override public void run() { System.out.format("開始執(zhí)行 %s ...%n", new Date()); @SuppressWarnings("unchecked") //取得設備列表 List<Equipment> ipList = template.query("select e.* from equipment e ", ParameterizedBeanPropertyRowMapper.newInstance(Equipment.class)); if (ipList != null) { for (Equipment equipment : ipList) { try { //執(zhí)行向各個設備采集數(shù)據(jù)并保存數(shù)據(jù)庫 threadPool.execute(new CollectSalesOrderTask(template,equipment.getIp())); } catch (Exception ex) { ex.printStackTrace(); } } } }} CollectSalesOrderTask.java
public class CollectSalesOrderTask implements Runnable { private String ip; private JdbcTemplate template; public CollectSalesOrderTask(JdbcTemplate template, String ip) { this.template = template; this.ip = ip; } @Override public void run() { // 連接設備 System.out.format("執(zhí)行采集數(shù)據(jù) %s ...%n", ip); //接收設備數(shù)據(jù) List<Report> list = JhscaleCommunicationUtils.getDeviceSales(this.ip); //保存本地數(shù)據(jù)庫 if (list != null && !list.isEmpty()) storeSalesOrder(list); }} 注意:
遇到的一個問題處理,即PC機作為服務器使用,可能長時間不關機,隔天之后會報如下錯誤:
Caused by: com.MySQL.jdbc.CommunicationsException: Communications link failure due to underlying exception:
原因:Mysql服務器默認的“wait_timeout”是8小時【也就是默認的值默認是28800秒】,也就是說一個connection空閑超過8個小時,Mysql將自動斷開該connection,通俗的講就是一個連接在8小時內沒有活動,就會自動斷開該連接
新聞熱點
疑難解答