gpt4 book ai didi

java - 从在 Tomcat 中运行的 Web 应用程序写入 Socket 时出现 NullPointerException

转载 作者:行者123 更新时间:2023-11-28 23:47:35 25 4
gpt4 key购买 nike

我在写入套接字时遇到了这个奇怪的问题。该应用程序是在 Tomcat 5.5 下运行的 Web 应用程序。所讨论的功能是从 servlet 获取请求,进行一些处理并通过 SSLSocket 将该请求发送到外部系统。

这是我的 MessageProcessor 类:

public class MessageProcessor {

private final int socketTimeout;
private final int threadTimeout;

private final ExecutorService executor = Executors.newCachedThreadPool();

private static class Holder {
static MessageProcessor instance = new MessageProcessor();
}

public static MessageProcessor instance() {
return Holder.instance;
}
static {
instance();
}

private MessageProcessor() {
socketTimeout = /*get from property*/
threadTimeout = /*get from property*/

//Setting Java SSL details
System.setProperty("javax.net.ssl.trustStore") /*from property*/;
System.setProperty("javax.net.ssl.trustStorePassword") /*from property*/;

socketFactory = SSLSocketFactory.getDefault();
}

public String submit(String msg) {
Worker worker = new Worker(msg);
try {
return executor.submit(worker).get(threadTimeout,
TimeUnit.MILLISECONDS);
} catch (Exception e) {
e.printStackTrade();
}
return "";
}

public String submitDirect(String msg) throws Exception {
Worker worker = new Worker(msg);
return worker.call();
}

class Worker implements Callable<String> {
private String message;

public Worker(String requestMsg) {
this.message = requestMsg;
}

public String call() throws Exception {

socket = socketFactory.createSocket();


StringBuilder responseSb = new StringBuilder();
StringBuilder frameMarkerSb = new StringBuilder();
BufferedWriter socketWriter = null;
BufferedReader socketReader = null;

try {

socketWriter =
new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
socketWriter.write(message);
socketWriter.flush();

socketReader =
new BufferedReader(new InputStreamReader(socket.getInputStream(),
"UTF-8"));
/* do something else */
/* receive response */

} catch (InterruptedIOException e) {
//Error handling
} catch (Exception e) {
//Error handling
} finally {
socketWriter.close();
socketReader.close();
socket.close();
}
return response;
}
}
}

我以这种方式向 servlet 中的此 MessageProcessor 提交消息:

protected void doPost(HttpServletRequest req,
HttpServletResponse resp) throws ServletException,
IOException {

String requestStr = req.getParameter("data");
String responseStr = "";

try {
responseStr =
MessageProcessor.instance().submit(requestStr.trim());
responseStr =
MessageProcessor.instance().submitDirect(requestStr.trim());
} catch (Exception e) {
//Error handling
}
resp.setContentType("text/HTML");

resp.getWriter().write(responseStr);
}

问题出在 submit() 上。在 submit() 中,任务被提交到一个缓存的线程池,它抛出了这个异常。在 socketWriter.flush() 处抛出异常。

  2012-09-06 13:17:36,007 [http-8080-1] ERROR processor.MessageProcessor - Error submitting new task
java.util.concurrent.ExecutionException: java.lang.NullPointerException
at java.util.concurrent.FutureTask$Sync.innerGet(FutureTask.java:232)
at java.util.concurrent.FutureTask.get(FutureTask.java:91)
at com.scb.race.bridge.processor.MessageProcessor.submit(MessageProcessor.java:70)
at com.scb.race.bridge.servlet.CheckServlet.doPost(CreditCheckServlet.java:31)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:647)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:269)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:188)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:172)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:117)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:108)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:174)
at org.apache.coyote.http11.Http11AprProcessor.process(Http11AprProcessor.java:843)
at org.apache.coyote.http11.Http11AprProtocol$Http11ConnectionHandler.process(Http11AprProtocol.java:679)
at org.apache.tomcat.util.net.AprEndpoint$Worker.run(AprEndpoint.java:1303)
at java.lang.Thread.run(Thread.java:619)
Caused by: java.lang.NullPointerException
at com.scb.race.bridge.processor.MessageProcessor$MessageWorker.call(MessageProcessor.java:140)
at com.scb.race.bridge.processor.MessageProcessor$MessageWorker.call(MessageProcessor.java:78)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:303)
at java.util.concurrent.FutureTask.run(FutureTask.java:138)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)

如果我使用 submitDirect(),则直接调用 call() 方法而不提交到线程池,并且它工作正常。

我在 JUnit 中测试该类时没有问题。它只发生在 Tomcat 中。

如果有人能对此有所说明,我将不胜感激。对不起,如果我在这里放了很多代码。这让我发疯。

最佳答案

public String submitDirect(String pdcRequestStr) throws Exception {
Worker worker = new Worker(msg);
return worker.call();
}

将 new Worker(msg) 改成 new Worker(pdcRequestStr)

关于java - 从在 Tomcat 中运行的 Web 应用程序写入 Socket 时出现 NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12296058/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com