在windows上远程使用simplerabbitmq注意事项

1 修改rabbitmq安装目录中etc下rabbitmq.config.example文件,如下:

%% -*- mode: erlang -*-
%% ----------------------------------------------------------------------------
%% RabbitMQ Sample Configuration File.
%%
%% Related doc guide: http://www.rabbitmq.com/configure.html. See
%% http://rabbitmq.com/documentation.html for documentation ToC.
%% ----------------------------------------------------------------------------
[
 {rabbit,
  [%%
   %% Networking
   %% ====================
   %%
   %% Related doc guide: http://www.rabbitmq.com/networking.html.


   %% By default, RabbitMQ will listen on all interfaces, using
   %% the standard (reserved) AMQP port.
   %%
   %% {tcp_listeners, [5672]},
   {tcp_listeners, [5672]},
   {loopback_users, ["jc"]},    说明:这里的jc为新添加的管理员

..........

2  网页访问端口默认为15672,程序中访问端口默认为5671。


3 测试代码。

发送消息代码:

#include "SimpleAmqpClient/SimpleAmqpClient.h"
#include <iostream>
#pragma comment ( lib,"SimpleAmqpClient.2.lib" ) 
using namespace AmqpClient;

int _tmain(int argc, _TCHAR* argv[])
{
  std::string queue_name = "hemo";
  
 // AmqpClient::Channel::ptr_t channel;
  
  auto channel =  Channel::Create("192.168.0.119");
  //创建channel

  channel->DeclareQueue(queue_name, false, true, false, false);
  //创建队列,第一个参数为队列名称

  std::string message;
  std::cin >> message;

  channel->BasicPublish("", queue_name,
                         BasicMessage::Create(message));
  //第一个是exchange名称,第二个参数是routing_key(此处可理解为消息会被送往的队列)。

  std::cout << "[x] send " << message << std::endl;

  getchar();
  return 0;
}


接受消息代码:

#include <SimpleAmqpClient/SimpleAmqpClient.h>
#include <iostream>
#pragma comment ( lib,"SimpleAmqpClient.2.lib" )

int _tmain(int argc, _TCHAR* argv[])
{
  std::string queue_name = "hemo";
  printf("hello\n");
  AmqpClient::Channel::ptr_t channel = AmqpClient::Channel::Create("192.168.0.119");
  printf("hello 1 \n");
  channel->DeclareQueue(queue_name, false, true, false, false);
    printf("hello 2 \n");
  std::string consumer_tag = channel->BasicConsume(queue_name, "");
  //第二个参数为消费者名称,返回值也是消费者名称。
    printf("hello 3\n");
  while (1) {
    std::cout << "[y] wait for the message" << std::endl;

    AmqpClient::Envelope::ptr_t envelope =
        channel->BasicConsumeMessage(consumer_tag);

    std::string buffer = envelope->Message()->Body();
    //消息放在信封里,需要解析

    std::cout << "[y] receve " << buffer << std::endl;
  }

  channel->BasicCancel(consumer_tag);
  //关闭消费者。

  return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值