void RabbitMQConnection::deleteExchange (const std::string &exchange_name,
    bool ifUnused)
{
  amqp_bytes_t exchange = amqp_cstring_bytes (exchange_name.c_str() );

  amqp_exchange_delete (conn, 1, exchange, ifUnused);
}
Esempio n. 2
0
/**
 * Internal function used to initialize the connection to 
 * the RabbitMQ server. Also used to reconnect to the server
 * in case the connection fails and to redeclare exchanges
 * and queues if they are lost
 * 
 */
static int 
init_conn(MQ_INSTANCE *my_instance)
{ 
  int rval = 0;
  int amqp_ok = AMQP_STATUS_OK;

  if(my_instance->use_ssl){

    if((my_instance->sock = amqp_ssl_socket_new(my_instance->conn)) != NULL){

      if((amqp_ok = amqp_ssl_socket_set_cacert(my_instance->sock,my_instance->ssl_CA_cert)) != AMQP_STATUS_OK){
	skygw_log_write(LOGFILE_ERROR,
			"Error : Failed to set CA certificate: %s", amqp_error_string2(amqp_ok));
	goto cleanup;
      }
      if((amqp_ok = amqp_ssl_socket_set_key(my_instance->sock,
					    my_instance->ssl_client_cert,
					    my_instance->ssl_client_key)) != AMQP_STATUS_OK){
	skygw_log_write(LOGFILE_ERROR,
			"Error : Failed to set client certificate and key: %s", amqp_error_string2(amqp_ok));
	goto cleanup;
      }
    }else{

      amqp_ok = AMQP_STATUS_SSL_CONNECTION_FAILED;
      skygw_log_write(LOGFILE_ERROR,
		      "Error : SSL socket creation failed.");
      goto cleanup;
    }

    /**SSL is not used, falling back to TCP*/
  }else if((my_instance->sock = amqp_tcp_socket_new(my_instance->conn)) == NULL){
    skygw_log_write(LOGFILE_ERROR,
		    "Error : TCP socket creation failed.");
    goto cleanup;    

  }

  /**Socket creation was successful, trying to open the socket*/
  if((amqp_ok = amqp_socket_open(my_instance->sock,my_instance->hostname,my_instance->port)) != AMQP_STATUS_OK){
    skygw_log_write(LOGFILE_ERROR,
		    "Error : Failed to open socket: %s", amqp_error_string2(amqp_ok));
    goto cleanup;
  }
  amqp_rpc_reply_t reply;
  reply = amqp_login(my_instance->conn,my_instance->vhost,0,AMQP_DEFAULT_FRAME_SIZE,0,AMQP_SASL_METHOD_PLAIN,my_instance->username,my_instance->password);
  if(reply.reply_type != AMQP_RESPONSE_NORMAL){
    skygw_log_write(LOGFILE_ERROR,
		    "Error : Login to RabbitMQ server failed.");
    
    goto cleanup;
  }
  amqp_channel_open(my_instance->conn,my_instance->channel);
  reply = amqp_get_rpc_reply(my_instance->conn);  
  if(reply.reply_type != AMQP_RESPONSE_NORMAL){
    skygw_log_write(LOGFILE_ERROR,
		    "Error : Channel creation failed.");
    goto cleanup;
  }

  amqp_exchange_declare(my_instance->conn,my_instance->channel,
			amqp_cstring_bytes(my_instance->exchange),
			amqp_cstring_bytes(my_instance->exchange_type),
			0, 1,
			amqp_empty_table);

  reply = amqp_get_rpc_reply(my_instance->conn);  

  if(reply.reply_type != AMQP_RESPONSE_NORMAL){
    skygw_log_write(LOGFILE_ERROR,
		    "Error : Exchange declaration failed,trying to redeclare the exchange.");
    if(reply.reply_type == AMQP_RESPONSE_SERVER_EXCEPTION){
      if(reply.reply.id == AMQP_CHANNEL_CLOSE_METHOD){
	amqp_send_method(my_instance->conn,my_instance->channel,AMQP_CHANNEL_CLOSE_OK_METHOD,NULL);
      }else if(reply.reply.id == AMQP_CONNECTION_CLOSE_METHOD){
	amqp_send_method(my_instance->conn,my_instance->channel,AMQP_CONNECTION_CLOSE_OK_METHOD,NULL);
      }
      
      my_instance->channel++;
      amqp_channel_open(my_instance->conn,my_instance->channel);
    
      amqp_exchange_delete(my_instance->conn,my_instance->channel,amqp_cstring_bytes(my_instance->exchange),0);
      amqp_exchange_declare(my_instance->conn,my_instance->channel,
			    amqp_cstring_bytes(my_instance->exchange),
			    amqp_cstring_bytes(my_instance->exchange_type),
			    0, 1,
			    amqp_empty_table);
      reply = amqp_get_rpc_reply(my_instance->conn);  
    }
    if(reply.reply_type != AMQP_RESPONSE_NORMAL){
      skygw_log_write(LOGFILE_ERROR,
		      "Error : Exchange redeclaration failed.");
      goto cleanup;
    }
  }

  if(my_instance->queue){

    

    amqp_queue_declare(my_instance->conn,my_instance->channel,
		       amqp_cstring_bytes(my_instance->queue),
		       0, 1, 0, 0,
		       amqp_empty_table);
    reply = amqp_get_rpc_reply(my_instance->conn);  
    if(reply.reply_type != AMQP_RESPONSE_NORMAL){
      skygw_log_write(LOGFILE_ERROR,
		      "Error : Queue declaration failed.");
      goto cleanup;
    }

 
    amqp_queue_bind(my_instance->conn,my_instance->channel,
		    amqp_cstring_bytes(my_instance->queue),
		    amqp_cstring_bytes(my_instance->exchange),
		    amqp_cstring_bytes(my_instance->key),
		    amqp_empty_table);
    reply = amqp_get_rpc_reply(my_instance->conn);  
    if(reply.reply_type != AMQP_RESPONSE_NORMAL){
      skygw_log_write(LOGFILE_ERROR,
		      "Error : Failed to bind queue to exchange.");
      goto cleanup;
    }
  }
  rval = 1;

 cleanup:

  return rval;
 
}