定时器中实现数据库表数据移动的功能,Exception in thread "Timer-0" isExist java.lang.NullPointerException定时器中线程报错。

 1 package com.shawnway.trade.marketdata.constants;
 2 import java.sql.SQLException;
 3 import java.util.Calendar;
 4 import java.util.Date;
 5 import java.util.Timer;
 6 import java.util.TimerTask;
 7 
 8 import javax.persistence.EntityManager;
 9 import javax.persistence.PersistenceContext;
10 
11 import org.slf4j.Logger;
12 import org.slf4j.LoggerFactory;
13 import org.springframework.beans.factory.annotation.Autowired;
14 import org.springframework.stereotype.Component;
15 import org.springframework.web.bind.annotation.RequestMapping;
16 import org.springframework.web.bind.annotation.RequestMethod;
17 import org.springframework.web.bind.annotation.ResponseBody;
18 
19 import com.shawnway.trade.marketdata.services.ChartService;
20 
21 @Component
22 public class TimerConfig {
23         @Autowired
24         private ChartService chartService;
25         @PersistenceContext
26         private  EntityManager em;
27         public TimerConfig(ChartService ct){//关键点解决 null指针错误,
28             chartService=ct;
29         }
30       // Timer.scheduleAtFixedRate(TimerTask task,Date firstTime,long period)
31       //每天的24:00:00执行迁移操作
32     public void init() {
33         Calendar calendar = Calendar.getInstance();
34         calendar.set(Calendar.HOUR_OF_DAY, 15); // 控制时
35         calendar.set(Calendar.MINUTE, 3);    // 控制分
36         calendar.set(Calendar.SECOND, 0);    // 控制秒
37      
38         Date time = calendar.getTime();   
39      
40         Timer timer = new Timer();
41         timer.scheduleAtFixedRate(new TimerTask() {
42        
43           public void run() {
44               System.out.println("处理器开始运行");
45               
46             try {
47                 moveStableMes();
48             } catch (SQLException e) {
49                 // TODO Auto-generated catch block
50                 e.printStackTrace();
51             }
52           }
53         }, time, 1000 * 60 * 60 * 24);// 这里设定将延时每天固定执行
54     }
55       @RequestMapping(value ="/move/moveStableMes", method = RequestMethod.GET)
56       @ResponseBody
57     public  void moveStableMes() throws SQLException{
58         //首先判定存储表是否存在,如果存在则转移数据
59         //如果不存在,先创建该存储数据表,在转移数据
60         //情况临时存储表。(临时表是用来保存当天的数据。)
61         Calendar c = Calendar.getInstance();
62         int year=c.get(c.YEAR);
63         int month=c.get(c.MONTH)+1;
64         int today = c.get(c.DAY_OF_MONTH);
65         String tbname="";
66         if(month<=9) 
67             tbname="market_data_candlechart_"+Integer.toString(year)+"0"+Integer.toString(month);//数据库的名字
68         else 
69             tbname="market_data_candlechart_"+Integer.toString(year)+Integer.toString(month);//数据库的名字
70         String temperTB="market_data_candlechart";//存储临时信息的表名
71         System.out.println("执行到了isExist");
72         boolean flag=chartService.isExist(tbname);//判定对应的数据库是否存在
73         System.out.println("isExist结束");
74         if(flag){//如果已经存在了,就可以直接把临时表中的数据插入到对应的表中
75             chartService.moveMesToOldTB(tbname,temperTB);//将临时表中的数据移动到tbname名称的表中。    
76         }else{
77             chartService.createTemperTB(tbname);//如果不存在,需要先创建一个对应名称的表
78             chartService.moveMesToOldTB(tbname,temperTB);//将临时表中的数据移动到tbname名称的表中。
79         }
80         //转移完数据后,清洗临时表的数据
81         chartService.deletTemperTB(temperTB);
82     }
83     
84 }

 

 1 package com.shawnway.trade.marketdata;
 2 
 3 
 4 
 5 import java.io.FileNotFoundException;
 6 
 7 import org.apache.catalina.Server;
 8 import org.apache.catalina.Service;
 9 import org.apache.catalina.connector.Connector;
10 import org.apache.catalina.valves.RemoteIpValve;
11 import org.springframework.beans.factory.annotation.Autowired;
12 import org.springframework.boot.SpringApplication;
13 import org.springframework.boot.autoconfigure.SpringBootApplication;
14 import org.springframework.boot.context.embedded.ConfigurableEmbeddedServletContainer;
15 import org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizer;
16 import org.springframework.boot.context.embedded.tomcat.TomcatConnectorCustomizer;
17 import org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory;
18 import org.springframework.context.annotation.Bean;
19 import org.springframework.context.annotation.PropertySource;
20 import org.springframework.core.env.Environment;
21 
22 import com.shawnway.trade.marketdata.constants.SystemConfig;
23 import com.shawnway.trade.marketdata.constants.TimerConfig;
24 import com.shawnway.trade.marketdata.core.collect.MarketDataCollectHandler;
25 import com.shawnway.trade.marketdata.core.ctp.CTPApiHandler;
26 import com.shawnway.trade.marketdata.core.ctp.CTPGatewayProxy;
27 import com.shawnway.trade.marketdata.core.ctp.CTPMarketDataHandler;
28 import com.shawnway.trade.marketdata.core.ctp.CTPZeroMQHandler;
29 import com.shawnway.trade.marketdata.core.es.EsMarketDataHandler;
30 import com.shawnway.trade.marketdata.core.es.EsunnyApiHandler;
31 import com.shawnway.trade.marketdata.core.es.EsunnyGatewayProxy;
32 import com.shawnway.trade.marketdata.core.sp.SharppointApiHandler;
33 import com.shawnway.trade.marketdata.core.sp.SharppointGatewayProxy;
34 import com.shawnway.trade.marketdata.core.sp.SpMarketDataHandler;
35 import com.shawnway.trade.marketdata.services.ChartService;
36 import com.shawnway.trade.marketdata.services.MapContainer;
37 
38 @PropertySource({ "file:${config.dir}/config/web.properties" })
39 @SpringBootApplication
40 public class ApplicationLauncher {
41     @Autowired
42     private Environment env;
43     @Autowired
44     private ChartService chartService;
45     
46     @Bean(name = { "timerConfig" }, initMethod = "init")
47     public TimerConfig timerConfig() {
48         System.out.println("timerConfig已经开始运行了~");
49         return new TimerConfig(chartService);//
chartService传入进去,解决空指针的错误


50  } 51 public static void main(String[] args) throws Exception { 52 System.setProperty("config.dir", System.getProperty("user.dir")); 53 final String dir = System.getProperty("config.dir"); 54 System.setProperty("logging.config", dir + "/config/logging.xml"); 55 SpringApplication.run(ApplicationLauncher.class, args); 56  } 57 }

 

posted @ 2016-09-02 15:23  Adding  阅读(7490)  评论(0编辑  收藏  举报