定时器中实现数据库表数据移动的功能,Exception in thread "Timer-0" isExist java.lang.NullPointerException定时器中线程报错。
package com.shawnway.trade.marketdata.constants;
import java.sql.SQLException;
import java.util.Calendar;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask; import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext; import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody; import com.shawnway.trade.marketdata.services.ChartService; @Component
public class TimerConfig {
@Autowired
private ChartService chartService;
@PersistenceContext
private EntityManager em;
public TimerConfig(ChartService ct){//关键点解决 null指针错误,
chartService=ct;
}
// Timer.scheduleAtFixedRate(TimerTask task,Date firstTime,long period)
//每天的24:00:00执行迁移操作
public void init() {
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, 15); // 控制时
calendar.set(Calendar.MINUTE, 3); // 控制分
calendar.set(Calendar.SECOND, 0); // 控制秒 Date time = calendar.getTime(); Timer timer = new Timer();
timer.scheduleAtFixedRate(new TimerTask() { public void run() {
System.out.println("处理器开始运行"); try {
moveStableMes();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}, time, 1000 * 60 * 60 * 24);// 这里设定将延时每天固定执行
}
@RequestMapping(value ="/move/moveStableMes", method = RequestMethod.GET)
@ResponseBody
public void moveStableMes() throws SQLException{
//首先判定存储表是否存在,如果存在则转移数据
//如果不存在,先创建该存储数据表,在转移数据
//情况临时存储表。(临时表是用来保存当天的数据。)
Calendar c = Calendar.getInstance();
int year=c.get(c.YEAR);
int month=c.get(c.MONTH)+1;
int today = c.get(c.DAY_OF_MONTH);
String tbname="";
if(month<=9)
tbname="market_data_candlechart_"+Integer.toString(year)+"0"+Integer.toString(month);//数据库的名字
else
tbname="market_data_candlechart_"+Integer.toString(year)+Integer.toString(month);//数据库的名字
String temperTB="market_data_candlechart";//存储临时信息的表名
System.out.println("执行到了isExist");
boolean flag=chartService.isExist(tbname);//判定对应的数据库是否存在
System.out.println("isExist结束");
if(flag){//如果已经存在了,就可以直接把临时表中的数据插入到对应的表中
chartService.moveMesToOldTB(tbname,temperTB);//将临时表中的数据移动到tbname名称的表中。
}else{
chartService.createTemperTB(tbname);//如果不存在,需要先创建一个对应名称的表
chartService.moveMesToOldTB(tbname,temperTB);//将临时表中的数据移动到tbname名称的表中。
}
//转移完数据后,清洗临时表的数据
chartService.deletTemperTB(temperTB);
} }
package com.shawnway.trade.marketdata; import java.io.FileNotFoundException; import org.apache.catalina.Server;
import org.apache.catalina.Service;
import org.apache.catalina.connector.Connector;
import org.apache.catalina.valves.RemoteIpValve;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.embedded.ConfigurableEmbeddedServletContainer;
import org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizer;
import org.springframework.boot.context.embedded.tomcat.TomcatConnectorCustomizer;
import org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment; import com.shawnway.trade.marketdata.constants.SystemConfig;
import com.shawnway.trade.marketdata.constants.TimerConfig;
import com.shawnway.trade.marketdata.core.collect.MarketDataCollectHandler;
import com.shawnway.trade.marketdata.core.ctp.CTPApiHandler;
import com.shawnway.trade.marketdata.core.ctp.CTPGatewayProxy;
import com.shawnway.trade.marketdata.core.ctp.CTPMarketDataHandler;
import com.shawnway.trade.marketdata.core.ctp.CTPZeroMQHandler;
import com.shawnway.trade.marketdata.core.es.EsMarketDataHandler;
import com.shawnway.trade.marketdata.core.es.EsunnyApiHandler;
import com.shawnway.trade.marketdata.core.es.EsunnyGatewayProxy;
import com.shawnway.trade.marketdata.core.sp.SharppointApiHandler;
import com.shawnway.trade.marketdata.core.sp.SharppointGatewayProxy;
import com.shawnway.trade.marketdata.core.sp.SpMarketDataHandler;
import com.shawnway.trade.marketdata.services.ChartService;
import com.shawnway.trade.marketdata.services.MapContainer; @PropertySource({ "file:${config.dir}/config/web.properties" })
@SpringBootApplication
public class ApplicationLauncher {
@Autowired
private Environment env;
@Autowired
private ChartService chartService; @Bean(name = { "timerConfig" }, initMethod = "init")
public TimerConfig timerConfig() {
System.out.println("timerConfig已经开始运行了~");
return new TimerConfig(chartService);//
chartService传入进去,解决空指针的错误
} public static void main(String[] args) throws Exception { System.setProperty("config.dir", System.getProperty("user.dir")); final String dir = System.getProperty("config.dir"); System.setProperty("logging.config", dir + "/config/logging.xml"); SpringApplication.run(ApplicationLauncher.class, args); } }
定时器中实现数据库表数据移动的功能,Exception in thread "Timer-0" isExist java.lang.NullPointerException定时器中线程报错。的更多相关文章
- jmeter 获取数据库表数据作为参数
jmeter - 获取数据库表数据作为参数 在jmeter中使用数据库表数据首先需要设置数据库连接,然后在创建JDBC取样器 1.创建配置元件 JDBC Connection Configuratio ...
- C# 利用mysql.data 在mysql中创建数据库及数据表
C# 利用mysql.data 在mysql中创建数据库及数据表 using System; using System.Collections.Generic; using System.Linq; ...
- MySQL数据库中查询数据库表、字段总数量,查询数据总量
最近要查询一些数据库的基本情况,由于以前用oracle数据库比较多,现在换了MySQL数据库,就整理了一部分语句记录下来. 1.查询数据库表数量 #查询MySQL服务中数据库表数据量 SELECT C ...
- mssql sqlserver 使用sql脚本 清空所有数据库表数据的方法分享
摘要: 下文讲述清空数据库中所有表信息的方法分享,如下所示: 实验环境:sql server 2008 实现思路: 1.禁用所有约束,外键 2.禁用所有触发器 3.删除表数据 4.开启触发器 5.开启 ...
- C#程序中从数据库取数据时需注意数据类型之间的对应,int16\int32\int64
private void btn2_Click(object sender, RoutedEventArgs e) { using (SqlConnection ...
- db2数据库中查找数据库表
模糊查找db2数据库中的数据库表: select tabname,remarks from syscat.tables where TABNAME like 'DM%' select 'DROP T ...
- 在function module 中向数据库插入数据
http://www.sapjx.com/abap-function-module.html 1: 应该在function module 中向数据库插入数据
- MO拆分计划行程序中写入PRODUCTIONORDERS表数据出现重复导致报错(BUG)20180502
错误提示:ORA-00001: 违反唯一约束条件 (ABPPMGR.C0248833319_6192)ORA-06512: 在 "STG.FP_MO_SPLIT", line 19 ...
- MSSQL 删除数据库表数据
--删除数据库表数据 慎用 create PROCEDURE sp_DeleteAllData AS ) ) ) ) ) ) begin try begin tran -- 失效索引,触发器 open ...
随机推荐
- 使用 PowerDesigner 和 PDMReader 逆向生成 MySQL 数据字典
下面提到的软件大家可以在下面的链接下载. 大家可以参考下面的操作录制视频来完成相关的操作. 使用 PowerDesigner 和 PDMReader 逆向生成 MySQL 数据字典.wmv_免费高速下 ...
- 使用MyBatis Generator自动创建代码( SSM框架)
步骤: 1.找到该文件目录 (上图文件下载地址:http://download.csdn.net/download/u014617413/9668872) 2.修改generatorConfig.xm ...
- C#回顾 –6.特性
1.特性是什么? Attribute 用来对类.属性.方法等标注额外的信息,贴一个标签(附着物) 通俗:给 类 或 类成员 贴一个标签,就像航空部为你的行李贴一个标签一样 个人理解,特性就是修饰 ...
- 【转载】CentOS服务器配置VPN详解
转载来自: https://bbs.aliyun.com/read/162297.html http://www.wanghailin.cn/centos-7-vpn/ 操作系统:CentOS 6.3 ...
- HTML5本地存储——Web SQL Database
在HTML5 WebStorage介绍了html5本地存储的Local Storage和Session Storage,这两个是以键值对存储的解决方案,存储少量数据结构很有用,但是对于大量结构化数据就 ...
- C++基本语法
一.static成员变量和static成员函数 1.普通成员变量每个对象有各自的一份,而静态成员变量一共就一份,为所有对象共享 2.普通成员函数必须具体作用于某个对象,而静态成员函数并不具体作用于某个 ...
- 搭建eclipse+github开发环境
开发环境 1.jdk:jdk1.8.0_60 2.eclipse:eclipse-jee-mars-R-win32-x86_64.zip 配置步骤 1.配置本地git目录(可选) eclipse-je ...
- SQL性能学习汇总 00
.在一条语句中不要重复使用相同的函数 .尽可能在存储过程中使用临时变量和临时表 如 IF (Object_ID('tempdb..#T') IS NOT NULL) DROP TABLE #T SEL ...
- json-lib——JsonConfig详细使用说明
在使用json-lib包中JSONObject.fromObject(bean,cfg)时,可能出现以下几种情况: 1.(防止自包含)转换的对象包含自身对象,或者对象A下面挂了对象B,对象B下面又挂了 ...
- SOAPUI使用教程-验证SOAP服务
当soapUI创建一个功能性TestCase 一个很常见的场景是你想一些SOAP / WSDL服务验证响应检查返回正确的结果. 一旦你导入了您想要测试的WSDL服务这样做很容易: 添加一个新的SOAP ...