I'm working with activemq-cpp and I am attempting to send binary data using BytesMessage. I have a producer and a consumer set up to send and receive the message. The connection and session is working properly because I am able to publish to a topic and allow the consumer to pick up the message using TextMessage. After verifying connectivity, I've changed my TextMessage implementation to BytesMessage. The problem I am having now is in transcoding BytesMessages and getting the data out of the message after it is received.
In my producer, I have:
void producer() {
try {
//....setup code
//temporary vector
vector<unsigned char> vec;
unsigned char temp1 = 'a';
vec.push_back(temp1);
vec.push_back(temp1);
vec.push_back(temp1);
BytesMessage * message = session->createBytesMessage();
message->writeBytes(vec);
cout << "SIZE IS: " << vec.size() << endl;
producer->send( message );
delete message;
} catch (CMSException& e) {
e.printStackTrace();
}
}
In my consumer, I have:
void begin(){
// setup code to get session, etc.
shared_ptr<BytesMessage> bytemessage =
boost::dynamic_pointer_cast<BytesMessage>(message);
vector<unsigned char> temp;
bytemessage->readBytes(temp);
cout << "SIZE IS: " << temp.size() << endl;
}
The Producer prints out a size of 3, which is correct. But the consumer prints out a size of 0, indicating that it didn't read in the message that was previously written correctly. Which leads me to ask, what am I doing wrong?
I've attempted to write and read it locally and still I'm unable to do so:
void producer() {
try {
//....setup code
//temporary vector
vector<unsigned char> vec;
unsigned char temp1 = 'a';
vec.push_back(temp1);
vec.push_back(temp1);
vec.push_back(temp1);
BytesMessage * message = session->createBytesMessage();
message->writeBytes(vec);
cout << "SIZE IS: " << vec.size() << endl;
message->reset();
vector<unsigned char> temp2;
message->readBytes(temp2);
cout << "SIZE IS2: " << temp2.size() << endl;
delete message;
} catch (CMSException& e) {
e.printStackTrace();
}
}
The cout after the write prints out 3 but the cout after the read still prints 0.
Am I not writing in the data correctly? Any point in the right direction is much appreciated. Thanks!
Have you read the API docs for cms::BytesMessage, its summed up fairly well in there. Basically you need to allocate the amount of space in the vector you want it to fill. Since your vector is not sized it returns nothing, you could create the vector using the getBodyLength method of BytesMessage.
The API docs are located here.
-Tim www.fusesource.com