sending Soap message with timeout
sbutt Jul 2, 2009 8:55 AMHi Folks,
I have implemented a basic soap client (javax.xml.soap.*).
public Message processSOAPRequest(Message message) {
try {
MessageFactory msgFactory = MessageFactory.newInstance();
SOAPMessage soap = msgFactory.createMessage();
SOAPPart soapPart = soap.getSOAPPart();
byte[] buffer = ((String) message.getBody().get()).getBytes();
ByteArrayInputStream stream = new ByteArrayInputStream(buffer);
StreamSource source = new StreamSource(stream);
soapPart.setContent(source);
String action = config.getAttribute(SOAP_ACTION);
if (action != null) {
MimeHeaders headers = soap.getMimeHeaders();
headers.addHeader("SOAPAction", action);
}
/////////////////////////////////////////////////////////
SOAPMessage reply = sendSOAPMessage(soap);
/////////////////////////////////////////////////////////
ByteArrayOutputStream out = new ByteArrayOutputStream();
reply.writeTo(out);
String soapMessage = new String(out.toByteArray());
message.getBody().add(soapMessage);
} catch (SOAPException e) {
logger.error("SOAPException : " + e);
} catch (IOException e) {
logger.error("IOException : " + e);
}
return message;
}
private SOAPMessage sendSOAPMessage(SOAPMessage soap) {
SOAPConnection connection = null;
SOAPMessage reply = null;
try {
SOAPConnectionFactory soapConnFactory = SOAPConnectionFactory
.newInstance();
connection = soapConnFactory.createConnection();
String destination = config.getAttribute(URL_ENDPOINT);
reply = connection.call(soap, destination);
} catch (Exception e) {
logger.error(e);
} finally {
try {
if (connection != null) {
connection.close();
}
} catch (SOAPException e) {
logger.error(e);
}
}
return reply;
}
The input message is JBossEsb message, which I convert to soap message and then send it using SOAPConnection, which works fine.
The problem with SOAPConnection class is that it does not 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 exceptions.
Could somebody help me in suggesting a solution to this problem? My main concern is to include timeout feature that is my 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
<dependency> <groupId>axis</groupId> <artifactId>axis</artifactId> <version>1.4</version> </dependency>
maven dependencies for axis.
Awaiting replies.
Thanks.