Posts

Showing posts with the label spring batch

Spring Security: In-Memory Authentication example

Below is an example configuration using the WebSecurityConfigurerAdapter that configures an in-memory user store with a single user: @Configuration public class SecurityConfiguration extends WebSecurityConfigurerAdapter {     @Override     protected void configure(AuthenticationManagerBuilder auth) throws Exception {         UserDetails user = User.withDefaultPasswordEncoder()             .username("user")             .password("password")             .roles("USER")             .build();         auth.inMemoryAuthentication()             .withUser(user);     } } The recommended way of doing this is registering an InMemoryUserDetailsManager bean: @Configuration public class SecurityConfiguration {     @Bean     public InMemoryUserDetailsManager userDeta...

Spring supplier Example

Non Spring Developers Let us assume for a second that you are not a Spring developer and not familiar with Spring Integration which already provides abstractions for ROME. In that case, we can certainly use ROME directly to produce feed records. For example, this is a valid Supplier for this scenario. public Supplier<SyndEntry> feedSupplier() { return () -> { //Use the ROME framework directly to produce syndicated entries. } } The benefit here is that we can develop the supplier without any knowledge of Spring, and it can be deployed to a serverless environment directly, using the abstractions provided by that environment or by relying on a framework like Spring Cloud Function. This essentially means that if you are a Java developer without much Spring Framework skills, you can still write the functions using just the interfaces defined in the java.util.function package such as Function, Supplier and Consumer, by providing the business logic.  Spring Developers Add ...

Introducing Java Functions for Spring Cloud Stream Applications - Part 1

Last week Spring posted Introducing Java Functions for Spring Cloud Stream Applications - Part 0 to announce the release of Spring Cloud Stream applications 2020.0.0-M2. Here, explore function composition, one of the more powerful features enabled by the function oriented architecture presented in Part 0. If you haven’t had a chance to read Part 0, now would be a great time! Function Composition Function composition has a solid theoretical foundation in mathematics and computer science. In practical terms, it is a way to join a sequence of functions to create a more complex function. Let’s look at a simple example using Java functions. We have two functions, reverse and upper. Each accepts a String as input and produces a String as output. We can compose them using the built-in andThen method. The composite function is itself a Function<String, String>. If you run this, it will print ESREVER. Function<String, String> reverse = s -> new StringBuilder(s).reve...

Spring Boot 2.x actual state machine

Image
Spring StateMachine is a state machine framework. In the Spring framework project, developers can obtain a business state machine through simple configuration without having to manage the definition and initialization of the state machine. In this article today, we use a case to learn the usage of Spring StateMachine framework. Case introduction Suppose there is such an object in a business system. It has three states: draft, pending release, and release completed. The business actions for these three states are relatively simple, namely: go online, release, and rollback. The business state machine is shown below. Actual combat Next, based on the above business state machine, a Spring StateMachine demonstration. Create a basic Spring Boot project, add the Spring StateMachine dependency in the main pom file: <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/200...

Spring Batch - Chunk processing

Image
One of the great advantages of Spring Batch is the Chunk-oriented processing. This time, let's take a look at what Chunk-oriented processing is. What is Chunk? Chunk in Spring Batch refers to the number of rows processed between each commit when working with chunks of data . In other words, Chunk-oriented processing means reading data one at a time, creating a chunk called Chunk, and then processing transactions in chunk units . The transaction is important here. Because it executes the transaction in chunk unit, if it fails, it is rolled back as much as the corresponding chunk , and it reflects the range of the previously committed transaction. As the chunk-oriented processing means that the data is processed in chunk units in the end, it is expressed as follows. The figure in the official document only deals with individual items . Please note that the picture above is a bit different because it covers even chunk units. Reader reads one data Processor read...

Spring Batch - MultiThread execution Step

Image
In general, Spring Batch runs in a single thread. That means everything runs sequentially. Spring Batch supports various ways to execute it in parallel. This time, we will look at how to execute a step with one of them, multi-thread. Introduction Spring Batch's multi-thread step uses Spring's TaskExecutorto execute each thread in chunk units . TaskExecutorDepending on which is selected here, new threads may be continuously created for every chunk unit ( SimpleAsyncTaskExecutor) or executed while reusing only the specified number of threads in the thread pool. ( ThreadPoolTaskExecutor) The first thing to do to configure a multi-threaded environment in Spring Batch is to check if the Reader and Writer you want to use support multi-threading. You should always check each Reader and Writer's Javadoc for that thread-safe phrase. If not, you must select a reader and writer that are thread-safe, and if you must use the reader, you can convert to thread-safe using ...

Spring Batch - Use bulk writes in MongoItemWriter

Use bulk writes in MongoItemWriter Up until now, the MongoItemWriter used MongoOperations.save() in a for loop to save items to the database. In this release, we replaced this mechanism with a single call to BulkOperations. With this change, the MongotItemWriter is 25x faster than the previous version, according to benchmark mongo-item-writer-benchmark.

Use of bulk writes in RepositoryItemWriter

Up to spring-batch version 4.2, it was required to specify the method name to use to save an item to the database. This method was then called in a for loop to save all items. In order to use CrudRepository.saveAll, it was required to extend RepositoryItemWriter and override write(List), which is not convenient. In spring-batch v4.3.0 release, RepositoryItemWriter use CrudRepository.saveAll by default. This changes improves the performance of the writer by a factor of 2, according to our benchmark repository-item-writer-benchmark.

Configuration of Spring Batch tests with JUnit 5

Configuration of Spring Batch tests with JUnit 5 Similar to how many Spring Boot test annotations are meta-annotated with @ExtendWith(SpringExtension.class) (like @SpringBootTest, @WebMvcTest, and others), we updated @SpringBatchTest to be meta-annotated with @ExtendWith(SpringExtension.class). This simplifies the configuration when writing tests with JUnit Jupiter. Please note that this feature does not affect JUnit 4 users, it only concerns JUnit 5 based tests.

Spring Batch JpaPagingItemReader Example

JpaPagingItemReader Spring Batch 4.3.0 Add support for named queries in JpaPagingItemReader. Up until now, it was possible to use named queries with the JpaPagingItemReader. However, this required the creation of a custom query provider, as follows: JpaPagingItemReader<Foo> reader = new JpaPagingItemReaderBuilder<Foo>()     .name("fooReader")     .queryProvider(new AbstractJpaQueryProvider() {        @Override        public Query createQuery() {           return getEntityManager().createNamedQuery("allFoos", Foo.class);        }        @Override        public void afterPropertiesSet() throws Exception {        }     })     // set other properties on the reader     .build(); In this release, we introduced a JpaNamedQueryProvider next to the JpaNativeQueryProvider...