Inherits: IMessageQueueConnection, IReferenceable, IConfigurable, IOpenable
Description
The NatsConnection class is used to define NATS connections using the default driver.
Important points
- By defining a connection and sharing it through multiple message queues, you can reduce the number of used database connections.
Configuration parameters
- client_id: (optional) name of the client id
- connection(s):
- discovery_key: (optional) key to retrieve the connection from IDiscovery
- host: host name or IP address
- port: port number (default: 27017)
- uri: resource URI or connection string with all parameters in it
- credential(s):
- store_key: (optional) key to retrieve the credentials from ICredentialStore
- username: username
- password: user’s password
- options:
- retry_connect: (optional) turns on/off automated reconnect when the connection is lost (default: true)
- max_reconnect: (optional) maximum number of reconnection attempts (default: 3)
- reconnect_timeout: (optional) number of milliseconds to wait on each reconnection attempt (default: 3000)
- flush_timeout: (optional) number of milliseconds to wait on flushing messages (default: 3000)
References
- *:logger:*:*:1.0 - (optional) ILogger components to pass log messages
- *:discovery:*:*:1.0 - (optional) IDiscovery services
- *:credential-store:*:*:1.0 (optional) credential stores to resolve credentials
Constructors
Creates a new instance of the connection component.
publicNatsConnection()
Fields
Instance methods
CheckOpen
Checks if the connection is open.
Raises an error if the connection is closed.
privatevoid CheckOpen()
CloseAsync
Closes a component and frees used resources.
publicTask CloseAsync(IContext context)
- context: IContext - (optional) a context to trace execution through a call chain.
Configure
Configures the component by passing its configuration parameters.
publicvoid Configure(ConfigParams config)
- config: ConfigParams - configuration parameters to be set.
CreateQueueAsync
Creates a message queue. If the connection doesn’t support this function, it exists without error.
publicTask CreateQueueAsync(string name)
- name: string - name of the queue to be created.
DeleteQueueAsync
Deletes a message queue. If the connection doesn’t support this function, it exists without error.
publicTask DeleteQueueAsync(string name)
- name: string - name of the queue to be deleted.
GetConnection
Gets the connection.
publicIConnection GetConnection()
- returns: IConnection - connection to NATS
IsOpen
Checks if the component is open.
publicbool IsOpen()
- returns: bool - true if the component is open and false otherwise.
OpenAsync
Opens the component.
publicTask OpenAsync(IContext context)
- context: IContext - (optional) a context to trace execution through a call chain.
PublishAsync
Publishes a message to a specified topic.
publicTask PublishAsync(string subject, Msg message)
- subject: string - subject (topic) where the message will be placed
- message: Msg - message to be published
ReadQueueNames
Reads a list of registered queue names. If the connection doesn’t support this function, it returns an empty list.
publicTask<List<string>> ReadQueueNamesAsync()
- returns: Task<List<string>> - queue names.
SetReferences
Sets references to dependent components.
publicvoid SetReferences(IReferences references)
- references: IReferences - references to locate the component’s dependencies.
SubscribeAsync
Subscribes to a topic.
publicTask SubscribeAsync(string subject, string queue, INatsMessageListener listener)
- subject: string - subject (topic) name
- options: string - subscription options
- listener: INatsMessageListener - message listener
UnsubsUnsubscribeAsynccribe
Unsubscribes from a previously subscribed topic.
publicTask UnsubscribeAsync(string subject, string queue, INatsMessageListener listener)
- subject: string - subject (topic) name
- queue: string - subscription queue
- listener: INatsMessageListener - message listener