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 0436141
Showing
7 changed files
with
269 additions
and
118 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; | ||
} | ||
} | ||
} |
122 changes: 122 additions & 0 deletions
122
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,122 @@ | ||
// 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.Reflection; | ||
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 IListener CreateFor(KafkaTriggerAttribute attribute, | ||
ITriggeredFunctionExecutor executor, | ||
bool singleDispatch, | ||
KafkaOptions options, | ||
string brokerList, | ||
string topic, | ||
string consumerGroup, | ||
string eventHubConnectionString, | ||
ILogger logger) | ||
{ | ||
var valueType = GetValueType(attribute, out string avroSchema); | ||
return (IListener)typeof(KafkaListenerFactory) | ||
.GetMethod(nameof(CreateFor), BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.InvokeMethod) | ||
.MakeGenericMethod(attribute.KeyType, valueType) | ||
.Invoke(null, new object[] | ||
{ | ||
executor, | ||
singleDispatch, | ||
options, | ||
brokerList, | ||
topic, | ||
consumerGroup, | ||
eventHubConnectionString, | ||
logger, avroSchema | ||
}); | ||
} | ||
|
||
private static Type GetValueType(KafkaTriggerAttribute attribute, out string avroSchema) | ||
{ | ||
avroSchema = null; | ||
|
||
var valueType = attribute.ValueType; | ||
if (valueType == null) | ||
{ | ||
if (!string.IsNullOrEmpty(attribute.AvroSchema)) | ||
{ | ||
avroSchema = attribute.AvroSchema; | ||
return typeof(Avro.Generic.GenericRecord); | ||
} | ||
else | ||
{ | ||
return typeof(string); | ||
} | ||
} | ||
else | ||
{ | ||
if (typeof(ISpecificRecord).IsAssignableFrom(valueType)) | ||
{ | ||
var specificRecord = (ISpecificRecord)Activator.CreateInstance(valueType); | ||
avroSchema = specificRecord.Schema.ToString(); | ||
} | ||
} | ||
|
||
return valueType; | ||
} | ||
|
||
private 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); | ||
} | ||
} | ||
} |
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.