We are subscribing to streaming notifications with MS Exchange 2016 via the following method:
service.subscribeToStreamingNotificationsOnAllFolders(EventType.Copied,EventType.Created, EventType.Deleted, EventType.Modified, EventType.Moved, EventType.NewMail);
switch (itemEvent.getEventType()) {
case Deleted:
delete(mailbox, itemEvent.getItemId());
break;
case Moved:
try {
delete(mailbox, itemIdNoChangeKey(itemEvent.getOldItemId()));
} finally {
create(mailbox, Item.bind(service, itemIdNoChangeKey(itemEvent.getItemId())));
}
break;
case Modified:
update(mailbox, Item.bind(service, itemIdNoChangeKey(itemEvent.getItemId()), itemProp()));
break;
case Copied:
case Created:
create(mailbox, Item.bind(service, itemIdNoChangeKey(itemEvent.getItemId()), itemProp()));
break;
}
Sometimes the calls above succeed, but more often than not, the Items.bind() fails with either:
"Access is denied. Check credentials and try again., The process failed to get the correct properties.,errorCode='ErrorAccessDenied'"
OR
"The specified object was not found in the store., The process failed to get the correct properties.,errorCode='ErrorItemNotFound'"
This is depite the fact that
Can anyone please advise why we would be receiving ErrorAccessDenied and ErrorItemNotFound errors? Is it do with the processing of Exchange system related message or folders? How can we process these items or if we cannot how can they be skipped without causing unnecessary load on the Exchange server?
In my case, my code was impersonating the wrong mailbox. Turns out, I needed to maintain a map as follows:
private Map<StreamingSubscription, String> reverseSubscriptions = Collections.synchronizedMap(new HashMap<>());
Then in notificationEventDelegate(..) method...
Call
String mailbox = reverseSubscriptions.get(args.getSubscription());
When processing item event...
private void processItemEvent(ExchangeService service, String mailbox, ItemEvent itemEvent) throws Exception {
synchronized(service) {
service.setImpersonatedUserId(impersonateAccount(mailbox));
service.getHttpHeaders().put("X-AnchorMailbox", mailbox);
service.getHttpHeaders().put("X-PreferServerAffinity", "true");
try {
...
} catch (Exception ie) {
...
}
}
}
}