int main(int argc, char const *const *argv) { char const *hostname; int port, status; char const *exchange; char const *exchangetype; amqp_socket_t *socket; amqp_connection_state_t conn; if (argc < 5) { fprintf(stderr, "Usage: amqps_exchange_declare host port exchange " "exchangetype [cacert.pem [key.pem cert.pem]]\n"); return 1; } hostname = argv[1]; port = atoi(argv[2]); exchange = argv[3]; exchangetype = argv[4]; conn = amqp_new_connection(); socket = amqp_ssl_socket_new(conn); if (!socket) { die("creating SSL/TLS socket"); } if (argc > 5) { status = amqp_ssl_socket_set_cacert(socket, argv[5]); if (status) { die("setting CA certificate"); } } if (argc > 7) { status = amqp_ssl_socket_set_key(socket, argv[7], argv[6]); if (status) { die("setting client key/cert"); } } status = amqp_socket_open(socket, hostname, port); if (status) { die("opening SSL/TLS connection"); } die_on_amqp_error(amqp_login(conn, "/", 0, 131072, 0, AMQP_SASL_METHOD_PLAIN, "guest", "guest"), "Logging in"); amqp_channel_open(conn, 1); die_on_amqp_error(amqp_get_rpc_reply(conn), "Opening channel"); amqp_exchange_declare(conn, 1, amqp_cstring_bytes(exchange), amqp_cstring_bytes(exchangetype), 0, 0, amqp_empty_table); die_on_amqp_error(amqp_get_rpc_reply(conn), "Declaring exchange"); die_on_amqp_error(amqp_channel_close(conn, 1, AMQP_REPLY_SUCCESS), "Closing channel"); die_on_amqp_error(amqp_connection_close(conn, AMQP_REPLY_SUCCESS), "Closing connection"); die_on_error(amqp_destroy_connection(conn), "Ending connection"); return 0; }
/** * Declares a persistent, non-exclusive and non-passive queue that * auto-deletes after all the messages have been consumed. * @param my_session MQ_SESSION instance used to declare the queue * @param qname Name of the queue to be declared * @return Returns 0 if an error occurred, 1 if successful */ int declareQueue(MQ_INSTANCE *my_instance, MQ_SESSION* my_session, char* qname) { int success = 1; amqp_rpc_reply_t reply; spinlock_acquire(&my_instance->rconn_lock); amqp_queue_declare(my_instance->conn,my_instance->channel, amqp_cstring_bytes(qname), 0, 1, 0, 1, amqp_empty_table); reply = amqp_get_rpc_reply(my_instance->conn); if(reply.reply_type != AMQP_RESPONSE_NORMAL){ success = 0; skygw_log_write(LOGFILE_ERROR, "Error : Queue declaration failed."); } amqp_queue_bind(my_instance->conn,my_instance->channel, amqp_cstring_bytes(qname), amqp_cstring_bytes(my_instance->exchange), amqp_cstring_bytes(my_session->uid), amqp_empty_table); reply = amqp_get_rpc_reply(my_instance->conn); if(reply.reply_type != AMQP_RESPONSE_NORMAL){ success = 0; skygw_log_write(LOGFILE_ERROR, "Error : Failed to bind queue to exchange."); } spinlock_release(&my_instance->rconn_lock); return success; }
/* * Initialize RabbitMQ connection */ static rsRetVal initRabbitMQ(instanceData *pData) { amqp_socket_t *asocket; amqp_exchange_declare_t edReq; DEFiRet; DBGPRINTF("omrabbitmq: trying connect to '%s' at port %d\n", pData->host, pData->port); pData->conn = amqp_new_connection(); asocket = amqp_tcp_socket_new(pData->conn); if (!asocket) { errmsg.LogError(0, RS_RET_ERR, "omrabbitmq: Error allocating tcp socket"); pData->conn = NULL; ABORT_FINALIZE(RS_RET_SUSPENDED); } if (die_on_error(amqp_socket_open(asocket, (char*) pData->host, pData->port), "Opening socket")) { pData->conn = NULL; ABORT_FINALIZE(RS_RET_SUSPENDED); } if (die_on_amqp_error(amqp_login(pData->conn, (char*) pData->vhost, 0, 131072, 0, AMQP_SASL_METHOD_PLAIN, pData->user, pData->password), "Logging in")) { pData->conn = NULL; ABORT_FINALIZE(RS_RET_SUSPENDED); } amqp_channel_open(pData->conn, RABBITMQ_CHANNEL); if (die_on_amqp_error(amqp_get_rpc_reply(pData->conn), "Opening channel")) { pData->conn = NULL; ABORT_FINALIZE(RS_RET_SUSPENDED); } if(pData->exchange_type != NULL) { edReq.ticket = 0; edReq.exchange = amqp_cstring_bytes(pData->exchange); edReq.type = amqp_cstring_bytes(pData->exchange_type); edReq.passive = 0; edReq.durable = pData->durable; edReq.auto_delete = pData->auto_delete; edReq.internal = 0; edReq.nowait = 0; edReq.arguments = amqp_empty_table; amqp_simple_rpc_decoded(pData->conn, RABBITMQ_CHANNEL, AMQP_EXCHANGE_DECLARE_METHOD, AMQP_EXCHANGE_DECLARE_OK_METHOD, &edReq); if(die_on_amqp_error(amqp_get_rpc_reply(pData->conn), "Declaring exchange")) { pData->conn = NULL; ABORT_FINALIZE(RS_RET_SUSPENDED); } } finalize_it: RETiRet; }
bool rpc_open(struct status *status) { bool retval = false; int ret = 0; DEBUG(2, ("[*] Opening RPC\n")); /* Adquire lock */ ret = pthread_spin_lock(&status->lock); if (ret) { fprintf(stderr, "[!] pthread_spin_lock: %s\n", strerror(ret)); return false; } /* Open connection */ status->conn = get_amqp_connection(); if (!status->conn) { retval = false; goto unlock; } /* Declare the control queue */ amqp_queue_declare(status->conn, 1, /* Channel */ amqp_cstring_bytes("Zentyal.OpenChange.Migrate.Control"), 0, /* Passive */ 0, /* Durable */ 1, /* Exclusive */ 1, /* Auto delete */ amqp_empty_table); if (amqp_get_rpc_reply(status->conn).reply_type != AMQP_RESPONSE_NORMAL) { fprintf(stderr, "[!] Error declaring queue\n"); retval = false; goto unlock; } /* Start the consumer */ amqp_basic_consume(status->conn, 1, amqp_cstring_bytes("Zentyal.OpenChange.Migrate.Control"), /* Queue */ amqp_cstring_bytes("Zentyal.OpenChange.Migrate.Control"), /* Tag */ 0, /* no local */ 1, /* no ack */ 0, /* exclusive */ amqp_empty_table); if (amqp_get_rpc_reply(status->conn).reply_type != AMQP_RESPONSE_NORMAL) { fprintf(stderr, "[!] Error starting consumer\n"); retval = false; goto unlock; } retval = true; unlock: /* Release lock */ pthread_spin_unlock(&status->lock); return retval; }
int main(int argc, char const * const *argv) { char const *hostname; int port; char const *exchange; char const *bindingkey; int sockfd; amqp_connection_state_t conn; amqp_bytes_t queuename; if (argc < 3) { fprintf(stderr, "Usage: amqp_consumer host port\n"); return 1; } hostname = argv[1]; port = atoi(argv[2]); exchange = "amq.direct"; /* argv[3]; */ bindingkey = "test queue"; /* argv[4]; */ conn = amqp_new_connection(); die_on_error(sockfd = amqp_open_socket(hostname, port), "Opening socket"); amqp_set_sockfd(conn, sockfd); die_on_amqp_error(amqp_login(conn, "/", 0, 131072, 0, AMQP_SASL_METHOD_PLAIN, "guest", "guest"), "Logging in"); amqp_channel_open(conn, 1); die_on_amqp_error(amqp_get_rpc_reply(conn), "Opening channel"); { amqp_queue_declare_ok_t *r = amqp_queue_declare(conn, 1, amqp_empty_bytes, 0, 0, 0, 1, amqp_empty_table); die_on_amqp_error(amqp_get_rpc_reply(conn), "Declaring queue"); queuename = amqp_bytes_malloc_dup(r->queue); if (queuename.bytes == NULL) { fprintf(stderr, "Out of memory while copying queue name"); return 1; } } amqp_queue_bind(conn, 1, queuename, amqp_cstring_bytes(exchange), amqp_cstring_bytes(bindingkey), amqp_empty_table); die_on_amqp_error(amqp_get_rpc_reply(conn), "Binding queue"); amqp_basic_consume(conn, 1, queuename, amqp_empty_bytes, 0, 1, 0, amqp_empty_table); die_on_amqp_error(amqp_get_rpc_reply(conn), "Consuming"); run(conn); die_on_amqp_error(amqp_channel_close(conn, 1, AMQP_REPLY_SUCCESS), "Closing channel"); die_on_amqp_error(amqp_connection_close(conn, AMQP_REPLY_SUCCESS), "Closing connection"); die_on_error(amqp_destroy_connection(conn), "Ending connection"); return 0; }
int main(int argc, const char **argv) { const char *hostname; int port; const char *exchange; const char *routingkey; const char *messagebody; const char *exchangetype = "direct"; if (argc < 6) { fprintf(stderr, "Usage: emit_log_direct host port exchange routingkey messagebody\n"); return 1; } hostname = argv[1]; port = atoi(argv[2]); exchange = argv[3]; routingkey = argv[4]; messagebody = argv[5]; int sockfd; int channelid = 1; amqp_connection_state_t conn; conn = amqp_new_connection(); die_on_error(sockfd = amqp_open_socket(hostname, port), "Opening socket"); amqp_set_sockfd(conn, sockfd); die_on_amqp_error(amqp_login(conn, "/", 0, 131072, 0, AMQP_SASL_METHOD_PLAIN, "guest", "guest"),"Logging in"); amqp_channel_open(conn, channelid); die_on_amqp_error(amqp_get_rpc_reply(conn), "Opening channel"); amqp_exchange_declare(conn,channelid,amqp_cstring_bytes(exchange),amqp_cstring_bytes(exchangetype),0,1, amqp_empty_table); die_on_amqp_error(amqp_get_rpc_reply(conn),"Declaring exchange"); { amqp_basic_properties_t props; props._flags = AMQP_BASIC_DELIVERY_MODE_FLAG; /*props.content_type = amqp_cstring_bytes("text/plain");*/ props.delivery_mode = 2; /* persistent delivery mode */ die_on_error(amqp_basic_publish(conn, channelid, amqp_cstring_bytes(exchange), amqp_cstring_bytes(routingkey), 0, 0, &props, amqp_cstring_bytes(messagebody)), "Publishing"); } die_on_amqp_error(amqp_channel_close(conn, 1, AMQP_REPLY_SUCCESS), "Closing channel"); die_on_amqp_error(amqp_connection_close(conn, AMQP_REPLY_SUCCESS), "Closing connection"); die_on_error(amqp_destroy_connection(conn), "Ending connection"); return 0; }
static gboolean afamqp_dd_connect(AMQPDestDriver *self, gboolean reconnect) { int sockfd; amqp_rpc_reply_t ret; if (reconnect && self->conn) { ret = amqp_get_rpc_reply(self->conn); if (ret.reply_type == AMQP_RESPONSE_NORMAL) return TRUE; } self->conn = amqp_new_connection(); sockfd = amqp_open_socket(self->host, self->port); if (sockfd < 0) { gchar *errstr = amqp_error_string(-sockfd); msg_error("Error connecting to AMQP server", evt_tag_str("driver", self->super.super.super.id), evt_tag_str("error", errstr), evt_tag_int("time_reopen", self->super.time_reopen), NULL); g_free(errstr); return FALSE; } amqp_set_sockfd(self->conn, sockfd); ret = amqp_login(self->conn, self->vhost, 0, 131072, 0, AMQP_SASL_METHOD_PLAIN, self->user, self->password); if (!afamqp_is_ok(self, "Error during AMQP login", ret)) return FALSE; amqp_channel_open(self->conn, 1); ret = amqp_get_rpc_reply(self->conn); if (!afamqp_is_ok(self, "Error during AMQP channel open", ret)) return FALSE; if (self->declare) { amqp_exchange_declare(self->conn, 1, amqp_cstring_bytes(self->exchange), amqp_cstring_bytes(self->exchange_type), 0, 0, amqp_empty_table); ret = amqp_get_rpc_reply(self->conn); if (!afamqp_is_ok(self, "Error during AMQP exchange declaration", ret)) return FALSE; } msg_debug ("Connecting to AMQP succeeded", evt_tag_str("driver", self->super.super.super.id), NULL); return TRUE; }
bool initAmqp(amqp_connection_state_t &conn, amqp_socket_t *socket, int &status, amqp_bytes_t &queuename) { conn = amqp_new_connection(); socket = amqp_tcp_socket_new(conn); if (!socket) { die("creating TCP socket"); return false; } status = amqp_socket_open(socket, hostname, port); if (status) { die("opening TCP socket"); return false; } if (!die_on_amqp_error(amqp_login(conn, "/", 0, 131072, 0, AMQP_SASL_METHOD_PLAIN, amqp_user, amqp_pass), "Logging in")) { return false; }; amqp_channel_open(conn, 1); if (!die_on_amqp_error(amqp_get_rpc_reply(conn), "Opening channel")) { return false; } // passive 0 durable 1 auto-delete 0 internal 0 exchange amqp_exchange_declare_ok_t_ *er = amqp_exchange_declare(conn, 1, amqp_cstring_bytes(exchange), amqp_cstring_bytes(exchange_type), 0, 1, 0, 0, amqp_empty_table); if (!die_on_amqp_error(amqp_get_rpc_reply(conn), "Declaring exchange")) { return false; } // passive 0 durable 1 exclusive 0 auto-delete 0 queue amqp_queue_declare_ok_t *r = amqp_queue_declare(conn, 1, amqp_cstring_bytes(bindingkey), 0, 1, 0, 0, amqp_empty_table); if (!die_on_amqp_error(amqp_get_rpc_reply(conn), "Declaring queue")) { return false; } queuename = amqp_bytes_malloc_dup(r->queue); if (queuename.bytes == NULL) { fprintf(stderr, "Out of memory while copying queue name"); return false; } amqp_queue_bind(conn, 1, queuename, amqp_cstring_bytes(exchange), amqp_cstring_bytes(bindingkey), amqp_empty_table); if (!die_on_amqp_error(amqp_get_rpc_reply(conn), "Binding queue")) { return false; } // no-local 0 no-ack 0 exclusive 0 consumer amqp_basic_consume(conn, 1, queuename, amqp_cstring_bytes(consumer_name), 1, 0, 0, amqp_empty_table); if (!die_on_amqp_error(amqp_get_rpc_reply(conn), "Consuming")) { return false; } return true; }
int main(int argc, char const *const *argv) { char const *hostname; int port, status; char const *exchange; char const *bindingkey; char const *queue; amqp_socket_t *socket = NULL; amqp_connection_state_t conn; if (argc < 6) { fprintf(stderr, "Usage: amqp_bind host port exchange bindingkey queue\n"); return 1; } hostname = argv[1]; port = atoi(argv[2]); exchange = argv[3]; bindingkey = argv[4]; queue = argv[5]; conn = amqp_new_connection(); socket = amqp_tcp_socket_new(); if (!socket) { die("creating TCP socket"); } status = amqp_socket_open(socket, hostname, port); if (status) { die("opening TCP socket"); } amqp_set_socket(conn, socket); die_on_amqp_error(amqp_login(conn, "/", 0, 131072, 0, AMQP_SASL_METHOD_PLAIN, "guest", "guest"), "Logging in"); amqp_channel_open(conn, 1); die_on_amqp_error(amqp_get_rpc_reply(conn), "Opening channel"); amqp_queue_bind(conn, 1, amqp_cstring_bytes(queue), amqp_cstring_bytes(exchange), amqp_cstring_bytes(bindingkey), amqp_empty_table); die_on_amqp_error(amqp_get_rpc_reply(conn), "Unbinding"); die_on_amqp_error(amqp_channel_close(conn, 1, AMQP_REPLY_SUCCESS), "Closing channel"); die_on_amqp_error(amqp_connection_close(conn, AMQP_REPLY_SUCCESS), "Closing connection"); die_on_error(amqp_destroy_connection(conn), "Ending connection"); return 0; }
void Channel::listen(Callable_envelope& callback){ amqp_connection_state_t conn = this->conn_ptr->_getconn_n(); //open channel //amqp_channel_open(conn, channel); //die_on_amqp_error(amqp_get_rpc_reply(conn), "Opening channel"); //set consumer amqp_basic_consume(conn, this->channel_n, amqp_cstring_bytes(this->queue_name.c_str()), amqp_empty_bytes, 0, 1, 0, amqp_empty_table);//auto ack consumer die_on_amqp_error(amqp_get_rpc_reply(this->conn_ptr->_getconn_n()), "rabbitmq_listen Consuming"); { while (1) { amqp_rpc_reply_t res; amqp_envelope_t envelope; amqp_maybe_release_buffers(conn); res = amqp_consume_message(conn, &envelope, NULL, 0); if (AMQP_RESPONSE_NORMAL != res.reply_type) { //this solution is not really work when network changed. label_listen_reconnet: Log::log.warning("Channel::listen, AMQP response abnormal\n"); Log::log.warning("Channel::listen, reconnecting...\n"); //first close previous amqp_channel_close(this->conn_ptr->_getconn_n(), this->channel_n, AMQP_REPLY_SUCCESS); amqp_connection_close(this->conn_ptr->_getconn_n(), AMQP_REPLY_SUCCESS); amqp_destroy_connection(this->conn_ptr->_getconn_n()); //reconnect to rabbitMQ server! this->conn_ptr->reconnnect(); amqp_channel_open(this->conn_ptr->_getconn_n(), channel_n); if(amqp_get_rpc_reply(this->conn_ptr->_getconn_n()).reply_type !=AMQP_RESPONSE_NORMAL){ Log::log.warning("Channel::publish:Opening channel_n error\n"); sleep(2); goto label_listen_reconnet; }else{ continue; } } callback.callback(envelope); amqp_destroy_envelope(&envelope); } } die_on_amqp_error(amqp_channel_close(conn, this->channel_n, AMQP_REPLY_SUCCESS), "Closing channel"); }
amqp_connection_state_t make_connection(void) { int s; struct amqp_connection_info ci; amqp_connection_state_t conn; init_connection_info(&ci); s = amqp_open_socket(ci.host, ci.port); die_amqp_error(s, "opening socket to %s:%d", ci.host, ci.port); conn = amqp_new_connection(); amqp_set_sockfd(conn, s); die_rpc(amqp_login(conn, ci.vhost, 0, 131072, 0, AMQP_SASL_METHOD_PLAIN, ci.user, ci.password), "logging in to AMQP server"); if (!amqp_channel_open(conn, 1)) { die_rpc(amqp_get_rpc_reply(conn), "opening channel"); } return conn; }
int amqp_link(struct amqp_state_t *amqp) { amqp_rpc_reply_t r; amqp->props._flags = AMQP_BASIC_CONTENT_TYPE_FLAG | AMQP_BASIC_DELIVERY_MODE_FLAG; amqp->props.delivery_mode = 2; amqp->props.content_type = amqp_cstring_bytes("application/octet-stream"); amqp->conn = amqp_new_connection(); cfg.amqp.fd = amqp_open_socket(cfg.amqp.host, cfg.amqp.port); if (cfg.amqp.fd < 0) { LOG("unable to open amqp socket!\n"); return(-1); } amqp_set_sockfd(amqp->conn, cfg.amqp.fd); r = amqp_login(amqp->conn, cfg.amqp.vhost, 0, 131072, 0, AMQP_SASL_METHOD_PLAIN, cfg.amqp.user, cfg.amqp.pass); if (r.reply_type != AMQP_RESPONSE_NORMAL) { LOG("problem logging in amqp broker\n"); return(-1); } amqp_channel_open(amqp->conn, 1); r = amqp_get_rpc_reply(amqp->conn); if (r.reply_type != AMQP_RESPONSE_NORMAL) { LOG("problem opening amqp channel\n"); return(-1); } return(0); }
/* * Initialize RabbitMQ connection */ static rsRetVal initRabbitMQ(instanceData *pData) { int sockfd; DEFiRet; DBGPRINTF("omrabbitmq: trying connect to '%s' at port %d\n", pData->host, pData->port); pData->conn = amqp_new_connection(); if (die_on_error(sockfd = amqp_open_socket((char*) pData->host, pData->port), "Opening socket")) { pData->conn = NULL; ABORT_FINALIZE(RS_RET_SUSPENDED); } amqp_set_sockfd(pData->conn, sockfd); if (die_on_amqp_error(amqp_login(pData->conn, (char*) pData->vhost, 0, 131072, 0, AMQP_SASL_METHOD_PLAIN, pData->user, pData->password), "Logging in")) { pData->conn = NULL; ABORT_FINALIZE(RS_RET_SUSPENDED); } amqp_channel_open(pData->conn, 1); if (die_on_amqp_error(amqp_get_rpc_reply(pData->conn), "Opening channel")) { pData->conn = NULL; ABORT_FINALIZE(RS_RET_SUSPENDED); } finalize_it: RETiRet; }
void AMQPBase::openChannel() { amqp_channel_open(*cnn, channelNum); amqp_rpc_reply_t res = amqp_get_rpc_reply(*cnn); THROW_AMQP_EXC_IF_FAILED(res, "open channel"); opened = 1; }
static int lc_bus_queue_init(amqp_connection_state_t *conn, uint32_t queue_id) { amqp_queue_declare_ok_t *res = NULL; amqp_rpc_reply_t ret; if (queue_id >= NUM_LC_BUS_QUEUE) { return LC_BUS_ERR; } res = amqp_queue_declare( *conn, LC_BUS_CHANNEL, amqp_cstring_bytes(lc_bus_queue[queue_id]), 0, /* passive */ 1, /* durable */ 0, /* exclusive */ 0, /* auto_delete */ amqp_empty_table /* arguments */ ); if (!res) { ret = amqp_get_rpc_reply(*conn); if (ret.reply_type != AMQP_RESPONSE_NORMAL) { LB_SYSLOG(LOG_ERR, "[%s] failed, channel=%u %s\n", lc_bus_queue[queue_id], LC_BUS_CHANNEL, amqp_rpc_reply_string(&ret)); return LC_BUS_ERR; } } LB_SYSLOG(LOG_INFO, "[%s] succeed channel=%u.\n", lc_bus_queue[queue_id], LC_BUS_CHANNEL); return LC_BUS_OK; }
Channel::Channel(RabbitMQConnection& conn){ this->channel_n = conn._getChannel_n(); this->conn_ptr=&conn; this->queue_name.clear(); amqp_channel_open(conn._getconn_n(), channel_n); die_on_amqp_error(amqp_get_rpc_reply(conn._getconn_n()), "Opening channel_n"); }
int main(int argc, char const * const *argv) { char const *hostname; int port; int rate_limit; int message_count; int sockfd; amqp_connection_state_t conn; if (argc < 5) { fprintf(stderr, "Usage: amqp_producer host port rate_limit message_count\n"); return 1; } hostname = argv[1]; port = atoi(argv[2]); rate_limit = atoi(argv[3]); message_count = atoi(argv[4]); conn = amqp_new_connection(); die_on_error(sockfd = amqp_open_socket(hostname, port), "Opening socket"); amqp_set_sockfd(conn, sockfd); die_on_amqp_error(amqp_login(conn, "/", 0, 131072, 0, AMQP_SASL_METHOD_PLAIN, "guest", "guest"), "Logging in"); amqp_channel_open(conn, 1); die_on_amqp_error(amqp_get_rpc_reply(conn), "Opening channel"); send_batch(conn, "test queue", rate_limit, message_count); die_on_amqp_error(amqp_channel_close(conn, 1, AMQP_REPLY_SUCCESS), "Closing channel"); die_on_amqp_error(amqp_connection_close(conn, AMQP_REPLY_SUCCESS), "Closing connection"); die_on_error(amqp_destroy_connection(conn), "Ending connection"); return 0; }
static int noit_rabbimq_connect(iep_thread_driver_t *dr) { struct amqp_driver *driver = (struct amqp_driver *)dr; if(!driver->connection) { int sidx = driver->nconnects++ % driver->nhosts; struct timeval timeout; amqp_rpc_reply_t r, *rptr; mtevL(mtev_error, "AMQP connect: %s:%d\n", driver->hostname[sidx], driver->port); BUMPSTAT(connects); driver->hostidx = sidx; timeout.tv_sec = driver->heartbeat; timeout.tv_usec = 0; driver->sockfd = amqp_open_socket(driver->hostname[sidx], driver->port, &timeout); if(driver->sockfd < 0) { mtevL(mtev_error, "AMQP connect failed: %s:%d\n", driver->hostname[sidx], driver->port); return -1; } if(setsockopt(driver->sockfd, SOL_SOCKET, SO_SNDBUF, &desired_sndbuf, sizeof(desired_sndbuf)) < 0) mtevL(mtev_debug, "rabbitmq: setsockopt(SO_SNDBUF, %ld) -> %s\n", (long int)desired_sndbuf, strerror(errno)); if(setsockopt(driver->sockfd, SOL_SOCKET, SO_RCVBUF, &desired_rcvbuf, sizeof(desired_rcvbuf)) < 0) mtevL(mtev_debug, "rabbitmq: setsockopt(SO_RCVBUF, %ld) -> %s\n", (long int)desired_rcvbuf, strerror(errno)); driver->has_error = 0; driver->connection = amqp_new_connection(); amqp_set_basic_return_cb(driver->connection, noit_rabbitmq_brcb, driver); amqp_set_sockfd(driver->connection, driver->sockfd); r = amqp_login(driver->connection, driver->vhost, 0, 131072, driver->heartbeat, AMQP_SASL_METHOD_PLAIN, driver->username, driver->password); if(r.reply_type != AMQP_RESPONSE_NORMAL) { mtevL(mtev_error, "AMQP login failed\n"); amqp_connection_close(driver->connection, AMQP_REPLY_SUCCESS); amqp_destroy_connection(driver->connection); if(driver->sockfd >= 0) close(driver->sockfd); driver->sockfd = -1; driver->connection = NULL; return -1; } amqp_channel_open(driver->connection, 1); rptr = amqp_get_rpc_reply(); if(rptr->reply_type != AMQP_RESPONSE_NORMAL) { mtevL(mtev_error, "AMQP channe_open failed\n"); amqp_connection_close(driver->connection, AMQP_REPLY_SUCCESS); amqp_destroy_connection(driver->connection); if(driver->sockfd >= 0) close(driver->sockfd); driver->sockfd = -1; driver->connection = NULL; return -1; } mtev_gettimeofday(&driver->last_hb, NULL); return 0; } /* 1 means already connected */ return 1; }
static char *camqp_strerror (camqp_config_t *conf, /* {{{ */ char *buffer, size_t buffer_size) { amqp_rpc_reply_t r; r = amqp_get_rpc_reply (conf->connection); switch (r.reply_type) { case AMQP_RESPONSE_NORMAL: sstrncpy (buffer, "Success", sizeof (buffer)); break; case AMQP_RESPONSE_NONE: sstrncpy (buffer, "Missing RPC reply type", sizeof (buffer)); break; case AMQP_RESPONSE_LIBRARY_EXCEPTION: #if HAVE_AMQP_RPC_REPLY_T_LIBRARY_ERRNO if (r.library_errno) return (sstrerror (r.library_errno, buffer, buffer_size)); #else if (r.library_error) return (sstrerror (r.library_error, buffer, buffer_size)); #endif else sstrncpy (buffer, "End of stream", sizeof (buffer)); break; case AMQP_RESPONSE_SERVER_EXCEPTION: if (r.reply.id == AMQP_CONNECTION_CLOSE_METHOD) { amqp_connection_close_t *m = r.reply.decoded; char *tmp = camqp_bytes_cstring (&m->reply_text); ssnprintf (buffer, buffer_size, "Server connection error %d: %s", m->reply_code, tmp); sfree (tmp); } else if (r.reply.id == AMQP_CHANNEL_CLOSE_METHOD) { amqp_channel_close_t *m = r.reply.decoded; char *tmp = camqp_bytes_cstring (&m->reply_text); ssnprintf (buffer, buffer_size, "Server channel error %d: %s", m->reply_code, tmp); sfree (tmp); } else { ssnprintf (buffer, buffer_size, "Server error method %#"PRIx32, r.reply.id); } break; default: ssnprintf (buffer, buffer_size, "Unknown reply type %i", (int) r.reply_type); } return (buffer); } /* }}} char *camqp_strerror */
int main(int argc, char const *const *argv) { char const *hostname; int port, status; int rate_limit; int message_count; amqp_socket_t *socket; amqp_connection_state_t conn; if (argc < 5) { fprintf(stderr, "Usage: amqps_producer host port rate_limit message_count " "[cacert.pem [key.pem cert.pem]]\n"); return 1; } hostname = argv[1]; port = atoi(argv[2]); rate_limit = atoi(argv[3]); message_count = atoi(argv[4]); conn = amqp_new_connection(); socket = amqp_ssl_socket_new(conn); if (!socket) { die("creating SSL/TLS socket"); } if (argc > 5) { status = amqp_ssl_socket_set_cacert(socket, argv[5]); if (status) { die("setting CA certificate"); } } if (argc > 7) { status = amqp_ssl_socket_set_key(socket, argv[7], argv[6]); if (status) { die("setting client cert"); } } status = amqp_socket_open(socket, hostname, port); if (status) { die("opening SSL/TLS connection"); } die_on_amqp_error(amqp_login(conn, "/", 0, 131072, 0, AMQP_SASL_METHOD_PLAIN, "guest", "guest"), "Logging in"); amqp_channel_open(conn, 1); die_on_amqp_error(amqp_get_rpc_reply(conn), "Opening channel"); send_batch(conn, "test queue", rate_limit, message_count); die_on_amqp_error(amqp_channel_close(conn, 1, AMQP_REPLY_SUCCESS), "Closing channel"); die_on_amqp_error(amqp_connection_close(conn, AMQP_REPLY_SUCCESS), "Closing connection"); die_on_error(amqp_destroy_connection(conn), "Ending connection"); return 0; }
void AMQPBase::closeChannel() { if (opened) { amqp_channel_close(*cnn, channelNum, AMQP_REPLY_SUCCESS); amqp_rpc_reply_t res = amqp_get_rpc_reply(*cnn); THROW_AMQP_EXC_IF_FAILED(res, "close channel"); opened = 0; } }
void Channel::unbinding(const std::string& routing_key){ if(this->queue_name.empty()) return; amqp_queue_unbind(this->conn_ptr->_getconn_n(), this->channel_n, amqp_cstring_bytes(this->queue_name.c_str()), amqp_cstring_bytes(this->conn_ptr->getDefaultExchange().c_str()), amqp_cstring_bytes(routing_key.c_str()), amqp_empty_table); die_on_amqp_error(amqp_get_rpc_reply(this->conn_ptr->_getconn_n()), "Unbinding"); }
static _Bool camqp_is_error (camqp_config_t *conf) /* {{{ */ { amqp_rpc_reply_t r; r = amqp_get_rpc_reply (conf->connection); if (r.reply_type == AMQP_RESPONSE_NORMAL) return (0); return (1); } /* }}} _Bool camqp_is_error */
static bool camqp_is_error(camqp_config_t *conf) /* {{{ */ { amqp_rpc_reply_t r; r = amqp_get_rpc_reply(conf->connection); if (r.reply_type == AMQP_RESPONSE_NORMAL) return false; return true; } /* }}} bool camqp_is_error */
amqp_channel_open_ok_t *amqp_channel_open(amqp_connection_state_t state, amqp_channel_t channel) { amqp_rpc_reply_t *amqp_rpc_reply; amqp_rpc_reply = amqp_get_rpc_reply(); *amqp_rpc_reply = AMQP_SIMPLE_RPC(state, channel, CHANNEL, OPEN, OPEN_OK, amqp_channel_open_t, AMQP_EMPTY_BYTES); return RPC_REPLY(amqp_channel_open_ok_t); }
void RabbitMQConnection::bindQueue (const std::string &queue_name, const std::string &exchange_name) { amqp_bytes_t queue = amqp_cstring_bytes (queue_name.c_str() ); amqp_bytes_t exchange = amqp_cstring_bytes (exchange_name.c_str() ); amqp_queue_bind (conn, 1, queue, exchange, amqp_empty_bytes, amqp_empty_table); exception_on_error (amqp_get_rpc_reply (conn), "Binding queue"); }
int create_queue_item(void *buf, size_t len) { printf("length: %zu\n", len); amqp_connection_state_t conn = amqp_new_connection(); amqp_socket_t *socket = amqp_tcp_socket_new(conn); if (!socket) { printf("Error creating TCP socket.\n"); return -1; } int status = amqp_socket_open(socket, "localhost", 5672); if (status) { printf("Error opening TCP socket.\n"); return -1; } // Open channel if (handle_amqp_error(amqp_login(conn, "/", 0, 131072, 0, AMQP_SASL_METHOD_PLAIN, "guest", "guest"), "Login")) { return -1; } amqp_channel_open(conn, 1); if (handle_amqp_error(amqp_get_rpc_reply(conn), "Get Reply")) { return -1; } // Send message { amqp_basic_properties_t props; props._flags = AMQP_BASIC_CONTENT_TYPE_FLAG | AMQP_BASIC_DELIVERY_MODE_FLAG; props.content_type = amqp_cstring_bytes("text/plain"); props.delivery_mode = 2; /* persistent delivery mode */ status = amqp_basic_publish(conn, 1, amqp_cstring_bytes(""), amqp_cstring_bytes("test-route"), 0, 0, &props, amqp_cstring_bytes(buf)); if (status < 0) { printf("Error publishing\n"); return -1; } printf("Published the thingy\n"); } // Close channel amqp_channel_close(conn, 1, AMQP_REPLY_SUCCESS); amqp_connection_close(conn, AMQP_REPLY_SUCCESS); amqp_destroy_connection(conn); return 0; }
void RabbitMQConnection::consumeQueue (const std::string &queue_name, const std::string &tag) { amqp_bytes_t queue = amqp_cstring_bytes (queue_name.c_str() ); amqp_bytes_t tag_id = amqp_cstring_bytes (tag.c_str() ); amqp_basic_consume (conn, 1, queue, tag_id, /* no_local */ false, /* no_ack */ false, /* exclusive */ false, amqp_empty_table); exception_on_error (amqp_get_rpc_reply (conn), "Consuming"); }
static int noit_rabbimq_connect(iep_thread_driver_t *dr) { struct amqp_driver *driver = (struct amqp_driver *)dr; if(!driver->connection) { int sidx = driver->nconnects++ % driver->nhosts; struct timeval timeout; amqp_rpc_reply_t r, *rptr; noitL(noit_error, "AMQP connect: %s:%d\n", driver->hostname[sidx], driver->port); BUMPSTAT(connects); driver->hostidx = sidx; timeout.tv_sec = driver->heartbeat; timeout.tv_usec = 0; driver->sockfd = amqp_open_socket(driver->hostname[sidx], driver->port, &timeout); if(driver->sockfd < 0) { noitL(noit_error, "AMQP connect failed: %s:%d\n", driver->hostname[sidx], driver->port); return -1; } driver->has_error = 0; driver->connection = amqp_new_connection(); amqp_set_basic_return_cb(driver->connection, noit_rabbitmq_brcb, driver); amqp_set_sockfd(driver->connection, driver->sockfd); r = amqp_login(driver->connection, driver->vhost, 0, 131072, driver->heartbeat, AMQP_SASL_METHOD_PLAIN, driver->username, driver->password); if(r.reply_type != AMQP_RESPONSE_NORMAL) { noitL(noit_error, "AMQP login failed\n"); amqp_connection_close(driver->connection, AMQP_REPLY_SUCCESS); amqp_destroy_connection(driver->connection); driver->connection = NULL; return -1; } amqp_channel_open(driver->connection, 1); rptr = amqp_get_rpc_reply(); if(rptr->reply_type != AMQP_RESPONSE_NORMAL) { noitL(noit_error, "AMQP channe_open failed\n"); amqp_connection_close(driver->connection, AMQP_REPLY_SUCCESS); amqp_destroy_connection(driver->connection); driver->connection = NULL; return -1; } gettimeofday(&driver->last_hb, NULL); return 0; } /* 1 means already connected */ return 1; }
static void amqp_local_phase2(XactEvent event, void *arg) { amqp_rpc_reply_t *reply; struct brokerstate *bs; switch(event) { case XACT_EVENT_COMMIT: for(bs = HEAD_BS; bs; bs = bs->next) { if(bs->inerror) local_amqp_disconnect_bs(bs); bs->inerror = 0; if(!bs->uncommitted) continue; if(bs->conn) amqp_tx_commit(bs->conn, 2, AMQP_EMPTY_TABLE); reply = amqp_get_rpc_reply(); if(reply->reply_type != AMQP_RESPONSE_NORMAL) { elog(WARNING, "amqp could not commit tx mode on broker %d, reply_type=%d, library_errno=%d", bs->broker_id, reply->reply_type, reply->library_errno); local_amqp_disconnect_bs(bs); } bs->uncommitted = 0; } break; case XACT_EVENT_ABORT: for(bs = HEAD_BS; bs; bs = bs->next) { if(bs->inerror) local_amqp_disconnect_bs(bs); bs->inerror = 0; if(!bs->uncommitted) continue; if(bs->conn) amqp_tx_rollback(bs->conn, 2, AMQP_EMPTY_TABLE); reply = amqp_get_rpc_reply(); if(reply->reply_type != AMQP_RESPONSE_NORMAL) { elog(WARNING, "amqp could not rollback tx mode on broker %d, reply_type=%d, library_errno=%d", bs->broker_id, reply->reply_type, reply->library_errno); local_amqp_disconnect_bs(bs); } bs->uncommitted = 0; } break; case XACT_EVENT_PREPARE: /* nothin' */ return; break; } }