Class StreamContext
-
- All Implemented Interfaces:
public final class StreamContextContext for JetStream stream operations
Provides access to stream management and operations including:
Stream information and updates
Message purging and deletion
Consumer listing
Every operation reports failure by throwing a eu.vstoyanov.natsy.exception.NatsyException. The lookups - getMessage, getConsumer - answer
nullfor something that is not there; everything else names a thing it acts on, so a missing one is eu.vstoyanov.natsy.exception.NatsyNotFoundException.Example usage:
val streamCtx = js.stream("MY_STREAM") { subjects = listOf("orders.*") maxMessages = 1000 } // Update the stream streamCtx.update { maxMessages = 2000 } // Purge every message streamCtx.purge() // Purge a single subject, or a subtree with "orders.>" streamCtx.purge("orders.cancelled") // List consumers streamCtx.consumers().collect { consumer -> println(consumer.name) }
-
-
Method Summary
Modifier and Type Method Description final StringgetName()Name of the stream final StreamInfogetInfo()Get current stream information final StreamInfoupdate(Function1<StreamConfigBuilder, Unit> block)Update the stream configurationApplies the configuration block to update stream settings. final Booleandelete()Delete the streamPermanently deletes the stream and all its messages. final PurgeResponsepurge(String subject)Purge messages from the streamWithout a subject every message in the stream is removed. final BooleandeleteMessage(Long sequence)Delete a specific message by sequence number final MessageInfogetMessage(Long sequence)Get a specific message by sequence number final Flow<ConsumerInfo>consumers()List all consumers on this streamA failure reaches the collector rather than completing the flow: an empty result means the stream has no consumers, and nothing else. final ConsumerContextconsumer(String consumerName, Function1<ConsumerConfigBuilder, Unit> block)Create or update a consumer on this streamIf the consumer doesn't exist, a new one is created from the configuration block. final ConsumerContextgetConsumer(String consumerName)Get an existing consumer on this stream by name -
-
Method Detail
-
getInfo
final StreamInfo getInfo()
Get current stream information
- Returns:
the stream's current state as the server holds it
-
update
final StreamInfo update(Function1<StreamConfigBuilder, Unit> block)
Update the stream configuration
Applies the configuration block to update stream settings. Only the properties assigned in the block are changed; everything else keeps the value the server currently holds, including settings natsy's DSL does not model.
- Parameters:
block- Configuration block with stream updates- Returns:
the updated stream information
-
delete
final Boolean delete()
Delete the stream
Permanently deletes the stream and all its messages. This operation cannot be undone.
- Returns:
whether the server reported the deletion as done
-
purge
final PurgeResponse purge(String subject)
Purge messages from the stream
Without a subject every message in the stream is removed. With a subject only matching messages are removed, using standard NATS wildcards:
*matches exactly one token -orders.*matchesorders.createdbut notorders.created.eu>matches one or more trailing tokens -orders.>matches both
A subject of
>therefore purges the entire stream, same as passing null. A blank subject is rejected rather than honoured: an empty filter is dropped during request serialization, so it would silently widen a targeted purge into a whole-stream wipe.- Parameters:
subject- Optional subject filter; when null the entire stream is purged- Returns:
purge statistics
-
deleteMessage
final Boolean deleteMessage(Long sequence)
Delete a specific message by sequence number
- Parameters:
sequence- The sequence number of the message to delete- Returns:
whether the server reported the deletion as done
-
getMessage
final MessageInfo getMessage(Long sequence)
Get a specific message by sequence number
- Parameters:
sequence- The sequence number of the message to retrieve- Returns:
the message, or
nullif the stream holds no message at that sequence
-
consumers
final Flow<ConsumerInfo> consumers()
List all consumers on this stream
A failure reaches the collector rather than completing the flow: an empty result means the stream has no consumers, and nothing else.
- Returns:
Flow of ConsumerInfo
-
consumer
final ConsumerContext consumer(String consumerName, Function1<ConsumerConfigBuilder, Unit> block)
Create or update a consumer on this stream
If the consumer doesn't exist, a new one is created from the configuration block. If it already exists, only the properties the block assigns are changed; everything else keeps the value the server currently holds, including settings natsy's DSL does not model.
Example:
val consumerCtx = streamCtx.consumer("MY_CONSUMER") { durable = "MY_CONSUMER" ackPolicy = AckPolicy.EXPLICIT maxDeliver = 5 }- Parameters:
consumerName- Optional name for the consumer (null for ephemeral)block- Configuration block for the consumer- Returns:
a context for the consumer that is now in place
-
getConsumer
final ConsumerContext getConsumer(String consumerName)
Get an existing consumer on this stream by name
- Parameters:
consumerName- The name of the consumer- Returns:
a context for the consumer, or
nullif the stream has no consumer by that name
-
-
-
-