spring-batch | Spring Boot Batch Starter Example | Object-Relational Mapping library
kandi X-RAY | spring-batch Summary
kandi X-RAY | spring-batch Summary
The project aims to provide a simple example of how to use spring batch to read records from a database table, process them and insert the results into another database table. We will use gradle as our build tool, MySQL as our database.
Support
Quality
Security
License
Reuse
Top functions reviewed by kandi - BETA
- Process a record
- Returns the first name
- Gets the id
- Gets the lifecycle last name
- Set the full name
- Sets the id
- Set the random number
- Step 1
- Add record processor
- Spring bean reader
- Verifies that the database is properly formatted
- Overwrites the default setter to set the prepared statement
- Entry point for the application
- Import user
- Add a writer item writer
spring-batch Key Features
spring-batch Examples and Code Snippets
Community Discussions
Trending Discussions on spring-batch
QUESTION
I'm getting this error because one of the fields in my domain class is a LocalDate. Stack trace below. Jackson is complaining about Java 8 date/time not being supported by default, but Spring Boot 2.5
comes with com.fasterxml.jackson.datatype:jackson-datatype-jsr310
. How do I get Spring Batch to support my LocalDate
field in my domain class?
If I change the field to be a java.util.Date
it works perfectly.
Thanks!
...ANSWER
Answered 2021-Jun-10 at 03:14You can add serializer and deserializer for it.
QUESTION
I am using spring-batch
in spring-boot
application. The Spring Boot version is 2.3.3.RELEASE
.
What I intend to achieve
I have to read a xml file
containing thousands of Transactions
with header tag
(fileInformation). Do some business logic on transaction and then write the file back with the updated values in transaction. I am using StaxEventItemReader
for reading the file and StaxEventItemWriter
for writing to the file. Then i have couple of ItemProcessors
for handling the business logic. Xml file looks like :
ANSWER
Answered 2021-Jun-10 at 07:38You step is doing too much. I would beak things down to two steps:
- Step 1: extracts the file information header and puts it in the job execution context
- Step 2: reads the file information header from the execution context and uses it in whatever step-scoped bean needed for that step (for example the stax callback in your case)
Here is a quick example:
QUESTION
I am doing a study on the feasibility of a Spring Batch composed of two datasources. A SQL datasource for the Spring Batch metadata and a MongoDB datasource (with transactional use) for the business data. The transactional aspect raises several questions here.
The following topic: Spring batch with MongoDB and transactions and related resources provide a number of answers to my questions.
The answer mentions the use of Spring's JtaTransactionManager
to manage distributed transactions on the two datasources.
This technique uses the 2PC protocol. It is also the most robust solution if I understood correctly. https://www.infoworld.com/article/2077963/distributed-transactions-in-spring--with-and-without-xa.html?page=2
On the other hand, I found some resources about Spring's ChainedTransactionManager
. This technique uses the best effort 1PC protocol. This solution is less robust, if I understand correctly the system can be in an inconsistent state in case of a problem in the infrastructure (network failure for example).
The ChainedTransactionManager
has the advantage of being easier to implement and offers better performance. I saw that it is deprecated https://github.com/spring-projects/spring-data-commons/issues/2232.
What are the concrete risks of using the ChainedTransactionManager
in a Spring Batch? In case of an error, can I have inconsistencies between the Spring batch metadata and the business data in Mongo?
I imagine there are also considerations to take into account with retry or chunk skip strategies?
Thanks a lot for your help.
...ANSWER
Answered 2021-Jun-07 at 12:58In case of an error, can I have inconsistencies between the Spring batch metadata and the business data in Mongo?
Yes, that's is the risk you should be aware of.
A common technique to avoid that is to disable state management and use the process indicator pattern. You can find an example here.
QUESTION
Basically I have a Spring Batch that queries a Database and implements Partitioner to get the Jobs, and assign the Jobs to a ThreadPoolTaskExecutors in a SlaveStep.
The Reader reads (Job) from the Database. The Writer loads the data into a csv file in an Azure Blob Storage.
The Job Partitioner and Reader works fine. The Writer writes to one file, then it closes, and the other jobs cannot finish because the stream is closed. I get the following error:
...ANSWER
Answered 2021-Jun-02 at 07:56You did not share the entire stack trace to see when this error happens exactly, but it seems that the close
method is called more than once. I think this is not due to a concurrency issue, as I see you are using one writer per thread in a partitioned step. So I would make this method "re-entrant" by checking if the output stream is already closed before closing it (there is no isClosed
method on an output stream, so you can use a custom boolean around that).
That said, I would first confirm that the close
method is called twice and if so, investigate why is that and fix the root cause.
QUESTION
I am trying to implement a Spring batch job where in order to process a record , it require 2-3 db calls which is slowing down the processing of records(size is 1 million).If I go with chunk based processing it would process each record separately and would be slow in performance. So, I need to process 1000 records in one go as bulk processing which would reduce the db calls and performance would increase. But my question is If I implement Tasklet then I would lose the functionality of restartability and retrial/skip features too and if implemented using AggregateInputReader I am not sure what would be the impact on restartability and transaction handling. As per the below thread AggregateReader should work but not sure its impact on transaction handling and restartability in case of failure:
...ANSWER
Answered 2021-May-31 at 08:55The first extension point in the chunk-oriented processing model that gives you access to the list of items to be written is the ItemWriteListener#beforeWrite(List items)
. So if you do not want to enrich items one at a time in an ItemProcessor
, you can use that listener to do the enrichment for the entire chunk at once.
QUESTION
Recently we run a test which created different Spring batch job instances concurrently (e.g 10 threads in parallel, job names are similar but different, e.g with the same prefix). And it's fairly easy to trigger deadlockerror reported from MySQL exception is
org.springframework.dao.DeadlockLoserDataAccessException: PreparedStatementCallback; SQL [INSERT into BATCH_JOB_INSTANCE(JOB_INSTANCE_ID, JOB_NAME, JOB_KEY, VERSION) values (?, ?, ?, ?)]; Deadlock found when trying to get lock; try restarting transaction; nested exception is com.mysql.jdbc.exceptions.jdbc4.MySQLTransactionRollbackException: Deadlock found when trying to get lock; try restarting transaction at org.springframework.jdbc.support.SQLErrorCodeSQLExceptionTranslator.doTranslate(SQLErrorCodeSQLExceptionTranslator.java:267) at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:72) at org.springframework.jdbc.core.JdbcTemplate.translateException(JdbcTemplate.java:1443) at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:633) at org.springframework.jdbc.core.JdbcTemplate.update(JdbcTemplate.java:862) at org.springframework.jdbc.core.JdbcTemplate.update(JdbcTemplate.java:917) at org.springframework.jdbc.core.JdbcTemplate.update(JdbcTemplate.java:922) at org.springframework.batch.core.repository.dao.JdbcJobInstanceDao.createJobInstance(JdbcJobInstanceDao.java:120) at org.springframework.batch.core.repository.support.SimpleJobRepository.createJobExecution(SimpleJobRepository.java:140) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:344)
We searched for existing reports regarding to deadlock and find that some are specific to SQLServer like this: (https://github.com/spring-projects/spring-batch/issues/1448). After analysis of the isolation level used for creating jobs(SERIALIZABLE) and the operation sequence, we think the deadlock could be trigged as following:
1、before creating a job instance, the code will first query batch_job_instance table to check if the instance already exists(about 3 times), under SERIALIZABLE mode, this will hold shared next-key lock (https://dev.mysql.com/doc/refman/5.7/en/innodb-next-key-locking.html) in MySQL which lock records that are in scope related to the job name.
2、thread 2 want to create job2 and insert a row in batch_job_instance and thread 3 want to do the same thing, as both threads hold the same read next-key lock and the rows that need to be inserted are also in the key scope, the deadlock will happen.
Refer to the link here(https://docs.spring.io/spring-batch/trunk/apidocs/org/springframework/batch/core/repository/support/AbstractJobRepositoryFactoryBean.html#setIsolationLevelForCreate-java.lang.String-), we tried changing the isolation level to REPEATABLE_READ and this worked without any deadlock.
So the key question here is :
Is setting isolation level to REPEATABLE_READ the recommended solution here and is there any side effect of this solution as it's not set as default option ?
Thanks a lot!
...ANSWER
Answered 2021-May-28 at 13:08we tried changing the isolation level to REPEATABLE_READ and this worked without any deadlock.
So the key question here is : Is setting isolation level to REPEATABLE_READ the recommended solution here and is there any side effect of this solution as it's not set as default option ?
Yes, that's the way to go. If SERIALIZABLE is too aggressive, you can use a less aggressive isolation level for the job repository. That's why the setIsolationLevelForCreate
is provided. This is actually documented in its Javadoc:
QUESTION
we plan to use Spring Batch for mission critical production batching system. And I am wondering which version we should choose now?
From the version history in github(https://github.com/spring-projects/spring-batch/tags): version 4.2.7 is marked with .RELEASE, while 4.3.3 is not.
So the questions are:
1、what's the difference regarding the versions with or without .RELEASE ending, is the ".RELEASE" ending verison more stable?
2、which version is recommended for production now ?
Thanks a lot!
ANSWER
Answered 2021-May-27 at 08:33Both are production ready releases, one for the 4.2 line and the other for the 4.3 line. I recommend using the latest 4.3 as it has an extended support lifetime. Spring Batch follows the same support terms as Spring Boot, and each version will be supported as long as the latest Spring Boot version that brings it is supported, see https://github.com/spring-projects/spring-boot/wiki/Supported-Versions.
For example:
- the latest Spring Boot version that brings Spring Batch 4.2 is 2.3 which will be OSS supported until May 2021.
- The latest Spring Boot version that brings Spring Batch 4.3 is 2.5 which will be OSS supported until May 2022.
- etc
Spring Boot 2.6 is expected on November 2021 and will likely bring Spring Batch 4.3 as well, which extends the OSS support for Spring Batch 4.3 for another year. So I recommend using the latest version from 4.3 to benefit from a longer support period since you are planning to use it in production. FTR, there is an ongoing effort to update the website/wiki with all these support details across the portfolio, so keep tuned.
In regard to the version name scheme, we (the Spring engineering team) have decided to switch from the old naming scheme (.BUILD-SNAPSHOT
, .RELEASE
, etc) to a new naming scheme. This has been announced on our blog here: Updates to Spring Versions.
QUESTION
I am trying to write to an Azure Storage using Spring.
I am configuring the resource inside a Bean instead of Autowiring it from the Class.
...ANSWER
Answered 2021-May-26 at 02:38The searchLocation
should start with azure-blob://
or azure-file://
. The "blob" in your comment is incorrect.
azure-blob://foo/bar.csv
means the "bar.csv" blob in "foo" container. Please check your storage, make sure the blob exists.
For example, my blob URL is https://pamelastorage123.blob.core.windows.net/pamelac/test.txt
, so azure-blob://pamelac/test.txt
is right.
StorageExampleApplication.java:
QUESTION
In my Spring batch job, I'm trying to share data between steps using JobExecutionContext, which works only if i keep the steps single threaded as follows:
...ANSWER
Answered 2021-May-17 at 08:31I'm trying to share data between steps using JobExecutionContext, which works only if i keep the steps single threaded
Relying on the execution context to share data between multi-threaded steps is incorrect, because the keys will be overridden by concurrent threads. The reference documentation explicitly mentions to turn off state management in multi-threaded environment:
- Javadoc:
remember to use saveState=false if used in a multi-threaded client
- Reference doc:
it is not recommended to use job-scoped beans in multi-threaded or partitioned steps
That said, I don't see what key could be shared from a multi-threaded step to the next step (as threads are executed in parallel), but if you really need to do that, you should use another method like defining a shared bean that is thread safe.
QUESTION
I need to upgrade my maven
project to Java 11
, so I am wondering:
- What is the min version of
Spring Framework
andspring-batch version
supported in Java 11? - What to be change in the pom?
As of now I know:
- My project is not a spring-boot.
- I know the
Spring version
should be at least5.1
. - ...
ANSWER
Answered 2021-May-11 at 07:49I know the Spring version should be at least 5.1
2.1.9.RELEASE
You seem to be upgrading Spring Batch from v2. Since your Spring version should be at least v5.1, you need to upgrade Spring Batch to v4 (which is based on Spring Framework v5) and requires Java 8 at a minimum. So the answer to your question about the minimum Java version is Java 8, but you should be able to use Java 11 if you want.
Community Discussions, Code Snippets contain sources that include Stack Exchange Network
Vulnerabilities
No vulnerabilities reported
Install spring-batch
we will set up the build.gradle in project directory with plugin java and spring-boot. Also, we will add following dependencies -. spring-boot plugin that collects all the jars on the classpath and builds a single, runnable archive jar, which makes it more easier to execute the service. Here is what our build.gradle looks like -.
spring-boot-starter-batch - to pull in spring batch classes.
spring-boot-starter-data-jpa - to handle the datasource and pull in spring-jdbc dependencies.
mysql-connector-java - java connector for MySQL database.
Now, we will have spring create our database tables each time it runs. You can skip this step if you manually have created the tables or tables with data are already present.
Support
Reuse Trending Solutions
Find, review, and download reusable Libraries, Code Snippets, Cloud APIs from over 650 million Knowledge Items
Find more librariesStay Updated
Subscribe to our newsletter for trending solutions and developer bootcamps
Share this Page