【发布时间】:2013-06-03 18:31:38
【问题描述】:
我的任务是创建与以下代码具有类似功能的 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
还有其他几个事件的代码与上面提供的代码相似。从 cmets 中,我了解到该代码旨在异步处理 SendAggregateEvent 方法。我不明白为什么要使用委托修饰符,或者如何在 Java 中复制这种类型的异步处理。
也来自阅读这个帖子
我知道在 java 中模拟委托功能没有“简单”的方法。是否需要委托功能才能异步处理 SendAggregateEvent 方法?如果没有,有人可以建议我怎么做吗?
【问题讨论】:
-
在 C# 中,它使用委托作为一种简单的方法来异步运行该方法,使用委托的
BeginInvoke方法。有关这方面的文档,请参阅 stackoverflow.com/questions/14961450/…。
标签: c# java asynchronous delegates