2011-05-21 68 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