3

When i define more than one datasource in my spring cloud task application , it throws an exception. This is how i have defined the datasources

@Primary
@Bean(name="datasource1")
@ConfigurationProperties(prefix="spring.datasource")
public javax.sql.DataSource primaryDataSource() {
    return  DataSourceBuilder.create().build();
}

@Bean(name="datasource2")
@ConfigurationProperties(prefix="spring.datasource1")
public javax.sql.DataSource primaryDataSource1() {
    return  DataSourceBuilder.create().build();
}

@Bean
public TaskConfigurer taskConfigurer() {
    return new DefaultTaskConfigurer(primaryDataSource());
}

I have seen suggestions to put @Primary , defining TaskConfigurer like above, but none of them is working.Has any one faced this kind of problem ?

Thanks, Neel

Neel
  • 33
  • 6
  • how about using profile? – bananas Nov 19 '16 at 20:12
  • Hi Flying Zombie, Thanks for responding. Even if i use profile , i need to have both the datasources in all the profiles. So , problem would still come i guess. Not sure though – Neel Nov 19 '16 at 20:17
  • Can you show the exception which is thrown ? – ps-aux Nov 19 '16 at 20:34
  • I am still not sure what are you looking for? if you want to use multiple databases then you must change your approach like using `hibernate` and you have not specified what is your final goal, if you specify your end goal then it will be easy for others :) – bananas Nov 19 '16 at 20:57
  • This is the exception I am getting:Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.cloud.task.batch.configuration.TaskBatchExecutionListenerFactoryBean]: Factory method 'taskBatchExecutionListener' threw exception; nested exception is java.lang.IllegalStateException: Expected one datasource and found 2 End Goal: Configuring Spring cloud task application with multiple data sources. One data source will be used by Spring Cloud Task to store all the job execution details, other datasource will be used for my application data. – Neel Nov 19 '16 at 21:35

2 Answers2

3

You're going to need to override the listener. It, like the other autoconfig around tasks, doesn't know what datasource to use when you've defined more than one. I've created an issue to address this in a future version: https://github.com/spring-cloud/spring-cloud-task/issues/252

Michael Minella
  • 20,843
  • 4
  • 55
  • 67
  • Thanks for the response Michael. Do i need to override both the listeners TaskBatchExecutionListener and TaskBeanExecutionListener. If that is the case, can you provide instructions or pointers to documentation as to how to create these configurations? – Neel Nov 22 '16 at 05:05
  • I'm not familier with the `TaskBeanExecutionListener`. If you mean the `TaskLifecycleListener`, no. You can use a `TaskConfigurer` to address the configuration of that. The fix for the issue above will actually be using the `TaskConfigurer` to configure the `TaskBatchExecutionListener` as well. – Michael Minella Nov 28 '16 at 15:32
  • Could you please let me know the steps how i can use DefaultTaskConfigurer to configure TaskBatchExecutionListener. Internally TaskBatchExecutionListener is using TaskBatchDao, JdbcTaskBatchDao , do i need to create those beans with manually providing the data sources. Thanks – Neel Nov 30 '16 at 05:50
  • You can't use the `DefaultTaskConfigurer` yet. We haven't added that capability. – Michael Minella Nov 30 '16 at 16:21
  • I am actually confused how TaskConfigurer and TaskBatchExecutionListener are connected. I dont see any method relating to TaskBatchExecutionListener in TaskConfigurer interface. Are you saying update TaskConfigurer interface to add one more method which gives TaskBatchExecutionListener bean. Then do i need to implement TaskConfigurer interface myself. Otherwise keep DefaultTaskConfigurer as it is and write a class which extends TaskBatchExecutionListener and provide dependencies TaskBatchDao/JdbcExecutionTaskBatchDao manually. Please correct me if I am going towards wrong direction. – Neel Nov 30 '16 at 20:33
  • They are not connected yet. It will be in a future release. All you want to do is to override the definition for the `TaskBatchExecutionListener` and configure it with what you require. – Michael Minella Nov 30 '16 at 20:43
  • I have written a class which extends TaskBatchExecutionListener. Now I am getting a different exception with message "The application context is required to have exactly 1 instance of the TaskBatchExecutionListener but has 2". When i debugged the code looks like its failing at TaskBatchExecutionListenerBeanPostProcessor. postProcessBeforeInitialization() method. – Neel Nov 30 '16 at 22:57
  • Override the bean definition...not extend the class. – Michael Minella Dec 01 '16 at 15:05
1

You need to override DefaultTaskConfigurer

@Configuration
public class BatchConfigurer extends DefaultTaskConfigurer
{

    public BatchConfigurer(@Qualifier("batchDataSource") DataSource dataSource) 
    {
        super(dataSource);
    }

}
Dhruv Saksena
  • 209
  • 2
  • 13