0
下面我有一个套接字程序代码片段。我在做什么,如果有数据,那么我在第一部分中使用dbconn1进行处理。之后根据收到的数据我得到了一个变量callWebService,如果这被设置为调用WebService = 1,那么我有一个if语句来创建另一个新的连接和调用和外部web服务。由于有时Web服务停止运行,导致我的整个第一部分停止运行,所以我将它们分开。我不知道这是一个正确的方法吗?还是应该将该部分分成一个单独的线程?因为我注意到当WebService下跌它引起我的资源上去导致数据库连接是托起调用webservice应该是相同的线程或单独的线程?
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;
}
很多时候我从WebService如果下得到这个错误,这是更有效的处理。
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服务链接中的不确定性。那么您建议抓住web服务下来的情况处理情况并释放连接? – user5313398
我已经更新了我的问题与从web服务的经常错误消息?所以我设置了一个Web服务调用的时间? connection.setConnectTimeout(30 * 1000); connection.setReadTimeout(30 * 1000); – user5313398