int main(int argc, char **argv) { natsConnection *conn = NULL; natsOptions *opts = NULL; natsSubscription *sub = NULL; natsStatistics *stats = NULL; natsMsg *msg = NULL; natsStatus s; opts = parseArgs(argc, argv, usage); printf("Listening %ssynchronously on '%s' with name '%s'.\n", (async ? "a" : ""), subj, name); s = natsOptions_SetErrorHandler(opts, asyncCb, NULL); if (s == NATS_OK) s = natsConnection_Connect(&conn, opts); if (s == NATS_OK) { if (async) s = natsConnection_QueueSubscribe(&sub, conn, subj, name, onMsg, NULL); else s = natsConnection_QueueSubscribeSync(&sub, conn, subj, name); } // For maximum performance, set no limit on the number of pending messages. if (s == NATS_OK) s = natsSubscription_SetPendingLimits(sub, -1, -1); if (s == NATS_OK) s = natsSubscription_AutoUnsubscribe(sub, (int) total); if (s == NATS_OK) s = natsStatistics_Create(&stats); if ((s == NATS_OK) && async) { while (s == NATS_OK) { s = printStats(STATS_IN|STATS_COUNT,conn, sub, stats); if (count == total) break; if (s == NATS_OK) nats_Sleep(1000); } } else if (s == NATS_OK) { int64_t last = 0; for (count = 0; (s == NATS_OK) && (count < total); count++) { s = natsSubscription_NextMsg(&msg, sub, 10000); if (s != NATS_OK) break; if (start == 0) start = nats_Now(); if (nats_Now() - last >= 1000) { s = printStats(STATS_IN|STATS_COUNT,conn, sub, stats); last = nats_Now(); } natsMsg_Destroy(msg); } } if (s == NATS_OK) { printPerf("Received", total, start, elapsed); } else { printf("Error: %d - %s\n", s, natsStatus_GetText(s)); nats_PrintLastErrorStack(stderr); } // Destroy all our objects to avoid report of memory leak natsStatistics_Destroy(stats); natsSubscription_Destroy(sub); natsConnection_Destroy(conn); natsOptions_Destroy(opts); // To silence reports of memory still in used with valgrind nats_Close(); return 0; }
int main(int argc, char **argv) { natsConnection *conn = NULL; natsStatistics *stats = NULL; const char *subj = NULL; const char *txt = NULL; int64_t total = 0; int64_t count = 0; int64_t start = 0; int64_t last = 0; int64_t elapsed= 0; natsStatus s; if (argc != 4) { printf("Usage: %s <subject> <msg content> <count>\n", argv[0]); exit(1); } subj = argv[1]; txt = argv[2]; total = atol(argv[3]); printf("Sending %" PRId64 " messages to subject '%s'\n", total, subj); s = natsConnection_ConnectTo(&conn, NATS_DEFAULT_URL); if (s == NATS_OK) s = natsStatistics_Create(&stats); if (s == NATS_OK) start = nats_Now(); for (count = 0; (s == NATS_OK) && (count < total); count++) { s = natsConnection_PublishString(conn, subj, txt); if (nats_Now() - last >= 1000) { s = printStats(STATS_OUT, conn, NULL, stats, 0, 0); last = nats_Now(); } } if (s == NATS_OK) s = natsConnection_FlushTimeout(conn, 1000); if (s == NATS_OK) { printPerf("Sent", total, start, elapsed); } else { printf("Error: %d - %s\n", s, natsStatus_GetText(s)); } // Destroy all our objects to avoid report of memory leak natsStatistics_Destroy(stats); natsConnection_Destroy(conn); // To silence reports of memory still in used with valgrind nats_Close(); return 0; }