zoukankan      html  css  js  c++  java
  • spring的定时任务配置

    工作中需求:每天早上定时将数据查询出来只作为excel上传至远程服务器

    配置spring的xml文件

    <!--配置spring的定时任务bean  触发器Trigger[] 集合 他的值为list里面的定时触发器对象 -->
        <bean id="schedulerTasksWeb"
              class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
            <property name="triggers">
                <list>
                    <ref bean="exportTask1" />
                </list>
            </property>
        </bean>
    
        <!--配置包扫描-->
        <context:component-scan base-package="com.spark.service.mkExcel"/>
        <!--定时导入-->
    
        <!--配置定时任务的rgetObject- 要定时执行哪个类哪个方法-->
        <bean id="testTask"
              class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
            <property name="concurrent" value="false" />
            <property name="targetObject" ref="test" />
            <property name="targetMethod" value="say" />
        </bean>
    
        <!--配置定时任务是哪个JobDetail类 引用上面的  cronExpression执行周期 与linux的crontab一样-->
        <bean id="exportTask1"
              class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
            <property name="jobDetail" ref="testTask" />
            <property name="cronExpression" value="*/10 * * * * ?" />
        </bean>
    服务端url的接口代码
    @RequestMapping(value = "/stat/upload")
        public Object uploadStat(@RequestParam(value="datafile", required = false)MultipartFile multipartFile) {
            if(multipartFile==null) {
                return false;
            }
            String fileName = FileUtil.uploadFile(multipartFile);
            if(fileName!=null) {
                ...
                return true;
            } else {
                return false;
            }
        }

    客户端上传文件值一个url接口的方法

    public class PostUpload {
    
      @Test
        public void UploadFile() {
            try {
                File imgFile = new File("E:\MyProject\sparkStu_web\data\ss.text");
                if (imgFile.exists()) {
                    HttpClient client = new DefaultHttpClient();
    
                    HttpPost httpPost = new HttpPost("http://xxxx/stat/upload");
                    MultipartEntity entity = new MultipartEntity();
                    entity.addPart("datafile", new FileBody(imgFile));
    
                    httpPost.setEntity(entity);
                    HttpResponse response = client.execute(httpPost);
    
                    HttpEntity responseEntity = response.getEntity();
                    String result = EntityUtils.toString(responseEntity);
                    System.out.println(result);
                    if ("false".equals(result)){
                        System.out.println("上传失败");
                    }
                    else{System.out.println("上传成功");}
    
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

    从Dao层借口返回的list集合数据形成excel

      List<ContPageDay> list = this.contPageDayService.expList2(params);
            log.error("数据共===="+list.get(list.size()));
            //HSSFWorkbook workbook = new HSSFWorkbook();
    //        XSSFWorkbook workbook = new XSSFWorkbook();
            SXSSFWorkbook workbook = new SXSSFWorkbook(1000);
            Sheet sheet = null;
            Row row;
            Cell cell;
    
    //        XSSFFont font =workbook.createFont();
            Font font =workbook.createFont();
            font.setColor(IndexedColors.RED.index);
            font.setFontHeightInPoints((short)12);
    //        font.setBoldweight(XSSFFont.BOLDWEIGHT_NORMAL);
            font.setBoldweight(Font.BOLDWEIGHT_NORMAL);
    //        XSSFCellStyle style1 = workbook.createCellStyle();
            CellStyle style1 = workbook.createCellStyle();
            style1.setFont(font);
    //        style1.setAlignment(XSSFCellStyle.ALIGN_CENTER);
            style1.setAlignment(CellStyle.ALIGN_CENTER);
    
            int sheetNum = 1;
    
            for (int i = 0; i < list.size(); i++) {
                int remainder = i % 65535;
                if (remainder == 0) {
                    sheet = workbook.createSheet("内容受访明细表" + sheetNum);
                    sheet.setColumnWidth(0, 20*256);
                    sheet.setColumnWidth(1, 20*256);
                    sheet.setColumnWidth(2, 20*256);
                    sheet.setColumnWidth(3, 20*256);
                    sheet.setColumnWidth(4, 20*256);
                    sheet.setColumnWidth(5, 20*256);
    
                    row = sheet.createRow(0);
                    row.setRowStyle(style1);
                    row.setHeight((short)350);
    
                    cell = row.createCell(0);
                    cell.setCellValue("日期");
                    cell.setCellStyle(style1);
    
                    cell = row.createCell(1);
                    cell.setCellValue("源内容ID");
                    cell.setCellStyle(style1);
    
                    cell = row.createCell(2);
                    cell.setCellValue("源内容标题");
                    cell.setCellStyle(style1);
    
                    cell = row.createCell(3);
                    cell.setCellValue("IP数");
                    cell.setCellStyle(style1);
    
                    cell = row.createCell(4);
                    cell.setCellValue("UV数");
                    cell.setCellStyle(style1);
    
                    cell = row.createCell(5);
                    cell.setCellValue("PV数");
                    cell.setCellStyle(style1);
                    sheetNum++;
                }
    //              
                row = sheet.createRow(remainder + 1);
                cell = row.createCell(0);
                cell.setCellValue(list.get(i).getStatDate());
                cell = row.createCell(1);
                cell.setCellValue(list.get(i).getSrcContId());
                cell = row.createCell(2);
                cell.setCellValue(list.get(i).getSrcContName());
                cell = row.createCell(3);
                cell.setCellValue(list.get(i).getIp());
                cell = row.createCell(4);
                cell.setCellValue(list.get(i).getUv());
                cell = row.createCell(5);
                cell.setCellValue(list.get(i).getPv());
    
            }
            File file=new File("E:\data\ss.xls");
            if (!file.exists()){
                file.mkdir();
            }
            FileOutputStream outputStream = new FileOutputStream(file);
            workbook.write(outputStream);
            outputStream.flush();
            outputStream.close();
            log.info("成功");
        }
  • 相关阅读:
    自己奋斗才有用,人要坚持到成功为止
    自己需要的都是靠自己争取,人生只有自己才靠得住
    不如意也是要过日子,人是不可能和天斗的
    懂得满足也是好的,人要学会知足才会感到幸福
    不要让自己有后悔的时候,人都是要不负自己的青春年少
    ES6 の 块级作用域
    javascript中this的指向分析
    let & var 的区别
    npm 全局安装环境配置
    visibilitychange => 获取页面的可见性
  • 原文地址:https://www.cnblogs.com/hejunhong/p/10446923.html
Copyright © 2011-2022 走看看