axis-java-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Sebastian J. (JIRA)" <j...@apache.org>
Subject [jira] Commented: (AXIS2-1436) In JAXB RI, construction of JAXBContext is expensive - only do it once.
Date Wed, 05 Aug 2009 14:13:15 GMT

    [ https://issues.apache.org/jira/browse/AXIS2-1436?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=12739514#action_12739514
] 

Sebastian J. commented on AXIS2-1436:
-------------------------------------

Hopefully someone listens:

Why is a JAXBContext created for every single operation?
According to https://jaxb.dev.java.net/guide/Performance_and_thread_safety.html only one JAXBContext
should be created (per application).


> In JAXB RI, construction of JAXBContext is expensive - only do it once.
> -----------------------------------------------------------------------
>
>                 Key: AXIS2-1436
>                 URL: https://issues.apache.org/jira/browse/AXIS2-1436
>             Project: Axis 2.0 (Axis2)
>          Issue Type: Improvement
>          Components: databinding
>    Affects Versions: 1.1
>            Reporter: Chris McClelland
>            Priority: Minor
>
> I propose a patch to:
> http://svn.apache.org/viewvc/webservices/axis2/branches/java/1_1/modules/jaxbri/src/org/apache/axis2/jaxbri/template/JaxbRIDatabindingTemplate.xsl?revision=464450&content-type=text%2Fplain&pathrev=464450
> in order to achieve this. The idea is to keep just one static final copy of the JAXBContext
for each type, with name mangling to replace the "." with "_". So for each class (e.g, com.foo.xsd.Echo)...
> private static final javax.xml.bind.JAXBContext com_foo_xsd_Echo;
> private static final java.util.HashMap<Class, javax.xml.bind.JAXBContext> classContextMap
=
>     new java.util.HashMap<Class, javax.xml.bind.JAXBContext>();
> static {
>     javax.xml.bind.JAXBContext jc;
>     jc = null;
>     try {
>         jc = javax.xml.bind.JAXBContext.newInstance(com.foo.xsd.Echo.class);
>     }
>     catch ( javax.xml.bind.JAXBException ex ) {
>         // Should never happen
>         System.err.println("Unable to create JAXBContext for class: com.foo.xsd.Echo");
>         Runtime.getRuntime().exit(-1);
>     }
>     finally {
>         com_foo_xsd_Echo = jc;
>         classContextMap.put(com.foo.xsd.Echo.class, jc);
>     }
> }
> I'm fairly sure that the JAXBException above will never be thrown under "well-configured"
circumstances. Even so, forcing the JVM to quit might be a bit extreme. Suggestions welcome!
> Patch follows...
> --- old/org/apache/axis2/jaxbri/template/JaxbRIDatabindingTemplate.xsl  2006-10-17 23:01:21.000000000
+0100
> +++ new/org/apache/axis2/jaxbri/template/JaxbRIDatabindingTemplate.xsl  2006-10-17 23:06:13.000000000
+0100
> @@ -17,10 +17,38 @@
>  
>          <xsl:for-each select="param[not(@type = preceding-sibling::param/@type)]">
>              <xsl:if test="@type!=''">
> +                private static final javax.xml.bind.JAXBContext <xsl:value-of select="translate(@type,'.','_')"/>;
> +            </xsl:if>
> +        </xsl:for-each>
> +
> +        private static final java.util.HashMap&lt;Class,javax.xml.bind.JAXBContext&gt;
classContextMap = new java.util.HashMap&lt;Class,javax.xml.bind.JAXBContext&gt;();
> +
> +        static {
> +            javax.xml.bind.JAXBContext jc;
> +            <xsl:for-each select="param[not(@type = preceding-sibling::param/@type)]">
> +                <xsl:if test="@type!=''">
> +                    jc = null;
> +                    try {
> +                        jc = javax.xml.bind.JAXBContext.newInstance(<xsl:value-of
select="@type"/>.class);
> +                    }
> +                    catch ( javax.xml.bind.JAXBException ex ) {
> +                        System.err.println("Unable to create JAXBContext for class:
<xsl:value-of select='@type'/>");
> +                        Runtime.getRuntime().exit(-1);
> +                    }
> +                    finally {
> +                        <xsl:value-of select="translate(@type,'.','_')"/> = jc;
> +                        classContextMap.put(<xsl:value-of select="@type"/>.class,
jc);
> +                    }
> +                </xsl:if>
> +            </xsl:for-each>
> +        }
> +
> +        <xsl:for-each select="param[not(@type = preceding-sibling::param/@type)]">
> +            <xsl:if test="@type!=''">
>  
>                  private org.apache.axiom.om.OMElement toOM(<xsl:value-of select="@type"/>
param, org.apache.axiom.soap.SOAPFactory factory, boolean optimizeContent) {
>                      try {
> -                        javax.xml.bind.JAXBContext context = javax.xml.bind.JAXBContext.newInstance(<xsl:value-of
select="@type"/>.class);
> +                        javax.xml.bind.JAXBContext context = <xsl:value-of select="translate(@type,'.','_')"/>;
>                          javax.xml.bind.Marshaller marshaller = context.createMarshaller();
>                          marshaller.setProperty(javax.xml.bind.Marshaller.JAXB_FRAGMENT,
Boolean.TRUE);            
>          
> @@ -56,7 +84,7 @@
>              java.lang.Class type,
>              java.util.Map extraNamespaces) {
>              try {
> -                javax.xml.bind.JAXBContext context = javax.xml.bind.JAXBContext.newInstance(
type );
> +                javax.xml.bind.JAXBContext context = classContextMap.get(type);
>                  javax.xml.bind.Unmarshaller unmarshaller = context.createUnmarshaller();
>  
>                  return unmarshaller.unmarshal(param.getXMLStreamReader(), type).getValue();
> @@ -119,7 +147,7 @@
>  
>              public javax.xml.stream.XMLStreamReader getReader() throws javax.xml.stream.XMLStreamException
{
>                  try {
> -                    javax.xml.bind.JAXBContext context = javax.xml.bind.JAXBContext.newInstance(outClazz);
> +                    javax.xml.bind.JAXBContext context = classContextMap.get(outClazz);
>                      org.apache.axiom.om.impl.builder.SAXOMBuilder builder = new org.apache.axiom.om.impl.builder.SAXOMBuilder();
>                      javax.xml.bind.Marshaller marshaller = context.createMarshaller();
>                      marshaller.marshal(outObject, builder);

-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.


Mime
View raw message