Spring SOAP端点 - 缓存JaxbContext

时间:2015-07-22 06:52:42

标签: java spring web-services soap spring-ws

我写了一个简单的SOAP端点,主要是按照这里的Spring教程:https://spring.io/guides/gs/producing-web-service/

下面是用于拦截请求的类(假设注入了存储库对象):

@Endpoint
public class SampleEndpoint {

  @PayloadRoot(namespace = NAMESPACE_URI, localPart = "SampleRequest")
  public
  @ResponsePayload
  JAXBElement<SampleResponseType> sampleQuery(
        @RequestPayload JAXBElement<SampleRequestType> request) {

    ObjectFactory factory = new ObjectFactory();
    SampleResponseType response = repository.query(request.getValue());
    JAXBElement<SampleResponseType> jaxbResponse = factory.createSampleResponse(response);
    return jaxbResponse;

  }

}

该服务正确执行。我遇到的一个问题是性能,尤其是在解组响应时。平均而言,将对象解组为XML响应需要几秒钟。有没有办法缓存/注入JaxbContext Spring正在使用这个过程来改进这个时间?

这是我用于此端点的Web服务配置文件。我曾尝试在Saaj和Axiom消息工厂之间进行交替,但没有看到很多性能变化:

@EnableWs
@Configuration
public class WebServiceConfig extends WsConfigurerAdapter {

@Bean
public SaajSoapMessageFactory soap12MessageFactory() {
    SaajSoapMessageFactory factory = new SaajSoapMessageFactory();
    factory.setSoapVersion(SoapVersion.SOAP_12);
    return factory;
}

@Bean
public AxiomSoapMessageFactory axiomSoapMessageFactory() {
    AxiomSoapMessageFactory factory = new AxiomSoapMessageFactory();
    factory.setSoapVersion(SoapVersion.SOAP_12);
    factory.setPayloadCaching(false);
    return factory;
}

@Bean
public ServletRegistrationBean dispatcherServlet(
        ApplicationContext applicationContext) {
    MessageDispatcherServlet servlet = new MessageDispatcherServlet();
    servlet.setApplicationContext(applicationContext);
    servlet.setTransformWsdlLocations(true);
    servlet.setMessageFactoryBeanName("soap12MessageFactory");
    return new ServletRegistrationBean(servlet, "/ws/*");
}

@Bean(name = "wsdlname")
public DefaultWsdl11Definition xcpdDefaultXcpdWsdl11Definition(XsdSchema
     schema) {
    DefaultWsdl11Definition wsdl11Definition = new DefaultWsdl11Definition();
    wsdl11Definition.setCreateSoap11Binding(false);
    wsdl11Definition.setCreateSoap12Binding(true);
    wsdl11Definition.setPortTypeName("xcpdPort");
    wsdl11Definition.setLocationUri("/ws");
    wsdl11Definition
            .setTargetNamespace("http://somenamespace.org/");
    wsdl11Definition.setSchema(schema);
    return wsdl11Definition;
}

@Bean
public XsdSchema schema() {
    return new SimpleXsdSchema(
            new ClassPathResource(
                    "schema.xsd"));
}
}

1 个答案:

答案 0 :(得分:0)

我们两天前就解决了这个问题。我们看到的第一个问题是JVM的客户端版本安装在服务器上。我安装了JVM的服务器版本并更改了Tomcat以使用该实例。一些Web服务的性能得到了显着改善。以前简单的请求需要三秒钟,现在它们需要250毫秒。但是在测试我写的Spring服务时,我没有看到太多改进。

为解决上一个问题,我在Tomcat的Java选项中添加了以下内容:

-Dcom.sun.xml.internal.bind.v2.runtime.JAXBContextImpl.fastBoot=true

重新启动Tomcat实例后,服务的响应时间现在都低于一秒。之前的请求最多需要30秒才能完成。我们使用的服务器JRE如下:

http://www.oracle.com/technetwork/java/javase/downloads/server-jre8-downloads-2133154.html