Конфигуратор Spring Batch неправильно переопределил диспетчер транзакций с помощью jarJAVA

Программисты JAVA общаются здесь
Ответить
Anonymous
 Конфигуратор Spring Batch неправильно переопределил диспетчер транзакций с помощью jar

Сообщение Anonymous »

Я работаю над проектом Spring Batch и новичок в Spring. Когда мы запускаем его из IntelliJ, все работает нормально, и мы видим такую ​​строку:
2024-10-03 07:22:29 [main] INFO [org.springframework.beans.factory.support.DefaultListableBeanFactory] DefaultListableBeanFactory.registerBeanDefinition:821 - Overriding bean definition for bean 'transactionManager' with a different definition: replacing [Root bean: class [null]; scope=; abstract=false; lazyInit=false; autowireMode=3; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=org.springframework.batch.core.configuration.annotation.SimpleBatchConfiguration; factoryMethodName=transactionManager; initMethodName=null; destroyMethodName=(inferred); defined in class path resource [org/springframework/batch/core/configuration/annotation/SimpleBatchConfiguration.class]] with [Root bean: class [null]; scope=; abstract=false; lazyInit=false; autowireMode=3; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=abcToXyzTransactionManagerConfiguration; factoryMethodName=transactionManager; initMethodName=null; destroyMethodName=(inferred); defined in class path resource [com/workspace/xyz/batch/abctoxyz/configuration/AbcToXyzTransactionManagerConfiguration.class]]

Однако, когда мы собираем jar и запускаем его, мы получаем прямо противоположное, и это не работает:
2024-10-03 07:25:12 [main] INFO [org.springframework.beans.factory.support.DefaultListableBeanFactory] DefaultListableBeanFactory.registerBeanDefinition:821 - Overriding bean definition for bean 'transactionManager' with a different definition: replacing [Root bean: class [null]; scope=; abstract=false; lazyInit=false; autowireMode=3; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=abcToXyzTransactionManagerConfiguration; factoryMethodName=transactionManager; initMethodName=null; destroyMethodName=(inferred); defined in class path resource [com/workspace/xyz/batch/abctoxyz/configuration/AbcToXyzTransactionManagerConfiguration.class]] with [Root bean: class [null]; scope=; abstract=false; lazyInit=false; autowireMode=3; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=org.springframework.batch.core.configuration.annotation.SimpleBatchConfiguration; factoryMethodName=transactionManager; initMethodName=null; destroyMethodName=(inferred); defined in class path resource [org/springframework/batch/core/configuration/annotation/SimpleBatchConfiguration.class]]

