我有REST服务
@Path("/rest")
@Component
public class MyRestService
{
@Inject
private MyBean bean;
@GET
@Path("/do")
public String start()
{
this.logger.info("Before do " + Thread.currentThread().getId());
String result = this.bean.do();
this.logger.info("After do " + Thread.currentThread().getId());
return result;
}
}
调用注入Spring单例bean的方法(里面有一些状态)
@Service
public class MyBean
{
public String do()
{
// do something big...
}
}
当我在浏览器中调用“... / rest / do”时,第一次调用按预期进行,但如果我在另一个选项卡中进行相同的调用,则此调用将等待,直到第一次调用完成以在同一线程中处理第二次调用。
如果我做第二次调用为“... / rest / do?async = true”它不等待并在新线程中处理第二个请求,但是如果我同时执行两个请求“... / rest / do? async = true“ - 第二个再次等待第一个完成。
这种行为可能是什么原因?实际上是预期的吗?
我的web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version="2.5">
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
<display-name>My REst</display-name>
<!-- spring configuration by annotations -->
<context-param>
<param-name>contextClass</param-name>
<param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext</param-value>
</context-param>
<!-- spring configuration class -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>app.Config</param-value>
</context-param>
<!-- to return data according to extension -->
<context-param>
<param-name>resteasy.media.type.mappings</param-name>
<param-value>json : application/json, xml : application/xml</param-value>
</context-param>
<!-- this has to match with resteasy-servlet url-pattern -->
<context-param>
<param-name>resteasy.servlet.mapping.prefix</param-name>
<param-value>/rest</param-value>
</context-param>
<listener>
<listener-class>org.jboss.resteasy.plugins.server.servlet.ResteasyBootstrap</listener-class>
</listener>
<!-- resteasy spring connector (to use DI in rest-resources) -->
<listener>
<listener-class>org.jboss.resteasy.plugins.spring.SpringContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>resteasy-servlet</servlet-name>
<servlet-class>org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher</servlet-class>
</servlet>
<!-- this has to match with resteasy-servlet url-pattern -->
<servlet-mapping>
<servlet-name>resteasy-servlet</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
<!-- Bind Jboss's TransactionManager (EntityManagerFactory) to JNDI -->
<persistence-unit-ref>
<persistence-unit-ref-name>persistence/ReferenceDataDS</persistence-unit-ref-name>
<persistence-unit-name>ReferenceDataDS</persistence-unit-name>
</persistence-unit-ref>
</web-app>
答案 0 :(得分:1)
您可以在调试模式下解决运行应用程序服务器的此问题。用两个不同的客户进行测试,例如Firefox,Chrome,IE,Opera,...等待答案暂停所有线程。
答案 1 :(得分:0)
由于我只花了整整一天的时间来解决这个确切的问题(没有意识到更改的参数会导致并行执行):
罪魁祸首是浏览器。
当URL略有不同时,所有它们都通过添加不同的片段或参数并行执行它们。