Consuming from a topic

petegehrman
petegehrman Member Posts: 43 ✭✭

There seem to be 2 ways of consuming messages from a topic with the JCSMP client:
1. Add a topic subscription directly to your session, then create a message consumer
2. Create a topic & topic endpoint (non-durable in my case), then create a flow receiver to the topic
Can anyone explain the difference between the 2 approaches, and when to use each one?
Thanks!

Comments

  • TomF
    TomF Member, Employee Posts: 412 Solace Employee

    Hi @petegehrman,

    Sure thing:
    1. This is for messages delivered to your consumer with the DIRECT quality of Service. If your consumer is offline, it won't get messages;
    2. Note: you don't create a "Topic," you create a Topic Endpoint with a subscription. This is a JMS concept. The Topic Endpoint persists messages, so if your consumer is offline messages build up on the Topic Endpoint and are then delivered to your consumer when it comes online. Messages are never lost. Generally, it's better not to use Topic Endpoints and use Queues;
    3. Queues. A persistent message store, so again if your consumer is offline messages build up and are then delivered to your consumer when it comes online. Messages are never lost. Queues can subscribe to topics.

    See this explanation of message durability. Then see this explanation of queues subscribing to topics.

  • petegehrman
    petegehrman Member Posts: 43 ✭✭
    edited October 2021 #3

    Thanks Tom, I'm confused about this sample JCSMP code in SimpleFlowToTopic.java:
    Can you explain what the non-durable topic endpoint and temporary topic are?

    // Create Topic Endpoints and Topics to receive messages.
                TopicEndpoint topicEndpoint;
                Topic topic;
                if (useDurable) {
                    topicEndpoint = JCSMPFactory.onlyInstance().createDurableTopicEndpointEx(SampleUtils.SAMPLE_TOPICENDPOINT);
                    topic = JCSMPFactory.onlyInstance().createTopic(SampleUtils.SAMPLE_TOPIC);
                } else {
                    // Creating non-durable Topic Endpoint and temporary Topic.
                    topicEndpoint = session.createNonDurableTopicEndpoint();
                    topic = session.createTemporaryTopic();
                }
    
          // Create and start the receiver.
          receiver = session.createFlow(topicEndpoint, topic, this);
          receiver.start();
    
  • TomF
    TomF Member, Employee Posts: 412 Solace Employee

    @petegehrman, the TopicEndpoint object is an API object representing a Topic Endpoint on your message broker. It can be to used to create ("provision") a Topic Endpoint on the broker itself. It may be that you already have a Topic Endpoint on the broker, in which case you still need a TopicEndpoint API object.

    The Topic API object is an API representation of a topic. There is no one to one mapping between an API topic and a message topic: the Topic API object can be used to create a topic on a message you're about to publish, and/or it can be used to create a subscription to that topic.

    Durability relates to what happens when the application disconnects. A durable Topic Endpoint will remain on the broker until deleted. A non-durable Topic Endpoint will be deleted after a delay (1 minute, AFAIK) after the application that created it disconnects.

    A gentle reminder: if you're using JCSMP and you're not using JMS you almost certainly don't want to use Topic Endpoints, durable or otherwise. If you're using JMS and you're not using an existing JMS architecture you probably don't want to use Topic Endpoints.

  • nram
    nram Member, Employee Posts: 80 Solace Employee

    Hi @petegehrman ,
    Topic subscriber provides no delivery guarantee. If the broker runs out of resources or if the subscriber is busy, messages are dropped (there is still underlying TCP delivery guarantee)
    Non-Durable topic subscriber: As long as the client is connected, there is delivery guarantee. Once the client disconnects, the temporary endpoint is deleted (after some delay). So if messages are published after the client disconnects, they are lost.
    Durable topic subscriber: Once created, the endpoint persists until deleted manually. Messages are persisted even when the client goes offline after endpoint is created.
    You may find this blog post a good read.
    https://solace.com/blog/solace-endpoints-durable-vs-non-durable/

  • petegehrman
    petegehrman Member Posts: 43 ✭✭

    Thanks very much for the clarification everyone. It looks like I was doing things correctly ... since I'm not using JMS, I'll stick to using a topic subscriber.

  • nirodha23
    nirodha23 Member Posts: 2

    @TomF
    Would you please let me know where the sample JCSMP code in SimpleFlowToTopic.java can be accessed.
    I want to subscribe to Durable Topic Endpoint. But my code is not working. Would you please provide a sample code.

  • Tamimi
    Tamimi Member, Administrator, Employee Posts: 541 admin
  • anithamartin
    anithamartin Member Posts: 4

    @TomF : In your response you have mentioned that both Topic endpoint as well as Queue have persistent message so that message are not lost when a consumer is offline. I am still not clear on the usecase when to use queue and when to use topic endpoints in Solace. It seems to me that we can use queue for all scenarios'. Then why do we have topic endpoint as separate ? Pl clarify.

  • TomF
    TomF Member, Employee Posts: 412 Solace Employee

    Hi @anithamartin, you're right, queues and topic endpoints fulfil very similar functions. In general, always use a queue unless you have a very good reason to use a topic endpoint.

    Topic endpoints are in the product to support the JMS Open Standard. Originally there were some important differences between the two, but these days, they are very similar.

  • nram
    nram Member, Employee Posts: 80 Solace Employee
    edited January 2022 #11

    @anithamartin , As @TomF mentioned, Topic endpoints satisfy JMS Durable subscriber requirements. Other than that, they are quite restrictive in their utility.

    • You can subscribe multiple topics to Queues. Topic endpoints have a single topic subscription.
    • Queues support multiple consumers for load-balancing or high-availability use-cases. Topic endpoints support single subscriber.
    • You can publish directly to the Queue or to any of the topics subscribed. With topic endpoint, publishing can be done only on the topic.
    • There are also some differences the way selectors are handled. Pl see here for details. This may be one of the use-cases for using topic endpoints.
    • Change to topic subscription to Queues is dynamic and all messages are preserved. Any change in subscription to topic endpoint will reset the topic endpoint and all persisted messages will be lost.
      Pl also check out this blog post.
  • anithamartin
    anithamartin Member Posts: 4

    Thanks for the response @TomF and @nram, few more clarification from your response details
    1. So if I understand correctly Topic Endpoint is a standalone topic by itself. As under topic endpoint I didn't find subscriptions. Pl confirm if this is correct
    2. On the point that only one consumer can be there for a topic endpoint; for testing I created one Topic endpoint, then attached that to a Queue and also consumed it from the 'Try Me' by giving the direct Topic endpoint as well. Then I sent a message from Postman (https://...solace.cloud:9443/myNewTopicEndpoint), I could see that message got queued both under Queue as well as under TopicEndpoint. Is this allowed?

  • TomF
    TomF Member, Employee Posts: 412 Solace Employee

    Hi @anithamartin,

    1. No! A Topic Endpoint is not a topic. In PubSub+, a topic is just an address on a message. A Topic Endpoint is a piece of abstracted storage in which messages will be persisted. When a Topic Endpoint is created it has no subscriptions. When a subscriber connects (binds) to a Topic Endpoint, the subscriber's subscription is placed on the Topic Endpoint, and messages sent to the subscription topic are persisted in the TE, then passed on to the subscriber. A queue operates in almost the identical way, with a few minor differences as listed above.
    2. A Topic Endpoint can have only one consumer. Remeber that a Topic Endpoint is not the same as a topic on a message. So, it is possible to have multiple Topic Endpoints AND queues with subscriptions to the same topic. A message can be sent to a matching topic and will be persisted in every matching Topic Endpoint and Queue.
    3. A queue can have multiple consumers, in which case messages on the queue are delivered in a round robin fashion to the consumers on that queue.