Я также прилагаю соответствующие файлы.
BatchConfiguration.java
@Configuration
@EnableBatchProcessing
public class BatchConfiguration {

@Autowired
private JobBuilderFactory jobBuilderFactory;

@Autowired
private StepBuilderFactory stepBuilderFactory;

@Autowired
@Qualifier("abcDs")
private DataSource abcDataSource;

@Autowired
@Qualifier("xyzDs")
private DataSource xyzDataSource;

@Autowired
private PlatformTransactionManager transactionManager;

@Autowired
private AbcToXyzConfiguration abcToXyzConfiguration;

@Autowired
private FileSystemOptions abcFileSystemOptions;

@Autowired
private FileSystemOptions xyzFileSystemOptions;

@Bean
public BatchConfigurer batchConfigurer() {
return new AbcToXyzBatchConfigurer(xyzDataSource, transactionManager);
}

@Bean
public DocumentItemReader documentReader() {
DocumentItemReader documentItemReader = new DocumentItemReader();
documentItemReader.setJdbcTemplate(new JdbcTemplate(abcDataSource));
documentItemReader.setTypeXyzConfigs(abcToXyzConfiguration.getTypeXyzConfigs());

return documentItemReader;
}

@Bean
public DocumentItemProcessor documentProcessor() {
DocumentItemProcessor documentItemProcessor = new DocumentItemProcessor();
documentItemProcessor.setXyzJdbcTemplate(new JdbcTemplate(xyzDataSource));

return documentItemProcessor;
}

@Bean
public DocumentItemWriter documentWriter() throws FileSystemException {
DocumentItemWriter documentItemWriter = new DocumentItemWriter();
documentItemWriter.setXyzJdbcTemplate(new JdbcTemplate(xyzDataSource));
documentItemWriter.setAbcJdbcTemplate(new JdbcTemplate(abcDataSource));
documentItemWriter.setFileSystemManager(fileSystemManager());
documentItemWriter.setAbcFileSystemOptions(abcFileSystemOptions);
documentItemWriter.setXyzFileSystemOptions(xyzFileSystemOptions);
documentItemWriter.setTempFolder(new File(abcToXyzConfiguration.getTempFolder()));
documentItemWriter.setMultiFolder(new File(abcToXyzConfiguration.getMultiFolder()));
documentItemWriter.setAbcFilePathPrefix(abcToXyzConfiguration.getAbcFilePrefix());
documentItemWriter.setXyzFilePathPrefix(abcToXyzConfiguration.getXyzFilePrefix());

return documentItemWriter;
}

@Bean
public DocumentSkipListener documentSkipListener() {
DocumentSkipListener documentSkipListener = new DocumentSkipListener();
documentSkipListener.setAbcJdbcTemplate(new JdbcTemplate(abcDataSource));

return documentSkipListener;
}

@Bean
public ReportDocumentSkipListener reportDocumentSkipListener() {
return new ReportDocumentSkipListener(abcToXyzReportCollection());
}

@Bean
public AbcToXyzReportCollection abcToXyzReportCollection() {
return new AbcToXyzReportCollection();
}

@Bean
public ReportDocumentItemProcessListener reportDocumentItemProcessListener() {
return new ReportDocumentItemProcessListener(abcToXyzReportCollection());
}

@Bean
public ReportDocumentItemWriteListener reportDocumentItemWriteListener() {
return new ReportDocumentItemWriteListener(abcToXyzReportCollection());
}

@Bean
public ReportGenerationTasklet reportGenerationTasklet() {
ReportGenerationTasklet reportGenerationTasklet = new ReportGenerationTasklet();
reportGenerationTasklet.setReportFileNamePrefix(abcToXyzConfiguration.getReportFileNamePrefix());
reportGenerationTasklet.setReportFolder(new File(abcToXyzConfiguration.getReportPath()));
reportGenerationTasklet.setReports(abcToXyzReportCollection());

return reportGenerationTasklet;
}

@Bean
public Job abcToXyzJob(JobCompletionNotificationListener listener) throws FileSystemException {

ReportJobExecutionDecider reportJobExecutionDecider = new ReportJobExecutionDecider(
abcToXyzConfiguration.isReportEnabled());
Flow reportOptionalFlow = new FlowBuilder("reportFlow").start(reportJobExecutionDecider)
.on(ReportJobExecutionDecider.GENERATE_REPORT_FLOW_STATUS).to(reportGenerationStep())
.from(reportJobExecutionDecider).on("*").end().build();

return jobBuilderFactory.get("abcToXyzJob").incrementer(new RunIdIncrementer()).listener(listener)
.flow(documentsStep()).next(reportOptionalFlow).end().build();
}

@Bean
public Step documentsStep() throws FileSystemException {
return stepBuilderFactory.get("documentsStep").chunk(1).reader(documentReader())
.processor(documentProcessor()).writer(documentWriter()).listener(reportDocumentItemProcessListener())
.listener(reportDocumentItemWriteListener()).faultTolerant().skipPolicy(new DocumentItemSkipPolicy())
.listener(documentSkipListener()).listener(reportDocumentSkipListener())
.transactionManager(transactionManager).build();
}

@Bean
public Step reportGenerationStep() {
return stepBuilderFactory.get("reportGenerationStep").tasklet(reportGenerationTasklet()).build();
}

@Bean
public FileSystemManager fileSystemManager() throws FileSystemException {
return VFS.getManager();
}

}

