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"); }
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); }
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; }
void init() { conn = amqp_new_connection(); amqp_socket_t* pSocket = amqp_tcp_socket_new(conn); if (pSocket == NULL) { ABORT("amqp create socket failed."); } printf("socket create succ.\n"); int state = amqp_socket_open(pSocket, hostip.c_str(), port); if (state < 0) { ABORT("amqp open socket failed."); } printf("socket open succ.\n"); amqp_rpc_reply_t reply = amqp_login(conn, "/", 0, 131072, 0, AMQP_SASL_METHOD_PLAIN, username.c_str(), password.c_str()); if (reply.reply_type != AMQP_RESPONSE_NORMAL) { ABORT("amqp login failed."); } printf("amqp login succ.\n"); // 初始化 amqp_channel_open(conn, channel_id); check_amqp_reply("amqp open channel failed.", "amqp channel channel success."); }
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; }
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; }
/* * 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; }
/* * 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; }
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; }
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; }
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; }
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; }
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; }
/* * Open a channel and wait for 5 seconds to receive a channel open OK event */ static wiced_result_t amqp_ch_open( wiced_amqp_connection_t *conn, uint16_t channel ) { if ( amqp_channel_open( channel, conn ) != WICED_SUCCESS ) { return WICED_ERROR; } if ( amqp_wait_for( AMQP_EVENT_CHANNEL_RECV_OPEN_OK, 5000 ) != WICED_SUCCESS ) { return WICED_ERROR; } return WICED_SUCCESS; }
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; }
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; }
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; }
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"); }
void Channel::publish(const std::string& exchange,const std::string& routing_key, const std::string& mesg ){ 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 = 1; /* transient delivery mode */ int x = amqp_basic_publish(this->conn_ptr->_getconn_n(), this->channel_n, amqp_cstring_bytes(exchange.c_str()), amqp_cstring_bytes(routing_key.c_str()), 0, 0, &props, amqp_cstring_bytes(mesg.c_str())); if(x<0){ label_reconnet: //meaning error occur Log::log.warning("%s: %s\n", "publishing", amqp_error_string2(x)); Log::log.warning("Channel::publish: Reconnecting to RabbitMQ\n"); //first close all the things 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"); }else{ x = amqp_basic_publish(this->conn_ptr->_getconn_n(), this->channel_n, amqp_cstring_bytes(exchange.c_str()), amqp_cstring_bytes(routing_key.c_str()), 0, 0, &props, amqp_cstring_bytes(mesg.c_str())); } if(x<0){ sleep(2); goto label_reconnet; } } //die_on_error(x, "Publishing"); }
/** * php_amqp_connect * handles connecting to amqp * called by connect() and reconnect() */ void php_amqp_connect(amqp_connection_object *amqp_connection) { char str[256]; char ** pstr = (char **) &str; void * old_handler; /* create the connection */ amqp_connection->conn = amqp_new_connection(); amqp_connection->fd = amqp_open_socket(amqp_connection->host, amqp_connection->port); if (amqp_connection->fd < 1) { /* Start ignoring SIGPIPE */ old_handler = signal(SIGPIPE, SIG_IGN); amqp_destroy_connection(amqp_connection->conn); /* End ignoring of SIGPIPEs */ signal(SIGPIPE, old_handler); zend_throw_exception(amqp_connection_exception_class_entry, "Socket error: could not connect to host.", 0 TSRMLS_CC); return; } amqp_connection->is_connected = '\1'; amqp_set_sockfd(amqp_connection->conn, amqp_connection->fd); amqp_rpc_reply_t x = amqp_login(amqp_connection->conn, amqp_connection->vhost, 0, FRAME_MAX, AMQP_HEARTBEAT, AMQP_SASL_METHOD_PLAIN, amqp_connection->login, amqp_connection->password); if (x.reply_type != AMQP_RESPONSE_NORMAL) { amqp_error(x, pstr); zend_throw_exception(amqp_connection_exception_class_entry, *pstr, 0 TSRMLS_CC); return; } amqp_channel_open(amqp_connection->conn, AMQP_CHANNEL); x = amqp_get_rpc_reply(amqp_connection->conn); if (x.reply_type != AMQP_RESPONSE_NORMAL) { amqp_error(x, pstr); zend_throw_exception(amqp_connection_exception_class_entry, *pstr, 0 TSRMLS_CC); return; } amqp_connection->is_channel_connected = '\1'; }
static amqp_connection_state_t get_amqp_connection(void) { int status; amqp_connection_state_t conn; amqp_rpc_reply_t reply; amqp_socket_t *socket = NULL; conn = amqp_new_connection(); if (!conn) { fprintf(stderr, "[!] Error getting ampq connection\n"); return NULL; } socket = amqp_tcp_socket_new(conn); if (!socket) { amqp_destroy_connection(conn); fprintf(stderr, "[!] Error creating the TCP socket!\n"); return NULL; } status = amqp_socket_open(socket, "localhost", 5672); if (status) { amqp_connection_close(conn, AMQP_REPLY_SUCCESS); amqp_destroy_connection(conn); fprintf(stderr, "[!] Error opening the TCP socket!\n"); return NULL; } reply = amqp_login(conn, "/", 0, 131072, 0, AMQP_SASL_METHOD_PLAIN, "guest", "guest"); if (reply.reply_type != AMQP_RESPONSE_NORMAL) { amqp_connection_close(conn, AMQP_REPLY_SUCCESS); amqp_destroy_connection(conn); fprintf(stderr, "[!] Error loggging into server\n"); return NULL; } if (!amqp_channel_open(conn, 1)) { amqp_channel_close(conn, 1, AMQP_REPLY_SUCCESS); amqp_connection_close(conn, AMQP_REPLY_SUCCESS); fprintf(stderr, "[!] Error opening channel 1\n"); return NULL; } return conn; }
amqp_connection_state_t make_connection(void) { int status; amqp_socket_t *socket = NULL; struct amqp_uri *uri = alloc_amqp_uri(); amqp_connection_state_t conn; init_connection_info(uri); conn = amqp_new_connection(); if (uri->ssl) { #ifdef WITH_SSL socket = amqp_ssl_socket_new(conn); if (!socket) { die("creating SSL/TLS socket"); } if (amqp_cacert) { amqp_ssl_socket_set_cacert(socket, amqp_cacert); } if (amqp_key) { amqp_ssl_socket_set_key(socket, amqp_cert, amqp_key); } #else die("librabbitmq was not built with SSL/TLS support"); #endif } else { socket = amqp_tcp_socket_new(conn); if (!socket) { die("creating TCP socket (out of memory)"); } } host_port_t * hp = ((host_port_t *)uri->host_port_array.elts); status = amqp_socket_open(socket, hp->host, hp->port); if (status) { die("opening socket to %s:%d", hp->host, hp->port); } die_rpc(amqp_login(conn, uri->vhost, 0, 131072, amqp_heartbeat, AMQP_SASL_METHOD_PLAIN, uri->user, uri->password), "logging in to AMQP server"); if (!amqp_channel_open(conn, 1)) { die_rpc(amqp_get_rpc_reply(conn), "opening channel"); } free_amqp_uri(uri); return conn; }
static int rmq_reconnect(evi_reply_sock *sock) { rmq_params_t * rmqp = (rmq_params_t *)sock->params; if (!rmqp || !(rmqp->flags & RMQ_PARAM_EXCH)) { LM_ERR("not enough socket info\n"); return -1; } // rmq_print(sock); if (!(rmqp->flags & RMQ_PARAM_CONN) || !rmqp->conn) { /* init new connection */ if (!(rmqp->conn = amqp_new_connection())) { LM_ERR("cannot create new connection\n"); return -1; } rmqp->flags |= RMQ_PARAM_CONN; rmqp->sock = amqp_open_socket(sock->address.s, sock->port); if (rmqp->sock < 0) { LM_ERR("cannot opens socket\n"); goto destroy_rmqp; } amqp_set_sockfd(rmqp->conn, rmqp->sock); if (rmq_error("Logging in", amqp_login(rmqp->conn, RMQ_DEFAULT_VHOST, 0, RMQ_DEFAULT_MAX, 0, AMQP_SASL_METHOD_PLAIN, rmqp->flags & RMQ_PARAM_USER ? rmqp->user.s : RMQ_DEFAULT_UP, rmqp->flags & RMQ_PARAM_PASS ? rmqp->pass.s : RMQ_DEFAULT_UP))) goto destroy_rmqp; } if (!(rmqp->flags & RMQ_PARAM_CHAN)) { rmqp->channel = 1; amqp_channel_open(rmqp->conn, rmqp->channel); rmqp->flags |= RMQ_PARAM_CHAN; if (rmq_error("Opening channel", amqp_get_rpc_reply(rmqp->conn))) goto destroy_rmqp; } return 0; destroy_rmqp: rmq_destroy_param(rmqp); return -1; }
amqp_connection_state_t make_connection(void) { int status; amqp_socket_t *socket = NULL; struct amqp_connection_info ci; amqp_connection_state_t conn; init_connection_info(&ci); conn = amqp_new_connection(); if (ci.ssl) { #ifdef WITH_SSL socket = amqp_ssl_socket_new(); if (!socket) { die("creating SSL/TLS socket"); } if (amqp_cacert) { amqp_ssl_socket_set_cacert(socket, amqp_cacert); } if (amqp_key) { amqp_ssl_socket_set_key(socket, amqp_cert, amqp_key); } #else die("librabbitmq was not built with SSL/TLS support"); #endif } else { socket = amqp_tcp_socket_new(); if (!socket) { die("creating TCP socket (out of memory)"); } } status = amqp_socket_open(socket, ci.host, ci.port); if (status) { die("opening socket to %s:%d", ci.host, ci.port); } amqp_set_socket(conn, socket); 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; }
amqp_connection_state_t setup_connection_and_channel(void) { amqp_connection_state_t connection_state_ = amqp_new_connection(); amqp_socket_t *socket = amqp_tcp_socket_new(connection_state_); assert(socket); int rc = amqp_socket_open(socket, "localhost", AMQP_PROTOCOL_PORT); assert(rc == AMQP_STATUS_OK); amqp_rpc_reply_t rpc_reply = amqp_login( connection_state_, "/", 1, AMQP_DEFAULT_FRAME_SIZE, AMQP_DEFAULT_HEARTBEAT, AMQP_SASL_METHOD_PLAIN, "guest", "guest"); assert(rpc_reply.reply_type == AMQP_RESPONSE_NORMAL); amqp_channel_open_ok_t *res = amqp_channel_open(connection_state_, fixed_channel_id); assert(res != NULL); return connection_state_; }
static amqp_connection_state_t *lc_bus_get_connection_atomic() { pthread_t t = pthread_self(); amqp_rpc_reply_t ret; int res; int i; for (i = 0; i < LC_BUS_MAX_CONNECTION; ++i) { if (!connection_tid[i] || pthread_equal(connection_tid[i], t)) { break; } } if (i < LC_BUS_MAX_CHANNEL) { if (!connection_tid[i]) { res = init_amqp_connection(lc_conn + i); if (res == LC_BUS_OK) { LB_SYSLOG(LOG_INFO, "init_amqp_connection ret=%d\n", res); } else { LB_SYSLOG(LOG_ERR, "init_amqp_connection ret=%d\n", res); return NULL; } if (!amqp_channel_open(lc_conn[i], LC_BUS_CHANNEL)) { ret = amqp_get_rpc_reply(lc_conn[i]); if (ret.reply_type != AMQP_RESPONSE_NORMAL) { LB_SYSLOG(LOG_ERR, "error in opening channel %u, %s\n", LC_BUS_CHANNEL, amqp_rpc_reply_string(&ret)); return NULL; } } connection_tid[i] = t; } } else { LB_SYSLOG(LOG_ERR, "connection is not enough, maximal=%u.\n", LC_BUS_MAX_CONNECTION); return NULL; } return lc_conn + i; }
static amqp_connection_state_t setup_amqp_connection() { amqp_connection_state_t connection = amqp_new_connection(); amqp_socket_t *socket = amqp_tcp_socket_new(connection); #ifdef FIX_SIG_PIPE // why doesn't librabbitmq do this, eh? int sockfd = amqp_get_sockfd(connection); int one = 1; // setsockopt(sockfd, SOL_SOCKET, SO_NOSIGPIPE, &one, sizeof(one)); #endif int status = amqp_socket_open(socket, rabbit_host, rabbit_port); assert_x(!status, "Opening RabbitMQ socket", __FILE__, __LINE__); die_on_amqp_error(amqp_login(connection, "/", 0, OUR_FRAME_MAX, 0, AMQP_SASL_METHOD_PLAIN, "guest", "guest"), "Logging in to RabbitMQ"); amqp_channel_open(connection, 1); die_on_amqp_error(amqp_get_rpc_reply(connection), "Opening AMQP channel"); return connection; }
static int send(struct stats_buffer *sf) { int status; char const *exchange; char const *routingkey; amqp_socket_t *socket = NULL; amqp_connection_state_t conn; exchange = "amq.direct"; routingkey = "tacc_stats"; conn = amqp_new_connection(); socket = amqp_tcp_socket_new(conn); status = amqp_socket_open(socket, sf->sf_host, atoi(sf->sf_port)); amqp_login(conn, "/", 0, 131072, 0, AMQP_SASL_METHOD_PLAIN, "guest", "guest"); amqp_channel_open(conn, 1); amqp_get_rpc_reply(conn); { 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 */ amqp_basic_publish(conn, 1, amqp_cstring_bytes(exchange), amqp_cstring_bytes(routingkey), 0, 0, &props, amqp_cstring_bytes(sf->sf_data)); } amqp_channel_close(conn, 1, AMQP_REPLY_SUCCESS); amqp_connection_close(conn, AMQP_REPLY_SUCCESS); amqp_destroy_connection(conn); return 0; }