Activemq-cpp BytesMessage
我正在使用 activemq-cpp,并尝试使用 BytesMessage 发送二进制数据。我设置了一个生产者和一个消费者来发送和接收消息。连接和会话工作正常,因为我能够发布到主题并允许消费者使用 TextMessage 获取消息。验证连接后,我将 TextMessage 实现更改为 BytesMessage。我现在遇到的问题是对 BytesMessages 进行转码并在收到消息后从消息中获取数据。
在我的生产者中,我有:
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();
}
}
在我的消费者中,我有:
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;
}
生产者打印出尺寸 3,这是正确的。但是消费者打印出的大小为0,说明它没有读入之前正确写入的消息。这让我问,我做错了什么?
我尝试在本地写入和读取它,但仍然无法这样做:
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();
}
}
写入后的 cout 打印出 3,但读取后的 cout 仍打印出 0。
我是否没有正确写入数据?任何方向正确的观点都会受到高度赞赏。谢谢!
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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您是否阅读过 cms::BytesMessage 的 API 文档,其中总结得相当好。基本上,您需要在向量中分配您想要填充的空间量。由于您的向量未调整大小,因此不会返回任何内容,因此您可以使用 BytesMessage 的 getBodyLength 方法创建向量。
API 文档位于此处。
-蒂姆 www.fusesource.com
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