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

    KD-tree 强制在线就不能愉快的做这道题了. 我们用KD-tree维护平面上的点,这样建出来的树高大概是log,复杂度过得去,但是插入过多会使树深很深,这样就能卡死,那么我们每个10000次插入就 ...

  2. Ubuntu 安装indicator-sysmonitor

    之前就像安装一个软件用来查看Ubuntu的CPU, 内存, 网速情况, 终于让我碰到了--indicator-sysmonitor 仅需三条命令, 你值得拥有: sudo add-apt-reposi ...

  3. shell脚本 列出所有网卡的ip地址

    #!/bin/bashfor i in `ifconfig | grep -o ^[a-z0-9]*`do ifconfig $i|sed -n 2p|awk '{ print $2 }'|tr -d ...

  4. 【网络爬虫】【java】微博爬虫(四):数据处理——jsoup工具解析html、dom4j读写xml

    之前提到过,对于简单的网页结构解析,可以直接通过观察法.手工写正则解析,可以做出来,比如网易微博.但是对于结构稍微复杂点的,比如新浪微博,如果还用正则,用眼睛一个个去找,未免太麻烦了. 本文介绍两个工 ...

  5. Win10 VC++运行库集合|VC++ 2005 2008 2010 2012 2015

    在Win10系统中很多朋友在运行一些软件时会遇到缺少.DLL的情况,主要是没有安装VC++运行库下面小编收集了Win10 VC++运行库集合,大家安装上去就可以了~ 微软常用软件运行库合集(vc201 ...

  6. ASP.NET Core编程实现基本身份认证

    概览 在HTTP中,基本认证(Basic access authentication,简称BA认证)是一种用来允许网页浏览器或其他客户端程序在请求资源时提供用户名和口令形式的身份凭证的一种登录验证方式 ...

  7. E20181216-hm

    intersect vt. (指线条.道路等) 相交,交叉; vt. 横断,横切,横穿;

  8. 玩转phpstorm之terminal

    启动快捷键  Alt+F12 启动之后,像这样 第一次进入这个配置默认显示的是cmd窗口,现在我们将其换成git 命令行窗口,毕竟git才是boss first 将cmd换成我们是先在电脑上安装完成的 ...

  9. 525. Contiguous Array

    Given a binary array, find the maximum length of a contiguous subarray with equal number of 0 and 1. ...

  10. 基于GPU的优化处理

    http://www.cnblogs.com/wuhanhoutao/archive/2007/11/10/955293.html 早期的三维场景绘制,显卡只是为屏幕上显示像素提供一个缓存,所有的图形 ...