|
5 | 5 | import io.reactivex.Single; |
6 | 6 | import io.reactivex.schedulers.Schedulers; |
7 | 7 | import io.reflectoring.reactive.batch.MessageHandler.Result; |
| 8 | +import org.reactivestreams.Subscriber; |
| 9 | +import org.reactivestreams.Subscription; |
| 10 | + |
8 | 11 | import java.util.ArrayList; |
9 | 12 | import java.util.List; |
10 | 13 | import java.util.concurrent.LinkedBlockingDeque; |
11 | 14 | import java.util.concurrent.ThreadPoolExecutor; |
12 | 15 | import java.util.concurrent.TimeUnit; |
13 | | -import java.util.concurrent.atomic.AtomicInteger; |
14 | | -import org.reactivestreams.Subscriber; |
15 | | -import org.reactivestreams.Subscription; |
16 | 16 |
|
17 | 17 | public class ReactiveBatchProcessor { |
18 | 18 |
|
19 | | - private final static Logger logger = new Logger(); |
20 | | - |
21 | | - private final int MESSAGE_BATCHES = 10; |
22 | | - |
23 | | - private final int BATCH_SIZE = 3; |
24 | | - |
25 | | - private final int THREADS = 4; |
26 | | - |
27 | | - private final MessageHandler messageHandler = new MessageHandler(); |
28 | | - |
29 | | - public void start() { |
30 | | - retrieveMessageBatches() |
31 | | - .doOnNext(batch -> logger.log(batch.toString())) |
32 | | - .flatMap(batch -> Flowable.fromIterable(batch.getMessages())) |
33 | | - .flatMapSingle(message -> messageHandler.handleMessage(message) |
34 | | - .subscribeOn(threadPoolScheduler(THREADS, 10))) |
35 | | - .subscribeWith(subscriber()); |
36 | | - } |
37 | | - |
38 | | - private Subscriber<MessageHandler.Result> subscriber() { |
39 | | - return new Subscriber<>() { |
40 | | - private Subscription subscription; |
41 | | - |
42 | | - @Override |
43 | | - public void onSubscribe(Subscription subscription) { |
44 | | - this.subscription = subscription; |
45 | | - subscription.request(THREADS); |
46 | | - logger.log("subscribed"); |
47 | | - } |
48 | | - |
49 | | - @Override |
50 | | - public void onNext(Result message) { |
51 | | - subscription.request(THREADS); |
52 | | - } |
53 | | - |
54 | | - @Override |
55 | | - public void onError(Throwable t) { |
56 | | - logger.log("error"); |
57 | | - } |
58 | | - |
59 | | - @Override |
60 | | - public void onComplete() { |
61 | | - logger.log("completed"); |
62 | | - } |
63 | | - }; |
64 | | - } |
65 | | - |
66 | | - private Scheduler threadPoolScheduler(int poolSize, int queueSize) { |
67 | | - return Schedulers.from(new ThreadPoolExecutor( |
68 | | - poolSize, |
69 | | - poolSize, |
70 | | - 0L, |
71 | | - TimeUnit.SECONDS, |
72 | | - new LinkedBlockingDeque<>(queueSize) |
73 | | - )); |
74 | | - } |
75 | | - |
76 | | - public boolean allMessagesProcessed() { |
77 | | - return this.messageHandler.getProcessedMessages().get() == MESSAGE_BATCHES * BATCH_SIZE; |
78 | | - } |
79 | | - |
80 | | - private Flowable<MessageBatch> retrieveMessageBatches() { |
81 | | - return Flowable.range(1, MESSAGE_BATCHES) |
82 | | - .map(this::messageBatch); |
83 | | - } |
84 | | - |
85 | | - private MessageBatch messageBatch(int batchNumber) { |
86 | | - List<Message> messages = new ArrayList<>(); |
87 | | - for (int i = 1; i <= BATCH_SIZE; i++) { |
88 | | - messages.add(new Message(String.format("%d-%d", batchNumber, i))); |
| 19 | + private final static Logger logger = new Logger(); |
| 20 | + |
| 21 | + private final int MESSAGE_BATCHES = 10; |
| 22 | + |
| 23 | + private final int BATCH_SIZE = 3; |
| 24 | + |
| 25 | + private final int THREADS = 4; |
| 26 | + |
| 27 | + private final MessageHandler messageHandler = new MessageHandler(); |
| 28 | + |
| 29 | + public void start() { |
| 30 | + |
| 31 | + Scheduler threadPoolScheduler = threadPoolScheduler(THREADS, 10); |
| 32 | + |
| 33 | + retrieveMessageBatches() |
| 34 | + .doOnNext(batch -> logger.log(batch.toString())) |
| 35 | + .flatMap(batch -> Flowable.fromIterable(batch.getMessages())) |
| 36 | + .flatMapSingle(message -> Single.defer(() -> Single.just(messageHandler.handleMessage(message))) |
| 37 | + .doOnSuccess(result -> logger.log("message handled")) |
| 38 | + .subscribeOn(threadPoolScheduler)) |
| 39 | + .subscribeWith(subscriber()); |
| 40 | + } |
| 41 | + |
| 42 | + private Subscriber<MessageHandler.Result> subscriber() { |
| 43 | + return new Subscriber<>() { |
| 44 | + private Subscription subscription; |
| 45 | + |
| 46 | + @Override |
| 47 | + public void onSubscribe(Subscription subscription) { |
| 48 | + this.subscription = subscription; |
| 49 | + subscription.request(THREADS); |
| 50 | + logger.log("subscribed"); |
| 51 | + } |
| 52 | + |
| 53 | + @Override |
| 54 | + public void onNext(Result message) { |
| 55 | + subscription.request(THREADS); |
| 56 | + } |
| 57 | + |
| 58 | + @Override |
| 59 | + public void onError(Throwable t) { |
| 60 | + logger.log("error"); |
| 61 | + } |
| 62 | + |
| 63 | + @Override |
| 64 | + public void onComplete() { |
| 65 | + logger.log("completed"); |
| 66 | + } |
| 67 | + }; |
| 68 | + } |
| 69 | + |
| 70 | + private Scheduler threadPoolScheduler(int poolSize, int queueSize) { |
| 71 | + return Schedulers.from(new ThreadPoolExecutor( |
| 72 | + poolSize, |
| 73 | + poolSize, |
| 74 | + 0L, |
| 75 | + TimeUnit.SECONDS, |
| 76 | + new LinkedBlockingDeque<>(queueSize), |
| 77 | + new RetryRejectedExecutionHandler() |
| 78 | + )); |
| 79 | + } |
| 80 | + |
| 81 | + public boolean allMessagesProcessed() { |
| 82 | + return this.messageHandler.getProcessedMessages() == MESSAGE_BATCHES * BATCH_SIZE; |
| 83 | + } |
| 84 | + |
| 85 | + private Flowable<MessageBatch> retrieveMessageBatches() { |
| 86 | + return Flowable.range(1, MESSAGE_BATCHES) |
| 87 | + .map(this::messageBatch); |
| 88 | + } |
| 89 | + |
| 90 | + private MessageBatch messageBatch(int batchNumber) { |
| 91 | + List<Message> messages = new ArrayList<>(); |
| 92 | + for (int i = 1; i <= BATCH_SIZE; i++) { |
| 93 | + messages.add(new Message(String.format("%d-%d", batchNumber, i))); |
| 94 | + } |
| 95 | + return new MessageBatch(messages); |
89 | 96 | } |
90 | | - return new MessageBatch(messages); |
91 | | - } |
92 | 97 |
|
93 | 98 |
|
94 | 99 | } |
0 commit comments