My service needs to set up some data and connections (e.g thirdy party tokens, hazelcast connection) before starting to consume from Incoming
channels. But I noticed that the amqp
connections also happen at startup and as soon as they connect they start consuming before the aforementioned setup has finished. So is it possible to delay consumption from incoming queues?
Asked
Active
Viewed 548 times
0

Obb
- 163
- 4
- 12
-
Did you find a solution? I have exactly the same problem... – Simon Wick Nov 03 '20 at 10:49
3 Answers
1
I found a (maybe dirty) solution to delay for a specific time (here 1 minute). But it needs to be implemented in every consumer:
@ApplicationScoped
@ActivateRequestContext
public class MyConsumer {
private static LocalDateTime startupTime;
void onStart(@Observes StartupEvent startup) {
startupTime = LocalDateTime.now();
}
@Incoming("my-queue")
public CompletionStage<Message<MyClass>> process(final Message<MyClass> msg) {
long start = startupTime.plusMinutes(1).toEpochSecond(ZoneOffset.UTC);
long now = LocalDateTime.now().toEpochSecond(ZoneOffset.UTC);
if (start > now) {
return Uni.createFrom().item(msg)
.onItem().delayIt()
.by(Duration.ofSeconds(start - now))
.invoke(getRunnable(msg)).subscribeAsCompletionStage();
}
return Uni.createFrom().item(msg).onItem().invoke(getRunnable(msg)).subscribeAsCompletionStage();
}
private Runnable getRunnable(Message<MyClass> msg) {
return () -> {
// Your Business Code...
msg.ack();
};
}
}

Simon Wick
- 249
- 1
- 11
0
I used a similar solution but actually consuming all the content into queue's, after im ready to consume I first work on the queues and than activate consumption for real time via boolean. (kind of a backpressure approach) but im not super happy with it either.(in my scenario its not important if records are missing in case of a service failure, so I also directly commit the messages.
@Incoming("my-authors")
@Acknowledgment(Acknowledgment.Strategy.PRE_PROCESSING)
public void getAuthorChanges(Envelope envelope) {
// if cache ready // else store
if (!authorCacheReady && !kafkaEnabled) {
authorChanges.add(envelope);
return;
}
... proceed if active
}
and the function handle the queue:
public void enableConsumptionAfterCacheInit() {
authorCacheReady = true;
while (!authorChanges.isEmpty()) {
getAuthorChanges(authorChanges.poll());
}
kafkaEnabled = true;
LOG.info("Starting realtime kafka consumption for Authors..");
}