我已经完成了很多线程,但没有得到关于如何在任何Web服务调用上实现线程池的具体解决方案,我在其中创建了一个REST服务。
只是提出一个高层次的想法 -
1)通过servlet调用Web服务 2)如果先前的请求没有完全处理并返回响应,则每个请求servlet命中Web服务的请求都应该被放入队列 3)假设应用服务器在8080端口上启动并运行,我很难实现线程池给出错误 - java.net.BindException:已在使用的地址:JVM_Bind 4)我不确定如何在servlet中将请求放入队列中进行Web服务调用时端到端地处理它
以下是代码说明 -
1)使用doPost()调用我的REST服务的servlet类
HttpClient httpClient = HttpClientBuilder.create().build();
String requrl = "http://myappserver:8080/TestApp/test/testExecute.service";
String jsonvalue = "{\"param1\":\"value1"," +
"\"param2\":\"value2\"}";
LOG.info("json value: " + jsonvalue);
HttpPost postRequest = new HttpPost(requrl);
String message = jsonvalue;
StringEntity input = new StringEntity(message);
input.setContentType("application/json");
postRequest.setEntity(input);
HttpResponse response = httpClient.execute(postRequest);
2)网络服务 -
import java.io.Serializable;
import org.codehaus.jackson.annotate.JsonPropertyOrder;
import org.codehaus.jackson.annotate.JsonWriteNullProperties;
import org.codehaus.jackson.map.annotate.JsonSerialize;
@JsonWriteNullProperties(false)
@JsonPropertyOrder({
"param1",
"param2"
})
public class TestExecuteRequest implements Serializable {
private static final long serialVersionUID = 1L;
@JsonSerialize(include=JsonSerialize.Inclusion.ALWAYS)
protected String param1;
@JsonSerialize(include=JsonSerialize.Inclusion.ALWAYS)
protected String param2;
/**
* @return the param1
*/
public String getParam1() {
return param1;
}
/**
* @param param1 the param1 to set
*/
public void setParam1(String param1) {
this.param1 = param1;
}
/**
* @return the param2
*/
public String getParam2() {
return param2;
}
/**
* @param param2 the param2 to set
*/
public void setParam2(String param2) {
this.param2 = param2;
}
}
Class having web service definition -
import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Response;
import org.jboss.resteasy.annotations.providers.jaxb.Formatted;
@Path("test")
public interface TestFrame {
@POST
@Consumes("application/json")
@Formatted
@Produces("application/json")
@Path("/testExecute.service")
Response executeTestSuite(TestExecuteRequest testExeRequest);
}
Web service performing some operations in TestService class
public class TestService implements TestFrame {
// Logic to perform all operations after web service has been called
}
任何人都可以帮助我,在上述任何类中编写线程池的位置,以便在servlet中调用上述Web服务时可以一个接一个地处理请求吗?
非常感谢您的帮助。
谢谢你, MN
答案 0 :(得分:0)
尝试首先解决与线程无关的“已使用端口”错误。
我很难实现线程池给出错误 - java.net.BindException:已经在使用的地址:JVM_Bind
这意味着您的端口已被其他进程使用。您可以使用netstat命令查看正在使用您的端口的进程。如果你在Linux机器上试试这个
netstat -annp | grep 8080
请注意,-p标志将为您提供使用8080端口的进程ID。