Project

Profile

Help

Bug #4216 » XPathTest.java

really starts multiple threads now - Stefan Fritz, 2019-05-10 08:53

 
1
import java.io.StringReader;
2

    
3
import javax.xml.transform.sax.SAXSource;
4
import javax.xml.xpath.XPath;
5
import javax.xml.xpath.XPathConstants;
6
import javax.xml.xpath.XPathExpression;
7
import javax.xml.xpath.XPathFactory;
8

    
9
import org.xml.sax.InputSource;
10

    
11
import net.sf.saxon.lib.FeatureKeys;
12
import net.sf.saxon.om.NodeInfo;
13
import net.sf.saxon.xpath.XPathEvaluator;
14

    
15
public class XPathTest {
16

    
17
        public static void main(String[] args) throws Exception {
18

    
19
                final String xml = "<?xml version=\"1.0\"?> <message>    <headers>       <header name=\"CREATED_BY\"></header>       <header name=\"xx.isMultipart\">true</header>       <header name=\"JMSMessageID\">ID:ffffffffcbed081a:a5ab5a1:16A9D21B12A</header>       <header name=\"JMSExpiration\">0</header>       <header name=\"JMS_acme_ExtendedType\">x-acmeXX-multipart</header>       <header name=\"JMSRedelivered\">false</header>       <header name=\"JMSTimestamp\">1557414392106</header>       <header name=\"JMSDeliveryMode\">2</header>       <header name=\"JMSPriority\">0</header>       <header name=\"JMSDestination\">t.esb.SRIUnsolicitedInboundService1</header>       <header name=\"InboundEvent\">acme.apps.po.publish</header>       <header name=\"CHANNEL\">AUTOMATIC</header>       <header name=\"COUNTRY_CODE\">XX</header>    </headers>    <parts>       <part type=\"text/xml\" id=\"name\"> <WfEvent>    <Name>acme.apps.po.publish</Name>    <Key>acme.apps.po.publish305066</Key>    <Priority>       <test>123</test>       <test>234</test>    </Priority>    <SendDate>2019/05/01 12:21:40</SendDate>    <ReceiveDate>null</ReceiveDate>    <CorrelationId>null</CorrelationId>    <Parameters>              <JMSXDeliveryCount>1</JMSXDeliveryCount>              <JMS_OracleConnectionID>null</JMS_OracleConnectionID>              <JMSXGroupSeq>null</JMSXGroupSeq>              <JMSXAppID>null</JMSXAppID>              <JMSXRecvTimestamp>1556713300401</JMSXRecvTimestamp>              <JMS_OracleExcpQ>null</JMS_OracleExcpQ>              <JMS_OracleHeaderOnly>null</JMS_OracleHeaderOnly>              <JMS_OracleOriginalMessageID>null</JMS_OracleOriginalMessageID>              <JMSXGroupID>null</JMSXGroupID>              <JMSXUserID>null</JMSXUserID>              <JMS_OracleDeliveryMode>null</JMS_OracleDeliveryMode>              <JMS_OracleDelay>0</JMS_OracleDelay>              <JMSXState>0</JMSXState>    </Parameters>    <Data>       <EVENT_DATA>          <ESB_PO_PUB>             <PO_NUMBER>7340017377</PO_NUMBER>             <VENDOR_NUM>205415</VENDOR_NUM>             <VENDOR_SITE_CODE>ISP-FAI-734-NZD</VENDOR_SITE_CODE>             <ES_REQUISITION_LINE_ID>38946630</ES_REQUISITION_LINE_ID>             <QUANTITY>1</QUANTITY>             <UNIT_PRICE>36</UNIT_PRICE>             <CURRENCY_CODE>NZD</CURRENCY_CODE>             <RATE_TYPE/>             <RATE_DATE/>             <RATE/>             <NEED_BY_DATE>01-MAY-19</NEED_BY_DATE>             <ITEM_DESCRIPTION>4970438311 RTU-MEI CASH ACCEPTOR SC66 / SCL6607R</ITEM_DESCRIPTION>             <UNIT_MEAS_LOOKUP_CODE>Each</UNIT_MEAS_LOOKUP_CODE>             <SHIPMENT_NUM>1</SHIPMENT_NUM>             <DISTRIBUTION_NUM>1</DISTRIBUTION_NUM>             <PO_LINE_NUM>1</PO_LINE_NUM>             <CHARGE_ACCOUNT>734-715000000-1525-8871-000-000000-00</CHARGE_ACCOUNT>             <ES_REQUISITION_LINE_NUMBER>1</ES_REQUISITION_LINE_NUMBER>             <ES_REQUISITION_NUMBER>132201</ES_REQUISITION_NUMBER>             <CATEGORY>00000000</CATEGORY>             <ES_INSTANCE>WORLD</ES_INSTANCE>          </ESB_PO_PUB>       </EVENT_DATA>    </Data>    <FromAgent>              <Name>acme_PO_QAGENT</Name>              <System>ER64R06.WORLD</System>    </FromAgent> </WfEvent> </part>    </parts> </message> ";
20
                final String expression = "/message/headers/header [@name=\"COUNTRY_CODE\"]=\"GB\" or /message/headers/header [@name=\"COUNTRY_CODE\"]=\"IE\" or /message/headers/header [@name=\"COUNTRY_CODE\"]=\"IN\" or /message/headers/header [@name=\"COUNTRY_CODE\"]=\"BE\" or /message/headers/header [@name=\"COUNTRY_CODE\"]=\"JP\"";
21

    
22
                // XPathFactory xpathFactory =
23
                // XPathFactory.newInstance(XPathConstants.DOM_OBJECT_MODEL);
24
                XPathFactory xpathFactory = new net.sf.saxon.xpath.XPathFactoryImpl();
25
                XPath xpath = xpathFactory.newXPath();
26
                // xpath.setNamespaceContext(nc);
27
                final XPathEvaluator xpathEvaluator_ = (XPathEvaluator) xpath;
28
                
29
                //xpathEvaluator_.getConfiguration().setConfigurationProperty(FeatureKeys.OPTIMIZATION_LEVEL, "-k");
30

    
31
                for (int i = 0; i < 20; i++) {
32

    
33
                        new Thread() {
34
                                
35
                                
36
                                
37
                                public void run() {
38

    
39
                                        try {
40
                                                for (int c = 0; c < 10000; c++) {
41
                                                        InputSource source = new InputSource(new StringReader(xml));
42
                                                        XPathExpression xpathExpression = xpathEvaluator_.compile(expression);
43
                                                        NodeInfo doc = xpathEvaluator_.getConfiguration().buildDocumentTree(new SAXSource(source))
44
                                                                        .getRootNode();
45
                                                        Boolean result = (Boolean) xpathExpression.evaluate(doc, XPathConstants.BOOLEAN);
46

    
47
                                                        // This seems to reduce/solve it but not sure why
48
                                                        // Runtime.getRuntime().gc();
49
                                                        // ran it with -Xmx1g -XX:+UseG1GC                                                         
50
                                                        System.out.println(this.getName() + " -- "+c +": "+ Runtime.getRuntime().totalMemory()/1024/1000);
51
                                                        
52
                                                        Thread.sleep(500);
53
                                                        
54
                                                }
55
                                        } catch (Exception e) {
56
                                                e.printStackTrace();
57
                                        }
58

    
59
                                };
60
                        }.start();
61
                }
62

    
63
        }
64
}
(3-3/3)