2016-10-07 27 views
3

Springブートプロジェクト内でSpringバッチを構成しようとしています。データソースなしで使用したいのですが。私はResourcelessTransactionManagerが行く方法だが、私はそれを動作させることができないことがわかった。問題はすでに3つの別のdataSourcesが定義されていますが、springBatchでそれらのいずれかを使用したくないということです。DataSourceを持たないSpringブート+ Springバッチ

私はデフォルト実装DefaultBatchConfigurerをチェックしましたが、dataSourceが見つからない場合は、私が欲しいものを正確に行います。問題は、私はそれらの3つを持っていないし、任意のものを使用したいです。

私はそれを望んでいないので、hsqlまたは他のメモリDBを使用することをお勧めしません。

+0

に初期化子を設定する私が何をしたいことは不可能だと思います。少なくとも使用とメモリ内のデータベース –

+0

このhttp://docs.spring.io/spring-batch/reference/html/configureJob.html#inMemoryRepositoryによると私は正しく理解することが可能でなければなりません。 – Majky

+0

次にSpring Batchを手動で設定し、自動設定を使用しないでください。あなたが望むことをする独自の 'BatchConfigurer'を作成してください。 Beanとして登録し、バッチはデータソースなしで設定されます。 –

答えて

1

@SpringBootApplicationのDataSourceAutoConfigurationを除外することができます。以下のサンプルコードを参照してください。

import org.springframework.batch.core.Job; 
import org.springframework.batch.core.Step; 
import org.springframework.batch.core.StepContribution; 
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing; 
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory; 
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory; 
import org.springframework.batch.core.scope.context.ChunkContext; 
import org.springframework.batch.core.step.tasklet.Tasklet; 
import org.springframework.batch.repeat.RepeatStatus; 
import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.boot.SpringApplication; 
import org.springframework.boot.autoconfigure.SpringBootApplication; 
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration; 
import org.springframework.context.annotation.Bean; 

@SpringBootApplication(exclude = { DataSourceAutoConfiguration.class }) 
@EnableBatchProcessing 
public class SampleBatchApplication { 

@Autowired 
private JobBuilderFactory jobs; 

@Autowired 
private StepBuilderFactory steps; 

@Bean 
protected Tasklet tasklet() { 

    return new Tasklet() { 
     @Override 
     public RepeatStatus execute(StepContribution contribution, ChunkContext context) { 
      return RepeatStatus.FINISHED; 
     } 
    }; 
} 

@Bean 
public Job job() throws Exception { 
    return this.jobs.get("job").start(step1()).build(); 
} 

@Bean 
protected Step step1() throws Exception { 
    return this.steps.get("step1").tasklet(tasklet()).build(); 
} 

public static void main(String[] args) throws Exception { 
    System.exit(SpringApplication.exit(SpringApplication.run(SampleBatchApplication.class, args))); 
    } 
} 

とサンプルテストクラス

import org.junit.Rule; 
import org.junit.Test; 
import org.springframework.boot.SpringApplication; 
import org.springframework.boot.test.rule.OutputCapture; 
import static org.assertj.core.api.Assertions.assertThat; 
public class SampleBatchApplicationTests { 
@Rule 
public OutputCapture outputCapture = new OutputCapture(); 

@Test 
public void testDefaultSettings() throws Exception { 
    assertThat(SpringApplication.exit(SpringApplication.run(SampleBatchApplication.class))).isEqualTo(0); 
    String output = this.outputCapture.toString(); 
    assertThat(output).contains("completed with the following parameters"); 
    } 
} 
+0

これは動作していません – Majky

+0

サンプルクラスとテストクラスを追加しました。それは私のために働いています。私はデータソースを使いたくないので、私はResourcelessTransactionManagerを使っていません。これを実行すると、「データソースが提供されていません...マップベースのJobRepositoryを使用しています」と「No TaskExecutorが設定されていて、同期エグゼキュータがデフォルトに設定されています。 – abaghel

+0

正確には、私は3つのデータソースを持っていますが、バッチにそれらのどれかを使いたくありません。ごめんなさい。 – Majky

2

、ご使用の構成に複数のDataSource(関係なく、あなたがそれらを使用する場合のかどうか)がある場合は、あなたがあなた自身のBatchConfigurerを定義する必要があります。これは、フレームワークがそのような状況で何をすべきかを知る唯一の方法です。

現在のドキュメントでBatchConfigurer詳細を読むことができます:http://docs.spring.io/spring-batch/trunk/apidocs/org/springframework/batch/core/configuration/annotation/BatchConfigurer.html

+0

これは私がこのシナリオを動作させるための唯一の方法です。ただし、明確にするためには、自分のBatchConfigurerクラスを完全に実装する必要があります。 'DefaultBatchConfigurer'を拡張するだけでは不十分です。これは' DataSource'を探すためです。私は、関連するスニペットを 'DefaultBatchConfigurer'からコピーしました。あなたのクラスが '@ Component'としてマークされている限り、他のものより優先されるはずです。 – Mac

+1

別の注意点は、私はモジュラーバッチ構成を使用し、自分のconfigクラスがロードされる方法を制御する必要があるということです。私はGitHubに非常にシンプルなデモプロジェクトを入れ、関連する部分を説明しました:https://github.com/macdaddyaz/spring-batch-inmem – Mac

3

が、私はそれが任意のデータソースを無視するように、それはマップベースJobRepositoryを設定します結果として、DefaultBatchConfigurerクラスを拡張することでこの問題を回避ました。

例:

@Configuration 
@EnableBatchProcessing 
public class BatchConfig extends DefaultBatchConfigurer { 

    @Override 
    public void setDataSource(DataSource dataSource) { 
     //This BatchConfigurer ignores any DataSource 
    } 
} 
1

我々は、同様の問題を持っていた私たちは春のブートJDBCを使用していたし、私たちはDBに春のバッチテーブルを格納したくなかったが、我々はまだ私たちのために春のトランザクション管理を使用していました情報元。

独自のBatchConfigurerの実装が終了しました。

@Component 
public class TablelessBatchConfigurer implements BatchConfigurer { 
    private final PlatformTransactionManager transactionManager; 
    private final JobRepository jobRepository; 
    private final JobLauncher jobLauncher; 
    private final JobExplorer jobExplorer; 
    private final DataSource dataSource; 

    @Autowired 
    public TablelessBatchConfigurer(DataSource dataSource) { 
     this.dataSource = dataSource; 
     this.transactionManager = new DataSourceTransactionManager(this.dataSource); 

     try { 
      final MapJobRepositoryFactoryBean jobRepositoryFactory = new MapJobRepositoryFactoryBean(this.transactionManager); 
      jobRepositoryFactory.afterPropertiesSet(); 
      this.jobRepository = jobRepositoryFactory.getObject(); 

      final MapJobExplorerFactoryBean jobExplorerFactory = new MapJobExplorerFactoryBean(jobRepositoryFactory); 
      jobExplorerFactory.afterPropertiesSet(); 
      this.jobExplorer = jobExplorerFactory.getObject(); 

      final SimpleJobLauncher simpleJobLauncher = new SimpleJobLauncher(); 
      simpleJobLauncher.setJobRepository(this.jobRepository); 
      simpleJobLauncher.afterPropertiesSet(); 
      this.jobLauncher = simpleJobLauncher; 
     } catch (Exception e) { 
      throw new BatchConfigurationException(e); 
     } 
    } 
    // ... override getters 
} 

と偽

spring.batch.initializer.enabled=false 
関連する問題