计算Spring Job的执行时间

时间:2019-04-15 00:01:56

标签: java spring spring-boot

我有这个非常基本的Spring Job示例。

@Scheduled(fixedRate = 90000)
    public void myScheduler() throws Exception {

        // Print here execution time into console and save into DB
    }

我必须做一些非常繁重的计算。有没有一种方法可以计算总执行时间?侦听器有一个解决方案,但是我想在作业内部执行此操作,因为我想在Job实施代码中执行此操作。

4 个答案:

答案 0 :(得分:6)

使用@Aspect

可以很好地完成

首先,添加到您的pom.xml

<dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-aop</artifactId>
</dependency>

第二,确保您的班级有@Component@EnableScheduling

最后,为Spring的Scheduled注释创建一个Aspect类

@Aspect
@Component
public class TimeScheduler {

    @Around("@annotation(org.springframework.scheduling.annotation.Scheduled)")
    public void timeScheduledMethod(ProceedingJoinPoint joinPoint) throws Throwable {
        System.out.println("starting stopwatch");
        Object result = null;
        StopWatch watch = new StopWatch();
        try {
            watch.start();
            result = joinPoint.proceed();
        } finally {
            watch.stop();
            long executionTime = watch.getLastTaskTimeMillis();
            String className = joinPoint.getTarget().getClass().getSimpleName();
            String methodName = joinPoint.getSignature().getName();

            // print to log/console all the details you need: Time took, 
            // method name, class name etc...
            System.out.println("Time took: [" + executionTime + "] ms");
            System.out.println("Class: " + className);
            System.out.println("Method: " + methodName);

           // db.save(executionTime)
        }
    }
} 

请注意,通过这种方式,需要从Aspect类中保留executeTime时间,因为使用@Scheduled的方法无法获取任何要保存的参数。

答案 1 :(得分:2)

如果您的项目/应用很轻,则可以放一些:

long startTime = System.getCurrentTimeInMillis();
// whatever long processing here
long endTime = System.getCurrentTimeInMillis();
long processingTime = endTime - startTime;
// do print 
// do store to DB

但是,如果您的项目/应用很大(即大量的Scheduler),则可能要向其注入方面(也称为切入点)之前/之后。

请参阅this Stackoverflow answer

答案 2 :(得分:0)

您可以使用Spring Framework的StopWatch来计算执行时间。对于多个任务,您也可以使用秒表。

    StopWatch stopWatch = new StopWatch("StopWatchDemo");
    StopWatchDemo stopWatchDemo = new StopWatchDemo();

    stopWatch.start("StopWatchTask1");
       stopWatchDemo.performTask1();
    stopWatch.stop();

    stopWatch.start("StopWatchTask2");
        stopWatchDemo.performTask2();
    stopWatch.stop();

获取所有任务(此处为task1和task2)的总执行时间

    System.out.println("StopWatchDemo tasks took total: " + stopWatch.getTotalTimeSeconds() + " seconds");

获取明智的任务执行时间

for(TaskInfo taskInfo :stopWatch.getTaskInfo()) {
    System.out.println(taskInfo.getTaskName()+"-"+taskInfo.getTimeSeconds()+" secs");
}

答案 3 :(得分:0)

@echo off
setlocal

title Shutdown Script
color 0A

set seconds=1

:start
cls
echo.
echo Select a number:
echo.
echo [1] Restart (Default Setting)
echo.
echo [2] Restart Reregister Applications
echo.
echo [3] Restart UEFI/BIOS
echo.
echo [4] Restart Advanced Boot Options
echo.
echo [5] Shutdown (Default Setting)
echo.
echo [6] Shutdown Reregister Applications
echo.
echo [7] Sign Out User
echo.
echo [8] Exit
echo.

choice /c 12345678 /m "Enter your choice:"
if errorlevel 8 goto :end
if errorlevel 7 (
cls
echo.
echo Sign out
choice /c yne /m "Are you sure you want to continue Y or N or [E]xit?"
if errorlevel 3 goto end
if errorlevel 2 goto startover
if errorlevel 1 shutdown /l
goto error
)

if errorlevel 6 (
cls
echo.
echo Shutdown PC and Re-register any applications on next boot
choice /c yne /m "Are you sure you want to continue Y or N or [E]xit?"
if errorlevel 3 goto end
if errorlevel 2 goto startover
if errorlevel 1 shutdown /sg /t %seconds%
goto error
)

if errorlevel 5 (
cls
echo.
echo Shutdown PC ^(Default Setting^)
choice /c yne /m "Are you sure you want to continue Y or N or [E]xit?"
if errorlevel 3 goto end
if errorlevel 2 goto startover
if errorlevel 1 shutdown /s /t %seconds%
goto error
)

if errorlevel 4 (
cls
echo.
echo Restart PC and load the advanced boot options menu
choice /c yne /m "Are you sure you want to continue Y or N or [E]xit?"
if errorlevel 3 goto end
if errorlevel 2 goto startover
if errorlevel 1 shutdown /r /o /t %seconds%
goto error
)

if errorlevel 3 (
cls
echo.
echo Restart PC to UEFI/BIOS menu
choice /c yne /m "Are you sure you want to continue Y or N or [E]xit?"
if errorlevel 3 goto end
if errorlevel 2 goto startover
if errorlevel 1 shutdown /r /fw /t %seconds%
goto error
)

if errorlevel 2 (
cls
echo.
echo Restart PC and Re-register any applications
choice /c yne /m "Are you sure you want to continue Y or N or [E]xit?"
if errorlevel 3 goto end
if errorlevel 2 goto startover
if errorlevel 1 shutdown /g /t %seconds%
goto error
)

if errorlevel 1 (
cls
echo.
echo Restart PC ^(Default Setting^)
choice /c yne /m "Are you sure you want to continue Y or N or [E]xit?"
if errorlevel 3 goto end
if errorlevel 2 goto startover
if errorlevel 1 shutdown /r /t %seconds%
goto error
)

:startover
cls
echo.
echo Restarting script
timeout 2 >nul
goto start

:error
cls
echo.
echo You might be here because of a bad input selection
timeout 2 >nul
echo.
echo Perhaps try another input
endlocal
exit /b

:end
cls
echo.
echo Exiting
timeout 2 >nul
endlocal
exit /b

即时类需要Java8