我是开发cxf webservice的新手,我正在通过SOAP-UI对此进行测试。但我对WS呼叫套接字有一些疑问。
这是我的代码:
@WebService(targetNamespace = "http://ws.profep.com/", portName = "GpmsGWPort", serviceName = "GpmsGWService")
public class GpmsGW {
private static Logger log = Logger.getLogger(GpmsGW.class);
Properties prop = new Properties();
public GpmsGW() {
super();
log.debug("--GpmsGW was Init--");
}
private boolean loadConfig(){
try{
InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream("ConConfig.properties");
prop.load(new InputStreamReader(is, "UTF-8"));
return true;
}catch(Exception e){
log.error("", e);
return false;
}
}
public MsgRs sendMsg(MsgRq msgRq){
log.debug("--MESSAGE POST--");
loadConfig();
MsgRs msgRs = new MsgRs();
Socket socketIn = null, socketOut = null;
DataInputStream dis;
DataOutputStream dos;
try{
socketIn = new Socket(prop.getProperty("rIp"), Integer.parseInt(prop.getProperty("rPort")));
socketOut = new Socket(prop.getProperty("sIp"), Integer.parseInt(prop.getProperty("sPort")));
int timeout = 60000;
socketIn.setSoTimeout(timeout);
socketOut.setSoTimeout(timeout);
dis = new DataInputStream(socketIn.getInputStream());
dos = new DataOutputStream(socketOut.getOutputStream());
// String request = renderRequest(msgRq) ;
String request = testReq(msgRq) ; //測試用
byte[] reqmsg = Utility.hexStr2Bytes(Utility.stringToHex(request));
dos.write(Utility.addHeaderLLLL(reqmsg));
dos.flush();
byte[] inLen = new byte[4];
dis.read(inLen, 0, 4);
byte[] res = new byte[Integer.parseInt(new String(inLen))];
dis.readFully(res);
String response = new String(res);
log.info("WS Response=[" + response + "]");
msgRs = parseResponse(response);
}
catch(SocketTimeoutException se){
log.error("WS socket is timeout!!!");
return genTimeoutResponse(msgRq);
}
catch(Exception e2){
log.error("", e2);
try {
if(socketIn != null){socketIn.close();}
if(socketOut != null){socketOut.close();}
} catch (IOException e) {
log.error("Exception occurred : " + e);
}
}
return msgRs;
}
问题:但是如果SockeTimeoutException
发生,Tomcat日志异常,soapUI没有响应消息..如何处理Exception并在我的代码中返回正确的xml响应?
Message: Outbound Message
---------------------------
ID: 4
Response-Code: 200
Encoding: UTF-8
Content-Type: text/xml
Headers: {}
Payload: <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Body><ns2:sendMsgResponse xmlns:ns2="http://ws.profep.com/"><return/></ns2:sendMsgResponse></soap:Body></soap:Envelope>
--------------------------------------
May 24, 2016 11:06:29 上午 org.apache.cxf.phase.PhaseInterceptorChain doDefaultLogging
Warring: Interceptor for {http://ws.profep.com/}GpmsGWService#{http://ws.profep.com/}sendMsg has thrown exception, unwinding now
org.apache.cxf.interceptor.Fault: Could not send Message.
at org.apache.cxf.interceptor.MessageSenderInterceptor$MessageSenderEndingInterceptor.handleMessage(MessageSenderInterceptor.java:64)
at org.apache.cxf.phase.PhaseInterceptorChain.doIntercept(PhaseInterceptorChain.java:272)
at org.apache.cxf.interceptor.OutgoingChainInterceptor.handleMessage(OutgoingChainInterceptor.java:83)
at org.apache.cxf.phase.PhaseInterceptorChain.doIntercept(PhaseInterceptorChain.java:272)
at org.apache.cxf.transport.ChainInitiationObserver.onMessage(ChainInitiationObserver.java:121)
at org.apache.cxf.transport.http.AbstractHTTPDestination.invoke(AbstractHTTPDestination.java:249)
at org.apache.cxf.transport.servlet.ServletController.invokeDestination(ServletController.java:248)
at org.apache.cxf.transport.servlet.ServletController.invoke(ServletController.java:222)
at org.apache.cxf.transport.servlet.ServletController.invoke(ServletController.java:153)
at org.apache.cxf.transport.servlet.CXFNonSpringServlet.invoke(CXFNonSpringServlet.java:181)
at org.apache.cxf.transport.servlet.AbstractHTTPServlet.handleRequest(AbstractHTTPServlet.java:289)
at org.apache.cxf.transport.servlet.AbstractHTTPServlet.doPost(AbstractHTTPServlet.java:209)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:650)
at org.apache.cxf.transport.servlet.AbstractHTTPServlet.service(AbstractHTTPServlet.java:265)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:505)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:169)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:956)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:436)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1078)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:625)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:316)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:745)
Caused by: org.apache.catalina.connector.ClientAbortException: java.net.SocketException: Software caused connection abort: socket write error
at org.apache.catalina.connector.OutputBuffer.doFlush(OutputBuffer.java:367)
at org.apache.catalina.connector.OutputBuffer.flush(OutputBuffer.java:331)
at org.apache.catalina.connector.Response.flushBuffer(Response.java:594)
at org.apache.catalina.connector.ResponseFacade.flushBuffer(ResponseFacade.java:306)
at org.apache.cxf.transport.http.AbstractHTTPDestination$WrappedOutputStream.close(AbstractHTTPDestination.java:763)
at org.apache.cxf.io.CacheAndWriteOutputStream.postClose(CacheAndWriteOutputStream.java:56)
at org.apache.cxf.io.CachedOutputStream.close(CachedOutputStream.java:223)
at org.apache.cxf.transport.AbstractConduit.close(AbstractConduit.java:56)
at org.apache.cxf.transport.http.AbstractHTTPDestination$BackChannelConduit.close(AbstractHTTPDestination.java:692)
at org.apache.cxf.interceptor.MessageSenderInterceptor$MessageSenderEndingInterceptor.handleMessage(MessageSenderInterceptor.java:62)
... 33 more
Caused by: java.net.SocketException: Software caused connection abort: socket write error
at java.net.SocketOutputStream.socketWrite0(Native Method)
at java.net.SocketOutputStream.socketWrite(SocketOutputStream.java:113)
at java.net.SocketOutputStream.write(SocketOutputStream.java:159)
at org.apache.coyote.http11.InternalOutputBuffer.realWriteBytes(InternalOutputBuffer.java:215)
at org.apache.tomcat.util.buf.ByteChunk.flushBuffer(ByteChunk.java:480)
at org.apache.coyote.http11.InternalOutputBuffer.flush(InternalOutputBuffer.java:119)
at org.apache.coyote.http11.AbstractHttp11Processor.action(AbstractHttp11Processor.java:800)
at org.apache.coyote.Response.action(Response.java:172)
at org.apache.catalina.connector.OutputBuffer.doFlush(OutputBuffer.java:363)
... 42 more
五月 24, 2016 11:06:29 上午 org.apache.cxf.phase.PhaseInterceptorChain doDefaultLogging
警告: Interceptor for {http://ws.profep.com/}GpmsGWService#{http://ws.profep.com/}sendMsg has thrown exception, unwinding now
org.apache.cxf.binding.soap.SoapFault: Error writing to XMLStreamWriter.
at org.apache.cxf.binding.soap.interceptor.SoapOutInterceptor$SoapOutEndingInterceptor.handleMessage(SoapOutInterceptor.java:314)
at org.apache.cxf.binding.soap.interceptor.SoapOutInterceptor$SoapOutEndingInterceptor.handleMessage(SoapOutInterceptor.java:291)
at org.apache.cxf.phase.PhaseInterceptorChain.doIntercept(PhaseInterceptorChain.java:272)
at org.apache.cxf.interceptor.AbstractFaultChainInitiatorObserver.onMessage(AbstractFaultChainInitiatorObserver.java:113)
at org.apache.cxf.phase.PhaseInterceptorChain.doIntercept(PhaseInterceptorChain.java:336)
at org.apache.cxf.interceptor.OutgoingChainInterceptor.handleMessage(OutgoingChainInterceptor.java:83)
at org.apache.cxf.phase.PhaseInterceptorChain.doIntercept(PhaseInterceptorChain.java:272)
at org.apache.cxf.transport.ChainInitiationObserver.onMessage(ChainInitiationObserver.java:121)
at org.apache.cxf.transport.http.AbstractHTTPDestination.invoke(AbstractHTTPDestination.java:249)
at org.apache.cxf.transport.servlet.ServletController.invokeDestination(ServletController.java:248)
at org.apache.cxf.transport.servlet.ServletController.invoke(ServletController.java:222)
at org.apache.cxf.transport.servlet.ServletController.invoke(ServletController.java:153)
at org.apache.cxf.transport.servlet.CXFNonSpringServlet.invoke(CXFNonSpringServlet.java:181)
at org.apache.cxf.transport.servlet.AbstractHTTPServlet.handleRequest(AbstractHTTPServlet.java:289)
at org.apache.cxf.transport.servlet.AbstractHTTPServlet.doPost(AbstractHTTPServlet.java:209)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:650)
at org.apache.cxf.transport.servlet.AbstractHTTPServlet.service(AbstractHTTPServlet.java:265)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:505)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:169)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:956)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:436)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1078)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:625)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:316)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:745)
Caused by: javax.xml.stream.XMLStreamException: No open start element, when trying to write end element
at com.ctc.wstx.sw.BaseStreamWriter.throwOutputError(BaseStreamWriter.java:1537)
at com.ctc.wstx.sw.BaseStreamWriter.reportNwfStructure(BaseStreamWriter.java:1566)
at com.ctc.wstx.sw.BaseNsStreamWriter.doWriteEndTag(BaseNsStreamWriter.java:648)
at com.ctc.wstx.sw.BaseNsStreamWriter.writeEndElement(BaseNsStreamWriter.java:280)
at org.apache.cxf.binding.soap.interceptor.SoapOutInterceptor$SoapOutEndingInterceptor.handleMessage(SoapOutInterceptor.java:301)
... 36 more
答案 0 :(得分:0)
好的,我只是通过其他方式解决我的问题.. 因为肥皂是我的转移格式.. 我可以向客户提供saop故障(例外) 以下是我的代码..
public MsgRs sendMsg(MsgRq msgRq) throws CustomException{
log.debug("--MESSAGE POST--");
loadConfig();
MsgRs msgRs = new MsgRs();
Socket socketIn = null, socketOut = null;
DataInputStream dis;
DataOutputStream dos;
try{
socketIn = new Socket(prop.getProperty("rIp"), Integer.parseInt(prop.getProperty("rPort")));
socketOut = new Socket(prop.getProperty("sIp"), Integer.parseInt(prop.getProperty("sPort")));
int timeout = 30000;
socketIn.setSoTimeout(timeout);
socketOut.setSoTimeout(timeout);
dis = new DataInputStream(socketIn.getInputStream());
dos = new DataOutputStream(socketOut.getOutputStream());
// String request = renderRequest(msgRq) ;
String request = testReq(msgRq) ; //測試用
byte[] reqmsg = Utility.hexStr2Bytes(Utility.stringToHex(request));
dos.write(Utility.addHeaderLLLL(reqmsg));
dos.flush();
byte[] inLen = new byte[4];
dis.read(inLen, 0, 4);
byte[] res = new byte[Integer.parseInt(new String(inLen))];
dis.readFully(res);
String response = new String(res);
log.info("WS Response=[" + response + "]");
msgRs = parseResponse(response);
}
catch(SocketTimeoutException se){
log.error("WS socket is timeout!!!");
// msgRs = genTimeoutResponse(msgRq);
throw new CustomException("socket is timeout.");
}
catch(Exception e2){
log.error("", e2);
try {
if(socketIn != null){socketIn.close();}
if(socketOut != null){socketOut.close();}
} catch (IOException e) {
log.error("Exception occurred : " + e);
}
throw new CustomException("Exception occurred : " + e2);
}
return msgRs;
}
@WebFault(name="CustomException")
public class CustomException extends Exception{
public CustomException(String message) {
super(message);
}
}
然后我只是通过SOAP-UI测试..
它没有异常发生并获得相应的响应消息。