在spring任务调度的基础上增加多线程

三种方式:

(1)使用OpenSymphony Quartz 调度器

(2)使用JDK Timer支持类

(3)SpringTaskExecutor抽象

spring 容器配置

<!-- 接收数据 -->
<!-- 异步线程池 -->
<bean id="threadPool" class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor">
<!-- 核心线程数 -->
<property name="corePoolSize" value="10" />
<!-- 最大线程数 -->
<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秒后开始执行 -->
<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;
} //注入数据源
public void setDataSource(DataSource dataSource) {
this.template = new JdbcTemplate(dataSource);
} @Override
public 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();
}
}
} } }

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("执行采集数据 %s ...%n", ip);
//接收设备数据
List<Report> list = JhscaleCommunicationUtils.getDeviceSales(this.ip);
//保存本地数据库
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小时内没有活动,就会自动断开该连接

补充---spring多线程任务调度的更多相关文章

  1. Spring的任务调度@Scheduled注解——task:scheduler和task:executor的解析

    原文地址: https://blog.csdn.net/yx0628/article/details/80873774 一个简单的Spring定时任务的 demo,全部代码见下载地址:https:// ...

  2. spring 多线程 注入 服务层 问题

    在用多线程的时候,里面要用到Spring注入服务层,或者是逻辑层的时候,一般是注入不进去的.具体原因应该是线程启动时没有用到Spring实例不池.所以注入的变量值都为null. 详细:http://h ...

  3. spring多线程与定时任务

    本篇主要描述一下spring的多线程的使用与定时任务的使用. 1.spring多线程任务的使用 spring通过任务执行器TaskExecutor来实现多线程与并发编程.通常使用ThreadPoolT ...

  4. spring多线程初探

    6月14号  晴  最高温度37   今天很热的一天啊,开发的任务现在正在测试阶段,手头没有什么工作任务,忙里偷闲,丰富一下我的blog. 前两天有个需求:调用第三方接口,这个接口的响应时间有点长,需 ...

  5. Spring Schedule 任务调度实现

    我们都知道任务调度可以用Quartz,但对于简单的定时任务,可以直接用Spring内置的Schedule来实现.可以由两种方式,注释+XML配置 注解方式: 注解也要先在sping.xml配置文件中配 ...

  6. Spring task任务调度详解

    spring内部有一个task是Spring自带的一个设定时间自动任务调度 task使用的时候很方便,但是他能做的东西不如quartz那么的多! 可以使用注解和配置两种方式,配置的方式如下 引入Spr ...

  7. Spring多线程批量发送邮件(ThreadPoolTaskExecutor)

    1,需求:使用多线程批量发送邮件 需要批量发送邮件大概400封左右,但是因为发送邮件受网络限制,所以经常导致等待超时.所以就想到了使用多线程来发邮件,因为是异步的所以返回结果不受发邮件影响. 2,思路 ...

  8. 解决spring多线程不共享事务的问题

    在一个事务中使用多线程操作数据库时,若同时存在对数据库的读写操作,可能出现数据读取的不准确,因为多线程将不会共享同一个事务(也就是说子线程和主线程的事务不一样),为了解决这个问题,可以使用spring ...

  9. spring多线程

    Spring4.x高级话题(二):多线程 一. 点睛 Spring通过任务执行器(TaskExecutor)来实现多线程和并发编程.使用ThreadPoolTaskExecutor可实现一个基于线程池 ...

随机推荐

  1. bzoj4004

    线性基 构成线性基的个数是定的,所以我们对价值进行贪心就行了,根据拟阵那套理论,我们排个序,然后能塞进去就塞,这样就求出最小值了. 思维江化,只要是多维向量都能用线性基搞. #include<b ...

  2. atof,atoi,atol,strtod,strtol,strtoul

    字符串处理函数 atof 将字串转换成浮点型数 atoi 字符串转换成整型数 atol 函数名: atol 功 能: 把字符串转换成长整型数 用 法: long atol(const char *np ...

  3. ViewerJS 一个在浏览器上查看 PDF 和电子表格的 JavaScript 库

    Viewer.js简介 http://viewerjs.org/ 下载Viewer.js压缩包,解压后将ViewerJS文件夹放在网站根目录下 在浏览器地址栏中输入网址http://172.16.8. ...

  4. dumpbin检查Dll

    用dumpbin.exe工具查看DLL,dumpbin.exe是VS自带的工具.版本VS2013,路径是:G:\VS2013\VC\bin\amd64\ 可以看到dumpbin.exe. 使用VS里的 ...

  5. 2-1Java简介

    java程序执行流程 java平台:

  6. .after()和.before()的关系

    .after() 是在相邻元素后面插入元素 .next() 获得匹配元素集合中每个元素紧邻的同胞元素 用法介绍: $(selector).after(content) content 必需.规定要插入 ...

  7. CodeForces 628B New Skateboard 思维

    B. New Skateboard time limit per test 1 second memory limit per test 256 megabytes input standard in ...

  8. Spring入门(四):使用Maven管理Spring项目

    让我们先回顾下本系列的前3篇博客: Spring入门(一):创建Spring项目 Spring入门(二):自动化装配bean Spring入门(三):通过JavaConfig装配bean 1.为什么要 ...

  9. 实战分析一个运行起来会卡死的Go程序

    序言 最近一位非常热心的网友建议结合demo来分析一下goroutine的调度器,而且还提供了一个demo代码,于是便有了本文,在此对这位网友表示衷心的感谢! 这位网友提供的demo程序可能有的gop ...

  10. PHP之操作数组

    https://www.jb51.net/Special/623.htm https://www.php.net/manual/zh/ref.array.php https://www.runoob. ...