Spring Remoting (HttpInvoker) and problem with bean creation (scope)
Hello,
I have a service that's exposed by HttpInvoker (org.springframework.remoting.httpinvoker.HttpInvo kerServiceExporter) and a test class with some tests.
applicationContext.xml
Code:
...
<bean id="logRequisicaoService"
class="log.LoggerRequisicaoImpl"/>
..
logRequisicao-servlet.xml
Code:
...
<bean id="httpLogRequisicaoService" class="org.springframework.remoting.httpinvoker.HttpInvokerServiceExporter"
p:service-ref="logRequisicaoService">
<property name="serviceInterface">
<value>log.LoggerRequisicao</value>
</property>
</bean>
<bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<prop key="/logRequisicao">httpLogRequisicaoService</prop>
</props>
</property>
</bean>
..
applicationContext-client.xml
Code:
...
<bean id="logRequisicaoService" class="org.springframework.remoting.httpinvoker.HttpInvokerProxyFactoryBean">
<property name="serviceUrl">
<value>http://localhost:8080/logremoto/logRequisicao</value>
</property>
<property name="serviceInterface">
<value>log.LoggerRequisicao</value>
</property>
</bean>
...
TestClass
Code:
...
@Test
public void test1() {
LoggerRequisicao logger = Utils.getBean("logRequisicaoService");
logger.info(1, "Loading XML Data");
logger.warn(1, "Another message");
logger.closeFile(1);
//assertions
logger.deleteLog(1);
}
@Test
public void test2() {
LoggerRequisicao logger = Utils.getBean("logRequisicaoService");
...
}
The problem is that my class (LoggerRequisicao) isn't thread-safe, so i can't have only one instance shared for all remote invocations. That's where my problems have started. I spent a good time looking for something about and i know i can define another scope for the bean different than singleton, which is the default. I tried different ways:
1)
applicationContext.xml
Code:
...
<bean id="logRequisicaoService"
class="log.LoggerRequisicaoImpl" scope="prototype"/>
..
logRequisicao-servlet.xml
Code:
...
<bean id="httpLogRequisicaoService"
class="org.springframework.remoting.httpinvoker.HttpInvokerServiceExporter"
p:service-ref="logRequisicaoService">
<property name="serviceInterface">
<value>log.LoggerRequisicao</value>
</property>
</bean>
..
Result: Just one instance is created. It's the same behavior when i hadn't defined the bean logRequisicaoService scope as prototype.
2)
applicationContext.xml
Code:
...
<bean id="logRequisicaoService"
class="log.LoggerRequisicaoImpl" scope="prototype"/>
..
logRequisicao-servlet.xml
Code:
...
<bean id="httpLogRequisicaoService"
class="org.springframework.remoting.httpinvoker.HttpInvokerServiceExporter"
p:service-ref="logRequisicaoService" scope="prototype">
<property name="serviceInterface">
<value>log.LoggerRequisicao</value>
</property>
</bean>
..
Result: One instance is created for each method call. For example:
Code:
LoggerRequisicao logger = Utils.getBean("logRequisicaoService");
logger.info(1, "Loading XML Data");
logger.warn(1, "Another message");
logger.closeFile(1);
3 or 4 instances would be created.
What i really need is that 1 instance was created by each getBean called. In the Test Class, 1 instance per test. I really researched about it and tested different ways, but i couldn't be able to make it works.
Does somebody have any idea of how to solve that problem?I really need to solve that and i don't know how....:confused:
Thank you veru much.
Luciano
Ps.: Sorry for the English