Skip to main content

Soap client with setTimeout method?

2 replies [Last post]
sbutt
Offline
Joined: 2009-07-02

Hi Folks,
I have implemented a soap client (javax.xml.soap.*).

</p>
<p>public Message processSOAPRequest(Message message) {<br />
    try {<br />
      MessageFactory msgFactory = MessageFactory.newInstance();<br />
      SOAPMessage soap = msgFactory.createMessage();<br />
      SOAPPart soapPart = soap.getSOAPPart();</p>
<p>      byte[] buffer = ((String) message.getBody().get()).getBytes();<br />
      ByteArrayInputStream stream = new ByteArrayInputStream(buffer);<br />
      StreamSource source = new StreamSource(stream);<br />
      soapPart.setContent(source);<br />
      String action = config.getAttribute(SOAP_ACTION);<br />
      if (action != null) {<br />
        MimeHeaders headers = soap.getMimeHeaders();<br />
        headers.addHeader("SOAPAction", action);<br />
      }</p>
<p>      /////////////////////////////////////////////////////////</p>
<p>      SOAPMessage reply = sendSOAPMessage(soap);</p>
<p>      /////////////////////////////////////////////////////////    </p>
<p>      ByteArrayOutputStream out = new ByteArrayOutputStream();<br />
      reply.writeTo(out);<br />
      String soapMessage = new String(out.toByteArray());<br />
      message.getBody().add(soapMessage);</p>
<p>    } catch (SOAPException e) {<br />
      logger.error("SOAPException : " + e);</p>
<p>    } catch (IOException e) {<br />
      logger.error("IOException : " + e);<br />
    }</p>
<p>    return message;</p>
<p>  }</p>
<p> private SOAPMessage sendSOAPMessage(SOAPMessage soap) {<br />
    SOAPConnection connection = null;<br />
    SOAPMessage reply = null;<br />
    try {<br />
      SOAPConnectionFactory soapConnFactory = SOAPConnectionFactory<br />
          .newInstance();<br />
      connection = soapConnFactory.createConnection();<br />
      String destination = config.getAttribute(URL_ENDPOINT);</p>
<p>      reply = connection.call(soap, destination);</p>
<p>    } catch (Exception e) {<br />
      logger.error(e);<br />
    } finally {<br />
      try {<br />
        if (connection != null) {<br />
          connection.close();<br />
        }<br />
      } catch (SOAPException e) {<br />
        logger.error(e);<br />
      }<br />
    }</p>
<p>    return reply;<br />
  }</p>
<p>

The input message is JBossEsb, which i convert to soap message and then send it using SOAPConnection. The problem with SOAPConnection class is that it doe snot provide setTimeout(..) method. I read some where that there is another class SOAPConnectionImpl (by axis), which extends SOAPConnection and has this setTimeout method.

I have tried to convert my existing implementation to SOAPConnectionImpl but i always get a classcast exception.

Could somebody help me in suggesting a solution to this problem? My main concern is to include timeout feature that is the WS consumer/client should timeout after a certain period if the server does not reply. Any other soap message sending implementation with timeout feature are also good.

I have included

	</p>
<p>    axis<br />
    axis<br />
    1.4</p>
<p>

maven dependencies for axis.

Awaiting replies.

Thanks.

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
vnoel
Offline
Joined: 2010-11-15

Here is a solution that allow to implement a TimeOut on a SOAP Client request with JAVA 6 :

[code]
// --------------------------
// SOAP Message creation
// --------------------------
SOAPMessage sm = MessageFactory.newInstance().createMessage();
sm.setProperty(SOAPMessage.WRITE_XML_DECLARATION, "true");
sm.setProperty(SOAPMessage.CHARACTER_SET_ENCODING, "UTF-8");

SOAPPart sp = sm.getSOAPPart();
SOAPEnvelope se = sp.getEnvelope();
se.setEncodingStyle("http://schemas.xmlsoap.org/soap/encoding/");
se.setAttribute("xmlns:SOAP-ENC", "http://schemas.xmlsoap.org/soap/encoding/");
se.setAttribute("xmlns:xsd", "http://www.w3.org/2001/XMLSchema");
se.setAttribute("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance");
SOAPBody sb = sm.getSOAPBody();

// Add here all field for the SOAPMessage

SOAPConnection connection = SOAPConnectionFactory.newInstance().createConnection();
// ------------------------------------------------------------
// URL creation with TimeOut definition
// ------------------------------------------------------------
URL endpoint = new URL(null,
"http://remotedomaine/webservice",
new URLStreamHandler() {
@Override
protected URLConnection openConnection(URL url) throws IOException {
URL clone_url = new URL(url.toString());
HttpURLConnection clone_urlconnection = (HttpURLConnection) clone_url.openConnection();
clone_urlconnection.setConnectTimeout(10000);
clone_urlconnection.setReadTimeout(10000);
return(clone_urlconnection);
}
});

try {
// ---------------------
// Send the SOP message
// ---------------------
SOAPMessage retour = connection.call(sm, endpoint);
}
catch(Exception e) {
if ((e instanceof com.sun.xml.internal.messaging.saaj.SOAPExceptionImpl) && (e.getCause()!=null) && (e.getCause().getCause()!=null) && (e.getCause().getCause().getCause()!=null)) {
System.err.println("[" + e + "] Error sending SOAP request. Initiale error cause = " + e.getCause().getCause().getCause());
}
else {
System.err.println("[" + e + "] Error sending SOAP request");
}
}[code]

durgany
Offline
Joined: 2003-06-16

I think you could use with WS-Security implementation.

{
wsp:Usage = Required attribute. Specifies how to process the message expiration requirement. Setting the attribute to wsp:Required specifies that the SOAP message must meet the message expiration assertion.

Age = Required attribute. Specifies the maximum expiration time for a SOAP message, in seconds. This value must be greater than zero.
}

The following URL has a nice description: http://e-docs.bea.com/wls/docs92/webserv/sec_assert.html#MessageAge