Creating Queues with the PubSub+ Go API

It is possible to provision queues on the Solace event broker using the Go API and a PersistentMessageReceiverBuilder. These queues can be durable or non-durable (see our blog post Understanding Solace Endpoints: Durable vs. Non-Durable), and are not provisioned on the event broker until you call Start() on your PersistentMessageReceiver.

Create a Non-Durable Queue

To create a non-durable queue, pass resource.QueueNonDurableExclusive(queueName) into the parameters of the Build() function when building a PeristentMessageReceiver. The queue is provisioned on the broker when you call the Start() function on your PersistentMessageReceiver. This non-durable queue is deleted after the client application that created the queue disconnects from the event broker. The following example shows how to create a non-durable queue:

nonDurableExclusiveQueue := resource.QueueNonDurableExclusive("queueName")  // Creates a reference to an exclusive, non-durable queue with the specified name
topic := resource.TopicSubscriptionOf(topicString)			     // Creates a TopicSubscription of the specified topic string

/* Creates an instance of PersistentMessageReceiverBuilder, which is used to create PersistentMessageReceivers. */              
persistentReceiver, err := messagingService.CreatePersistentMessageReceiverBuilder().
	WithSubscriptions(topic).					    // Sets a list of TopicSubscriptions to subscribe to when starting the receiver.
	Build(nonDurableExclusiveQueue)					    // Returns *IllegalArgumentError if the queue is nil.

If the client disconnects unexpectedly (for example, due to a networking issue), the queue is not deleted until 60 seconds later. This allows auto-reconnect logic to reconnect the client to the queue before it disappears.

Create a Durable Queue

To create a durable queue, call the WithMissingResourcesCreationStrategy()function when building your PersistentMessageReceiver. MissingResourcesCreationStrategy takes a parameter that has two possible values:

  • config.MissingResourcesCreationStrategy(config.PersistentReceiverDoNotCreateMissingResources)— The default value, which disables any attempt to create missing resources.

  • config.MissingResourcesCreationStrategy(config.PersistentReceiverCreateOnStartMissingResources)— Creates the queue provided in the Build() function as long as the client has sufficient permissions (an exception is thrown otherwise).

The queue is provisioned on the broker when you call the Start() function on your PersistentMessageReceiver.

For the PubSub+ Go API, durable endpoint provisioning is implemented using the Missing Resource Creation Strategy, which means that you cannot deprovision the endpoints.

The following example shows how to create a durable queue using the Missing Resource Creation Strategy:

durableExclusiveQueue := resource.QueueDurableExclusive("queueName")        // Creates an exclusive, durable queue with the specified name
topic := resource.TopicSubscriptionOf(topicString)			     // Creates a TopicSubscription of the specified topic string
strategy := config.MissingResourcesCreationStrategy(config.PersistentReceiverCreateOnStartMissingResources)      // Represents the various missing resource creation strategies available to publishers requiring guaranteed resources

/* Creates an instance of PersistentMessageReceiverBuilder, which is used to create PersistentMessageReceivers. */              
persistentReceiver, err := messagingService.CreatePersistentMessageReceiverBuilder().
	WithMissingResourcesCreationStrategy(strategy).                     // Defines what actions the API may take when missing resources are detected.
	WithSubscriptions(topic).					    // Sets a list of TopicSubscriptions to subscribe to when starting the receiver.
	Build(durableExclusiveQueue)					    // Returns *IllegalArgumentError if the queue is nil.

The PubSub+ event broker may be configured with queue templates, which allow you to set custom attributes on queues you create with the Go API. When using the Missing Resource Creation Strategy, queue templates will only override the provided queue attributes if the name of the queue you create matches the template name-filter. For information about queue templates see Endpoints, and for instructions on how to configure a queue template using the Solace CLI see Configuring Endpoint Templates.