在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. bzoj3489

    kdtree 3维kdtree,就是三个维度轮换着切,我们把每个元素看成一个点,坐标是上次出现的位置,下次出现的位置,自己的位置,第一个<l,第二个>r,第三个[l,r],然后kdtree ...

  2. SecureCRT rz上传文件失败

    SecureCRT 将 Windows 上的文件传至 Linux 端,小的文件没有问题能够正常上传,但是对于几百M的文件往往上传过程中失败. 解决办法:使用 rz -be,并且去掉对话框中" ...

  3. KVM虚拟机内无agent情况下的监控方法

    KVM虚拟机内无agent情况下的监控(ceilometer实现) 今天看到大家在群里讨论KVM虚拟机的监控问题,而且是要求VM内无agent情况下的监控.这方面确实没有深入研究,但尚有些openst ...

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

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

  5. CodeForces 628B New Skateboard 思维

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

  6. C#字体字号的改变

    using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using Sy ...

  7. SLAM学习资料整理(转)

    原文出处:http://www.cnblogs.com/wenhust/p/5942893.html 书籍: 1.必读经典 Thrun S, Burgard W, Fox D. <Probabi ...

  8. collections 中 typing 中对象的引用

    from typing import ( Callable as Callable, Container as Container, Hashable as Hashable, Iterable as ...

  9. Technocup 2017 - Elimination Round 1 (Unofficially Open for Everyone, Rated for Div. 2) D

    The organizers of a programming contest have decided to present t-shirts to participants. There are ...

  10. 简单总结ConcurrentHashMap

    一.HashTable hashTable是一个线程安全的容器,是线程安全版本的HashMap.但它的底层是和HashMap一样的,只是在方法上都加上了synchronized关键字. 这样子有什么后 ...