Skip to content

Instantly share code, notes, and snippets.

@rponte
Last active February 7, 2020 18:39
Show Gist options
  • Save rponte/b0d4b4970a6502d9da08 to your computer and use it in GitHub Desktop.
Save rponte/b0d4b4970a6502d9da08 to your computer and use it in GitHub Desktop.
Setting timeout and http headers on Spring's SimpleHttpInvokerRequestExecutor
import java.io.IOException;
import java.net.HttpURLConnection;
import org.springframework.context.annotation.Bean;
import org.springframework.remoting.httpinvoker.HttpInvokerRequestExecutor;
import org.springframework.remoting.httpinvoker.SimpleHttpInvokerRequestExecutor;
import org.springframework.stereotype.Component;
@Component
public class CustomSimpleHttpInvokerRequestExecutorFactory {
/**
* https://www.baeldung.com/httpclient-timeout
*/
@Bean
public HttpInvokerRequestExecutor customSimpleHttpInvoker() {
SimpleHttpInvokerRequestExecutor invoker = new SimpleHttpInvokerRequestExecutor() {
@Override
protected void prepareConnection(HttpURLConnection connection, int contentLength) throws IOException {
super.prepareConnection(connection, contentLength);
connection.setRequestProperty("Connection", "close"); // custom http header
}
};
invoker.setReadTimeout(5*1000); // custom read timeout
invoker.setConnectTimeout(15*1000); // custom connection timeout
return invoker;
}
}
@rponte
Copy link
Author

rponte commented Oct 24, 2018

More information about connection and socket timeouts, https://www.baeldung.com/httpclient-timeout

@rponte
Copy link
Author

rponte commented Mar 28, 2019

Programmatic timeout using a thread: https://gist.github.com/rponte/70ba7b6cf4d370288e1e

@rponte
Copy link
Author

rponte commented May 23, 2019

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment