I am creating an application which uses ews-java-api to connect to an MS Exchange server. Once the connection is authenticated, the api dictates use of ExchangeService object for searching mailboxes, fetching mails and then reading its contents.
The emails are read by different classes down the line for analysis and actions depending upon email content. All these classes must need one pre-authenticated instance of ExchangeService object.
The instance of the object is obtained as -
ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2010_SP2); ExchangeCredentials credentials = new WebCredentials("emailAddress", "password"); service.setCredentials(credentials);
In future there may be requirements to add more classes down the line as needed for more functionalities. All of them would be using the same instance of ExchangeService object.
My question is how should I design the sharing of ExchangeService instance among all these classes down the line? Keeping in mind that no one should inadvertently call the "close()" method on that instance, until whole of the application finishes.
On first look it might appear as a Database connection pool design sort of stuff, but its not. This application runs periodically every 5 minutes and connects to the Exchange server, does its job of analyzing unread mails (with all behavioral classes) and then ideally should close the connection.
One thought of singleton pattern also crossed my mind, but the issue of calling the "close()" method still remains.
Any design suggestion to simplify the system?
Thanks in Advance!