ews-java-api icon indicating copy to clipboard operation
ews-java-api copied to clipboard

ServiceRequestException: The request failed. 40

Open choonchernlim opened this issue 11 years ago • 21 comments

When invoking the following code in 2 running threads:-

@Override
public Set<ExchangeAppointmentBean> query(final String calendarEmail,
                                          final LocalDate startDate,
                                          final LocalDate endDate) {
    final ExchangeService service;
    final FindItemsResults<Appointment> findResults;
    final ExchangeCredentials credentials = new WebCredentials("user", "pwd");

    try {
        service = new ExchangeService(ExchangeVersion.Exchange2010_SP2);
        service.setCredentials(credentials);
        service.setUrl(new URI("https://hostname/ews/exchange.asmx"));

        final FolderId folderId = new FolderId(WellKnownFolderName.Calendar, new Mailbox(calendarEmail));
        final CalendarFolder calendarFolder = CalendarFolder.bind(service, folderId);
        final CalendarView calendarView = new CalendarView(startDate.toDate(), endDate.toDate());

        findResults = calendarFolder.findAppointments(calendarView);
    }
    catch (Exception e) {
        throw CommonUtils.createException(LOGGER, e, "Unexpected error occurred when querying Exchange Server");
    }

   ....

  service.close();
}    

... I get this error from the try-catch block:-

Caused by: microsoft.exchange.webservices.data.core.exception.service.remote.ServiceRequestException: The request failed. 40
    at microsoft.exchange.webservices.data.core.request.SimpleServiceRequestBase.internalExecute(SimpleServiceRequestBase.java:74)
    at microsoft.exchange.webservices.data.core.request.MultiResponseServiceRequest.execute(MultiResponseServiceRequest.java:158)
    at microsoft.exchange.webservices.data.core.ExchangeService.bindToFolder(ExchangeService.java:500)
    at microsoft.exchange.webservices.data.core.ExchangeService.bindToFolder(ExchangeService.java:519)
    at microsoft.exchange.webservices.data.core.service.folder.CalendarFolder.bind(CalendarFolder.java:60)
    at microsoft.exchange.webservices.data.core.service.folder.CalendarFolder.bind(CalendarFolder.java:75)
    at myproject.module.pto.service.impl.MicrosoftExchangeServiceImpl.query(MicrosoftExchangeServiceImpl.java:78)
    ... 6 more
Caused by: java.lang.ArrayIndexOutOfBoundsException: 40
    at org.apache.http.impl.auth.NTLMEngineImpl$NTLMMessage.addByte(NTLMEngineImpl.java:911)
    at org.apache.http.impl.auth.NTLMEngineImpl$NTLMMessage.addULong(NTLMEngineImpl.java:940)
    at org.apache.http.impl.auth.NTLMEngineImpl$Type1Message.getResponse(NTLMEngineImpl.java:1043)
    at org.apache.http.impl.auth.NTLMEngineImpl.getType1Message(NTLMEngineImpl.java:148)
    at org.apache.http.impl.auth.NTLMEngineImpl.generateType1Msg(NTLMEngineImpl.java:1628)
    at org.apache.http.impl.auth.NTLMScheme.authenticate(NTLMScheme.java:139)
    at org.apache.http.impl.auth.AuthSchemeBase.authenticate(AuthSchemeBase.java:138)
    at org.apache.http.impl.auth.HttpAuthenticator.doAuth(HttpAuthenticator.java:239)
    at org.apache.http.impl.auth.HttpAuthenticator.generateAuthResponse(HttpAuthenticator.java:202)
    at org.apache.http.impl.execchain.MainClientExec.execute(MainClientExec.java:262)
    at org.apache.http.impl.execchain.ProtocolExec.execute(ProtocolExec.java:184)
    at org.apache.http.impl.execchain.RetryExec.execute(RetryExec.java:88)
    at org.apache.http.impl.execchain.RedirectExec.execute(RedirectExec.java:110)
    at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:184)
    at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:82)
    at microsoft.exchange.webservices.data.core.request.HttpClientWebRequest.executeRequest(HttpClientWebRequest.java:292)
    at microsoft.exchange.webservices.data.core.request.ServiceRequestBase.getEwsHttpWebResponse(ServiceRequestBase.java:720)
    at microsoft.exchange.webservices.data.core.request.ServiceRequestBase.validateAndEmitRequest(ServiceRequestBase.java:639)
    at microsoft.exchange.webservices.data.core.request.SimpleServiceRequestBase.internalExecute(SimpleServiceRequestBase.java:62)

