我创建了一个spring boot webservice(provider)应用程序。我正在请求中传递timewait选项。如果timewait = n的值,我必须将响应编程延迟那么多时间,即n秒/毫秒。有没有办法可以做到这一点。 XSD给出如下:
<xs:element name="asyncServerRequest">
<xs:complexType>
<xs:sequence>
<xs:element name="header" type="xs:string"/>
<xs:element name="BSID" type="xs:string"></xs:element>
<xs:element name="encodedString" type="xs:string"></xs:element>
<xs:element name="timewait" type="xs:int"></xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="asyncServerResponse">
<xs:complexType>
<xs:sequence>
<xs:element name="responsecode" type="xs:int"/>
<xs:element name="reponsemessage" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
答案 0 :(得分:1)
您可以使用Thread.sleep(n)
。
我认为&#34; n&#34;值以秒为单位。
Integer seconds = n;
try {
Thread.sleep(seconds*1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//return your response here
如果你想集中这个&#34;延迟&#34;代码,然后您可以创建一个 DelayFilter ,如下所示。
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class DelayFilter implements Filter {
private static final String DELAY_PARAMETER = "n";
public void destroy() {
// TODO Auto-generated method stub
}
public void doFilter(ServletRequest req, ServletResponse resp,
FilterChain chain) throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest) req;
//get delay value (DELAY_PARAMETER or n) from your request
String delay = request.getParameter(DELAY_PARAMETER);
if (delay!=null && !"".equals(delay)) {
Integer seconds = Integer.valueOf(delay);
try {
Thread.sleep(seconds*1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
HttpServletResponse response = (HttpServletResponse) resp;
response.setHeader("Cache-Control", "no-cache, must-revalidate");
chain.doFilter(req, resp);
}
public void init(FilterConfig arg0) throws ServletException {
// TODO Auto-generated method stub
}
}