RabbitMQ C 客户端 超简单接收发送json接口-最近更新2019-03-24

接口超简单,只有发送和接受两个接口,json格式

C++版本参考地址

#include "stdafx.h"
#include "messagequeue/rabbitmqc.h"
#include 
#include 

void send_message(const char*);
void get_message(const char*);

int main()
{
	//{
	//	    "rabbitmq_server_ip": "localhost",
	//		"rabbitmq_server_port" : 5672,
	//		"rabbitmq_server_psw" : "guest",
	//		"rabbitmq_server_user" : "guest",
	//		"exchange_name" : "exchange_name",
	//		"queue_name" : "queue_name",
	//		"bind_key_name" : "bind_key_name",
	//		"message_array_for_send_for_one_time" : [
	//			"123",
	//				"456",
	//				"789",
	//				"ABC中国!"
	//		]
	//}

	const char* str_send = "{\"rabbitmq_server_ip\":\"localhost\",\"rabbitmq_server_port\":5672,\"rabbitmq_server_psw\":\"guest\",\"rabbitmq_server_user\":\"guest\",\"exchange_name\":\"exchange_name\",\"queue_name\":\"queue_name\",\"bind_key_name\":\"bind_key_name\",\"message_array_for_send_for_one_time\":[\"123\",\"456\",\"789\",\"ABC中国!\"]}";
	
	send_message(str_send);
	std::cout <<"发送数据到服务器:\n"<< str_send << std::endl;
	//{
	//	    "rabbitmq_server_ip": "localhost",
	//		"rabbitmq_server_port" : 5672,
	//		"rabbitmq_server_psw" : "guest",
	//		"rabbitmq_server_user" : "guest",
	//		"consume_message_number_per_time" : 4,
	//		"queue_name" : "queue_name"
	//}
	const char* str_request =
		"{\"rabbitmq_server_ip\": \"localhost\", \"rabbitmq_server_port\" : 5672, \"rabbitmq_server_psw\" : \"guest\", \"rabbitmq_server_user\" : \"guest\", \"consume_message_number_per_time\" : 4, \"queue_name\" : \"queue_name\" }";
	std::cout << "从RabbitMQ服务器取消息:\n" << str_request << std::endl;
	get_message(str_request);

    return 0;
}

void send_message(const char* str_send)
{
	const char* dllFile = "mq.dll";
	HMODULE libraryHandle = ::LoadLibraryA(dllFile);

	//发送消息
	if (libraryHandle != NULL)
	{
		typedef char* (_cdecl * EXECUTOR_FUNC)(const char*);
		EXECUTOR_FUNC function = (EXECUTOR_FUNC)GetProcAddress(libraryHandle, "send_message_to_mq_c");
		if (function)
		{
			char* errReturnedHaveToDelete = function(str_send);
			if (errReturnedHaveToDelete != nullptr)
			{
				delete errReturnedHaveToDelete;
				errReturnedHaveToDelete = nullptr;
			}
		}
	}
}
void get_message(const char* str_request)
{
	const char* dllFile = "mq.dll";
	HMODULE libraryHandle = ::LoadLibraryA(dllFile);

	//发送消息
	if (libraryHandle != NULL)
	{
		typedef char* (_cdecl * EXECUTOR_FUNC)(const char*, char**);
		EXECUTOR_FUNC function = (EXECUTOR_FUNC)GetProcAddress(libraryHandle, "get_message_from_mq_c");
		if (function)
		{
			char* response = nullptr;
			char* errReturnedHaveToDelete = function(str_request, &response);
			if (errReturnedHaveToDelete != nullptr)
			{
				delete errReturnedHaveToDelete;
				errReturnedHaveToDelete = nullptr;
			}
			std::cout << "从RabbitMQ服务器取到的消息:\n";
			std::cout << response << std::endl;
		}
	}
}

RabbitMQ C 客户端 超简单接收发送json接口-最近更新2019-03-24_第1张图片

你可能感兴趣的:(RabbitMQ)