choonchernlim avatar Jul 08 '15 16:07 choonchernlim

a simple approach should be to add a synchronized to that method

public synchronized Set<ExchangeAppointmentBean> query(..)
   [...]

serious6 avatar Jul 08 '15 17:07 serious6

seems to be a duplicate of #370 btw.

serious6 avatar Jul 08 '15 17:07 serious6

@serious6 Making the method synchronized is not the solution. The method creates a new ExchangeService every time it is invoked and doesn't share any variables, so I don't see a problem with this code.

It looks like some internal HttpComponents stuff is shared between instances, where it shouldn't be.

Also see: http://stackoverflow.com/questions/29681969/ntlm-authentication-failing-in-multithreaded-application

evpaassen avatar Jul 08 '15 18:07 evpaassen

I did stumble upon that SO post, but nothing I can do on my end to modify the connection manager.

I'm honestly not a big fan of using synchronized at all, but right now I made the synchronized block as small as possible to prevent the problem:-

private final Object lock = new Object();

@Override
public Set<ExchangeAppointmentBean> query(final String calendarEmail,
                                          final LocalDate startDate,
                                          final LocalDate endDate) {
    final ExchangeService service;
    final FindItemsResults<Appointment> findResults;
    final ExchangeCredentials credentials = new WebCredentials("user", "pwd");
    final FolderId folderId = new FolderId(WellKnownFolderName.Calendar, new Mailbox(calendarEmail));
    final CalendarView calendarView = new CalendarView(startDate.toDate(), endDate.toDate());
    final CalendarFolder calendarFolder;

    try {
        service = new ExchangeService(ExchangeVersion.Exchange2010_SP2);
        service.setCredentials(credentials);
        service.setUrl(new URI("https://hostname/ews/exchange.asmx"));

        // problematic part using HttpClient
        synchronized (lock) {
            calendarFolder = CalendarFolder.bind(service, folderId);
        }

        findResults = calendarFolder.findAppointments(calendarView);
    }
    catch (Exception e) {
        throw CommonUtils.createException(LOGGER, e, "Unexpected error occurred when querying Exchange Server");
    }

   ....

  service.close();
}   

This works for me, but I'm not certain if I like this solution. Thank you.

choonchernlim avatar Jul 08 '15 18:07 choonchernlim

It works only because making the method synchronized will eliminate all concurrency. I think this is a bug which should be fixed, however.

evpaassen avatar Jul 08 '15 20:07 evpaassen

This is affecting me as well. It is making this version of the API unusable for me. I have been able to mitigate the issue caused by this error by writing code to retry following this error but I am worried this solution will not scale very well.

bah7303 avatar Aug 24 '15 14:08 bah7303

I, too, am affected by this issue. The API should support multiple instantiations of the ExchangeSevice that may be used concurrently, IMHO.

codewheeney avatar Sep 28 '15 18:09 codewheeney

:+1:

pathikrit avatar Sep 28 '15 18:09 pathikrit

@codewheeney I implemented a dirty fix for our issue. Would you please give my branch a try?

schakko avatar Oct 12 '15 13:10 schakko

I am experience this issue as well, bottom of stack below for additional insight (which support other branch suggestions). I agree that the synchronized block is not a solution as I am attempting to mitigate performance issues by parallelizing the work on separate connections.

