forked from Azure/azure-functions-kafka-extension
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Reworking Listener creating in to a Factory pattern. Resolves two ite…
…ms in Azure#44
- Loading branch information
1 parent
e71c90f
commit bc62a2d
Showing
7 changed files
with
260 additions
and
106 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
src/Microsoft.Azure.WebJobs.Extensions.Kafka/Listeners/KafkaListenerAvro.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the MIT License. See License.txt in the project root for license information. | ||
|
||
|
||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Confluent.SchemaRegistry.Serdes; | ||
using Microsoft.Azure.WebJobs.Host.Executors; | ||
using Microsoft.Extensions.Logging; | ||
|
||
namespace Microsoft.Azure.WebJobs.Extensions.Kafka | ||
{ | ||
/// <summary> | ||
/// Kafka listener. | ||
/// Connects a Kafka trigger function with a Kafka Consumer | ||
/// </summary> | ||
internal class KafkaListenerAvro<TKey, TValue> : KafkaListener<TKey, TValue> | ||
{ | ||
private readonly string avroSchema; | ||
|
||
public KafkaListenerAvro( | ||
ITriggeredFunctionExecutor executor, | ||
bool singleDispatch, | ||
KafkaOptions options, | ||
string brokerList, | ||
string topic, | ||
string consumerGroup, | ||
string eventHubConnectionString, | ||
string avroSchema, | ||
ILogger logger) : base(executor, singleDispatch, options, brokerList, topic, consumerGroup, eventHubConnectionString, logger) | ||
{ | ||
this.avroSchema = avroSchema; | ||
} | ||
|
||
public override Task StartAsync(CancellationToken cancellationToken) | ||
{ | ||
var schemaRegistry = new LocalSchemaRegistry(avroSchema); | ||
AvroDeserializer<TValue> avroDeserializer = new AvroDeserializer<TValue>(schemaRegistry); | ||
SetConsumerAndExecutor(avroDeserializer, null, null); | ||
|
||
return Task.CompletedTask; | ||
} | ||
} | ||
} |
89 changes: 89 additions & 0 deletions
89
src/Microsoft.Azure.WebJobs.Extensions.Kafka/Listeners/KafkaListenerFactory.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the MIT License. See License.txt in the project root for license information. | ||
|
||
using System; | ||
using System.Linq; | ||
using Avro.Specific; | ||
using Microsoft.Azure.WebJobs.Host.Executors; | ||
using Microsoft.Azure.WebJobs.Host.Listeners; | ||
using Microsoft.Extensions.Logging; | ||
|
||
namespace Microsoft.Azure.WebJobs.Extensions.Kafka.Listeners | ||
{ | ||
internal static class KafkaListenerFactory | ||
{ | ||
public static KafkaListener<TKey, TValue> CreateFor<TKey, TValue>( | ||
ITriggeredFunctionExecutor executor, | ||
bool singleDispatch, | ||
KafkaOptions options, | ||
string brokerList, | ||
string topic, | ||
string consumerGroup, | ||
string eventHubConnectionString, | ||
ILogger logger, | ||
string avroSchema = null) | ||
{ | ||
if (typeof(ISpecificRecord).IsAssignableFrom(typeof(TValue))) | ||
{ | ||
if (string.IsNullOrWhiteSpace(avroSchema)) | ||
{ | ||
throw new ArgumentNullException(nameof(avroSchema), $@"parameter is required when creating an Avro-based Listener"); | ||
} | ||
|
||
return new KafkaListenerAvro<TKey, TValue>(executor, | ||
singleDispatch, | ||
options, | ||
brokerList, | ||
topic, | ||
consumerGroup, | ||
eventHubConnectionString, | ||
avroSchema, | ||
logger); | ||
} | ||
|
||
if (typeof(Google.Protobuf.IMessage).IsAssignableFrom(typeof(TValue))) | ||
{ | ||
return new KafkaListenerProtoBuf<TKey, TValue>(executor, | ||
singleDispatch, | ||
options, | ||
brokerList, | ||
topic, | ||
consumerGroup, | ||
eventHubConnectionString, | ||
logger); | ||
} | ||
|
||
return new KafkaListener<TKey, TValue>(executor, | ||
singleDispatch, | ||
options, | ||
brokerList, | ||
topic, | ||
consumerGroup, | ||
eventHubConnectionString, | ||
logger); | ||
} | ||
|
||
public static IListener CreateFor(Type keyType, Type valueType, | ||
ITriggeredFunctionExecutor executor, | ||
bool singleDispatch, | ||
KafkaOptions options, | ||
string brokerList, | ||
string topic, | ||
string consumerGroup, | ||
string eventHubConnectionString, | ||
ILogger logger, string avroSchema = null) => (IListener)typeof(KafkaListenerFactory) | ||
.GetMethods().Single(m => m.Name == nameof(CreateFor) && m.IsGenericMethod) | ||
.MakeGenericMethod(keyType, valueType) | ||
.Invoke(null, new object[] | ||
{ | ||
executor, | ||
singleDispatch, | ||
options, | ||
brokerList, | ||
topic, | ||
consumerGroup, | ||
eventHubConnectionString, | ||
logger, avroSchema | ||
}); | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
src/Microsoft.Azure.WebJobs.Extensions.Kafka/Listeners/KafkaListenerProtoBuf.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the MIT License. See License.txt in the project root for license information. | ||
|
||
|
||
using System; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Confluent.Kafka; | ||
using Microsoft.Azure.WebJobs.Host.Executors; | ||
using Microsoft.Extensions.Logging; | ||
|
||
namespace Microsoft.Azure.WebJobs.Extensions.Kafka | ||
{ | ||
/// <summary> | ||
/// Kafka listener. | ||
/// Connects a Kafka trigger function with a Kafka Consumer | ||
/// </summary> | ||
internal class KafkaListenerProtoBuf<TKey, TValue> : KafkaListener<TKey, TValue> | ||
{ | ||
public KafkaListenerProtoBuf( | ||
ITriggeredFunctionExecutor executor, | ||
bool singleDispatch, | ||
KafkaOptions options, | ||
string brokerList, | ||
string topic, | ||
string consumerGroup, | ||
string eventHubConnectionString, | ||
ILogger logger) : base(executor, singleDispatch, options, brokerList, topic, consumerGroup, eventHubConnectionString, logger) { } | ||
|
||
public override Task StartAsync(CancellationToken cancellationToken) | ||
{ | ||
// protobuf: need to create using reflection due to generic requirements in ProtobufDeserializer | ||
var valueDeserializer = (IDeserializer<TValue>)Activator.CreateInstance(typeof(ProtobufDeserializer<>).MakeGenericType(typeof(TValue))); | ||
SetConsumerAndExecutor(null, valueDeserializer, null); | ||
|
||
return Task.CompletedTask; | ||
} | ||
} | ||
} |
Oops, something went wrong.