定时器中实现数据库表数据移动的功能,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 ...
随机推荐
- .NET 获取客户端的操作系统版本、浏览器版本和IP地址
我们在使用.NET做网站的时候,很多情况下需要需要知道客户端的操作系统版本和浏览器版本,怎样获取客户端的操作系统和浏览器版本呢?我们可以通过分析UserAgent来获取. .NET 获取客户端的操作系 ...
- Android ImageView的scaleType属性与adjustViewBounds属性(转)
ImageView的scaleType的属性有好几种,分别是matrix(默认).center.centerCrop.centerInside.fitCenter.fitEnd.fitStart.fi ...
- python base64的加密与解密
Base64编码是一种“防君子不防小人”的编码方式.广泛应用于MIME协议,作为电子邮件的传输编码,生成的编码可逆,后一两位可能有“=”,生成的编码都是ascii字符. 优点:速度快,ascii字符, ...
- Linux环境下Nginx配置安装PHP
下边的安装配置方法,我试了一晚上没有成功,可能因为我的系统环境比较复杂,所以建议: 先安装PHP.使用yum命令安装,在安装配置MySQL,具体做法看博客中其他文章,至于Nginx服务器可以安装完这两 ...
- Daily Scrum Meeting ——NinthDay
一.Daily Scrum Meeting照片 二.Burndown Chart 三.项目进展 1.用户管理 2.下拉框与界面的整合 四.问题困难 黄志明(PM):Android Studio自带的S ...
- tomcat找不到class的情况分析
例如:java.lang.ClassNotFoundException: org.apache.axis2.AxisFault 1,真实的缺包,这是使用该jar包的java程序也会一般会直接报错,无法 ...
- BZOJ1453: [Wc]Dface双面棋盘
Description Input Output Sample Input Sample Output HINT 线段树套并查集应该是比较好写的做法,时间复杂度为O(N^3+M*NlogN). #in ...
- c# 获取项目的根目录
编写程序的时候,经常需要用的项目根目录.自己总结如下 1.取得控制台应用程序的根目录方法 方法1.Environment.CurrentDirectory 取得或设置当前工作目录的完整限定路径 ...
- 使用Git进行项目管理
首先在https://git.oschina.net进行注册以及登陆 登陆进去之后,如果想要创建项目,可以在 点击加号按钮,进行项目创建 3.这里以创建私有项目为例: 输入完成后,点击“创建”,进入下 ...
- C# 禁止修改已装箱了的值类型的字段值,但是可以通过接口的方式实现
C# 默认是不能修改已装箱了的值类型中字段的值,但是可以通过 值类型实现指定的接口来改变 首先定义一个接口 interface IChange { void Change(int a, int b); ...