我的任务是创建与下面的代码具有类似功能的 Java 代码。目前,我正在努力理解代码的作用以及如何在 Java 中模拟效果。
#region "Send Aggregate Event"
/// <summary>
/// Delegate for async sending the AggregateEvent
/// </summary>
/// <param name="request"></param>
public delegate void SendAggregateEventAsync(AE request);
SendAggregateEventAsync _sendAggregateEventAsync;
/// <summary>
/// IAsyncResult pattern to async send the AggregateEvent
/// </summary>
/// <param name="request"></param>
/// <param name="callback"></param>
/// <param name="state"></param>
/// <returns></returns>
public IAsyncResult BeginSendAggregateEvent(AE request, AsyncCallback callback, Object state)
{
_sendAggregateEventAsync = new SendAggregateEventAsync(SendAggregateEvent);
return _sendAggregateEventAsync.BeginInvoke(request, callback, state);
}
public void EndSendAggregateEvent(IAsyncResult result)
{
object state = result.AsyncState;
_sendAggregateEventAsync.EndInvoke(result);
}
/// <summary>
/// Send an aggregate event to the Device Webserver
/// </summary>
/// <param name="request">The AggregateEvent request</param>
public void SendAggregateEvent(AE request)
{
if (request == null) throw new ArgumentNullException("request");
String message = ChangeDatesToUTC(MessageHelper.SerializeObject( typeof(AE), request), new String[] { "EventTime" }, url);
SendMessage(message);
}
#endregion
还有其他几个事件都具有与上面提供的代码相似的代码。从评论中,我了解到该代码旨在异步处理 SendAggregateEvent 方法。我不明白为什么要使用委托修饰符,或者如何在 Java 中复制这种类型的异步处理。
也从阅读这个线程
我知道在 java 中没有“简单”的方法来模拟委托功能。是否需要委托功能才能异步处理 SendAggregateEvent 方法?如果没有,有人可以建议我怎么做吗?