Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

StreamingSubscriptionConnection.getIsOpen() returns false after keep-alive #742

Open
uklance opened this issue Apr 1, 2021 · 0 comments

Comments

@uklance
Copy link

uklance commented Apr 1, 2021

StreamingSubscriptionConnection.getIsOpen() returns true initially, but then starts returning false after the OnDisconnect handler fires (calling StreamingSubscriptionConnection.open())

The documentation here states:

After the time-out of the connection has elapsed, the OnDisconnect event is raised by the StreamingSubscriptionConnection instance. If your application still needs to monitor the subscription, you can just call the Open method of the StreamingSubscriptionConnection instance again to reconnect to the Exchange server. This behavior is expected and is not an error condition.

Please see my code below with comments showing when connection.getIsOpen() initially returns true, but starts returning false after the disconnect handler fires

public class EwsClient {
    private final ExchangeService exchangeService;
    private final Set<StreamingSubscriptionConnection> subscriptionConnections = ConcurrentHashMap.newKeySet();
    private final int subscriptionLifetimeMinutes = 2;

    // constructor

    public void subscribe(Collection<FolderId> folderIds, EventType[] eventTypes, INotificationEventDelegate eventHandler) throws Exception {
        StreamingSubscription subscription = exchangeService.subscribeToStreamingNotifications(folderIds, eventTypes);
        StreamingSubscriptionConnection connection = new StreamingSubscriptionConnection(exchangeService, subscriptionLifetimeMinutes), eventHandler);

        ISubscriptionErrorDelegate disconnectHandler = (sender, args) -> {
            try {
                connection.open(); // connection.getIsOpen() will start returning false now
            } catch (Exception e) {
                // I would expect connection.getIsOpen() will start returning false now
                log.error("Could not reconnect to the exchange server", e);
            }
        };
        connection.addSubscription(subscription);
        connection.addOnNotificationEvent(eventHandler);
        connection.addOnDisconnect(disconnectHandler);
        subscriptionConnections.add(connection);
        connection.open(); // connection.getIsOpen() will return true initially
    }

    // I use this method in a spring boot health check
    public boolean isSubscriptionsOpen() {
        return subscriptionConnections.stream().allMatch(connection -> {
            try {
                return connection.getIsOpen();
            } catch (Exception e) {
                log.error("Error validating subscription connection", e);
                return false;
            }
        });
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant