定时器中实现数据库表数据移动的功能,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 ...
随机推荐
- libcpmt.lib 与 msvcprt.lib
https://msdn.microsoft.com/en-us/library/2kzt1wy3(VS.80).aspx
- 会话控制:Cookie和session
HTTP(超文本传输协议)定义了通过万维网(WWW)传输文本.图形.视频和所有其他数据所有的规则.HTTP是一种无状态的协议,说明每次请求的处理都与之前或之后的请求无关.虽然这种简化实现对于HTTP的 ...
- Spring4.0编程式定时任务配置
看过很多定时调度的配置,大多使用XML配置,觉得比较麻烦,也比较老套.这里介绍一种基于spring4.0注解编程式配置定时任务,简单清晰,使用方便.. 至于引入spring相关jar这里不多说,直接切 ...
- thinkphp设置session有效时间
thinkphp的框架文件 ThinkPHP/Common/functions.php function session(){ 在这个方法中找到 thinkphp .1版 if(isset($name ...
- 如何持续集成/交付一个开源.NET函数库到Nuget.org
(此文章同时发表在本人微信公众号"dotNET每日精华文章",欢迎右边二维码来关注.) 题记:这是一个简单的入门向导,涉及到GitHub.AppVeyor和Nuget.org. 最 ...
- TextView链接点击和长按冲突
1.重写 import android.text.Layout; import android.text.Selection; import android.text.Spannable; impor ...
- WEB容器启动——web.xml加载详解
最近在看spring的源码,关于web.xml文件在容器(Tomcat.JBOSS等)启动时加载顺序问题很混乱,通过搜集资料,得出以下的结论: 1.加载顺序与它们在 web.xml 文件中的先后顺序无 ...
- FZU月赛20160416 ABEF
Problem A ABCDEFG Accept: 302 Submit: 442Time Limit: 1000 mSec Memory Limit : 32768 KB Proble ...
- Delphi中滚动文字的应用
1.添加一个Timer控件,Interval属性设置为20. 2.添加一个Label控件,Name为labMessage. 3.在Timer的OnTimer事件添加如下代码: procedure TF ...
- 数位DP GYM 100827 E Hill Number
题目链接 题意:判断小于n的数字中,数位从高到低成上升再下降的趋势的数字的个数 分析:简单的数位DP,保存前一位的数字,注意临界点的处理,都是套路. #include <bits/stdc++. ...