• Junit 4 测试中使用定时任务操作


    难度:测试中执行线程操作

      1 package com.hfepc.job.dataCollection.test;
      2 
      3 import java.util.Date;
      4 import java.util.List;
      5 import java.util.Map;
      6 import java.util.Timer;
      7 import java.util.TimerTask;
      8 import java.util.concurrent.CountDownLatch;
      9 
     10 import javax.annotation.Resource;
     11 
     12 import org.junit.Test;
     13 import org.junit.runner.RunWith;
     14 import org.pentaho.di.job.Job;
     15 import org.slf4j.Logger;
     16 import org.slf4j.LoggerFactory;
     17 import org.springframework.test.context.ActiveProfiles;
     18 import org.springframework.test.context.ContextConfiguration;
     19 import org.springframework.test.context.transaction.TransactionConfiguration;
     20 import org.springframework.transaction.annotation.Transactional;
     21 
     22 import com.hfepc.service.dataCollection.IDataCollectionService;
     23 import com.hfepc.service.zkinsp.MySpringJUnit4ClassRunner;
     24 
     25 @SuppressWarnings("unused")
     26 @RunWith(MySpringJUnit4ClassRunner.class)
     27 @ContextConfiguration(locations = { "classpath*:spring-context.xml",
     28         "classpath*:spring-servlet.xml", "classpath*:cxf-client.xml" })
     29 @ActiveProfiles("development")
     30 @Transactional
     31 @TransactionConfiguration(transactionManager = "transactionManager", defaultRollback = false)
     32 public class DataCollectionTest extends Job {
     33 
     34     private final Logger log = LoggerFactory
     35             .getLogger(DataCollectionTest.class);
     36 
     37     @Resource(name = "IDataCollectionService")
     38     private IDataCollectionService IDataCollectionService;
     39 
     40     private final int i = 3;
     41     /*
     42      * 线程计数器
     43      *     将线程数量初始化
     44      *     每执行完成一条线程,调用countDown()使计数器减1
     45      *     主线程调用方法await()使其等待,当计数器为0时才被执行
     46      */
     47     private final CountDownLatch latch = new CountDownLatch(i);
     48 
     49     @Test
     50     public void createTest() {
     51             Timer timer;
     52             timer = new Timer();
     53             timer.schedule(new TimerTaskTest03(), 0, 60000);
     54             //三个参数 
     55             //第一个执行内容:内容是定时任务的执行内容,通过实现抽象类来完成这个动作  
     56             //第二个参数内容:是在第多少时间之后开始执行定时任务内容,该参数不能小于0 
     57             //第三个参数内容:是在两个任务之间的执行时间间隔,该参数不能小于等于0
     58             try {
     59                 latch.await(); // 主线程等待
     60             } catch (InterruptedException e) {
     61                 e.printStackTrace();
     62             } 
    70
    91 private class TimerTaskTest03 extends TimerTask { 92 93 @Override 94 public void run() { 95 Date date1 = new Date(this.scheduledExecutionTime()); 96 String date = DateUtils.format(DateUtils.getCurDate(), 97 "yyyy/MM/dd");
    99
    log.error("本次执行该线程的时间为:" + date1); 100 log.info("时间:" + date); 101 String sql = "SELECT A.[Date] AS 'date_sta',A.barcode1 AS 'barcode1_sta',A.barcode2 AS 'barcode2_sta',A.HeightNum1Range AS 'HeightNum1Range',A.HeightNum2Range AS 'HeightNum2Range',A.HeightNum3Range AS 'HeightNum3Range',A.HeightData AS 'HeightData',A.HeightDL AS 'HeightDL',A.Num AS 'Num',A.CCDFlag AS 'CCDFlag',A.barcode3 AS 'barcode3',A.PressData AS 'PressData',B.SpurtData AS 'SpurtData',B.[Date] AS 'date_glu',B.CCDODRange AS 'CCDODRange',B.CCDODValue AS 'CCDODValue',B.CCDODFlag AS 'CCDODFlag',B.CCDConcentricityRange AS 'CCDConcentricityRange',B.CCDConcentricityValue AS 'CCDConcentricityValue',B.CCDConcentricityFlag AS 'CCDConcentricityFlag',B.barcode2 AS 'barcode2_glu',B.Static AS 'Static',B.SheetIron AS 'SheetIron',B.VerticalityFlag AS 'VerticalityFlag',B.TorsionData AS 'TorsionData' FROM[dbo].[StaticComponentsProductionData] A LEFT JOIN [dbo].[GlueDispenserProductionData] B ON A.barcode1 = B.barcode1 WHERE A.[Date] LIKE '" 102 + date + "%'"; 103 List<Map<String, Object>> rows = IDataCollectionService 104 .readSQLData(sql); 105 log.info("返回标签:" + rows.size()); 106 log.info("返回标签:" + (String) rows.get(0).get("barcode2_sta")); 108 } 109 } 110 111 }
    痛苦预示着超脱
  • 相关阅读:
    scp命令
    遇到的错误解决方法
    阿里云挂载数据盘
    正则表达式
    python例子三
    Linux shell快捷键
    《超级产品的本质:汽车大王亨利福特自传》书评
    学习嵌入式的一点建议【转】
    win7使用USB转串口连接mini2440方法
    吐血原创:mini2440和win7笔记本利用无路由功能的交换机共享上网(使用x-router软路由)
  • 原文地址:https://www.cnblogs.com/supperlhg/p/8084516.html
Copyright © 2020-2023  润新知