定时器中实现数据库表数据移动的功能,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 ...
随机推荐
- Tensorflow word2vec编译运行
Word2vec 更完整版本(非demo)的代码在 tensorflow/models/embedding/ 首先需要安装bazel 来进行编译 bazel可以下载最新的binary安装文件, ...
- thinphp下拉获取更多瀑布流效果
html页面 <body> <script type="text/javascript" src="jquery.min.js">< ...
- 初学微信小程序
最近微信推出了微信小程序,为此我学了几天,基本了解了组件及简单语法,但是今天我自己想要独立写一个demo时,忽然发现难道我的不是微信小程序的语法(我以前是iOS 开发,不用css),而是css样式的设 ...
- Tomcat中JVM内存溢出及合理配置及maxThreads如何配置(转)
来源:http://www.tot.name/html/20150530/20150530102930.htm Tomcat本身不能直接在计算机上运行,需要依赖于硬件基础之上的操作系统和一个Java虚 ...
- CodeChef COUNTARI Arithmetic Progressions(分块 + FFT)
题目 Source http://vjudge.net/problem/142058 Description Given N integers A1, A2, …. AN, Dexter wants ...
- 集中式vs分布式区别
记录一下我了解到的版本控制系统,集中式与分布式,它们之间的区别做下个人总结. 什么是集中式? 集中式开发:是将项目集中存放在中央服务器中,在工作的时候,大家只在自己电脑上操作,从同一个地方下载最新版本 ...
- [软件推荐]Windows文件夹多标签工具Clover
Clover 是 Windows Explorer 资源管理器的一个扩展,为其增加类似谷歌 Chrome 浏览器的多标签页功能,目前最新版本为:3.1.7 Clover 把 Chrome 标签页有的样 ...
- JSHint Options 翻译
Enforcing options When set to true, these options will make JSHint produce more warnings about your ...
- NOI 题库 7624
7624 山区建小学 描述 政府在某山区修建了一条道路,恰好穿越总共m个村庄的每个村庄一次,没有回路或交叉,任意两个村庄只能通过这条路来往.已知任意两个相邻的村庄之间的距离为di(为正整数),其中, ...
- BZOJ3069: [Pa2011]Hard Choice 艰难的选择
Description Byteasar是一个很纠结的人.每次他经过Bytetown的时候都知道有至少2条不同的路径可以选择,这导致他必须花很长时间来决定走哪条路.Byteasar最近听说了Bytet ...