定時器中實現數據庫表數據移動的功能,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 }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM