Current section

Files

Jump to
erlkaf test test_producer.erl
Raw

test/test_producer.erl

-module(test_producer).
-define(TOPIC, <<"benchmark">>).
-export([
delivery_report/2,
stats_callback/2,
create_producer/0,
produce/2,
produce/3
]).
-behaviour(erlkaf_producer_callbacks).
delivery_report(DeliveryStatus, Message) ->
io:format("received delivery report: ~p ~n", [{DeliveryStatus, Message}]),
ok.
stats_callback(ClientId, Stats) ->
io:format("stats_callback: ~p stats:~p ~n", [ClientId, length(Stats)]).
create_producer() ->
erlkaf:start(),
ProducerConfig = [
{bootstrap_servers, <<"172.17.33.123:9092">>},
{delivery_report_only_error, true},
{delivery_report_callback, ?MODULE}
],
ok = erlkaf:create_producer(client_producer, ProducerConfig),
ok = erlkaf:create_topic(client_producer, ?TOPIC, [{request_required_acks, 1}]).
produce(Key, Value) ->
ok = erlkaf:produce(client_producer, ?TOPIC, Key, Value).
produce(0, _Key, _Value) ->
ok;
produce(Count, Key, Value) ->
ok = erlkaf:produce(client_producer, ?TOPIC, Key, Value),
produce(Count -1, Key, Value).