القارئات والمعالجات والكتّاب
أنشئ تدفق المعالجة الدفعية القائم على القطع
القارئات والمعالجات والكتّاب درس مجاني في Spring Boot 4 Microservices & REST APIs على CoddyKit. هذا هو الدرس 3 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في Spring Boot 4 Microservices & REST APIs، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة Spring Boot 4 Microservices & REST APIs 4 دروس في المجموع.
بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.
The read-process-write triad
A chunk step has three collaborators:
- ItemReader - supplies items one at a time
- ItemProcessor - transforms or filters an item
- ItemWriter - persists a chunk of items
ItemReader interface
ItemReader.read() returns the next item, or null when the input is exhausted. Spring Batch keeps calling it until it returns null.
public interface ItemReader<T> {
T read() throws Exception;
}FlatFileItemReader
For CSV files, FlatFileItemReader reads lines and maps them to objects via a line mapper.
@Bean
public FlatFileItemReader<Person> reader() {
return new FlatFileItemReaderBuilder<Person>()
.name("personReader")
.resource(new ClassPathResource("people.csv"))
.delimited()
.names("firstName", "lastName")
.targetType(Person.class)
.build();
}JdbcCursorItemReader
To read from a database, use JdbcCursorItemReader or JdbcPagingItemReader. Paging is preferred for very large result sets to avoid holding a long cursor.
@Bean
public JdbcCursorItemReader<Person> dbReader(DataSource ds) {
return new JdbcCursorItemReaderBuilder<Person>()
.name("dbReader")
.dataSource(ds)
.sql("SELECT first_name, last_name FROM people")
.rowMapper(new PersonRowMapper())
.build();
}ItemProcessor interface
ItemProcessor transforms an input item into an output item. The input and output types can differ.
public interface ItemProcessor<I, O> {
O process(I item) throws Exception;
}Writing a processor
Here a processor uppercases names. Returning a transformed object is the common case.
public class UpperCaseProcessor implements ItemProcessor<Person, Person> {
@Override
public Person process(Person p) {
return new Person(p.getFirstName().toUpperCase(),
p.getLastName().toUpperCase());
}
}Filtering with the processor
If a processor returns null, the item is filtered out and never reaches the writer. This is how you skip records based on business rules.
public Person process(Person p) {
if (p.getLastName().isBlank()) {
return null; // drop this record
}
return p;
}ItemWriter interface
ItemWriter receives a chunk of items at once, not single items. This lets it batch the write into one efficient operation.
public interface ItemWriter<T> {
void write(Chunk<? extends T> chunk) throws Exception;
}JdbcBatchItemWriter
JdbcBatchItemWriter performs a batched SQL insert/update for the whole chunk, which is far faster than row-by-row writes.
@Bean
public JdbcBatchItemWriter<Person> writer(DataSource ds) {
return new JdbcBatchItemWriterBuilder<Person>()
.dataSource(ds)
.sql("INSERT INTO people (first_name, last_name) VALUES (:firstName, :lastName)")
.beanMapped()
.build();
}Wiring them into a step
The step ties the triad together with a chunk size. Items flow reader -> processor -> writer in chunks.
return new StepBuilder("step1", repo)
.<Person, Person>chunk(50, tx)
.reader(reader())
.processor(new UpperCaseProcessor())
.writer(writer(ds))
.build();Custom readers and writers
You can implement the interfaces directly for custom sources (a REST API, a queue). Spring also offers ItemStream so your component can save and restore its position for restarts.
Quick Check
Verify your understanding of the triad.
Recap
You assembled a chunk pipeline:
ItemReader.read()returns items until nullItemProcessortransforms; returning null filtersItemWriterwrites a wholeChunkat once- The step binds all three with a chunk size
الأسئلة الشائعة
هل درس «القارئات والمعالجات والكتّاب» مجاني؟
نعم — نص درس «القارئات والمعالجات والكتّاب» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Spring Boot 4 Microservices & REST APIs، انتقل إلى CoddyKit PRO. تتضمن دورة Spring Boot 4 Microservices & REST APIs 4 دروس في المجموع.
ماذا ستتعلم في «القارئات والمعالجات والكتّاب»؟
أنشئ تدفق المعالجة الدفعية القائم على القطع تتمرن على Spring Boot 4 Microservices & REST APIs مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.
هل أحتاج إلى خبرة سابقة لأبدأ Spring Boot 4 Microservices & REST APIs؟
لا تُشترط خبرة سابقة. Spring Boot 4 Microservices & REST APIs على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 3 من أصل 4.
كم من الوقت يستغرق درس «القارئات والمعالجات والكتّاب»؟
معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.
هل يمكنني كتابة وتشغيل أكواد في درس Spring Boot 4 Microservices & REST APIs هذا؟
نعم. كل درس في Spring Boot 4 Microservices & REST APIs يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.
جميع الدروس في هذه الدورة
- الجدولة باستخدام @Scheduled
- مهام Spring Batch وخطواتها
- القارئات والمعالجات والكتّاب
- إعادة التشغيل ومعالجة الأخطاء