Adding Subscriptions to Endpoints

Typically, messages are published to a Queue when it is set as the destination of the message. However, you can also add a Topic subscription to a Queue so that it receives any messages that are published to a matching Topic destination.

As shown in the figure below, when a Topic subscription is added to a Queue, it is possible for a message published to a Topic to be sent to a Queue, a Topic Endpoint, and directly to the message bus where it can be matched with clients’ Topic subscriptions.

Possible Routing of Messages Published to Topics

Adding Subscriptions to Queues

A client can add or remove Topic subscriptions whether it is bound to the Queue or not, but the Queue must have at least Modify-Topic privileges (refer to Configuring Permissions for Non-Owner Clients) for the client to add Topic subscriptions to it or remove Topic subscriptions from it.

A client cannot remove Topic subscriptions that an administrator has added to a Queue through the Solace CLI or SolAdmin.

When a Queue is deleted, all topic-to-queue mappings for that Queue are also deleted.

If persistent or non-persistent messages are published to a topic that matches a queue's subscriptions but the event broker cannot persist the message on the queue (for example, if the message size exceeds the queue's configured size limit or if the queue is over quota), the message is discarded. Whether the publisher receives a rejection (NACK) depends on the queue's reject-msg-to-sender-on-discard setting, which is enabled by default on the event broker. For more information, see Configuring Message Discard Handling.

To add a Topic subscription to a Queue, call a method or function listed below appropriate for the messaging API used, and pass in the Queue name and the Topic that you want to add to that Queue.

The subscription flags that can be set include:

  • Wait For Confirm
  • Request Confirm (for Java RTO, C and .NET)

To Add a Topic Subscription to a Queue

PubSub+ Messaging API Call

JCSMP

JCSMPSession.addSubscription(...)

Java RTO

SessionHandle.subscribe(...)

C

solClient_session_
endpointTopicSubscribeExt(...)

.NET

ISession.Subscribe(...)

JavaScript and Node.js

messageConsumer.addSubscription(...)

Related Samples

For an example of how to add to a Topic subscription to a Queue, refer to the TopicToQueueMapping sample for the appropriate messaging API.

Adding Subscriptions with Custom Callbacks to Queues

When using the Java RTO, C, or .NET APIs, you can add Topic subscriptions to the Flow bound to a Queue and use specific message receive callbacks or delegates to handle the Guaranteed messages that are received with matching Topics. This functionality is also known as topic dispatching because messages with a specific Topic are dispatched to a specific callback or delegate.

By default, each Topic subscription with a custom callback or delegate is added to the event broker, and it is removed when there is no longer a custom callback or delegate associated with the subscription. However, you can use the Local Dispatch Only subscribe flag to add a Topic subscription that uses a specific callback or delegate without adding the subscription to the event broker.

Error indications are not returned to the publishing client if the message cannot be delivered to a Queue for some reason (that is, the feedback to the publishing client is the same as what occurs when a message terminates on a Topic Endpoint).

To Add a Subscription with a Custom Callback to a Queue

PubSub+ Messaging API Call

JCSMP

Not applicable

Java RTO

FlowHandle.subscribe(...) and pass in the following:

  • a MessageDispatchTargetHandle

  • optional subscribe flags

  • a message correlation key

A MessageDispatchTargetHandle encapsulates a topic subscription and a message callback. Call Solclient.Allocator.newMessageDispatchTargetHandle(...) to create one.

C

solClient_flow_topicSubscribeWithDispatch(...) and pass in the following:

  • the Flow bound to the Queue

  • the Topic subscription that you want to add

  • message receive callback information

  • optional subscribe flags

.NET

IMessageDispatcher.Subscribe(...) and pass in an IDispatchTarget object and any optional subscribe flags.

An IDispatchTarget encapsulates a subscription (an ISubscription) and a message callback delegate (a MessageHandler). Call IFlow.CreateDispatchTarget() to create an IDispatchTarget instance.

JavaScript and Node.js

Not applicable

Adding Subscription Exceptions to Queues