AbcToXyzBatchConfigurer.java
public class AbcToXyzBatchConfigurer implements BatchConfigurer {

private static final Logger LOGGER = AppLoggerFactory.getLogger();

private DataSource dataSource;
private PlatformTransactionManager transactionManager;
private JobRepository jobRepository;
private JobLauncher jobLauncher;
private JobExplorer jobExplorer;

public void setDataSource(DataSource dataSource) {
this.dataSource = dataSource;
}

protected AbcToXyzBatchConfigurer() {
}

public AbcToXyzBatchConfigurer(DataSource dataSource, PlatformTransactionManager transactionManager) {
setDataSource(dataSource);
this.transactionManager = transactionManager;
}

@Override
public JobRepository getJobRepository() {
return jobRepository;
}

@Override
public PlatformTransactionManager getTransactionManager() {
return transactionManager;
}

@Override
public JobLauncher getJobLauncher() {
return jobLauncher;
}

@Override
public JobExplorer getJobExplorer() {
return jobExplorer;
}

@PostConstruct
public void initialize() {
try {
if (dataSource == null) {
LOGGER.warn("No datasource was provided...using a Map based JobRepository");

if (this.transactionManager == null) {
this.transactionManager = new ResourcelessTransactionManager();
}

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

MapJobExplorerFactoryBean jobExplorerFactory = new MapJobExplorerFactoryBean(jobRepositoryFactory);
jobExplorerFactory.afterPropertiesSet();
this.jobExplorer = jobExplorerFactory.getObject();
} else {
this.jobRepository = createJobRepository();

JobExplorerFactoryBean jobExplorerFactoryBean = new JobExplorerFactoryBean();
jobExplorerFactoryBean.setDataSource(this.dataSource);
jobExplorerFactoryBean.afterPropertiesSet();
this.jobExplorer = jobExplorerFactoryBean.getObject();
}

this.jobLauncher = createJobLauncher();
} catch (Exception e) {
throw new BatchConfigurationException(e);
}
}

protected JobLauncher createJobLauncher() throws Exception {
SimpleJobLauncher jobLauncher = new SimpleJobLauncher();
jobLauncher.setJobRepository(jobRepository);
jobLauncher.afterPropertiesSet();
return jobLauncher;
}

protected JobRepository createJobRepository() throws Exception {
JobRepositoryFactoryBean factory = new JobRepositoryFactoryBean();
factory.setDataSource(dataSource);
factory.setTransactionManager(transactionManager);
factory.afterPropertiesSet();
return factory.getObject();
}

}

AbcToXyzTransactionManagerConfiguration.java
@Configuration
@EnableConfigurationProperties(AtomikosProperties.class)
public class AbcToXyzTransactionManagerConfiguration {

@Autowired
private JtaProperties jtaProperties;

@Autowired
@Qualifier("abcDs")
private DataSource abcDataSource;

@Autowired
@Qualifier("xyzDs")
private DataSource xyzDataSource;

@Bean(initMethod = "init", destroyMethod = "shutdownForce")
@ConditionalOnMissingBean(UserTransactionService.class)
public UserTransactionServiceImp userTransactionService(AtomikosProperties atomikosProperties) {
Properties properties = new Properties();
if (StringUtils.hasText(this.jtaProperties.getTransactionManagerId())) {
properties.setProperty("com.atomikos.icatch.tm_unique_name", this.jtaProperties.getTransactionManagerId());
}
properties.setProperty("com.atomikos.icatch.log_base_dir", getLogBaseDir());
properties.putAll(atomikosProperties.asProperties());
return new UserTransactionServiceImp(properties);
}

private String getLogBaseDir() {
if (StringUtils.hasLength(this.jtaProperties.getLogDir())) {
return this.jtaProperties.getLogDir();
}
File home = new ApplicationHome().getDir();
return new File(home, "transaction-logs").getAbsolutePath();
}

@Bean(initMethod = "init", destroyMethod = "close")
@ConditionalOnMissingBean
public UserTransactionManager atomikosTransactionManager(UserTransactionService userTransactionService)
throws Exception {
UserTransactionManager manager = new UserTransactionManager();
manager.setStartupTransactionService(false);
manager.setForceShutdown(true);
return manager;
}

@Bean
public JtaTransactionManager transactionManager(UserTransaction userTransaction,
TransactionManager transactionManager) {
JtaTransactionManager jtaTransactionManager = new JtaTransactionManager(userTransaction, transactionManager);
jtaTransactionManager.setAllowCustomIsolationLevels(true);
return jtaTransactionManager;
}

}


Подробнее здесь: https://stackoverflow.com/questions/790 ... r-with-jar
Ответить

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

Вернуться в «JAVA»