|
@@ -0,0 +1,94 @@
|
|
|
+using Microsoft.Azure.ServiceBus;
|
|
|
+using System;
|
|
|
+using System.Collections.Generic;
|
|
|
+using System.Text;
|
|
|
+using System.Threading;
|
|
|
+using System.Threading.Tasks;
|
|
|
+using TEAMModelOS.SDK.Helper.Common.DateTimeHelper;
|
|
|
+using TEAMModelOS.SDK.Helper.Common.JsonHelper;
|
|
|
+using TEAMModelOS.SDK.Module.AzureServiceBus;
|
|
|
+using TEAMModelOS.Service.Services.Learn.Interfaces;
|
|
|
+
|
|
|
+namespace TEAMModelOS.Service.Services.Learn.Implements
|
|
|
+{
|
|
|
+
|
|
|
+ public class ServiceBusService : IServiceBusService
|
|
|
+ {
|
|
|
+ private readonly IAzureServiceBusService _serviceBus;
|
|
|
+ private static ISubscriptionClient subscriptionClient;
|
|
|
+ public ServiceBusService(IAzureServiceBusService azureServiceBus)
|
|
|
+ {
|
|
|
+ _serviceBus = azureServiceBus;
|
|
|
+ }
|
|
|
+
|
|
|
+ public async Task ReciveMessageAsync<T>(string TopicName)
|
|
|
+ {
|
|
|
+ subscriptionClient = _serviceBus.GetSubClient(TopicName).subscriptionClient;
|
|
|
+ RegisterOnMessageHandlerAndReceiveMessages();
|
|
|
+ await subscriptionClient.CloseAsync();
|
|
|
+ //return "";
|
|
|
+ }
|
|
|
+
|
|
|
+ public static void RegisterOnMessageHandlerAndReceiveMessages() {
|
|
|
+ // Configure the message handler options in terms of exception handling, number of concurrent messages to deliver, etc.
|
|
|
+ var messageHandlerOptions = new MessageHandlerOptions(ExceptionReceivedHandler)
|
|
|
+ {
|
|
|
+ // Maximum number of concurrent calls to the callback ProcessMessagesAsync(), set to 1 for simplicity.
|
|
|
+ // Set it according to how many messages the application wants to process in parallel.
|
|
|
+ MaxConcurrentCalls = 1,
|
|
|
+
|
|
|
+ // Indicates whether MessagePump should automatically complete the messages after returning from User Callback.
|
|
|
+ // False below indicates the Complete will be handled by the User Callback as in `ProcessMessagesAsync` below.
|
|
|
+ AutoComplete = false
|
|
|
+ };
|
|
|
+ subscriptionClient.RegisterMessageHandler(ProcessMessagesAsync, messageHandlerOptions);
|
|
|
+
|
|
|
+ }
|
|
|
+ public static async Task ProcessMessagesAsync(Message message, CancellationToken token)
|
|
|
+ {
|
|
|
+ // Process the message.
|
|
|
+ Console.WriteLine($"Received message: SequenceNumber:{message.SystemProperties.SequenceNumber} Body:{Encoding.UTF8.GetString(message.Body)}");
|
|
|
+
|
|
|
+ // Complete the message so that it is not received again.
|
|
|
+ // This can be done only if the subscriptionClient is created in ReceiveMode.PeekLock mode (which is the default).
|
|
|
+ await subscriptionClient.CompleteAsync(message.SystemProperties.LockToken);
|
|
|
+ //return message;
|
|
|
+
|
|
|
+ // Note: Use the cancellationToken passed as necessary to determine if the subscriptionClient has already been closed.
|
|
|
+ // If subscriptionClient has already been closed, you can choose to not call CompleteAsync() or AbandonAsync() etc.
|
|
|
+ // to avoid unnecessary exceptions.
|
|
|
+ }
|
|
|
+
|
|
|
+ public static Task ExceptionReceivedHandler(ExceptionReceivedEventArgs exceptionReceivedEventArgs)
|
|
|
+ {
|
|
|
+ Console.WriteLine($"Message handler encountered an exception {exceptionReceivedEventArgs.Exception}.");
|
|
|
+ var context = exceptionReceivedEventArgs.ExceptionReceivedContext;
|
|
|
+ Console.WriteLine("Exception context for troubleshooting:");
|
|
|
+ Console.WriteLine($"- Endpoint: {context.Endpoint}");
|
|
|
+ Console.WriteLine($"- Entity Path: {context.EntityPath}");
|
|
|
+ Console.WriteLine($"- Executing Action: {context.Action}");
|
|
|
+ return Task.CompletedTask;
|
|
|
+ }
|
|
|
+
|
|
|
+ public async Task<long> SendMessage<T>(string TopicName, string info, long startTime)
|
|
|
+ {
|
|
|
+ ITopicClient topicClient = _serviceBus.GetTopClient(TopicName).topicClient;
|
|
|
+ //设定开始时间
|
|
|
+ Dictionary<string, object> dict = new Dictionary<string, object>() {
|
|
|
+ { "name",typeof(T).Name},
|
|
|
+ { "info",info},
|
|
|
+ { "status",200}
|
|
|
+ };
|
|
|
+ var message = new Message(Encoding.UTF8.GetBytes(dict.ToApiJson()));
|
|
|
+ long time = startTime - new DateTimeOffset(DateTime.UtcNow).ToUnixTimeMilliseconds();
|
|
|
+ if (time <= 0)
|
|
|
+ {
|
|
|
+ return -1;
|
|
|
+ }
|
|
|
+ long SequenceNumber = await topicClient.ScheduleMessageAsync(message, new DateTimeOffset(DateTimeHelper.ConvertToDateTime(startTime)));
|
|
|
+ await topicClient.SendAsync(message);
|
|
|
+ await topicClient.CloseAsync();
|
|
|
+ return SequenceNumber;
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|