Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

which xml validator will work perfectly for multithreading project

Tags:

java

xml

schema

I have used jdom for xml validation against schema. The main problem there is that it gives an error

FWK005 parse may not be called while parsing

The main reason was that multiple of threads working for xerces validation at the same time. SO I got the solution that i have to lock that validation. which is not good

So I want to know which xml validator works perfectly for multithreading project

public static HashMap<String, String> validate(String xmlString, Validator validator) {

    HashMap<String, String> map = new HashMap<String, String>();
    long t1 = System.currentTimeMillis();
    DocumentBuilder builder = null;
    try {
        //obtain lock to proceed
//         lock.lock();

        try {
            builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
//                Source source = new DOMSource(builder.parse(new ByteArrayInputStream(xmlString.getBytes())));

            validator.validate(new StreamSource(new StringReader(xmlString)));
            map.put("ISVALID", "TRUE");
            logger.info("We have successfuly validated the schema");
        } catch (Exception ioe) {
            ioe.printStackTrace();
            logger.error("NOT2 VALID STRING IS :" + xmlString);
            map.put("MSG", ioe.getMessage());
            //         logger.error("IOException while validating the input XML", ioe);
        }
        logger.info(map);
        long t2 = System.currentTimeMillis();
        logger.info("XML VALIDATION TOOK:::" + (t2 - t1));

    } catch (Exception e) {
        logger.error(e);
    } finally {
        //release lock
//         lock.unlock();
        builder = null;
    }

    return map;
}

Thanks Sunil Kumar Sahoo

like image 498
Sunil Kumar Sahoo Avatar asked Apr 19 '10 07:04

Sunil Kumar Sahoo


1 Answers

I don't think any of the java xml validators are threadsafe. Options are to:

  1. Create a new instance each time you need to validate
  2. Create a pool of validators that you pull from
  3. Make use of ThreadLocal to cache validators
like image 173
Dave Avatar answered Sep 18 '22 12:09

Dave