[
Date Prev][
Date Next][
Thread Prev][
Thread Next][
Date Index][
Thread Index]
[
List Home]
Re: [jetty-users] how does org.eclipse.jetty.client.HttpClient or org.eclipse.jetty.client.api.Request set the IP for the socket connect
|
Hi,
On Fri, Feb 21, 2020 at 10:57 AM Cao, William (NSB - CN/Qingdao)
<william.cao@xxxxxxxxxxxxxxx> wrote:
>
> Hi Simoneļ¼
>
> Many thanks for your quick help.
> I plan to change code as below. Could you please help check if it is correct?
>
> ...
> HttpClient client = new HttpClient(clientTLSFactory);
> client.setBindAddress(new InetSocketAddress("10.9.101.100", 0)); //local IP
>
> client.setSocketAddressResolver(new SocketAddressResolver() {
> @Override
> public void resolve(String host, int port, Promise<List<InetSocketAddress>> promise) {
> try
> {
> //above host is the FQDN of remote https server after SRV record resolving,
> //in this example, it is "www.eclipse.org"
> //DO DNS A/AAAA lookup for above host to get a list of IPs
> //select one IP from above IPs list according to some rule,
> // and assign this IP to host
> host = <the selected IP>; //e.g, 198.41.30.198
>
> InetAddress[] addresses = InetAddress.getAllByName(host);
>
> List<InetSocketAddress> result = new ArrayList<>(addresses.length);
> for (InetAddress address : addresses)
> result.add(new InetSocketAddress(address, port));
>
> if (result.isEmpty())
> promise.failed(new UnknownHostException());
> else
> promise.succeeded(result);
> }
> catch (Throwable x)
> {
> promise.failed(x);
> }
> }
> });
>
> Request httpsreq = client.newRequest("https://www.eclipse.org/jetty/")
> .method(org.eclipse.jetty.http.HttpMethod.POST);
>
> httpsreq.header(headerName, headerValue);
> httpsreq.content(new BytesContentProvider("Hello Jetty"), "application/json");
> httpsreq.send(new Response.Listener.Adapter() { ......});
More or less correct.
Since you have just one numeric address (the selected IP), there is no
point in calling getAllByteName() as getByName() is sufficient.
And because it's already a numeric address, you will not incur in
blocking DNS lookup by calling getByName().
Again because it's just one address, you can use Collections.singletonList().
Promise completion is correct.
--
Simone Bordet
----
http://cometd.org
http://webtide.com
Developer advice, training, services and support
from the Jetty & CometD experts.