首页 > 其他 > 详细

使用RabbitMQ放置自己定义对象(不借助序列化工具,比如protobuffer)V2.0

时间:2017-06-26 14:06:21      阅读:697      评论:0      收藏:0      [点我收藏+]

怎样使用RabbitMQ盛放自己定义的对象呢?一般都会使用序列化工具在投放之前转换一次。从MQ取回的时候再逆序列化还原为本地对象。这里使用C++自带的强制类型装换,将本地对象的内存模型当做自然的序列化之后的数据,直接当做字节流放到string对象中,从MQ取回的时候用相同的方法再将string对象强制当做字节流转换为本地对象。

直接使用。非常棒

注意:这样的思路事实上有一个缺点就是本地上传,取回本地使用肯定是没问题的。假设本地上传,其它机器使用(本地是大端机器,其它机器是小端机器)就可能有问题,这也是序列化保证跨语言。跨机器的地方。

Student.h

#pragma once
#include <string>
using namespace std;
#include <iostream>

class Student
{
public:
	Student(void);
	~Student(void);
	string id(void) const ;
	void id(const string& id);
	int age(void) const ;
	void age(const int age);
	char* name(void) const ;
	void name(const char* name);
	char sex(void) const ;
	void sex(const char sex);
	friend ostream& operator<< (ostream& os ,const Student& stu);
private:
	Student(const Student&);
	Student& operator=(const Student&);
	string id_;
	int age_;
	char name_[10];
	char sex_;
};


Student.cpp

#include "Student.h"


Student::Student(void):sex_(0),age_(0)
{
	for (int i=0 ;i< 10;i++)
	{
		name_[i] = 0;
	}
}


Student::~Student(void)
{
}

string Student::id(void) const 
{
	return id_;
}
void Student::id(const string& id)
{
	this->id_ = id;
}
int Student::age(void) const 
{
	return age_;
}
void Student::age(const int age)
{
	age_ = age ;
}
char* Student::name(void) const 
{
	return (char*)name_;
}
void Student::name(const char* name)
{
	char* pname = this->name_;

	while((*pname++ = *name++) != ‘\0‘)
	{

	}
}

char Student::sex(void) const 
{
	return sex_;
}
void Student::sex(const char sex)
{
	sex_ = sex;
}

ostream& operator<<(ostream& os ,const Student& stu)
{
	os<<"id : "<<stu.id()
		<<" name : "<<stu.name()
		<<" sex : "<<stu.sex()
		<<" age : "<<stu.age()<<endl;
	return os;
}

main.cpp


#include "Student.h"
#include "RabbitMQ.h"
#include <vector>

int main(int, char *[])
{

	Student s;
	cout<<s<<endl;
	s.id("SCP001");
	s.name("zhangsan");
	s.age(24);
	s.sex(‘M‘);
	cout<<s<<endl;

	vector<string> stu_array;
	//for (int i=0;i<10;i++)
	//{
	//	string string_stu(reinterpret_cast<const char*>(&s),reinterpret_cast<const char*>(&s) + sizeof(Student));
	//	stu_array.push_back(string_stu);
	//}

	CRabbitMQ pro;
	CExchange exchange("exchange");
	string queue_name("queue");
	
	//mq::publish(stu_array,pro,exchange,queue_name);
	//cout<<"已经将students提交至MQserver"<<endl;
	stu_array.clear();

	mq::consume(stu_array,pro,exchange,queue_name);
	cout<<"例如以下是从MQserver取回的Students"<<endl;
	for(int i=0;i<stu_array.size();i++)
	{
		Student* pstu = (Student*)stu_array[i].c_str() ;
		cout<<*pstu<<endl;

	}

	//char* ps = (char*)&s;

	//string string_stu(reinterpret_cast<const char*>(&s),reinterpret_cast<const char*>(&s) + sizeof(Student));

	//Student* pstu = (Student*)string_stu.c_str();

	//cout<<*pstu<<endl;
	return 0;
};

这里只展示了思路,详细的MQ封装是自己封装的和标准的MQ不太一样,封装版本号见:

http://blog.csdn.net/calmreason/article/details/23346397

使用RabbitMQ放置自己定义对象(不借助序列化工具,比如protobuffer)V2.0

原文:http://www.cnblogs.com/lxjshuju/p/7080205.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!