2016-09-07 10 views
2

私は、スケジュールどおりのタスクを実行するスタンドアロンアプリケーションを開発したいと考えています。私はこれを達成するためにspring @scheduledとtaskschedulerを使用しています。実行可能なjarを使用してスプリングスケジュールされたタスクを実行する方法

のpom.xml:それは春4. *必要があり、私のMavenプロジェクトは、春3 使用し、他のプロジェクトの依存関係を持っているように私はここに私のコード(reference)で春のブートを使用することはできません

<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クラス:

@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); 
    } 

} 
予定方式を含む

Componentクラス

私は瓶を作ることができます。しかし、「主な」方法がないので、実行することができません。コマンドラインを使ってこのスケジューラを実行するには?私はこれの初心者です。誰でもこのことがどのように機能するのか説明できますか?

ありがとうございました。

答えて

1

あなたは春ブート瓶を持っている場合

春ブーツ(春・ブートの依存関係が含まれているあなたの親の瓶がある好きなようだ):yourjar.jar -jar

@SpringBootApplication 
public class Application { 

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


    } 

} 

のjava

メインメソッドを作成し、そのクラスを呼び出すだけです。

public class AppMain { 

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

} 

次にあなたが

をyourjar.jar -jarマニフェスト

MANIFEST.MF

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

コールJavaでこのメインクラスを追加することができます

関連する問題