EWS Java:如何使用流订阅创建侦听器
我想开发一个应用程序,阅读新的电子邮件 因此,我使用ews java api连接到microsoft exchange并获取所有新电子邮件 以前,他们使用“推送通知”,并使用while无限循环来获取所有电子邮件 现在,我想实现“流媒体订阅”: 客户端建立流式连接,然后与服务器保持连接最长30分钟,在此期间,exchange将通知任何已注册的事件 但有两个问题:EWS Java:如何使用流订阅创建侦听器,java,exchangewebservices,Java,Exchangewebservices,我想开发一个应用程序,阅读新的电子邮件 因此,我使用ews java api连接到microsoft exchange并获取所有新电子邮件 以前,他们使用“推送通知”,并使用while无限循环来获取所有电子邮件 现在,我想实现“流媒体订阅”: 客户端建立流式连接,然后与服务器保持连接最长30分钟,在此期间,exchange将通知任何已注册的事件 但有两个问题: 我身上什么也没发生 如何让我的程序保持活力。因为我希望我的程序仍能收听Microsoft Exchange 下面是流媒体的实现 Ma
- 我身上什么也没发生
- 如何让我的程序保持活力。因为我希望我的程序仍能收听Microsoft Exchange
Main(){
public void InitSubscriptions() throws Exception {
var service = exchangeServiceManager.getExchangeService();
WellKnownFolderName sd = WellKnownFolderName.Inbox;
FolderId folderId = new FolderId(sd);
List<FolderId> folder = new ArrayList<>();
folder.add(folderId);
IAsyncResult result = service.beginSubscribeToStreamingNotifications(new AsyncCallbackImpl(), service, folder, EventType.NewMail);
Callback(result);
}
private void Callback(IAsyncResult result) throws Exception {
// Retrieve the service from the passed state.
var service = exchangeServiceManager.getExchangeService();
var subscription = service.endSubscribeToStreamingNotifications(result);
var conn = new StreamingSubscriptionConnection(service, 30);
conn.addSubscription(subscription);
// Delegate handlers
conn.addOnNotificationEvent(new StreamingSubscriptionConnection.INotificationEventDelegate() {
@Override
public void notificationEventDelegate(Object sender, NotificationEventArgs args) {
StringBuilder sb = new StringBuilder();
for (NotificationEvent event : args.getEvents()) {
if (event instanceof microsoft.exchange.webservices.data.notification.ItemEvent) {
sb.append(" eventType=").append((event).getEventType()).append(",");
System.out.println("incoming events: " + sb.toString());
}
}
}
});
conn.addOnDisconnect(new StreamingSubscriptionConnection.ISubscriptionErrorDelegate() {
@Override
public void subscriptionErrorDelegate(Object sender, SubscriptionErrorEventArgs args) {
StreamingSubscriptionConnection connection = (StreamingSubscriptionConnection) sender;
try {
connection.open();
System.out.println("restarted");
} catch (Throwable e) {
e.printStackTrace();
}
}
});
conn.addOnSubscriptionError(new StreamingSubscriptionConnection.ISubscriptionErrorDelegate() {
@Override
public void subscriptionErrorDelegate(Object sender, SubscriptionErrorEventArgs args) {
if (args.getException() != null) {
System.err.println("Subscription error cause: " + args.getException().getMessage());
}
}
});
}