SOAP 타임아웃(java)

sangyong·2023년 4월 27일
0

Java

목록 보기
1/3

일을 하며 SOAP 방식의 통신을 처음 접하게 되었는데, 상대 서버 쪽에 문제가 생겼을 때 readTimeout이 안먹는 이슈가 생겼다.
여러 방법을 찾아봐도 제대로 작동하는 로직이 안나오고, 너무 예전 자료만 나와 적용하기 어려웠다.
다행히 작동하는 한 방법을 찾아서 글을 쓰게 되었다.
원래는 SOAPConnection 이나 SOAPMessage에 설정이나 property를 넣으려는 시도를 계속 했었는데, 찾은 방법은 아래와 같다.
방식은 URL classs 내의 Connection을 여는 메소드를 Override해서 timeout 설정이 있는 URLConnection에 직접적으로 거는 방법이었고, 로직은 같지만 적용 방법이 살짝 다른 부분이 있어 적용 방법을 공유해본다.


URL endpoint = new URL(new URL(coachingUrl), "", new URLStreamHandler() {  
   @Override  
   protected URLConnection openConnection(URL url) throws IOException {  
	  URL target = new URL(url.toString());  
	  URLConnection connection = target.openConnection();  
	  // Connection settings  
	  connection.setConnectTimeout(10000); // 10 sec  
	  connection.setReadTimeout(60000); // 1 min  
	  return (connection);  
   }  
});  
HttpURLConnection conn = (HttpURLConnection) endpoint.openConnection();

conn.setDefaultUseCaches(false);  
conn.setDoInput(true);  
conn.setDoOutput(true);  
conn.setRequestMethod("POST");  
conn.getOutputStream().write(dataBytes);  
in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));

SOAPConnectionFactory scf = SOAPConnectionFactory.newInstance();  
SOAPConnection connection = scf.createConnection();

URL endpoint = new URL(new URL(urlAddress), "", new URLStreamHandler() {  
   @Override  
   protected URLConnection openConnection(URL url) throws IOException {  
      URL target = new URL(url.toString());  
      URLConnection connection = target.openConnection();  
      // Connection settings  
      connection.setConnectTimeout(10000); // 10 sec  
      connection.setReadTimeout(60000); // 1 min  
      return (connection);  
   }  
});  
SOAPMessage responseSoapMessage = connection.call(requestSoapMessage, endpoint);

0개의 댓글