1

我正在开发一个从具有专有属性集的 Tibco EMS 队列中使用的应用程序。我将能够在活动和待机模式下运行我的应用程序的多个实例。当应用程序处于待机模式时,它不应该为独占队列创建消费者。

我已经实施了以下解决方案,但正在寻找更好的方法来做到这一点?

目前正在使用 tibemsQueueInfo_GetReceiverCount() 来获取接收器计数。但是这个 API 提供了为队列创建的所有消费者,在此之前我必须再调用 2 个 API。

是否有一个 API 会返回队列有一个活跃的消费者?

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);
    }   
4

1 回答 1

1

不,遗憾的是没有更简单的 API。如果有“直接访问”方法,它会在 tibemsAdmin 上,但没有。从结构上讲,API 是有意义的:你有一个管理对象,然后访问队列对象,然后询问该队列对象的属性。

此外,下面的代码也不起作用,因此消费者是否是“第二顺位”这一事实仅在消费者创建后才建立并静默等待轮到它。

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;
    }
}
于 2019-09-30T07:29:31.677 回答