java - 如何使用可执行jar运行spring计划任务

标签 java spring scheduled-tasks

我想开发一个独立的应用程序,它将按计划执行一些任务。我正在使用 spring @scheduled 和 taskscheduler 来实现这一点。我不能使用 spring boot,因为它需要 spring 4.* 并且我的 maven 项目依赖于其他使用 spring 3 的项目。 这是我的代码(reference):

Pom.xml :

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
    <groupId>com.pdp.ci</groupId>
    <artifactId>ci</artifactId>
    <version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>file-requester</artifactId>
<name>file-requester</name>
<packaging>jar</packaging>

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <java.version>1.7</java.version>
</properties>

<dependencies>
    <dependency>
        <groupId>com.pdp.ci</groupId>
        <artifactId>common-requester</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <exclusions>
            <exclusion>
                <groupId>org.apache.httpcomponents</groupId>
                <artifactId>httpclient</artifactId>
            </exclusion>
            <exclusion>
                <groupId>org.apache.httpcomponents</groupId>
                <artifactId>httpcore</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
    <dependency>
        <groupId>org.apache.httpcomponents</groupId>
        <artifactId>httpclient</artifactId>
        <version>4.4</version>
    </dependency>
    <dependency>
        <groupId>org.apache.httpcomponents</groupId>
        <artifactId>httpcore</artifactId>
        <version>4.4.4</version>
    </dependency>
</dependencies>

<build>
<plugins>
<plugin>
  <!-- Build an executable JAR -->
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-jar-plugin</artifactId>
  <version>3.0.2</version>
  <configuration>
  </configuration>
</plugin>

<plugin> 
<artifactId>maven-dependency-plugin</artifactId> 
<executions> 
  <execution> 
    <phase>install</phase> 
      <goals> 
        <goal>copy-dependencies</goal> 
      </goals> 
      <configuration> 
         <outputDirectory>${project.build.directory}/lib</outputDirectory> 
      </configuration> 
    </execution> 
  </executions> 
</plugin> 
</plugins>
</build>
</project>

包含预定方法的组件类:

package com.ci.ias;

@Component
public class CustomRequester{

    @Scheduled(fixedRate=2000)
    public void processFiles(){
        logger.info("Process started");
        //task logic
    }
}

配置类:

@Configuration
@EnableScheduling
@ComponentScan(basePackages="com.ci.ias")
public class Requester implements SchedulingConfigurer {

    @Override
    public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
        taskRegistrar.setScheduler(taskExecutor());
    }

    @Bean(destroyMethod="shutdown")
    public Executor taskExecutor() {
        return Executors.newScheduledThreadPool(100);
    }

}

我可以构建一个 jar。但无法运行,因为没有“主要”方法。如何使用命令行运行此调度程序?我是新手。谁能解释一下这是如何工作的?

谢谢。

最佳答案

如果你有 spring-boot jar(看起来你的父 jar 包含 spring-boot 的依赖项)

Spring 引导:

@SpringBootApplication
public class Application {

    public static void main(String[] args) {
        ApplicationContext ctx = SpringApplication.run(Application.class, args);


    }

}

java -jar yourjar.jar

或者只是创建一个main方法并调用那个类

public class AppMain {

    @SuppressWarnings({ "unused", "resource" })
    public static void main(String args[]){
        AbstractApplicationContext  context = new AnnotationConfigApplicationContext(Requester.class);
    }

}

然后你可以在你的 list 中添加这个主类

list .mf

Manifest-Version: 1.0
Main-Class: com.example.MainClass
Class-Path: anyjarsneededtorunapp.jar

调用java -jar yourjar.jar

关于java - 如何使用可执行jar运行spring计划任务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39378094/

相关文章:

java - 在java线程中每隔一小时运行一次作业

java - 当我使用 StatFs 获取 Android 设备的总内部存储大小时出错

java - 如何检查表单是否存在并打印表单的所有属性,HTMLUNIT JAVA

java - @Value注释语法问题

powershell - 使用 PowerShell 安装计划任务时失败时重试

java - 在 ScheduledThreadPoolExecutor 中终止之前等待任务完成

java - GRPC Java 登录测试

java - 如何在JKS中管理两个 key

java - DDD 什么时候应该创建域对象和持久化对象而不是将持久化对象用作域对象?

java - 带有 Spring Boot 应用程序的 Docker 卡在 RaspberryPi 上的 AnnotationConfigServletWebServerApplicationContext