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定时器中线程报错。的更多相关文章

  1. jmeter 获取数据库表数据作为参数

    jmeter - 获取数据库表数据作为参数 在jmeter中使用数据库表数据首先需要设置数据库连接,然后在创建JDBC取样器 1.创建配置元件 JDBC Connection Configuratio ...

  2. C# 利用mysql.data 在mysql中创建数据库及数据表

    C# 利用mysql.data 在mysql中创建数据库及数据表 using System; using System.Collections.Generic; using System.Linq; ...

  3. MySQL数据库中查询数据库表、字段总数量,查询数据总量

    最近要查询一些数据库的基本情况,由于以前用oracle数据库比较多,现在换了MySQL数据库,就整理了一部分语句记录下来. 1.查询数据库表数量 #查询MySQL服务中数据库表数据量 SELECT C ...

  4. mssql sqlserver 使用sql脚本 清空所有数据库表数据的方法分享

    摘要: 下文讲述清空数据库中所有表信息的方法分享,如下所示: 实验环境:sql server 2008 实现思路: 1.禁用所有约束,外键 2.禁用所有触发器 3.删除表数据 4.开启触发器 5.开启 ...

  5. C#程序中从数据库取数据时需注意数据类型之间的对应,int16\int32\int64

    private void btn2_Click(object sender, RoutedEventArgs e)         {             using (SqlConnection ...

  6. db2数据库中查找数据库表

    模糊查找db2数据库中的数据库表: select tabname,remarks from syscat.tables where TABNAME like  'DM%' select 'DROP T ...

  7. 在function module 中向数据库插入数据

    http://www.sapjx.com/abap-function-module.html 1: 应该在function module 中向数据库插入数据

  8. MO拆分计划行程序中写入PRODUCTIONORDERS表数据出现重复导致报错(BUG)20180502

    错误提示:ORA-00001: 违反唯一约束条件 (ABPPMGR.C0248833319_6192)ORA-06512: 在 "STG.FP_MO_SPLIT", line 19 ...

  9. MSSQL 删除数据库表数据

    --删除数据库表数据 慎用 create PROCEDURE sp_DeleteAllData AS ) ) ) ) ) ) begin try begin tran -- 失效索引,触发器 open ...

随机推荐

  1. MyBatis持久层框架使用总结

    MyBatis 本是apache的一个开源项目iBatis, 2010年这个项目由apache software foundation 迁移到了google code,并且改名为MyBatis . 2 ...

  2. EXPORT_SYMBOL的使用

    转自:http://blog.csdn.net/cailiwei712/article/details/7998525 在查看内核驱动代码的时候会经常看到在一些函数后面总会跟EXPORT_SYMBOL ...

  3. SQL 2012 Restore HeaderOnly

    --USE [master] GO /****** Object: StoredProcedure [dbo].[zsp_RestoreHeaderOnly] Script Date: 2014/1/ ...

  4. Android 控件的显示隐藏上下左右移动动画

    一.利用Android提供的左右移动工具类:AnimationUtils 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 LinearLayout ll_fi ...

  5. getter & setter

    ECMAScript 5: setter和getter的使用方式: 1.set/get: var person = { _name: '', get name() { return this._nam ...

  6. UVA 12544 - Beehives O(nm) 无向图最小环

    Bees are one of the most industrious insects. Since they collect nectarand pollen from flowers, they ...

  7. C语言 右左法则

    C指针声明解读之左右法则 C语言所有复杂的指针声明,都是由各种声明嵌套构成的.如何解读复杂指针声明呢?右左法则是一个既著名又常用的方法.不过,右左法则其实并不是C标准里面的内容,它是从C标准的声明规定 ...

  8. CodeChef COUNTARI Arithmetic Progressions(分块 + FFT)

    题目 Source http://vjudge.net/problem/142058 Description Given N integers A1, A2, …. AN, Dexter wants ...

  9. Maven 的插件和生命周期的绑定

    一.Maven 的生命周期 Maven 的生命周期是对所有的构建过程进行抽象和统一.Maven 的生命周期是抽象的,这意味着生命周期本身不做任何实际的工作,生命周期只是定义了一系列的阶段,并确定这些阶 ...

  10. ACM: FZU 2110 Star - 数学几何 - 水题

     FZU 2110  Star Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u  Pr ...