Streaming Notifications not triggering on Notification Event...
I have been trying for the past few days to get Streaming Notifications working put to no avail. I can see in the messages that data is coming back from Exchange, but from there they are not triggering the added notification event. I've stripped down the code to the very basics... Also to note that using Eclipse I am unable to get any breakpoints triggered while it is using the background thread. However, when I did interrupt the background thread and stepped through the code I could get it to trigger my notification event delegate without issue, but when running the application through as normal I could not.
Note: This is using a custom Exchange 2013 server on an internal network.
Example code: `public class EmailRunner implements INotificationEventDelegate, ISubscriptionErrorDelegate {
public static ExchangeService exchangeService = new ExchangeService(ExchangeVersion.Exchange2010_SP2);
private static StreamingSubscriptionConnection conn;
public static StreamingSubscription subscription;
public static void main(String[] args) {
EmailRunner runner = new EmailRunner();
runner.connect();
}
public void connect() {
ExchangeCredentials credentials = new WebCredentials("user", "pass");
exchangeService.setCredentials(credentials);
try {
exchangeService.setUrl(new URI("url"));
WellKnownFolderName sd = WellKnownFolderName.Inbox;
FolderId folderId = new FolderId(sd);
ArrayList<FolderId> folder = new ArrayList<FolderId>();
folder.add(folderId);
subscription = exchangeService.subscribeToStreamingNotifications(folder, EventType.NewMail);
conn = new StreamingSubscriptionConnection(exchangeService, 30);
conn.addSubscription(subscription);
conn.addOnNotificationEvent(this);
conn.addOnDisconnect(this);
conn.open();
EmailMessage msg = new EmailMessage(exchangeService);
msg.setSubject("Testing Streaming Notification on 16 Aug 2016");
msg.setBody(MessageBody.getMessageBodyFromText("Streaming Notification "));
msg.getToRecipients().add("[email protected]");
msg.send();
boolean loop = true;
int i = 0;
while (loop) {
Thread.sleep(20000);
msg.send();
if (i++ > 100) {
loop = false;
}
}
System.out.println("end........");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Override
public void subscriptionErrorDelegate(Object sender, SubscriptionErrorEventArgs args) {
System.out.println("error...." + args.getException());
}
@Override
public void notificationEventDelegate(Object sender, NotificationEventArgs args) {
System.out.println("Notification event....");
// First retrieve the IDs of all the new emails
List<ItemId> newMailsIds = new ArrayList<ItemId>();
Iterator<NotificationEvent> it = args.getEvents().iterator();
while (it.hasNext()) {
ItemEvent itemEvent = (ItemEvent) it.next();
if (itemEvent != null) {
newMailsIds.add(itemEvent.getItemId());
}
}
if (newMailsIds.size() > 0) {
// Now retrieve the Subject property of all the new emails in one
// call to EWS.
ServiceResponseCollection<GetItemResponse> responses;
try {
responses = exchangeService.bindToItems(newMailsIds, new PropertySet(ItemSchema.Subject));
System.out.println("count=======" + responses.getCount());
for (GetItemResponse response : responses) {
System.out.println("count=======" + responses.getClass().getName());
System.out.println("subject=======" + response.getItem().getSubject());
response.getItem().delete(DeleteMode.MoveToDeletedItems);
// Console.WriteLine("subject====" + response.Item.Subject);
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}`
Here is the logging that shows we are receiving the network traffic properly:
2016-08-16 11:34:39 [pool-1-thread-1] DEBUG org.apache.http.wire - http-outgoing-0 << "<Envelope xmlns="http://schemas.xmlsoap.org/soap/envelope/"><soap11:Header xmlns:soap11="http://schemas.xmlsoap.org/soap/envelope/"><ServerVersionInfo xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" MajorVersion="14" MinorVersion="3" MajorBuildNumber="224" MinorBuildNumber="2" Version="Exchange2010_SP2" xmlns="http://schemas.microsoft.com/exchange/services/2006/types" /></soap11:Header><soap11:Body xmlns:soap11="http://schemas.xmlsoap.org/soap/envelope/"><m:GetStreamingEventsResponse xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:t="http://schemas.microsoft.com/exchange/services/2006/types" xmlns:m="http://schemas.microsoft.com/exchange/services/2006/messages"><m:ResponseMessages><m:GetStreamingEventsResponseMessage ResponseClass="Success"><m:ResponseCode>NoError</m:ResponseCode><m:Notifications><m:Notification><t:SubscriptionId>IgB2bWtpZC1oZXhoYzAwMi5leGRldmNoaWxkLmxtaWcuY29tEAAAAPqQlSdb6wRNmCmdT+X/yO6tnOaXAsbTCA==</t:SubscriptionId><t:NewMailEvent><t:TimeStamp>2016-08-16T18:34:37Z</t:TimeStamp><t:ItemId Id="AAMkAGFlYjQ4NTc5LTBkOGMtNDM1Ni1hZTgwLTQ1YWIzMWZjNGQ2NgBGAAAAAABv1iHhBPyJRJ8vStul6D6nBwBZqwtToAjxTKuXu9yLHQhZAAAAAAEMAABZqwtToAjxTKuXu9yLHQhZAABjg8kzAAA=" ChangeKey="CQAAAA==" /><t:ParentFolderId Id="AQMkAGFlYjQ4NTc5LTBkOGMtNDM1Ni1hZTgwLTQ1YWIzMWZjNGQ2NgAALgAAA2/WIeEE/IlEny9K26XoPqcBAFmrC1OgCPFMq5e73IsdCFkAAAIBDAAAAA==" ChangeKey="AQAAAA==" /></t:NewMailEvent></m:Notification></m:Notifications></m:GetStreamingEventsResponseMessage></m:ResponseMessages></m:GetStreamingEventsResponse></soap11:Body></Envelope>[\r][\n]"
Please help nothing I am doing is working consistently.
I am experiencing same issue of getting no any notification with released API version 2.0 and Office 365, although I am able to observe incoming SOAP message of GetStreamingEventsResponse as below:
2016/09/13 14:42:49:848 SGT [DEBUG] wire - http-outgoing-0 << "<Envelope xmlns="http://schemas.xmlsoap.org/soap/envelope/"><soap11:Header xmlns:soap11="http://schemas.xmlsoap.org/soap/envelope/"><ServerVersionInfo xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" MajorVersion="15" MinorVersion="1" MajorBuildNumber="619" MinorBuildNumber="10" Version="V2016_07_13" xmlns="http://schemas.microsoft.com/exchange/services/2006/types" /></soap11:Header><soap11:Body xmlns:soap11="http://schemas.xmlsoap.org/soap/envelope/"><m:GetStreamingEventsResponse xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:t="http://schemas.microsoft.com/exchange/services/2006/types" xmlns:m="http://schemas.microsoft.com/exchange/services/2006/messages"><m:ResponseMessages><m:GetStreamingEventsResponseMessage ResponseClass="Success"><m:ResponseCode>NoError</m:ResponseCode><m:Notifications><m:Notification><t:SubscriptionId>JwBoazJwcjAybWIwODUxLmFwY3ByZDAyLnByb2Qub3V0bG9vay5jb20QAAAAxi2IPtXCmUmLvk+OsK82SeN+4wyh29MIEAAAAMfdsdk7FqtGgdo+Bv42Vsg=</t:SubscriptionId><t:ModifiedEvent><t:TimeStamp>2016-09-13T06:42:48Z</t:TimeStamp><t:ItemId Id="AAMkAGQ5YjFkZGM3LTE2M2ItNDZhYi04MWRhLTNlMDZmZTM2NTZjOABGAAAAAAC9rhsuRHbZSZD48gM0FbnPBwBo7uxdYky7Rb9vlT13IKApAAAAAAEMAABo7uxdYky7Rb9vlT13IKApAAAAV6hiAAA=" ChangeKey="CQAAAA==" /><t:ParentFolderId Id="AQMkAGQ5YjFkZABjNy0xNjNiLTQ2YWItODFkYS0zZTA2ZmUzNjU2YzgALgAAA72uGy5EdtlJkPjyAzQVuc8BAGju7F1iTLtFv2+VPXcgoCkAAAIBDAAAAA==" ChangeKey="AQAAAA==" /></t:ModifiedEvent><t:ModifiedEvent><t:TimeStamp>2016-09-13T06:42:48Z</t:TimeStamp><t:FolderId Id="AQMkAGQ5YjFkZABjNy0xNjNiLTQ2YWItODFkYS0zZTA2ZmUzNjU2YzgALgAAA72uGy5EdtlJkPjyAzQVuc8BAGju7F1iTLtFv2+VPXcgoCkAAAIBDAAAAA==" ChangeKey="AQAAAA==" /><t:ParentFolderId Id="AQMkAGQ5YjFkZABjNy0xNjNiLTQ2YWItODFkYS0zZTA2ZmUzNjU2YzgALgAAA72uGy5EdtlJkPjyAzQVuc8BAGju7F1iTLtFv2+VPXcgoCkAAAIBCAAAAA==" ChangeKey="AQAAAA==" /></t:ModifiedEvent></m:Notification></m:Notifications></m:GetStreamingEventsResponseMessage></m:ResponseMessages></m:GetStreamingEventsResponse></soap11:Body></Envelope>[\r][\n]"
Still trying to figure out what goes wrong...
My subsequent testing shows that it has something to do with the XML parsing library bundled in JRE that I am using, which is IBM JRE 7. With Oracle JRE 7, notifications work well. Should be caused by difference in parsing behaviors between these 2 JREs. My current workaround is to replace the Stax parser in JRE with open source one from Woodstax.