- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
有人要求我修复位于两个应用程序之间的 Servlet。它的目的是将 SAML 授权请求与 SAML v2.0/SAML 1.1 相互转换。所以它:
不要担心 SAML 问题,问题出在 HTTP 问题上。该代码完成了它的工作,但它在负载下受到很大影响。我通过测试发现,即使代码使用了来自 Apache httpcomponents 的 ThreadSafeClientConnManager
,命中 servlet 的每个请求都是以单线程方式处理的。更准确地说,当代码到达 HTTPClient.execute()
方法时,第一个创建连接的线程将在任何其他线程开始工作之前运行整个过程的其余部分。例如:
HTTPClient.execute()
HTTPClient.execute()
我在下面包含了代码。据我所知,所有必需的元素都已存在。任何人都可以看到任何会阻止此 servlet 同时为多个请求提供服务的错误或遗漏吗?
public class MappingServlet extends HttpServlet {
private HttpClient client;
private String pdp_url;
public void init() throws ServletException {
org.opensaml.Configuration.init();
pdp_url = getInitParameter("pdp_url");
ThreadSafeClientConnManager cm = new ThreadSafeClientConnManager();
HttpRoute route = new HttpRoute(new HttpHost(pdp_url));
cm.setDefaultMaxPerRoute(100);
cm.setMaxForRoute(route, 100);
cm.setMaxTotal(100);
client = new DefaultHttpClient(cm);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
long threadId = Thread.currentThread().getId();
log.debug("[THREAD " + threadId + "] client request received");
// Get the input entity (SAML2)
InputStream in = null;
byte[] query11 = null;
try {
in = request.getInputStream();
query11 = Saml2Requester.convert(in);
log.debug("[THREAD " + threadId + "] client request SAML11:\n" + query11);
} catch (IOException ex) {
log.error("[THREAD " + threadId + "]\n", ex);
return;
} finally {
if (in != null) {
try {
in.close();
} catch (IOException ioe) {
log.error("[THREAD " + threadId + "]\n", ioe);
}
}
}
// Proxy the request to the PDP
HttpPost httpPost = new HttpPost(pdp_url);
ByteArrayEntity entity = new ByteArrayEntity(query11);
httpPost.setEntity(entity);
HttpResponse httpResponse = null;
try {
httpResponse = client.execute(httpPost);
} catch (IOException ioe) {
log.error("[THREAD " + threadId + "]\n", ioe);
httpPost.abort();
return;
}
int sc = httpResponse.getStatusLine().getStatusCode();
if (sc != HttpStatus.SC_OK) {
log.error("[THREAD " + threadId + "] Bad response from PDP: " + sc);
httpPost.abort();
return;
}
// Get the response back from the PDP
InputStream in2 = null;
byte[] resp = null;
try {
HttpEntity entity2 = httpResponse.getEntity();
in2 = entity2.getContent();
resp = Saml2Requester.consumeStream(in2);
EntityUtils.consumeStream(in2);
log.debug("[THREAD " + threadId + "] client response received, SAML11: " + resp);
} catch (IOException ex) {
log.error("[THREAD " + threadId + "]", ex);
httpPost.abort();
return;
} finally {
if (in2 != null) {
try {
in2.close();
} catch (IOException ioe) {
log.error("[THREAD " + threadId + "]", ioe);
}
}
}
// Convert the response from SAML1.1 to SAML2 and send back
ByteArrayInputStream respStream = null;
byte[] resp2 = null;
try {
respStream = new ByteArrayInputStream(resp);
resp2 = Saml2Responder.convert(respStream);
} finally {
if (respStream != null) {
try {
respStream.close();
} catch (IOException ioe) {
log.error("[THREAD " + threadId + "]", ioe);
}
}
}
log.debug("[THREAD " + threadId + "] client response SAML2: " + resp2);
OutputStream os2 = null;
try {
os2 = response.getOutputStream();
os2.write(resp2.getBytes());
log.debug("[THREAD " + threadId + "] client response forwarded");
} catch (IOException ex) {
log.error("[THREAD " + threadId + "]\n", ex);
return;
} finally {
if (os2 != null) {
try {
os2.close();
} catch (IOException ioe) {
log.error("[THREAD " + threadId + "]\n", ioe);
}
}
}
}
public void destroy() {
client.getConnectionManager().shutdown();
super.destroy();
}
}
提前致谢!
最佳答案
HttpClient.execute()
不会返回,直到被调用的服务器发送完所有 http header 。您的代码工作正常。我认为被调用的服务才是真正的瓶颈。我已经为它创建了一个简单的概念验证代码(基于您的代码段):
import java.io.IOException;
import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.StatusLine;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.routing.HttpRoute;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager;
public class MyHttpClient {
private static final String url = "http://localhost:8080/WaitServlet";
private final DefaultHttpClient client;
public MyHttpClient() {
final ThreadSafeClientConnManager cm =
new ThreadSafeClientConnManager();
final HttpRoute route = new HttpRoute(new HttpHost(url));
cm.setDefaultMaxPerRoute(100);
cm.setMaxForRoute(route, 100);
cm.setMaxTotal(100);
client = new DefaultHttpClient(cm);
}
public void doPost() {
final HttpPost httpPost = new HttpPost(url);
HttpResponse httpResponse;
try {
httpResponse = client.execute(httpPost);
} catch (final IOException ioe) {
ioe.printStackTrace();
httpPost.abort();
return;
}
final StatusLine statusLine = httpResponse.getStatusLine();
System.out.println("status: " + statusLine);
final int statusCode = statusLine.getStatusCode();
if (statusCode != HttpStatus.SC_OK) {
httpPost.abort();
return;
}
}
}
还有一个测试:
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import org.junit.Test;
public class HttpClientTest {
@Test
public void test2() throws Exception {
final ExecutorService executorService =
Executors.newFixedThreadPool(16);
final MyHttpClient myHttpClient = new MyHttpClient();
for (int i = 0; i < 8; i++) {
final Runnable runnable = new Runnable() {
@Override
public void run() {
myHttpClient.doPost();
}
};
executorService.execute(runnable);
}
executorService.shutdown();
executorService.awaitTermination(150, TimeUnit.SECONDS);
}
}
最后,调用WaitServlet
:
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class WaitServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
doPost(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
try {
Thread.sleep(30 * 1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
final PrintWriter writer = resp.getWriter();
writer.println("wait end");
}
}
关于java - ThreadSafeClientConnManager 不是多线程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7597447/
我正在使用 ThreadSafeClientConnManager 在 Android 的后台线程中执行同步请求,设置为: HttpParams params = new BasicHttpParam
有人要求我修复位于两个应用程序之间的 Servlet。它的目的是将 SAML 授权请求与 SAML v2.0/SAML 1.1 相互转换。所以它: 从一个应用程序接收 HTTP SAML v2.0 授
我正在使用由 ThreadSafeClientConnManager (Apache httpcomponents 4.1.1) 创建的连接。响应是分块的(我期望),这是由 response.getE
我正在使用 httpcomponents 4.1.2 并且 ThreadSafeClientConnManager 连接不足。与旧的公共(public) HttpClient 不同,似乎没有任何方法可
我无法模拟一个类(使用 PowerMock 的 createMock 方法)。此类正在其构造函数中创建 ThreadSafeClientConnManager 类的对象。我在调用传递我的类名的 cre
对于我当前的应用程序,我从不同的“事件”中收集图像供应商”在西类牙。 Bitmap bmp=null; HttpGet httpRequest = new HttpGet(strURL);
我知道 ThreadSafeClientConnManager 使用连接池,当客户端需要连接时,从中选择一个连接。相反,SingleClientConnManager 仅使用一个连接。 我想了解的是:
我一直在尝试使用 Apache HttpClient (4.1.3) 和 ThreadSafeClientConnManager 实现连接池。当我尝试设置路由的最大连接数时,我遇到了一个问题。基本上我
我正在使用 ThreadSafeClientConnManager管理客户端连接池,因为我的应用程序有多个线程,它们同时连接到网络服务器。 摘要示例代码: HttpClient httpClient;
我用两种不同的方式定义 HttpClient:1. 普通: client = new DefaultHttpClient();2.线程安全: DefaultHttpClient getThreadSa
我正在使用 ThreadSafeClientConnManager 在 Java 应用程序中进行多线程处理。我的 ThreadSafeClientConnManager 对象是静态的,因此它会一直保留
对于 ThreadSafeClientConnManager.requestConnection(HttpRoute route, Object state),第二项“state”应该是什么? 最终,
HttpParams params = new BasicHttpParams(); ConnManagerParams.setMaxConnectionsPerRoute(params, n
我的log4j 属性文件 log4j.logger.devpinoyLogger=DEBUG, dest1, log4j.appender.dest1=org.apache.log4j.Rolling
我正在将我的代码移植到 android 6.0。由于 apache 类已在 API 23 中弃用并删除,因此我无法找到与之前的代码完全匹配的代码来建立 HTTPS 连接。以下是我的代码 try {
我为我的 android 应用程序集成了 twilio api,但是每当运行我的 android 应用程序时我都会遇到以下错误。 java.lang.NoSuchMethodError:org.apa
我是一名优秀的程序员,十分优秀!