Fairly new to the Spring Batch mechanism–I managed to get it up and running but my business logic does not seem to fit as a Spring Batch job. Allow me to explain:
The job is responsible of sending an email to the User as follows:
- Fetch the set of News items which satisfy a certain criteria (
Set<News>
) - Fetch the set of users who haven't received one of the News items yet (
Set<User>
) - For each user, send an e-mail for each News the user hasn't received.
- 'Tag' that user that the News has/have been received.
The entity bean definition are as follows:
@Entity
public class News {
...
// ID with a sequence generator (more recent news have a higher ID value)
private Integer id;
...
}
@Entity
public class User {
...
// last received news;
private Integer lastReceivedNewsId;
...
}
There is no one-to-many relationship in between, so in order to retrieve the unreceived news one could retrieve as follows:
SELECT n FROM db.news n
JOIN User u on n.id > u.lastReceivedNewsId;
I decided to use Spring Batch since my user base is currently at 28,000 and counting, settings steps 1 and 2 as the ItemReader
and steps 3 and 4 as the ItemWriter
.
The problem lies in the firs two steps: I decided to use JpaPagingItemReader<T>
as my reader but unfortunately it is incredibly rigid such that I can only specify a query with no change of mapping the result set to something else. In my example, I would first need to fetch the set of News, then query the database to fetch the set of Users based on the received News (Tuple<User, Set<News>>
).
What can I do to allow my reader to return a tuple of User and his/her unreceived set of News? What am I missing?
Much appreciated. Do let me know if you need any more information.