Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot create asynchronous JobLauncher #1655

Closed
ameraljovic opened this issue Oct 2, 2014 · 9 comments
Closed

Cannot create asynchronous JobLauncher #1655

ameraljovic opened this issue Oct 2, 2014 · 9 comments
Labels
status: duplicate A duplicate of another issue

Comments

@ameraljovic
Copy link

The Spring Boot implementations does not set any TaskExecutor so it defaults to SyncTaskExecutor. And there is no way (as I know) to override the default jobLauncher bean because of this line in BasicBatchConfigurer which creates a default jobLauncher:

@PostConstruct
    public void initialize() {
        try {
            this.transactionManager = createTransactionManager();
            this.jobRepository = createJobRepository();
            this.jobLauncher = createJobLauncher();
            this.jobExplorer = createJobExplorer();
        }
        catch (Exception ex) {
            throw new IllegalStateException("Unable to initialize Spring Batch", ex);
        }
    }

I also tried to autowire jobLauncher and set the TaskExecutor by myself. But because I can only autowire the interface, I can't call jobLauncher.setTaskExecutor(simpleAsyncTaskExecutor).

@baxterj
Copy link

baxterj commented Oct 3, 2014

How funny, i was having this problem just yesterday as well, with regular Spring. I was able to create my own SimpleJobLauncher by overriding the getJobLauncher() method and returning my own lazily-initialized instance. However my code breaks if I set a TaskExecutor that uses different threads.. probably just my implementation though.

@ameraljovic ameraljovic changed the title Cannot create asyncronous JobLauncher Cannot create asynchronous JobLauncher Oct 3, 2014
@wilkinsona
Copy link
Member

One option is to declare your own bean that implements BatchConfigurer.

To help with that, we could perhaps make createJobExplorer() protected so that you can extend BasicBatchConfigurer and override that one method.

@ameraljovic
Copy link
Author

You mean createJobExplorer()? Yes, you could do it that way.

@kakawait
Copy link
Contributor

In my case, I'm launching job from REST API, so I have to @Autowired JobLauncher inside my @Controller, I do like following (but I must not fit all case):

@Bean
public JobLauncher asyncJobLauncher() {
    SimpleJobLauncher jobLauncher = new SimpleJobLauncher();
    jobLauncher.setJobRepository(jobRepository);
    jobLauncher.setTaskExecutor(new SimpleAsyncTaskExecutor());
    return jobLauncher;
}

Pay attention on bean name, I must not be jobLauncher or it will not load.
Then when using it:

@Autowired
@Qualifier("asyncJobLauncher")
private JobLauncher jobLauncher

It's not clean but a bit easier than overriding whole BatchConfigurer that do not fit Spring Boot philosophy :)

But I'm waiting for a real solution

@Wifsimster
Copy link

@kakawait - I think i need to kiss you for this code 👍

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Nov 30, 2015
@wilkinsona wilkinsona removed the status: waiting-for-triage An issue we've not yet triaged label Nov 30, 2015
@tomerku
Copy link

tomerku commented Dec 28, 2016

Hi @kakawait,
When using the "public JobLauncher asyncJobLauncher()" my tasks aren't getting run at all,

What I did was:

@Autowired
private JobRepository jobRepository;

@Autowired
private Job job;

@Test
public void testLaunchJob() throws Exception {


	JobExecution execution = asyncJobLauncher().run(job, new JobParameters());

	System.out.println("\n\n\nTomer\n\n\n");

	System.out.println("Exit Status : " + execution.getStatus());
	System.out.println("Exit Status : " + execution.getAllFailureExceptions());
}

@Bean
public JobLauncher asyncJobLauncher() {
	SimpleJobLauncher jobLauncher = new SimpleJobLauncher();
	jobLauncher.setJobRepository(jobRepository);
	jobLauncher.setTaskExecutor(new SimpleAsyncTaskExecutor());
	return jobLauncher;
}

Help will be appreciated,

Thanks,
Tomer

@snicoll
Copy link
Member

snicoll commented Dec 28, 2016

@tomerku this tracker is not the right place for asking such question. Please ask them on Stackoverflow.

@tomerku
Copy link

tomerku commented Dec 28, 2016

Will do. Thanks.

@snicoll
Copy link
Member

snicoll commented Dec 28, 2016

Actually, I think this issue can be closed. createJobLauncher() is protected as of #4888 so all you need to do really is create your own BatchConfigurer that provides a custom JobLauncher.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: duplicate A duplicate of another issue
Projects
None yet
Development

No branches or pull requests

8 participants