|
| 1 | +/** |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +#include <pulsar/c/client.h> |
| 21 | +#include <stdio.h> |
| 22 | +#include <string.h> |
| 23 | +#include <time.h> |
| 24 | + |
| 25 | +void format_time(char *output){ |
| 26 | + time_t rawtime; |
| 27 | + struct tm * timeinfo; |
| 28 | + |
| 29 | + time(&rawtime); |
| 30 | + timeinfo = localtime(&rawtime); |
| 31 | + |
| 32 | + sprintf(output, "%d %d %d %d:%d:%d", |
| 33 | + timeinfo->tm_year + 1900, timeinfo->tm_mon + 1, timeinfo->tm_mday, |
| 34 | + timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec); |
| 35 | +} |
| 36 | + |
| 37 | +void custom_logger(pulsar_logger_level_t level, const char *file, int line, const char *message, |
| 38 | + void *ctx) { |
| 39 | + time_t mytime = time(NULL); |
| 40 | + char * time_str = ctime(&mytime); |
| 41 | + // Control the log level yourself. |
| 42 | + if (level >= pulsar_DEBUG) { |
| 43 | + format_time(time_str); |
| 44 | + printf("[%s] [%u] [%s] [%d] [%s] \n", time_str, level, file, line, message); |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +int main() { |
| 49 | + pulsar_client_configuration_t *conf = pulsar_client_configuration_create(); |
| 50 | + |
| 51 | + pulsar_client_configuration_set_logger(conf, custom_logger, NULL); |
| 52 | + pulsar_client_configuration_set_memory_limit(conf, 64 * 1024 * 1024); |
| 53 | + pulsar_client_t *client = pulsar_client_create("pulsar://localhost:6650", conf); |
| 54 | + |
| 55 | + pulsar_producer_configuration_t* producer_conf = pulsar_producer_configuration_create(); |
| 56 | + pulsar_producer_configuration_set_batching_enabled(producer_conf, 1); |
| 57 | + pulsar_producer_t *producer; |
| 58 | + |
| 59 | + pulsar_result err = pulsar_client_create_producer(client, "my-topic", producer_conf, &producer); |
| 60 | + if (err != pulsar_result_Ok) { |
| 61 | + printf("Failed to create producer: %s\n", pulsar_result_str(err)); |
| 62 | + return 1; |
| 63 | + } |
| 64 | + |
| 65 | + for (int i = 0; i < 10; i++) { |
| 66 | + const char* data = "my-content"; |
| 67 | + pulsar_message_t* message = pulsar_message_create(); |
| 68 | + pulsar_message_set_content(message, data, strlen(data)); |
| 69 | + |
| 70 | + err = pulsar_producer_send(producer, message); |
| 71 | + if (err == pulsar_result_Ok) { |
| 72 | + printf("Sent message %d\n", i); |
| 73 | + } else { |
| 74 | + printf("Failed to publish message: %s\n", pulsar_result_str(err)); |
| 75 | + return 1; |
| 76 | + } |
| 77 | + |
| 78 | + pulsar_message_free(message); |
| 79 | + } |
| 80 | + |
| 81 | + // Cleanup |
| 82 | + pulsar_producer_close(producer); |
| 83 | + pulsar_producer_free(producer); |
| 84 | + pulsar_producer_configuration_free(producer_conf); |
| 85 | + |
| 86 | + pulsar_client_close(client); |
| 87 | + pulsar_client_free(client); |
| 88 | + pulsar_client_configuration_free(conf); |
| 89 | +} |
0 commit comments