国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發(fā)設計 > 正文

補充---spring多線程任務調度

2019-11-14 23:59:37
字體:
來源:轉載
供稿:網(wǎng)友
補充---sPRing多線程任務調度

在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小時內沒有活動,就會自動斷開該連接


發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 政和县| 镇安县| 新昌县| 淮滨县| 广东省| 库尔勒市| 绍兴县| 固镇县| 屯昌县| 富蕴县| 海伦市| 文成县| 闽侯县| 磐安县| 佛坪县| 辰溪县| 徐水县| 乌鲁木齐县| 民和| 工布江达县| 铜梁县| 海伦市| 海丰县| 新巴尔虎左旗| 乌拉特后旗| 凉山| 嘉定区| 天长市| 华阴市| 小金县| 龙川县| 寿阳县| 黄山市| 长阳| 康定县| 龙胜| 民乐县| 三都| 凤山市| 五家渠市| 南宫市|