2011-07-21 54 views
1

我最近解決了problem of the direct streaming with Spring WS,感謝羅的快速響應。訣竅是在Axiom消息工廠中禁用有效負載緩存。我們可以在Spring WS中使用多個消息工廠嗎?

我現在從全球決策的痛苦,我們既可以啓用或禁用對整個系統的有效載荷緩存。我們願與流web服務和一些使驗證(這是不可能的,如果有效載荷禁用緩存)

所以是有可能的特定端點映射到特定消息的工廠?使用web服務客戶端很容易,但端點又如何?有任何想法嗎?

謝謝!

回答

2

你是對的,在客戶端通過將特定的WebServiceMessageFactory實例傳遞給WebServiceTemplate構造函數非常簡單。

總之,我想它也可以在服務器端。 如果你可以使用不同的URL映射,我發現最簡單的方法是基於兩個(或更多)型動物MessageDispatcherServletweb.xml配置。然後,您只需將初始參數傳遞給您喜歡的WebServiceMessageFactory實例的特定bean名稱即可。像這樣:

<servlet> 
    <servlet-name>ws1</servlet-name> 
    <servlet-class>org.springframework.ws.transport.http.MessageDispatcherServlet</servlet-class> 
    <init-param> 
     <param-name>contextConfigLocation</param-name> 
     <param-value>/WEB-INF/conf/ws1-servlet.xml</param-value> 
    </init-param> 
    <init-param> 
     <param-name>messageFactoryBeanName</param-name> 
     <param-value>defaultMessageFactory</param-value> 
    </init-param> 
</servlet> 

<servlet> 
    <servlet-name>ws2</servlet-name> 
    <servlet-class>org.springframework.ws.transport.http.MessageDispatcherServlet</servlet-class> 
    <init-param> 
     <param-name>contextConfigLocation</param-name> 
     <param-value>/WEB-INF/conf/ws2-servlet.xml</param-value> 
    </init-param> 
    <init-param> 
     <param-name>messageFactoryBeanName</param-name> 
     <param-value>streamingMessageFactory</param-value> 
    </init-param> 
</servlet> 

<servlet-mapping> 
    <servlet-name>ws1</servlet-name> 
    <url-pattern>/ws1</url-pattern> 
</servlet-mapping> 

<servlet-mapping> 
    <servlet-name>ws2</servlet-name> 
    <url-pattern>/ws2</url-pattern> 
</servlet-mapping> 

然後,你只需要添加你的不同的消息工廠bean到你的spring-ws上下文中。

希望這會有所幫助。

+0

非常感謝您!我們也找到了這個解決方案,我們不得不加強這一點,以確保並非所有的Web服務都可用於流式傳輸並被阻止。 – codevour

1

如果要在單個端點內切換messagefactory,可以通過擴展WebServiceMessageReceiverHandlerAdapter並覆蓋getMessageFactory方法來實現。

public class SwitchableMessageRecieverHandlerAdapter extends WebServiceMessageReceiverHandlerAdapter{ 

    private WebServiceMessageFactory alternateMessageFactory; 

    private String switchOn; 

    public void setSwitchOn(String switchOn) { 
    this.switchOn = switchOn; 
    } 

    public String getSwitchOn() { 
    return switchOn; 
    } 

    public WebServiceMessageFactory getAlternateMessageFactory() { 
     return alternateMessageFactory; 
    } 

    public void setAlternateMessageFactory(WebServiceMessageFactory alternateMessageFactory) { 
     this.alternateMessageFactory = alternateMessageFactory; 
    } 

    public void afterPropertiesSet() throws Exception { 
     Assert.notNull(alternateMessageFactory, "alternateMessageFactory is required"); 
     Assert.notNull(switchOn, "switchOn parameter is required"); 
    } 

    @Override 
    public WebServiceMessageFactory getMessageFactory() { 
     TransportContext transport = TransportContextHolder.getTransportContext(); 

     if(transport!=null && transport.getConnection() instanceof HttpServletConnection){ 
      HttpServletConnection httpCon = (HttpServletConnection)(transport.getConnection()); 
      if(getSwitchOn().equals(httpCon.getHttpServletRequest().getHeader("soapAction"))){ 
       return alternateMessageFactory; 
      } 
     } 

     return super.getMessageFactory(); 

    } 

} 

現在,通過提供alternateMessageFactory和肥皂動作配置您的上下文文件SwitchableMessageRecieverHandlerAdapter您要使用alternateMessageFactory

<bean id="messageFactory" class="org.springframework.ws.samples.mtom.service.AxiomMtomEnabledSoapMessageFactory"> 
     <property name="payloadCaching" value="true"/> 
     <property name="attachmentCaching" value="true"/> 
    </bean> 

    <bean id="saajMessageFactory" class="org.springframework.ws.soap.saaj.SaajSoapMessageFactory"/> 

    <bean id="switchableMessageReciever" class="SwitchableMessageRecieverHandlerAdapter"> 
     <property name="alternateMessageFactory" ref="saajMessageFactory"/> 
     <property name="switchOn" value="LoadImage"/> 

    </bean> 

現在通知MessageDispatcherServlet使用擴展WebServiceMessageReceiverHandlerAdapter通過添加網絡一個init-PARAM .XML

<servlet> 
    <servlet-name>spring-ws</servlet-name> 
    <servlet-class>org.springframework.ws.transport.http.MessageDispatcherServlet</servlet-class> 
    <init-param> 
     <param-name>transformWsdlLocations</param-name> 
     <param-value>true</param-value> 
    </init-param> 
    <init-param> 
     <param-name>messageReceiverHandlerAdapterBeanName</param-name> 
     <param-value>switchableMessageReciever</param-value> 
    </init-param> 
    </servlet> 
相關問題