Reputation: 115
I had some efficiency issues and wanted to speed up my application and I cached Unmarshallers:
Unmarshallers cached map:
private Map<String, Unmarshaller> cachedUnmarshallers = new HashMap<>();
Putting Unmarshallers to map:
cachedUnmarshallers.put(AKey, createUnmarshaller(AKey));
cachedUnmarshallers.put(BKey, createUnmarshaller(BKey));
cachedUnmarshallers.put(CKey, createUnmarshaller(CKey));
cachedUnmarshallers.put(DKey, createUnmarshaller(DKey));
Function creating Unmarshaller:
private Unmarshaller createUnmarshaller(String key) throws JAXBException {
JAXBContext jaxbContext =
JAXBContext.newInstance(keyToClassMap.get(key));
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
MessageValidationEventHandler validationEventHandler = new MessageValidationEventHandler();
unmarshaller.setEventHandler(validationEventHandler);
return unmarshaller;
}
And the problem with above code is that validationEventHandler is shared between threads and sometimes I am getting error from different thread while processing correct data.. Clearing errors from validationEventHandler before or after XML processing won't fix it because when two threads starts in the same time, one with correct xml, second with wrong one, both could see same errros in validationEventHandler.
If I would remove my map that caches Unmarshallers everything would again become slow, creating Unmarshallers takes half a second and in there are hundrets of files in zip to validate it takes long time. So do u know any idea how to solve it ?
Upvotes: 0
Views: 42