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

Add sample for reader-processor #133

Merged
merged 1 commit into from
Feb 2, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* Spring Batch Plus
*
* Copyright 2022-present NAVER Corp.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.navercorp.spring.batch.plus.sample.flux.readerprocessor

import org.junit.jupiter.api.Test
import org.springframework.batch.core.BatchStatus
import org.springframework.batch.core.Job
import org.springframework.batch.core.JobParametersBuilder
import org.springframework.batch.core.launch.JobLauncher
import org.springframework.beans.factory.getBean
import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.boot.runApplication

@SpringBootApplication
open class SampleApplicationTest {
@Test
fun run() {
val applicationContext = runApplication<SampleApplicationTest>()
val jobLauncher = applicationContext.getBean<JobLauncher>()
val job = applicationContext.getBean<Job>("testJob")

val jobParameters = JobParametersBuilder()
.addLong("totalCount", 20L)
.toJobParameters()
val jobExecution = jobLauncher.run(job, jobParameters)

assert(BatchStatus.COMPLETED == jobExecution.status)
println(jobExecution)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* Spring Batch Plus
*
* Copyright 2022-present NAVER Corp.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.navercorp.spring.batch.plus.sample.flux.readerprocessor

import com.navercorp.spring.batch.plus.step.adapter.ItemStreamFluxReaderProcessor
import org.springframework.batch.core.configuration.annotation.StepScope
import org.springframework.batch.item.ExecutionContext
import org.springframework.beans.factory.annotation.Value
import org.springframework.stereotype.Component
import reactor.core.publisher.Flux

@Component
@StepScope
open class SampleTasklet(
@Value("#{jobParameters['totalCount']}") private var totalCount: Long,
) : ItemStreamFluxReaderProcessor<Int, String> {
private var count = 0

override fun readFlux(executionContext: ExecutionContext): Flux<out Int> {
println("totalCount: $totalCount")
return Flux.generate { sink ->
if (count < totalCount) {
sink.next(count)
++count
} else {
sink.complete()
}
}
}

override fun process(item: Int): String? {
return "'$item'"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* Spring Batch Plus
*
* Copyright 2022-present NAVER Corp.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.navercorp.spring.batch.plus.sample.flux.readerprocessor

import com.navercorp.spring.batch.plus.kotlin.configuration.BatchDsl
import com.navercorp.spring.batch.plus.kotlin.step.adapter.asItemProcessor
import com.navercorp.spring.batch.plus.kotlin.step.adapter.asItemStreamReader
import org.springframework.batch.core.Job
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import org.springframework.transaction.PlatformTransactionManager

@Configuration
open class TestJobConfig(
private val batch: BatchDsl,
private val transactionManager: PlatformTransactionManager,
) {
@Bean
open fun testJob(
sampleTasklet: SampleTasklet,
): Job = batch {
job("testJob") {
step("testStep") {
chunk<Int, String>(3, transactionManager) {
reader(sampleTasklet.asItemStreamReader())
processor(sampleTasklet.asItemProcessor())
writer { chunk -> println(chunk.items) }
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* Spring Batch Plus
*
* Copyright 2022-present NAVER Corp.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.navercorp.spring.batch.plus.sample.iterable.readerprocessor

import org.junit.jupiter.api.Test
import org.springframework.batch.core.BatchStatus
import org.springframework.batch.core.Job
import org.springframework.batch.core.JobParametersBuilder
import org.springframework.batch.core.launch.JobLauncher
import org.springframework.beans.factory.getBean
import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.boot.runApplication

@SpringBootApplication
open class SampleApplicationTest {
@Test
fun run() {
val applicationContext = runApplication<SampleApplicationTest>()
val jobLauncher = applicationContext.getBean<JobLauncher>()
val job = applicationContext.getBean<Job>("testJob")

val jobParameters = JobParametersBuilder()
.addLong("totalCount", 20L)
.toJobParameters()
val jobExecution = jobLauncher.run(job, jobParameters)

assert(BatchStatus.COMPLETED == jobExecution.status)
println(jobExecution)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* Spring Batch Plus
*
* Copyright 2022-present NAVER Corp.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.navercorp.spring.batch.plus.sample.iterable.readerprocessor

import com.navercorp.spring.batch.plus.step.adapter.ItemStreamIterableReaderProcessor
import org.springframework.batch.core.configuration.annotation.StepScope
import org.springframework.batch.item.ExecutionContext
import org.springframework.beans.factory.annotation.Value
import org.springframework.stereotype.Component

@Component
@StepScope
open class SampleTasklet(
@Value("#{jobParameters['totalCount']}") private var totalCount: Long,
) : ItemStreamIterableReaderProcessor<Int, String> {
private var count = 0

override fun readIterable(executionContext: ExecutionContext): Iterable<Int> {
println("totalCount: $totalCount")
return Iterable {
object : Iterator<Int> {
override fun hasNext(): Boolean {
return count < totalCount
}

override fun next(): Int {
return count++
}
}
}
}

override fun process(item: Int): String? {
return "'$item'"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* Spring Batch Plus
*
* Copyright 2022-present NAVER Corp.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.navercorp.spring.batch.plus.sample.iterable.readerprocessor

import com.navercorp.spring.batch.plus.kotlin.configuration.BatchDsl
import com.navercorp.spring.batch.plus.kotlin.step.adapter.asItemProcessor
import com.navercorp.spring.batch.plus.kotlin.step.adapter.asItemStreamReader
import org.springframework.batch.core.Job
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import org.springframework.transaction.PlatformTransactionManager

@Configuration
open class TestJobConfig(
private val batch: BatchDsl,
private val transactionManager: PlatformTransactionManager,
) {
@Bean
open fun testJob(
sampleTasklet: SampleTasklet,
): Job = batch {
job("testJob") {
step("testStep") {
chunk<Int, String>(3, transactionManager) {
reader(sampleTasklet.asItemStreamReader())
processor(sampleTasklet.asItemProcessor())
writer { chunk -> println(chunk.items) }
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* Spring Batch Plus
*
* Copyright 2022-present NAVER Corp.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.navercorp.spring.batch.plus.sample.iterator.readerprocessor

import org.junit.jupiter.api.Test
import org.springframework.batch.core.BatchStatus
import org.springframework.batch.core.Job
import org.springframework.batch.core.JobParametersBuilder
import org.springframework.batch.core.launch.JobLauncher
import org.springframework.beans.factory.getBean
import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.boot.runApplication

@SpringBootApplication
open class SampleApplicationTest {
@Test
fun run() {
val applicationContext = runApplication<SampleApplicationTest>()
val jobLauncher = applicationContext.getBean<JobLauncher>()
val job = applicationContext.getBean<Job>("testJob")

val jobParameters = JobParametersBuilder()
.addLong("totalCount", 20L)
.toJobParameters()
val jobExecution = jobLauncher.run(job, jobParameters)

assert(BatchStatus.COMPLETED == jobExecution.status)
println(jobExecution)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* Spring Batch Plus
*
* Copyright 2022-present NAVER Corp.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.navercorp.spring.batch.plus.sample.iterator.readerprocessor

import com.navercorp.spring.batch.plus.step.adapter.ItemStreamIteratorReaderProcessor
import org.springframework.batch.core.configuration.annotation.StepScope
import org.springframework.batch.item.ExecutionContext
import org.springframework.beans.factory.annotation.Value
import org.springframework.stereotype.Component

@Component
@StepScope
open class SampleTasklet(
@Value("#{jobParameters['totalCount']}") private var totalCount: Long,
) : ItemStreamIteratorReaderProcessor<Int, String> {
private var count = 0

override fun readIterator(executionContext: ExecutionContext): Iterator<Int> {
println("totalCount: $totalCount")
return object : Iterator<Int> {
override fun hasNext(): Boolean {
return count < totalCount
}

override fun next(): Int {
return count++
}
}
}

override fun process(item: Int): String? {
return "'$item'"
}
}
Loading