Creating Queues with the Python API

It is possible to provision queues on the Solace event broker using the Python 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 Queue.non_durable_exclusive_queue("queue-name") 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:

# Define Topic subscriptions 
topics_sub = [TopicSubscription.of("solace/sample/1")]

# Queue name. This assumes that a persistent queue already exists on the broker with the right topic subscription 
non_durable_exclusive_queue = Queue.non_durable_exclusive_queue("sample-queue")			

# Create a PersistentMessageReceiver Builder which allows you to create a PersistentMessageReceiver  and start it
persistent_receiver= messaging_service.create_persistent_message_receiver_builder() \
           .build(non_durable_exclusive_queue)

# Start starts the configured PersistentMessageReceiver synchronously. Before this function is called, the receiver is considered off-duty
persistent_receiver.start()	
			
# Add any additional subscriptions to your receiver			
persistent_receiver.add_subscription(topics_sub)	

If the client disconnects unexpectedly (for example, due to a networking issue), the queue is not deleted until 60 seconds later. This behavior 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 with_missing_resources_creation_strategy()function when building your PersistentMessageReceiver. MissingResourcesCreationStrategy takes a parameter that has two possible values:

  • MissingResourcesCreationStrategy.DO_NOT_CREATE— The default value, which disables any attempt to create missing resources.

  • MissingResourcesCreationStrategy.CREATE_ON_START— 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+ Python API, durable endpoint provisioning is implemented using the Missing Resource Creation Strategy, which means that you cannot deprovision the endpoints from the API.

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

# Define Topic subscriptions 
topics_sub = [TopicSubscription.of("solace/sample/1")]

# Queue name
durable_exclusive_queue = Queue.durable_exclusive_queue("sample-queue")			

# Create a PersistentMessageReceiver Builder which allows you to create a PersistentMessageReceiver  and start it
persistent_receiver= messaging_service.create_persistent_message_receiver_builder() \
           .with_missing_resources_creation_strategy(MissingResourcesCreationStrategy.CREATE_ON_START)\
           .build(durable_exclusive_queue)

# Start starts the configured PersistentMessageReceiver synchronously. Before this function is called, the receiver is considered off-duty
persistent_receiver.start()
						
# Add any additional subscriptions to your receiver			
persistent_receiver.add_subscription(topics_sub)			

The PubSub+ event broker may be configured with queue templates, which allow you to set custom attributes on queues you create with the Python 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 and how to configure them, see Configuring Queue Templates.

You cannot use the PubSub+ Python API to deprovision queues created with the Missing Resource Creation Strategy. Use SEMP or the Solace PubSub+ event broker Command Line Interface (CLI) to deprovision these queues.