Caused by: java.lang.ArrayIndexOutOfBoundsException: 40 at org.apache.http.impl.auth.NTLMEngineImpl$NTLMMessage.addByte(NTLMEngineImpl.java:911) at org.apache.http.impl.auth.NTLMEngineImpl$NTLMMessage.addULong(NTLMEngineImpl.java:939) at org.apache.http.impl.auth.NTLMEngineImpl$Type1Message.getResponse(NTLMEngineImpl.java:1007) at org.apache.http.impl.auth.NTLMEngineImpl.getType1Message(NTLMEngineImpl.java:148) at org.apache.http.impl.auth.NTLMEngineImpl.generateType1Msg(NTLMEngineImpl.java:1628) at org.apache.http.impl.auth.NTLMScheme.authenticate(NTLMScheme.java:139) at org.apache.http.impl.auth.AuthSchemeBase.authenticate(AuthSchemeBase.java:138) at org.apache.http.impl.auth.HttpAuthenticator.doAuth(HttpAuthenticator.java:239) at org.apache.http.impl.auth.HttpAuthenticator.generateAuthResponse(HttpAuthenticator.java:202) at org.apache.http.impl.execchain.MainClientExec.execute(MainClientExec.java:262) at org.apache.http.impl.execchain.ProtocolExec.execute(ProtocolExec.java:184) at org.apache.http.impl.execchain.RetryExec.execute(RetryExec.java:88) at org.apache.http.impl.execchain.RedirectExec.execute(RedirectExec.java:110) at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:184) at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:82) at microsoft.exchange.webservices.data.core.request.HttpClientWebRequest.executeRequest(HttpClientWebRequest.java:292) at microsoft.exchange.webservices.data.core.request.ServiceRequestBase.getEwsHttpWebResponse(ServiceRequestBase.java:720) at microsoft.exchange.webservices.data.core.request.ServiceRequestBase.validateAndEmitRequest(ServiceRequestBase.java:639) at microsoft.exchange.webservices.data.core.request.SimpleServiceRequestBase.internalExecute(SimpleServiceRequestBase.java:62) ... 14 more

Monte-Hansen avatar Oct 29 '15 17:10 Monte-Hansen

I've come across this same issue and adding a synchronized to the method or adding a synchronized block does not correct this issue. Have there been any fixes for this?

danxfisher avatar Dec 09 '15 22:12 danxfisher

+1

prophe05 avatar May 17 '16 09:05 prophe05

+1

ralphavalon avatar May 30 '16 18:05 ralphavalon

+1

maroswal avatar May 30 '16 18:05 maroswal

Has anyone tried upgrading httpclient to 4.5.2 to fix the problem?

http://www.apache.org/dist/httpcomponents/httpclient/RELEASE_NOTES-4.5.x.txt [HTTPCLIENT-1715] NTLMEngineImpl#Type1Message not thread safe but declared as a constant. The issue was resolved in February 2016.

indriesergiu avatar Jun 07 '16 06:06 indriesergiu

@indriesergiu I will hopefully be attempting to upgrade the httpclient dependency very soon and will report any results here.

derylspielman avatar Jun 10 '16 15:06 derylspielman

@derylspielman Deryl, any luck on your end with the httpclient upgrade?

scorchio avatar Aug 07 '16 19:08 scorchio

Just an idea: could implementing #423 help in this?

scorchio avatar Aug 07 '16 19:08 scorchio

Hi guys, I've tested using the httpclient version 4.5.2 and it fixes the "The request failed. 40" issue, however, you will still run into "Connection is still allocated" issues (see https://github.com/OfficeDev/ews-java-api/issues/276). So at this point, if you use a new ExchangeService instance for each EWS request you can execute multiple EWS requests in parallel :)

indriesergiu avatar Sep 06 '16 14:09 indriesergiu

+1. although hI have not had problems since switching to 4.5.2 seems to fix it.

ghost avatar Dec 14 '16 02:12 ghost