1
votes

I am developing an application which consumes from a Tibco EMS Queue which has exclusive property set. I shall be able to run multiple instances of my application in Active and Standby mode. When the application is in standby mode it should not create a consumer to the exclusive queue.

I have implemented the below solution but am looking for a better way to do this?

Currently am using tibemsQueueInfo_GetReceiverCount() to get the receiver count. But this API gives all the consumers created for the queue and I have to call 2 more APIs before this.

Is there a single API that would just return that the queue has an active consumer?

status = tibemsAdmin_Create(&admin, server, userName, password, sslParams);
    if (status != TIBEMS_OK)
    {
        baseUtils_print("tibemsAdmin_Create create failed: %s\n", tibemsStatus_GetText(status));
        exit(1);
    }
    baseUtils_print("Amin creation successful\n");

    status = tibemsAdmin_GetQueue(admin, &queueInfo, name);
    if (status != TIBEMS_OK)
    {
        baseUtils_print("tibemsAdmin_GetQueue create failed: %s\n", tibemsStatus_GetText(status));
        exit(1);
    }
    baseUtils_print("Admin GetQueue successful \n");

    status = tibemsQueueInfo_GetReceiverCount(queueInfo, &receiverCount);
    if (status != TIBEMS_OK)
    {
        baseUtils_print("tibemsQueueInfo_GetReceiverCount create failed: %s\n", tibemsStatus_GetText(status));
        exit(1);
    }
    baseUtils_print("Queue: '%s', Active Consumers = '%d'\n",name, receiverCount);
    bool flag = true;
    int prevCount = 0;
    while(receiverCount)
    {
        prevCount = receiverCount;

        if(flag)
        {
            cout << "Consumer in Standby mode..."<<endl;
            flag = false;;
        }
        std::this_thread::sleep_for(std::chrono::seconds(3));       
        status = tibemsAdmin_GetQueue(admin, &queueInfo, name);

        if (status != TIBEMS_OK)
        {
            baseUtils_print("tibemsAdmin_GetQueue create failed: %s\n", tibemsStatus_GetText(status));
            exit(1);
        }

        status = tibemsQueueInfo_GetReceiverCount(queueInfo, &receiverCount);
        if (status != TIBEMS_OK)
        {
            baseUtils_print("tibemsQueueInfo_GetReceiverCount create failed: %s\n", tibemsStatus_GetText(status));
            exit(1);
        }

        if(receiverCount != prevCount)
            cout << "current receiver count = "<<receiverCount<<endl;
    }
    cout << "Consumer mode is Active"<<endl;

    status = tibemsSession_CreateConsumer(session,
            &msgConsumer,destination,NULL,TIBEMS_FALSE);
    if (status != TIBEMS_OK)
    {
        fail("Error creating tibemsMsgConsumer", errorContext);
    }   
1

1 Answers

1
votes

No, unfortunately there isn't an easier API. If there would be a 'direct-access' method it would be on tibemsAdmin, but there isn't. And structurally the API kind of makes sense: you have an Admin Object, then get to the Queue Object and then ask for properties of that Queue Object.

Also the code below will not work, so the fact if a consumer is 'the second in line' is only established after the consumer is created and silently waits for its turn.

while(true) // or better: while( !isStopped() ) ...
{
    status = tibemsSession_CreateConsumer(session, &msgConsumer,destination,NULL,TIBEMS_FALSE);
    if (status != TIBEMS_OK)
    {
        std::this_thread::sleep_for(std::chrono::seconds(3)); 
        cout << "Consumer in Standby mode ??"<<endl;      
    }
    else
    {
       cout << "Consumer mode is Active ??"<<endl;
       break;
    }
}