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 int noit_rabbimq_submit(iep_thread_driver_t *dr, const char *payload, size_t payloadlen) { int rv; amqp_bytes_t body; struct amqp_driver *driver = (struct amqp_driver *)dr; const char *routingkey = driver->routingkey; body.len = payloadlen; body.bytes = (char *)payload; if(*payload == 'M' || *payload == 'S' || *payload == 'C' || (*payload == 'H' && payload[1] == '1') || (*payload == 'F' && payload[1] == '1') || (*payload == 'B' && (payload[1] == '1' || payload[1] == '2'))) { char uuid_str[32 * 2 + 1]; int account_id, check_id; if(extract_uuid_from_jlog(payload, payloadlen, &account_id, &check_id, uuid_str)) { if(*routingkey) { char *replace; int newlen = strlen(driver->routingkey) + 1 + sizeof(uuid_str) + 2 * 32; replace = alloca(newlen); snprintf(replace, newlen, "%s.%x.%x.%d.%d%s", driver->routingkey, account_id%16, (account_id/16)%16, account_id, check_id, uuid_str); routingkey = replace; } } } rv = amqp_basic_publish(driver->connection, 1, amqp_cstring_bytes(driver->exchange), amqp_cstring_bytes(routingkey), 1, 0, NULL, body); if(rv < 0) { mtevL(mtev_error, "AMQP publish failed, disconnecting\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; } BUMPSTAT(publications); noit_rabbitmq_heartbeat(driver); noit_rabbitmq_read_frame(driver); amqp_maybe_release_buffers(driver->connection); if(driver->has_error) { 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; } 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; }
/* php_amqp_disconnect handles disconnecting from amqp called by disconnect(), reconnect(), and d_tor */ void php_amqp_disconnect(amqp_connection_object *amqp_connection) { void * old_handler; /* If we are trying to close the connection and the connection already closed, it will throw SIGPIPE, which is fine, so ignore all SIGPIPES */ /* Start ignoring SIGPIPE */ old_handler = signal(SIGPIPE, SIG_IGN); if (amqp_connection->is_channel_connected == '\1') { amqp_channel_close(amqp_connection->conn, AMQP_CHANNEL, AMQP_REPLY_SUCCESS); } amqp_connection->is_channel_connected = '\0'; if (amqp_connection->conn && amqp_connection->is_connected == '\1') { amqp_connection_close(amqp_connection->conn, AMQP_REPLY_SUCCESS); amqp_destroy_connection(amqp_connection->conn); } amqp_connection->is_connected = '\0'; if (amqp_connection->fd) { close(amqp_connection->fd); } /* End ignoring of SIGPIPEs */ signal(SIGPIPE, old_handler); return; }
void destroy() { amqp_channel_close(conn, channel_id, AMQP_REPLY_SUCCESS); amqp_connection_close(conn, AMQP_REPLY_SUCCESS); amqp_destroy_connection(conn); printf("amqp close and destroy connection.\n"); }
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; }
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 rpc_close(struct status *status) { int ret = 0; DEBUG(2, ("[*] Closing RPC\n")); /* Adquire lock */ ret = pthread_spin_lock(&status->lock); if (ret) { fprintf(stderr, "[!] pthread_spin_lock: %s\n", strerror(ret)); return; } if (status->conn) { if (amqp_channel_close(status->conn, 1, AMQP_REPLY_SUCCESS).reply_type != AMQP_RESPONSE_NORMAL) { fprintf(stderr, "[!] Error closing AMQP channel\n"); } if (amqp_connection_close(status->conn, AMQP_REPLY_SUCCESS).reply_type != AMQP_RESPONSE_NORMAL) { fprintf(stderr, "[!] Error closing AMQP connection\n"); } if (amqp_destroy_connection(status->conn) < 0) { fprintf(stderr, "[!] Error destroying AMQP connection\n"); } } status->conn = NULL; /* Release lock */ pthread_spin_unlock(&status->lock); }
void close_and_destroy_connection(amqp_connection_state_t connection_state_) { amqp_rpc_reply_t rpc_reply = amqp_connection_close(connection_state_, AMQP_REPLY_SUCCESS); assert(rpc_reply.reply_type == AMQP_RESPONSE_NORMAL); int rc = amqp_destroy_connection(connection_state_); assert(rc == AMQP_STATUS_OK); }
static void _amqp_connection_disconnect(AMQPDestDriver* self) { amqp_channel_close(self->conn, 1, AMQP_REPLY_SUCCESS); amqp_connection_close(self->conn, AMQP_REPLY_SUCCESS); _amqp_connection_deinit(self); }
static int exit_amqp_connection_atomic() { int res = LC_BUS_OK; amqp_rpc_reply_t ret; int i; for (i = 0; i < LC_BUS_MAX_CONNECTION; ++i) { if (connection_tid[i]) { ret = amqp_channel_close(lc_conn[i], LC_BUS_CHANNEL, AMQP_REPLY_SUCCESS); if (ret.reply_type != AMQP_RESPONSE_NORMAL) { LB_SYSLOG(LOG_ERR, "error in closing channel: %s\n", amqp_rpc_reply_string(&ret)); } ret = amqp_connection_close(lc_conn[i], AMQP_REPLY_SUCCESS); if (ret.reply_type != AMQP_RESPONSE_NORMAL) { LB_SYSLOG(LOG_ERR, "error in closing connection, %s\n", amqp_rpc_reply_string(&ret)); } res = amqp_destroy_connection(lc_conn[i]); if (res) { LB_SYSLOG(LOG_ERR, "error in destroy connection ret=%d\n", res); } connection_tid[i] = 0; } } return LC_BUS_OK; }
RabbitMQConnection::~RabbitMQConnection() { int fd; if (conn == NULL) { GST_DEBUG ("service already stopped"); return; } fd = amqp_socket_get_sockfd (socket); /* Errors are ignored during close */ if (!closeOnRelease) { /* close socket */ close (fd); } amqp_channel_close (conn, 1, AMQP_REPLY_SUCCESS); amqp_connection_close (conn, AMQP_REPLY_SUCCESS); amqp_destroy_connection (conn); if (closeOnRelease) { /* inform remote side that we are done */ shutdown (fd, SHUT_WR); } conn = NULL; }
static void afamqp_dd_disconnect(AMQPDestDriver *self) { amqp_channel_close(self->conn, 1, AMQP_REPLY_SUCCESS); amqp_connection_close(self->conn, AMQP_REPLY_SUCCESS); amqp_destroy_connection(self->conn); self->conn = NULL; }
/** * Close a session with the filter, this is the mechanism * by which a filter may cleanup data structure etc. * In the case of the MQ filter we simply close the connection to the server. * * @param instance The filter instance data * @param session The session being closed */ static void closeSession(FILTER *instance, void *session) { MQ_SESSION *my_session = (MQ_SESSION *)session; amqp_channel_close(my_session->conn,my_session->channel,AMQP_REPLY_SUCCESS); amqp_connection_close(my_session->conn,AMQP_REPLY_SUCCESS); amqp_destroy_connection(my_session->conn); }
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; }
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, 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 void local_amqp_disconnect_bs(struct brokerstate *bs) { if(bs && bs->conn) { int errorstate = bs->inerror; amqp_connection_close(bs->conn, AMQP_REPLY_SUCCESS); if(bs->sockfd >= 0) close(bs->sockfd); amqp_destroy_connection(bs->conn); memset(bs, 0, sizeof(*bs)); bs->inerror = errorstate; } }
static void shutdown_amqp_connection(amqp_connection_state_t connection, int amqp_rc, uint num_channels) { // close amqp connection, including all open channels if (amqp_rc >= 0) { for (int i=1; i <= num_channels; i++) log_amqp_error(amqp_channel_close(connection, i, AMQP_REPLY_SUCCESS), "closing AMQP channel"); log_amqp_error(amqp_connection_close(connection, AMQP_REPLY_SUCCESS), "closing AMQP connection"); } log_error(amqp_destroy_connection(connection), "closing AMQP connection"); }
static void closeAMQPConnection(instanceData *pData) { if (pData->conn != NULL) { die_on_amqp_error(amqp_channel_close(pData->conn, 1, AMQP_REPLY_SUCCESS), "amqp_channel_close"); die_on_amqp_error(amqp_connection_close(pData->conn, AMQP_REPLY_SUCCESS), "amqp_connection_close"); die_on_error(amqp_destroy_connection(pData->conn), "amqp_destroy_connection"); pData->conn = NULL; } }
void close_connection(amqp_connection_state_t conn) { int res; die_rpc(amqp_channel_close(conn, 1, AMQP_REPLY_SUCCESS), "closing channel"); die_rpc(amqp_connection_close(conn, AMQP_REPLY_SUCCESS), "closing connection"); res = amqp_destroy_connection(conn); die_amqp_error(res, "closing connection"); }
/* * Close a connection and wait for 5 seconds to receive a connection close OK event */ static wiced_result_t amqp_conn_close( wiced_amqp_connection_t *conn ) { if ( amqp_connection_close( conn ) != WICED_SUCCESS ) { return WICED_ERROR; } if ( amqp_wait_for( AMQP_EVENT_CONNECTION_RECV_CLOSE_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; }
AMQP::~AMQP() { if (channels.size()) { vector<AMQPBase*>::iterator i; for (i=channels.begin(); i!=channels.end(); i++) { delete *i; } } amqp_channel_close(cnn, 1, AMQP_REPLY_SUCCESS); amqp_connection_close(cnn, AMQP_REPLY_SUCCESS); amqp_destroy_connection(cnn); };
int main(int argc, char const *const *argv) { char const *hostname; int port; amqp_socket_t *socket; amqp_connection_state_t conn; struct timeval tval; struct timeval *tv; if (argc < 3) { fprintf(stderr, "Usage: amqp_connect_timeout host port [timeout_sec [timeout_usec=0]]\n"); return 1; } if (argc > 3) { tv = &tval; tv->tv_sec = atoi(argv[3]); if (argc > 4 ) { tv->tv_usec = atoi(argv[4]); } else { tv->tv_usec = 0; } } else { tv = NULL; } hostname = argv[1]; port = atoi(argv[2]); conn = amqp_new_connection(); socket = amqp_tcp_socket_new(conn); if (!socket) { die("creating TCP socket"); } die_on_error(amqp_socket_open_noblock(socket, hostname, port, tv), "opening TCP socket"); die_on_amqp_error(amqp_login(conn, "/", 0, 131072, 0, AMQP_SASL_METHOD_PLAIN, "guest", "guest"), "Logging in"); die_on_amqp_error(amqp_connection_close(conn, AMQP_REPLY_SUCCESS), "Closing connection"); die_on_error(amqp_destroy_connection(conn), "Ending connection"); printf ("Done\n"); return 0; }
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; }
/* * Functions */ static void camqp_close_connection (camqp_config_t *conf) /* {{{ */ { int sockfd; if ((conf == NULL) || (conf->connection == NULL)) return; sockfd = amqp_get_sockfd (conf->connection); amqp_channel_close (conf->connection, CAMQP_CHANNEL, AMQP_REPLY_SUCCESS); amqp_connection_close (conf->connection, AMQP_REPLY_SUCCESS); amqp_destroy_connection (conf->connection); close (sockfd); conf->connection = NULL; } /* }}} void camqp_close_connection */
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"); }