gpt4 book ai didi

java - 调用webservice应该是同一个线程还是单独的线程?

转载 作者:行者123 更新时间:2023-12-01 10:59:58 25 4
gpt4 key购买 nike

下面是一个套接字程序代码片段。如果有数据,我首先要做的就是在使用 dbconn1 的第一部分中进行处理。之后,根据收到的数据,我得到了一个变量 callWebService,如果将其设置为 callWebService=1,那么我有一个 if 语句来建立另一个新连接并调用外部 Web 服务。我已经将它们分开,因为有时网络服务会关闭并失败,导致我的整个第一部分停止。我不确定这是正确的方法还是应该将部分部分完全拆分成一个单独的线程?哪个处理起来更有效,因为我注意到当网络服务关闭时,它会导致我的资源上升,导致数据库连接被阻止

BufferedWriter writer1 = null; 
Connection dbconn1 = null;
Connection dbconn2 = null;
public void run() { // etc
writer1 = null;
String message="";
BufferedReader reader1 = null;
try {

writer1 = new BufferedWriter(new OutputStreamWriter(receivedSocketConn1.getOutputStream()));
reader1 = new BufferedReader(new InputStreamReader(receivedSocketConn1.getInputStream()));
receivedSocketConn1.setSoTimeout(60000);
int nextChar=0;
int callWebService=0;
while ((nextChar=reader1.read()) != -1) {
message += (char) nextChar;
if (nextChar == '*'){

try{
System.out.println("\n\n Trying establish a new db connection ");
dbconn1 = connectionPool.getConnection();
dbconn1.setAutoCommit(false);
System.out.println("\n\n Checking db connection status "+dbconn1.isClosed());
if ((dbconn1 == null) || dbconn1.isClosed()) {
System.out.println("\n\n db connection status is closed");
dbconn1 = connectionPool.getConnection();
dbconn1.setAutoCommit(false);
//other codes follow here.

// e.g. the callWebService=1;
dbconn1.commit();
}
}
catch (SQLException ex){
System.out.println("Error SQL Exception : "+ex.toString());
ex.printStackTrace(System.out);
try{
dbconn1.rollback();
}
catch (Exception rollback) {
System.out.println("\nRollback dbconn1 :");
rollback.printStackTrace(System.out);
}
}
catch (Exception e){
System.out.println("\nSQL Error here :");
e.printStackTrace(System.out);
try{
dbconn1.rollback();
}
catch (Exception rollback) {
System.out.println("\nRollback dbconn1 :");
rollback.printStackTrace(System.out);
}
}
finally{
try {

if ( dbconn1 != null ) {
dbconn1.close();
System.out.println("\n\n dbConn1 is being closed");
}

}
catch(SQLException ex){
System.out.println("SQLException has been caught for dbConn1 close");
ex.printStackTrace();
}

if(callWebService==1){

try{
System.out.println("\n\n Trying establish a new db connection ");
dbconn2 = connectionPool.getConnection();
dbconn2.setAutoCommit(false);
sendIncomingData(dataID, dataString)
dbconn2.commit();
}
catch (SQLException ex){
System.out.println("Error SQL Exception : "+ex.toString());
ex.printStackTrace(System.out);
try{
dbconn2.rollback();
}
catch (Exception rollback) {
System.out.println("\nRollback dbconn2 :");
rollback.printStackTrace(System.out);
}
}
catch (Exception e){
System.out.println("\nSQL Error here :");
e.printStackTrace(System.out);
try{
dbconn2.rollback();
}
catch (Exception rollback) {
System.out.println("\nRollback dbconn2 :");
rollback.printStackTrace(System.out);
}
}
finally{
try {

if ( dbconn2 != null ) {
dbconn2.close();
System.out.println("\n\n dbConn2 is being closed");
}

}
catch(SQLException ex){
System.out.println("SQLException has been caught for dbConn1 close");
ex.printStackTrace();
}
}



}

}
}


}
catch (SocketTimeoutException ex){
System.out.println("SocketTimeoutException has been caught ");
ex.printStackTrace();
}
catch (IOException ex) {
System.out.println("IOException has been caught ");
ex.printStackTrace();
}
catch (Exception ex) {

System.out.println("Exception has been caught");
ex.printStackTrace(System.out);
}
finally{

try {
if (writer1 != null ) {
writer1.close();
}
}
catch(IOException ex){
System.out.println("IOException has been caught for finally");
ex.printStackTrace(System.out);
}
}
}

void sendIncomingData(int dataID, String dataString) throws Exception {

try{
SOAPConnectionFactory soapConnectionFactory = SOAPConnectionFactory.newInstance();
SOAPConnection soapConnection = soapConnectionFactory.createConnection();
String url = "http://**********/webservice.asmx?WSDL";
SOAPMessage soapACK = soapConnection.call(soapCalling(dataID,dataString), url);
printSOAP(soapACK,dataID); //do some sql insert/update in this function
}
catch (Exception e){
e.printStackTrace();
System.err.println(e.toString());
throw e;
}

}
SOAPMessage createSOAPIncoming(int dataID,String dataString) throws Exception {
MessageFactory messageFactory = MessageFactory.newInstance();
SOAPMessage soapMessage = messageFactory.createMessage();
SOAPPart soapPart = soapMessage.getSOAPPart();

String serverURI = "http://*******";

// SOAP Envelope
SOAPEnvelope envelope = soapPart.getEnvelope();
SOAPBody body = envelope.getBody();
SOAPBodyElement element = body.addBodyElement(envelope.createName("*******"));
element.addChildElement("dataID").addTextNode(dataID);
element.addChildElement("dataString").addTextNode(dataString);
MimeHeaders headers = soapMessage.getMimeHeaders();
soapMessage.saveChanges();

/* Print the request message */
System.out.print("\n\n Request SOAP Message To ACK FOR = ");
soapMessage.writeTo(System.out);
System.out.print("\n\n After Print = ");
System.out.println();

return soapMessage;
}

如果网络服务关闭,我经常会收到此错误。

SEVERE: SAAJ0537: Invalid Content-Type. Could be an error message instead of a SOAP message
com.sun.xml.internal.messaging.saaj.SOAPExceptionImpl: com.sun.xml.internal.messaging.saaj.SOAPExceptionImpl: Invalid Content-Type:text/html. Is this an error message instead of a SOAP response?
at com.sun.xml.internal.messaging.saaj.client.p2p.HttpSOAPConnection.call(HttpSOAPConnection.java:148)
at sk1$ConnectionHandler.sendIncomingData(sk1.java:2753)
at sk1$ConnectionHandler.run(sk1.java:2057)
at java.lang.Thread.run(Thread.java:722)
Caused by: com.sun.xml.internal.messaging.saaj.SOAPExceptionImpl: Invalid Content-Type:text/html. Is this an error message instead of a SOAP response?
at com.sun.xml.internal.messaging.saaj.soap.MessageImpl.identifyContentType(MessageImpl.java:649)
at com.sun.xml.internal.messaging.saaj.soap.MessageFactoryImpl.createMessage(MessageFactoryImpl.java:85)
at com.sun.xml.internal.messaging.saaj.client.p2p.HttpSOAPConnection.post(HttpSOAPConnection.java:327)
at com.sun.xml.internal.messaging.saaj.client.p2p.HttpSOAPConnection.call(HttpSOAPConnection.java:144)

最佳答案

问题的解决方案不是多线程,而是业务逻辑的正确实现。

  • 如果您正在执行的 Web 服务调用和数据库操作在功能上是独立的,那么您可以将功能分成独立的 block 并释放连接或在另一个线程中实现该功能。
  • 如果 Web 服务的调用和数据库内容是相互依赖的,那么您必须找到一种方法来捕获 Web 服务关闭的情况,处理该情况并释放连接。

关于java - 调用webservice应该是同一个线程还是单独的线程?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33419927/

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