Class StreamContext

  • All Implemented Interfaces:

    
    public final class StreamContext
    
                        

    Context for JetStream stream operations

    Provides access to stream management and operations including:

    • Stream information and updates

    • Message purging and deletion

    • Consumer listing

    Example usage:

    val streamCtx = js.stream("MY_STREAM") {
        subjects = listOf("orders.*")
        maxMessages = 1000
    }.getOrThrow()
    
    // 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)
    }
    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private final String name
    • Constructor Summary

      Constructors 
      Constructor Description
    • Enum Constant Summary

      Enum Constants 
      Enum Constant Description
    • Method Summary

      Modifier and Type Method Description
      final String getName() Name of the stream
      final Result<StreamInfo> getInfo() Get current stream information
      final Result<StreamInfo> update(Function1<StreamConfigBuilder, Unit> block) Update the stream configurationApplies the configuration block to update stream settings.
      final Result<Boolean> delete() Delete the streamPermanently deletes the stream and all its messages.
      final Result<PurgeResponse> purge(String subject) Purge messages from the streamWithout a subject every message in the stream is removed.
      final Result<Boolean> deleteMessage(Long sequence) Delete a specific message by sequence number
      final Result<MessageInfo> getMessage(Long sequence) Get a specific message by sequence number
      final Flow<ConsumerInfo> consumers() List all consumers on this streamReturns a Flow of ConsumerInfo for all consumers attached to this stream.
      final Result<ConsumerContext> consumer(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 Result<ConsumerContext> getConsumer(String consumerName) Get an existing consumer on this stream by name
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

    • Method Detail

      • getInfo

         final Result<StreamInfo> getInfo()

        Get current stream information

        Returns:

        Result containing StreamInfo on success or exception on failure

      • update

         final Result<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:

        Result containing updated StreamInfo on success

      • delete

         final Result<Boolean> delete()

        Delete the stream

        Permanently deletes the stream and all its messages. This operation cannot be undone.

        Returns:

        Result containing true on successful deletion

      • purge

         final Result<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.* matches orders.created but not orders.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:

        Result containing PurgeResponse with purge statistics, or a failure if subject is blank or is not a valid NATS subject

      • deleteMessage

         final Result<Boolean> deleteMessage(Long sequence)

        Delete a specific message by sequence number

        Parameters:
        sequence - The sequence number of the message to delete
        Returns:

        Result containing true on successful deletion

      • getMessage

         final Result<MessageInfo> getMessage(Long sequence)

        Get a specific message by sequence number

        Parameters:
        sequence - The sequence number of the message to retrieve
        Returns:

        Result containing MessageInfo on success

      • consumers

         final Flow<ConsumerInfo> consumers()

        List all consumers on this stream

        Returns a Flow of ConsumerInfo for all consumers attached to this stream.

        Returns:

        Flow of ConsumerInfo

      • consumer

         final Result<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
        }.getOrThrow()
        Parameters:
        consumerName - Optional name for the consumer (null for ephemeral)
        block - Configuration block for the consumer
        Returns:

        Result containing ConsumerContext on success

      • getConsumer

         final Result<ConsumerContext> getConsumer(String consumerName)

        Get an existing consumer on this stream by name

        Parameters:
        consumerName - The name of the consumer
        Returns:

        Result containing ConsumerContext on success