Spring Book – Chapter 21 – Spring Batch

JMS

Java Messaging Service (JMS) is a message-oriented method of communicating between two or more endpoints by using either point-to-point communication via JMS queue or a publish-subscribe model via JMS topic.

Spring Batch provides two classes org.springframework.batch.item.jms.JmsItemReader and org.springframework.batch.item.jms.JmsItemWriter for reading and writing respectively.

JmsItemReader is an ItemReader for JMS using a JmsTemplate. The template should have a default destination, which will be used to provide items in read(). JmsItemwriter is an ItemWriter for JMS using a JmsTemplate. The template should have a default destination, which will be used to send items in write(List). Listing 23-19 below shows configuring JMS reader ad writer in the Spring configuration file.

Listing 23-19. Configuring JMS item reader and writer in Spring configuration file

All the available ItemReader’s in Spring Batch can be summarized as shown in Table 23-1 below.

Table 23-1. Available ItemReader’s in Spring Batch (Spring Batch Documentation)

Item Readers Description
AbstractItemCountingItemStreamItemReader Abstract base class that provides basic restart capabilities by counting the number of items returned from an .
ListItemReader Provides the items from a list, one at a time.
ItemReaderAdapter Adapts any class to the   interface.
AggregateItemReader An ItemReader that delivers a list as its item, storing up objects from the injected ItemReader until they are ready to be packed out as a collection.
FlatFileItemReader Reads from a flat file. Includes ItemStream and Skippable functionality.
StaxEventItemReader Reads via StAX.
JdbcCursorItemReader Reads from a database cursor via JDBC.
HibernateCursorItemReader Reads from a cursor based on an HQL query.
IbatisPagingItemReader Reads via iBATIS based on a query. Pages through the rows so that large datasets can be read without running out of memory.
JmsItemReader Given a Spring JmsOperations object and a JMS Destination or destination name to send errors, provides items received through the injected JmsOperations receive() method
JpaPagingItemReader Given a JPQL statement, pages through the rows, such that large datasets can be read without running out of memory.
JdbcPagingItemReader Given a SQL statement, pages through the rows, such that large datasets can be read without running out of memory.

All the available ItemWriter’s in Spring Batch can be summarized as shown in Table 23-2 below.

Table 23-2. Available Itemwriter’s in Spring Batch (Spring Batch Documentation)

Item Writer Description
AbstractItemStreamItemWriter Abstract base class that combines the ItemStream and ItemWriter interfaces.
CompositeItemWriter Passes an item to the process method of each in an injected List of ItemWriter objects
ItemWriterAdapter Adapts any class to the ItemWriter interface.
PropertyExtractingDelegatingItemWriter Extends AbstractMethodInvokingDelegator creating arguments on the fly. Arguments are created by retrieving the values from the fields in the item to be processed (via a SpringBeanWrapper) based on an injected array of field name
FlatFileItemWriter Writes to a flat file. Includes ItemStream and Skippable functionality. See section on Writing to a File
HibernateItemWriter This item writer is hibernate session aware and handles some transaction-related work that a non-“hibernate aware” item writer would not need to know about and then delegates to another item writer to do the actual writing.
JdbcBatchItemWriter Uses batching features from a PreparedStatement, if available, and can take rudimentary steps to locate a failure during a flush.
JpaItemWriter This item writer is JPA EntityManager aware and handles some transaction-related work that a non-“jpa aware”ItemWriter would not need to know about and then delegates to another writer to do the actual writing.
StaxEventItemWriter Uses an ObjectToXmlSerializer implementation to convert each item to XML and then writes it to an XML file using StAX.

Page Visitors: 22692

The following two tabs change content below.
Tomcy John

Tomcy John

Blogger & Author at javacodebook
He is an Enterprise Java Specialist holding a degree in Engineering (B-Tech) with over 10 years of experience in several industries. He's currently working as Principal Architect at Emirates Group IT since 2005. Prior to this he has worked with Oracle Corporation and Ernst & Young. His main specialization is on various web technologies and acts as chief mentor and Architect to facilitate incorporating Spring as Corporate Standard in the organization.
Tomcy John

Latest posts by Tomcy John (see all)

2 thoughts on “Spring Book – Chapter 21 – Spring Batch

  1. Thanks for sharing the information but what’s the difference between CursorItemReader’s setFetchSize() and PagingItemReader’s setPageSize()? isn’t it the same?

  2. Please provide an example for StoredProcedureItem Reader – which returns cursor and how to process cursor in processor – I am new to Spring Batch as part of my work I need to invoke Oracle Stored proc which takes one param as input and returns result set which I need to process in Spring Batch Processor.

    Thanks
    Laxmi

Leave a Reply

Your email address will not be published. Required fields are marked *