年底总结2-spring多线程任务调度
?1、spring framework开发参考手册中第23掌"spring中的定时调度(Scheduling)和线程池(Thread Pooling)"提到三种方式:
(1)使用OpenSymphony Quartz 调度器
(2)使用JDK Timer支持类
(3)Spring ? ? 5、java调用 (1) collectSalesOrderExecutor.java ? ? (2)CollectSalesOrderTask.java ? ? ? 6、遇到的一个问题处理,即PC机作为服务器使用,可能长时间不关机,隔天之后会报如下错误: ? 原因:Mysql服务器默认的“wait_timeout”是8小时【也就是默认的值默认是28800秒】,也就是说一个connection空闲超过8个小时,Mysql将自动断开该connection,通俗的讲就是一个连接在8小时内没有活动,就会自动断开该连接。 ? 解决:如下 ?<!-- 接收数据 --> <!-- 异步线程池 --> <bean id="threadPool" value="10" /> <!-- 最大线程数 --> <property name="maxPoolSize" value="100" /> <!-- 队列最大长度 >=mainExecutor.maxSize --> <property name="queueCapacity" value="1000" /> <!-- 线程池维护线程所允许的空闲时间 --> <property name="keepAliveSeconds" value="300" /> <!-- 线程池对拒绝任务(无线程可用)的处理策略 --> <property name="rejectedExecutionHandler"> <bean /> </property> </bean> <bean id="collectSalesOrderExecutor" ref="threadPool" /> <property name="dataSource" ref="dataSource" /> </bean> <bean id="springScheduleExecutorTask" ref="collectSalesOrderExecutor" /> <!-- 容器加载10秒后开始执行 --> <property name="delay" value="10000" /> <!-- 每次任务间隔 30秒--> <property name="period" value="30000" /> </bean> <bean id="springScheduledExecutorFactoryBean" > <list> <ref bean="springScheduleExecutorTask" /> </list> </property> </bean>
public class CollectSalesOrderExecutor extends TimerTask {//注入ThreadPoolTaskExecutor 到主线程中private ThreadPoolTaskExecutor threadPool;private JdbcTemplate template;public void setThreadPool(ThreadPoolTaskExecutor threadPool) {this.threadPool = threadPool;}//注入数据源public void setDataSource(DataSource dataSource) {this.template = new JdbcTemplate(dataSource);}@Overridepublic void run() {System.out.format("开始执行 %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 {//执行向各个设备采集数据并保存数据库threadPool.execute(new CollectSalesOrderTask(template,equipment.getIp()));} catch (Exception ex) {ex.printStackTrace();}}}}}
public class CollectSalesOrderTask implements Runnable {private String ip;private JdbcTemplate template;public CollectSalesOrderTask(JdbcTemplate template, String ip) {this.template = template;this.ip = ip;}@Overridepublic void run() {// 连接设备System.out.format("执行采集数据 %s ...%n", ip);//接收设备数据List<Report> list = JhscaleCommunicationUtils.getDeviceSales(this.ip);//保存本地数据库if (list != null && !list.isEmpty())storeSalesOrder(list);}}
Caused by: com.mysql.jdbc.CommunicationsException: Communications link failure due to underlying exception:
D:\MySQL\MySQL Server 5.1\my.ini[mysqld]# The TCP/IP Port the MySQL Server will listen onport=3306---------位置下面添加-----------# this is myown dinifition for mysql connection timeoutwait_timeout=31536000interactive_timeout=31536000