分类 Java 下的文章

1、增加开启注解

//开启定时任务
@EnableScheduling
//开启异步调用方法
@EnableAsync
public class SpringBootStarterApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringBootStarterApplication.class, args);
    }

}

2、创建任务类

@Component
public class TestTask {
    private static final SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");
    //定义每过3秒执行任务
    @Scheduled(fixedRate = 3000)
    //@Scheduled(cron = "4-40 * * * * ?")
    public void reportCurrentTime() {
        System.out.println("现在时间:" + dateFormat.format(new Date()));
    }
}

一些解释

  1. fixedRate:如果任务的处理时间大于fixedRate配置的时间,那么当任务结束的时候则会立马执行。如果是在相隔的时间段内处理完毕,假设设置的是5s,任务花了3s执行完毕,那么在2s后则会执行下一个任务。
  2. fixedDelay:如果设定的是相隔5s执行,当前任务假设需要花费8s,那么下一次的执行的时间为当前任务执行完毕后,相隔5s后再执行。

1、pom.xml修改如下:

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <configuration>
                <mainClass>com.App</mainClass>
                <layout>ZIP</layout>
                <includes>
                    <include>
                        <groupId>nothing</groupId>
                        <artifactId>nothing</artifactId>
                    </include>
                </includes>
            </configuration>
            <executions>
                <execution>
                    <goals>
                        <goal>repackage</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    <plugins>
<build>

2、然后使用cmd命令获得分离出来的lib

mvn dependency:copy-dependencies -DoutputDirectory=F:\lib  -DincludeScope=runtime

3、运行java

java -Dloader.path=/path/to/lib -jar /path/to/springboot-0.0.1-SNAPSHOT.jar

全局所有用户生效:/etc/profile,仅单个用户生效:~/.bash_profile中最后添加

export JAVA_HOME=/usr/local/jdk/jdk1.8.0_311
export CLASSPATH=.:$JAVA_HOME/jre/lib/rt.jar:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
export PATH=$PATH:$JAVA_HOME/bin