在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. stdout引发的curl 302跳转 crash

    重现步骤: 0.开发环境:Windows 7 SP1 64bit, VS2008 SP1 1.进程中修改了stdout这个句柄的值:调用了prinft和cout都会修改stdout,TRACE不会修改 ...

  2. 微信小程序开发之页面跳转并携带参数

    接口: wx.navigateTo({url:......})   保留当前页面,跳转到应用内指定URL页面,导航栏左上角有返回按钮 wx.redirecTo({url:.....})       关 ...

  3. ecplise常见的一些问题

    修改注释的字体,默认的字体太小了.

  4. Python3.6 的字符串内建函数

    1.capitalize(self) 将字符串的第一个字符转换为大写 2.casefold(self) 返回将字符串中所有大写字符转换为小写后生成的字符串 3.center(self, width, ...

  5. java web 学习-网络资源

    [网络收集] 1. JavaWeb学习总结——JSP中的九个内置对象 2. Jsp九大内置对象以及四个作用域 他人学习汇总资源 1. http://www.cnblogs.com/xdp-gacl/t ...

  6. 1 手写Java ArrayList核心源码

    手写ArrayList核心源码 ArrayList是Java中常用的数据结构,不光有ArrayList,还有LinkedList,HashMap,LinkedHashMap,HashSet,Queue ...

  7. 2016四川省赛A,C【写了1w个if的水题】

    A题: #include <iostream> #include <stdio.h> #include <string.h> #include <algori ...

  8. Linux系统查看网站访问日志

    日志地址 /www/wwwlogs/网站名称-access_log 下载到本地,改成txt文件 打开WPS,创建表格,导入数据,选择文件,然后点击下一步,直到选择文件类型时,选择分隔符号,下一步,把勾 ...

  9. ubuntu 14.04 源码编译mysql-5.7.17

    环境为 Ubuntu 12.04 64 位的桌面版 编译的mysql 版本为 5.7.18 首先需要安装一下依赖包 sudo apt-get install libncurses5-dev cmake ...

  10. Python scrapy框架爬取瓜子二手车信息数据

    项目实施依赖: python,scrapy ,fiddler scrapy安装依赖的包: 可以到https://www.lfd.uci.edu/~gohlke/pythonlibs/  下载 pywi ...