java 定时器 数据库表数据移动

该博客围绕Java定时器实现数据库表移动展开,虽未给出具体内容,但核心是利用Java定时器这一工具,达成数据库表的移动操作,属于后端开发中数据库管理相关内容。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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);
    } 
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值