class Crabbit::Environment

Overview

Owns Stream connections and creates producers, consumers, and management requests.

An environment lazily creates and reuses connections according to the current stream metadata. Closing it also closes all producers, consumers, super-stream resources, and pooled connections created through it.

environment = Crabbit::Environment.connect(
  "rabbitmq-stream://guest:guest@localhost:5552/%2f"
)
begin
  environment.create_stream("events") unless environment.stream_exists?("events")
ensure
  environment.close
end

Defined in:

crabbit/environment.cr

Constructors

Instance Method Summary

Constructor Detail

def self.connect(uri : String = Configuration::DEFAULT_URI, **options) : self #

Parses uri and returns an environment.

Named options are forwarded to Configuration.parse.


[View source]
def self.new(configuration : Configuration = Configuration.new, compression_codecs : CompressionCodecs = CompressionCodecs.new) #

Creates an environment without opening a connection.

Connections are established lazily by management and messaging methods. Pass a custom registry to replace or extend sub-entry compression codecs.


[View source]

Instance Method Detail

def close : Nil #

Idempotently closes all resources and network connections.


[View source]
def closed? : Bool #

Returns whether this environment was closed.


[View source]
def compression_codecs : CompressionCodecs #

Returns the compression-codec registry used by producers and consumers.


[View source]
def configuration : Configuration #

Returns the immutable connection configuration.


[View source]
def consumer(stream : String, options : ConsumerOptions = ConsumerOptions.new) : Consumer #

Creates a pull consumer for stream.

Retrieve deliveries with Consumer#receive, Consumer#receive?, or Consumer#each.


[View source]
def consumer(stream : String, options : ConsumerOptions = ConsumerOptions.new, &handler : Delivery -> ) : Consumer #

Creates a callback consumer for stream.

The block runs on handler fibers and each delivery is marked processed after the block returns, even when it raises.


[View source]
def create_stream(stream : String, options : StreamOptions = StreamOptions.new) : Nil #

Creates stream with the supplied retention and placement options.

Raises BrokerError when RabbitMQ rejects the operation, including when the stream already exists.


[View source]
def create_super_stream(name : String, partitions : Enumerable(String), binding_keys : Enumerable(String), arguments : Hash(String, String) = {} of String => String) : Nil #

Creates a super stream from matching partition and binding-key lists.

Each partition must have a corresponding binding key. arguments are passed to RabbitMQ unchanged.


[View source]
def delete_stream(stream : String) : Nil #

Deletes stream.


[View source]
def delete_super_stream(name : String) : Nil #

Deletes the super stream name and its partitions.


[View source]
def metadata(streams : Enumerable(String)) : Array(StreamMetadata) #

Returns metadata for every requested stream in input order.

Per-stream failures are represented by StreamMetadata#response_code.


[View source]
def partitions(super_stream : String) : Array(String) #

Returns the ordered partition stream names of super_stream.


[View source]
def producer(stream : String, options : ProducerOptions = ProducerOptions.new) : Producer #

Creates a producer for stream.


[View source]
def query_offset(reference : String, stream : String) : UInt64 | Nil #

Returns the stored consumer offset, or nil when none exists.


[View source]
def query_publisher_sequence(reference : String, stream : String) : UInt64 #

Returns the last publishing ID recorded for a named publisher.

RabbitMQ returns zero when no publisher sequence exists.


[View source]
def resolve_offset(stream : String, offset : OffsetSpecification, properties : Hash(String, String) = {} of String => String) : UInt64 #

Resolves a relative or timestamp offset to an absolute stream offset.

properties are forwarded to RabbitMQ's Resolve Offset Specification command and are useful for broker extensions.


[View source]
def route(routing_key : String, super_stream : String) : Array(String) #

Returns the partitions selected by routing_key and broker bindings.


[View source]
def store_offset(reference : String, stream : String, offset : UInt64) : Nil #

Stores offset for the consumer reference and stream.


[View source]
def stream_exists?(stream : String) : Bool #

Returns whether stream currently exists.

Errors other than ResponseCode::StreamDoesNotExist are raised.


[View source]
def stream_stats(stream : String) : StreamStats #

Returns broker statistics for stream.


[View source]
def super_stream_consumer(super_stream : String, options : ConsumerOptions, &handler : Delivery -> ) : SuperStreamConsumer #

Creates callback consumers for all current and future partitions of a super stream.


[View source]
def super_stream_producer(super_stream : String, options : SuperStreamProducerOptions) : SuperStreamProducer #

Creates a producer that routes messages across a super stream.


[View source]