0

I've worked with activemqcpp API before in a few projects, but I've always known what type the message are beforehand, so the dynamic casting to the corresponding message subclass was safe.

Now I'm building a wrapper for the MQ library and can't find a way to recognize from a base Message pointer (as returned by a receive) what message subclass does it match to cast it accordingly.

4

1 回答 1

1

如果您想以纯 C++ 方式做事,那么您可以使用 C++ RTTI 中的 typeid 运算符来检查对象以查看它是什么。

一种更简单的方法是转换为所有 CMS Message 实例派生自的底层消息类型:

activemq::core::commands::Message

这个类提供了一个方法getDataStructureType()methods,它通过OpenWire协议中使用的分配ID返回类型:

    const unsigned char ID_ACTIVEMQBLOBMESSAGE = 29;
    const unsigned char ID_ACTIVEMQBYTESMESSAGE = 24;
    const unsigned char ID_ACTIVEMQMAPMESSAGE = 25;
    const unsigned char ID_ACTIVEMQMESSAGE = 23;
    const unsigned char ID_ACTIVEMQOBJECTMESSAGE = 26;
    const unsigned char ID_ACTIVEMQSTREAMMESSAGE = 27;
    const unsigned char ID_ACTIVEMQTEXTMESSAGE = 28;

或者您可以尝试对每种类型进行动态转换,直到结果为非空。

于 2014-03-06T15:56:28.970 回答