2011-05-21 50 views
0

我正在編寫Apache CXF Web服務並使用Spring加載我的bean。我唯一的bean從Java調用外部進程(MATLAB)。我的豆定義看起來如下:我MatlabEngine豆加載多次的Spring beans

<bean id="matlabEngine" class="org.burch.pca.matlab.MatlabEngine" 
    init-method="start" scope="singleton"> 
    <constructor-arg value="${matlab.engine.path}"></constructor-arg> 
</bean> 

<bean 
    class="org.springframework.web.context.support.ServletContextPropertyPlaceholderConfigurer"> 
    <property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE" /> 
    <property name="searchContextAttributes" value="true" /> 
    <property name="contextOverride" value="true" /> 
    <property name="ignoreResourceNotFound" value="true" /> 
    <property name="locations"> 
     <list> 
      <value>classpath:/pca-engine.properties</value> 
     </list> 
    </property> 
</bean> 

件,如下是:

/** 
* Path to MATLAB engine. 
*/ 
private String pathToEngine; 

public MatlabEngine(String pathToEngine) throws MatlabConnectionException, MatlabInvocationException{ 
    super(); 
    setPathToEngine(pathToEngine); 
} 

/** 
* Starts engine and goes to path defined by argument 
* @param pathToEngine 
* @throws MatlabConnectionException 
* @throws MatlabInvocationException 
*/ 
public void start() throws MatlabConnectionException, MatlabInvocationException{ 
    //Create a factory 
    RemoteMatlabProxyFactory factory = new RemoteMatlabProxyFactory(); 

    //Get a proxy, launching MATLAB in the process 
    proxy = factory.getProxy(); 

    //Display welcoming messages in MATLAB Command Window 
    proxy.eval(MatlabCommandsRegistry.disp(MATLAB_ENGINE_WELCOME_1)); 
    proxy.eval(MatlabCommandsRegistry.disp(MATLAB_ENGINE_WELCOME_2)); 

    if(pathToEngine!= null && !"".equals(pathToEngine)){ 
     logM("Switching to engine directory..."); 
     String goToEngineRootDir = MatlabCommandsRegistry.cd(pathToEngine); 
     proxy.eval(goToEngineRootDir); 
     logM("Sucessfully changed engine dir to "+pathToEngine); 
    } 
} 

當我部署的Tomcat Web服務,它很好地帶來了MATLAB程序(豆被加載)。

但是,當我創建客戶端請求的Web服務端點使用此代碼:

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

    JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean(); 

    factory.getInInterceptors().add(new LoggingInInterceptor()); 
    factory.getOutInterceptors().add(new LoggingOutInterceptor()); 
    factory.setServiceClass(UploadService.class); 
    factory.setAddress("http://localhost:8080/auth-ws-1.0.0/services/upload"); 
    UploadService client = (UploadService) factory.create(); 

    UploadEntity resume=new UploadEntity(); 
    resume.setFileName("Image490"); 
    resume.setFileType("jpg"); 

    //Work arround data handler.... 
    DataSource source = new FileDataSource(new File("C:\\Users\\Pictures\\thumb.png")); 
    DataHandler dataHandle = new DataHandler(source); 
    ByteArrayOutputStream stream = new ByteArrayOutputStream(); 
    dataHandle.writeTo(stream); 
    resume.setPayload(stream.toByteArray()); 
    client.uploadFile(resume); 
    System.exit(0); 

} 

我的服務器,帶出MATLAB程序的新實例(豆被加載再次 - 非常重,不良)。我能做些什麼來讓只有一個bean用來處理所有的處理和所有請求?我是Spring的新手,我想我的問題是我在這裏處理多個上下文。我希望他們共享一個singleton bean的單個實例,但不知道如何管理它。

謝謝你的時間!

+0

你確實需要ServletContextPropertyPlaceholderConfigurer類的功能嗎?如果您不試圖在web.xml中解析上下文參數,則可以使用PropertyPlaceholderConfigurer類。 – MetroidFan2002 2011-05-21 16:50:33

回答

1

你應該爲你的bean啓用單例模式。

看看這個:http://static.springsource.org/spring/docs/1.2.x/reference/beans.html#beans-factory-modes

豆defenition 5月看起來是這樣的:

<!-- Spring property loading bean --> 
<bean 
    class="org.springframework.web.context.support.ServletContextPropertyPlaceholderConfigurer" singleton="true"> 
    <property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE" /> 
    <property name="searchContextAttributes" value="true" /> 
    <property name="contextOverride" value="true" /> 
    <property name="ignoreResourceNotFound" value="true" /> 
    <property name="locations"> 
     <list> 
      <value>classpath:/pca-engine.properties</value> 
     </list> 
    </property> 
</bean> 

我想你應該管理MATLAB程序lifecircle明智地減少資源的負荷。

+0

默認情況下,Spring bean是單身人士。我意識到我的Spring bean沒有連接到CXF Web服務端點後解決了問題。現在我只有一個MATLAB引擎bean的實例,它工作的很好。 – Zec 2011-05-22 12:52:27