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.

Nare
  • 71
  • 8

1 Answers1

1

If you want to do things the pure C++ way then you can play around with using the typeid operator from C++ RTTI to inspect the object to see what it is.

A simpler way is to cast to the underlying message type that all CMS Message instances are derived from:

activemq::core::commands::Message

This class offers a method getDataStructureType() methods that returns the type via an assigned ID used in the OpenWire protocol:

    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;

Or you can just try a dynamic cast to each type until the result is non-null.

Tim Bish
  • 17,475
  • 4
  • 32
  • 42
  • I was using the dynamic_cast approach, given the activemq-cpp documentation I didn't know a cast to activemq::core was doable since it doesn't appear in the hierarchy tree of cms::message. – Nare Mar 07 '14 at 17:49
  • This is very disappointing. typeid or dynamic cast, being very heavy handed, are not the "pure C++ way". Rather, they are basically a form of writing Java in C++. Tibco CMS's C API provides the tibems_GetBodyType() call, that returns an enum value. Simple, clean, and lightweight. Too bad we can't have that here. – arayq2 Apr 26 '18 at 15:42
  • If only the source was freely available for someone to modify to fit their needs..... – Tim Bish Apr 27 '18 at 02:25
  • I've only just started looking at activemq-cpp. This was one of the first issues that came up while considering a port of application code based on Tibco EMS. Hence the disappointment, as given how long activemq-cpp has been around, it seems that something otherwise this obviously useful was never meant to be in the "official" API. – arayq2 Apr 27 '18 at 03:16