Start New Only
Subscribe to a KubeMQ Events Store channel receiving only new events and skipping stored history, using the Java SDK.
Overview
Start-from-new turns a durable Events Store channel into a live-only feed — reach for it when a consumer only cares what happens from this moment forward and would rather skip a large backlog than pay to replay it. Dashboards, live notification fan-outs, and freshly-deployed services that don't need to catch up on history are the classic cases: any of the replay-from-start positions would mean churning through every historical event just to reach the live tail.
It works by setting eventsStoreType(EventsStoreType.StartNewOnly) on the EventsStoreSubscription passed to subscribeToEventsStore — the broker stamps the subscription's registration time as a watermark and delivers only events published after it, ignoring everything already stored. Gotchas: there's a race between registering and the publisher sending — a publish that lands before the broker fully registers you is silently skipped, so give the subscription a moment to settle before publishing; this position can never see anything published earlier, so use a start-from-first or start-from-sequence position when you need guaranteed replay; and reconnecting doesn't resume where you left off — a fresh StartNewOnly subscription starts from "now" again, with no cursor persisted across restarts.
Prerequisites
- KubeMQ server running on
localhost:50000 - Java SDK installed (
implementation 'io.kubemq.sdk:kubemq-sdk-Java:3.1.1'(Gradle) or Maven dependency from Getting Started)
Code
package io.kubemq.example.eventsstore;
import io.kubemq.sdk.common.ServerInfo;
import io.kubemq.sdk.pubsub.*;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
/**
* StartNewOnly Example
*
* Demonstrates subscribing with StartNewOnly which ignores all historical messages.
*/
public class StartNewOnlyExample {
private static final String ADDRESS = "localhost:50000";
private static final String CLIENT_ID = "java-eventsstore-start-new-only-client";
private static final String CHANNEL = "java-eventsstore.start-new-only";
public static void main(String[] args) throws InterruptedException {
// Create a client connected to the KubeMQ server
PubSubClient client = PubSubClient.builder().address(ADDRESS).clientId(CLIENT_ID).build();
client.ping();
// Create the events store channel
client.createEventsStoreChannel(CHANNEL);
// Send historical messages before subscribing (will be ignored)
for (int i = 1; i <= 3; i++) {
client.publishEventStore(EventStoreMessage.builder()
.id("hist-" + i).channel(CHANNEL)
.body(("Historical #" + i).getBytes()).build());
}
System.out.println("Sent 3 historical messages (will be ignored).\n");
AtomicInteger received = new AtomicInteger(0);
CountDownLatch latch = new CountDownLatch(2);
EventsStoreSubscription sub = EventsStoreSubscription.builder()
.channel(CHANNEL)
.eventsStoreType(EventsStoreType.StartNewOnly)
.onReceiveEventCallback(e -> {
received.incrementAndGet();
System.out.println(" Received: " + new String(e.getBody()));
latch.countDown();
})
.onErrorCallback(err -> System.err.println("Error: " + err))
.build();
// Subscribe with StartNewOnly (ignore all historical messages)
client.subscribeToEventsStore(sub);
System.out.println("Subscribed with StartNewOnly.");
Thread.sleep(500);
// Send new messages after subscribing
for (int i = 1; i <= 2; i++) {
client.publishEventStore(EventStoreMessage.builder()
.id("new-" + i).channel(CHANNEL)
.body(("New message #" + i).getBytes()).build());
}
latch.await(5, TimeUnit.SECONDS);
System.out.println("\nReceived " + received.get() + " new messages (historical ignored).");
// Clean up resources
sub.cancel();
client.deleteEventsStoreChannel(CHANNEL);
client.close();
}
}
How It Works
StartNewOnlyignores all events stored before the subscription is established; the three historical messages published beforesubscribeToEventsStoreare never delivered.- After a 500 ms settle, two new messages are published; only these two arrive in the callback, demonstrated by
received.get() == 2. - This mode is the EventsStore equivalent of a plain Events subscription: persistent delivery for late-joining subscribers is sacrificed in favour of skipping backlog.
Related
Was this page helpful?