I can’t speak English. Sorry in advance.
i used kafka consume in uniyt. and i want safe shutdown kafka. so i used Close() or Dispose() in OnApplicationQuit(), but unity not work and force quit.
using Confluent.Kafka;
using System;
using System.Threading;
using System.Threading.Tasks;
using UnityEngine;
public class _KafkaConsume : MonoBehaviour
{
private IConsumer<Ignore, string> _consumer;
private const string _topic = "testTopic";
private CancellationTokenSource _cancellationTokenSource;
private async void Start()
{
_cancellationTokenSource = new CancellationTokenSource();
var config = new ConsumerConfig
{
BootstrapServers = "ip",
AutoOffsetReset = AutoOffsetReset.Latest,
GroupId = "consumerGroup",
BrokerAddressFamily = BrokerAddressFamily.V4,
EnableAutoCommit = false,
};
_consumer = new ConsumerBuilder<Ignore, string>(config).Build();
_consumer.Subscribe(_topic);
await Task.Run(() => ConsumeMessagesAsync(_cancellationTokenSource.Token));
}
private async Task ConsumeMessagesAsync(CancellationToken cancellationToken)
{
while (!cancellationToken.IsCancellationRequested)
{
try
{
var consumeResult = _consumer.Consume(cancellationToken);
if (consumeResult != null && consumeResult.Message != null)
{
Debug.Log($"Received message: {consumeResult.Message.Value}");
}
}
catch (ConsumeException e)
{
Debug.LogError($"Error consuming message: {e.Error}");
}
}
}
private void OnApplicationQuit()
{
if (_consumer != null)
{
_consumer.Close(); // problematic part!!!!!!
}
}
}
just play game, and end game. then force quit for unity.
ask Chat GPT, he said : in code “problematic part” is absolutely necessary.
if delete that, my unity is work and not quit.
sorry my english is weird but someone help me. .. ..
Seol is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.