To add a Topic subscription exception to the given durable message Queue so that Guaranteed messages published to the topic are not delivered to the Queue, repeat the steps to add a subscription and append a leading "!" character to the Topic name. In other words, use the same call to create a subscription and pass in the Topic parameter with a leading "!" character (in the form !a/b/c).

Subscription exceptions are enabled by default. If subscription exceptions are disabled, the leading "!" is treated as a literal character. For more information about enabling and disabling subscription exceptions, refer to System-Level Subscription Exception Configuration.

Adding Subscriptions to Topic Endpoints

Typically, a Flow for a Topic Endpoint receives messages according to the single Topic defined for it when the Flow is created. (For the C API, a Flow property sets the Topic; for the .NET API, the ISubscription that is passed in sets the Topic.)

When using the Java RTO, C, or .NET APIs, you can add additional Topic subscriptions to a Flow bound to a Topic Endpoint and use specific message receive callbacks or delegates to handle Guaranteed messages for those Topics.

Dispatching messages for a Topic Endpoint to a custom callback or delegate can be useful when the Flow’s Topic contains wildcards because it enables you to separate out received messages with more precision. For example if a Flow’s Topic is part1/>, it could receive messages with the Topics part1/item1 and part1/item2. Using topic dispatch subscriptions, you can configure separate message receive callback routines: one for messages published to part1/item1, and one for messages published to part1/item2.

It is not possible to dynamically add subscriptions to a provisioned Topic Endpoint on the event broker. Therefore a subscription with a custom callback or message delegate for a Topic Endpoint is only saved to the messaging API’s local dispatch table. This is the same behavior that occurs when the Local Dispatch subscribe flag is enabled when adding a Topic subscription with a custom callback or delegate to a Queue (refer to Adding Subscriptions with Custom Callbacks to Queues).

To Add a Topic Subscription with Custom Callback or Delegate

PubSub+ Messaging API Call

JCSMP

Not applicable

Java RTO

FlowHandle.subscribe(...) and pass in the following:

  • a MessageDispatchTargetHandle

  • the LOCAL_ONLY subscribe flag (required)

  • optional subscribe flags

  • a message correlation key

A MessageDispatchTargetHandle encapsulates a topic subscription and a message callback. Call Solclient.Allocator.newMessageDispatchTargetHandle(...) to create one.

C

solClient_flow_topicSubscribeWithDispatch(...) and pass in the following:

  • the Flow bound to the Topic Endpoint

  • the Topic subscription that you want to add

  • message receive callback information

  • optional subscribe flags

.NET

IMessageDispatcher.Subscribe(...) and pass in an IDispatchTarget object and any optional subscribe flags.

An IDispatchTarget encapsulates a subscription (an ISubscription) and a message callback delegate (a MessageHandler). Call IFlow.CreateDispatchTarget(...) to create an IDispatchTarget instance.

JavaScript and Node.js

messageConsumer.addSubscription(...) and pass in the following:

  • the topic destination

  • optional correlation key

  • optional timeout period in milliseconds

Related Samples

For an example of how to add to a Topic subscription with a custom callback or delegate, refer to the TopicDispatch sample for the Java RTO, C, or .NET APIs.

Removing Subscriptions from Endpoints

To remove a Topic subscription that was added to a Queue or Topic Endpoint, call the method or function listed below appropriate for the messaging API used and pass in the Queue or Topic Endpoint, subscription, and any subscribe flags that are used.

To Remove a Topic Subscription from an Endpoint

PubSub+ Messaging API Call

JCSMP

JCSMPSession.removeSubscription(...)

Java RTO

  • FlowHandle.unsubscribe(...)

  • SessionHandle.unsubscribe(...) if passing an Endpoint as an argument.

C

  • solClient_session_endpointTopicUnsubscribe(...)

  • solClient_flow_topicUnsubscribeWithDispatch(...)

.NET

ISession.Unsubscribe(...)

JavaScript and Node.js

messageConsumer.removeSubscription(...)