Creating Queues with the Java API

You can create non-durable queues using the PubSub+ Java API when you create a PersistentMessageReceiver. To create a non-durable queue, pass Queue.nonDureableExclusiveQueue(queueName) into the parameters of the build() method. The queue is provisioned on the PubSub+ event broker when you call the start() method 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:

/* Creates an instance of PersistentMessageReceiverBuilder, which is used to create PersistentMessageReceiver objects. */              
final PersistentMessageReceiver receiver = service.createPersistentMessageReceiverBuilder()			
	.build(Queue.nonDurableExclusiveQueue("QUEUE_NAME")); // If it does not exist, this configuration will provision the non-durable queue on the broker when the start() method is called. 
	.start();                                            // Causes the service to resume regular duties. Before this method is called, the service is considered off-duty.

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.

To create a durable queue, call the withMissingResourcesCreationStrategy()method when building your PersistentMessageReceiver object. This interface 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() method as long as the client has sufficient permissions (an exception is thrown otherwise).

The queue is provisioned on the event broker when you call the start() method on your PersistentMessageReceiver.

For the PubSub+ Java 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:

/* Creates an instance of PersistentMessageReceiverBuilder, which is used to create PersistentMessageReceiver objects. */              
final PersistentMessageReceiver receiver = service.createPersistentMessageReceiverBuilder()
	.withMissingResourcesCreationStrategy(                      // Configures the missing resources creation strategy.
		MissingResourcesCreationStrategy.CREATE_ON_START)   // The strategy to attempt create missing resources when the connection is established.
	.build(Queue.durableExclusiveQueue("QUEUE_NAME"))           // If it does not exist, this configuration will provision the non-durable queue on the broker when the start() method is called.
	.start();                                            // Causes the service to resume regular duties. Before this method is called, the service is considered off-duty.

The event broker may be configured with queue templates, which allow you to set custom attributes on queues you create with the Java API. When using the Missing Resource Creation Strategy, queue templates will only override the default queue attributes if the name of the queue you create matches the name of the template. 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.