我想开发一个独立的应用程序,它将按计划执行某些任务。我正在使用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);
}
}
我能够建立一个罐子。但无法运行,因为没有“主要”方法。如何使用命令行运行此调度程序?我是新手。任何人都可以解释这是如何工作的?
感谢。
答案 0 :(得分:1)
如果你有spring-boot jar(似乎你的父jar包含spring-boot的依赖项)
Spring-boot:
@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);
}
}
然后,您可以在清单中添加此主要类
<强> MANIFEST.MF 强>
Manifest-Version: 1.0
Main-Class: com.example.MainClass
Class-Path: anyjarsneededtorunapp.jar
致电 java -jar yourjar.jar