question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

OkHttpClient is final

See original GitHub issue

What is the point of OkHttpClient be a final class?

Some frameworks, like spring, make intense use of proxies for almost everything.

I had a use case where I use to build the feign clients manually and now I’m trying to use spring-cloud-opentracing to do some distributed tracing.

One problem I’m facing is, on that package there’s a Aspect in order to decorate the client the their TracingClient, but the problem is: the aspect tries to make a proxy of the client implementation but the OkHttpClient is final.

tl;dr What is the possibility of making the OkHttpClient not final?

edit: The spring cloud opentracing feign can be found here: https://github.com/opentracing-contrib/java-spring-cloud/blob/master/instrument-starters/opentracing-spring-cloud-feign-starter/src/main/java/io/opentracing/contrib/spring/cloud/feign/FeignTracingAutoConfiguration.java#L80

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
kdavisk6commented, Jul 1, 2018

Based on my understanding of the history of this project and it’s modules, the Client interface is meant to be “open for extension, closed for modification” following the Open-Closed Principle. Both the ApacheHttpClient and the OkHttpClient are final in order to adhere to this.

With regards to OpenTracing, there is an OpenTracing implementation for Feign available that appears to indicate that you can use the provided OkHttpClient with OpenTracing

https://github.com/OpenFeign/feign-opentracing

In your particular case, you want to use Spring’s Open Tracing support for Spring Boot, which as you’ve pointed out, uses an Aspect to instrument a Feign client that is not using a TracingClient explicitly. Since you have mentioned that you are manually configuring Feign, if you use a TracingClient explicitly, the Aspect is not necessary.

GitHub github = Feign.builder()
                     .client(new TracingClient(new OkHttpClient(), tracer))
                     .target(GitHub.class, "https://api.github.com");

Looking at both feign-opentracing and the Spring project, if you want to use a provided Client, you must use TracingClient directly.

Now, with that said, I don’t see an issue with changing the current Client implementations away from final. Let’s ask the other contributors what they think.

@velo, @spencergibb, @adriancole Thoughts?

0reactions
williamokanocommented, Jul 16, 2018

Couldn’t do as you suggested.

I created a springboot autoconfiguration which creates a bean for TracingClient decorated with OkHttpClient. I think it’s probably for the best to keep OkHttpClient as final.

Read more comments on GitHub >

github_iconTop Results From Across the Web

OkHTTP Client Java initialization: should it be static or just final?
I'm using OkHttp library in my java application. I have a class that makes requests and uses OkHttpClient class, which is then used...
Read more >
OkHttpClient - OkHttp - Square Open Source
This builds a client that shares the same connection pool, thread pools, and configuration. Use the builder methods to add configuration to the...
Read more >
OkHttpClient (OkHttp 3.12.7 API) - javadoc.io
public class OkHttpClient extends Object implements Cloneable, Call. ... The singleton HTTP client. public final OkHttpClient client = new OkHttpClient();.
Read more >
A Guide to OkHttp - Baeldung
In this tutorial, we'll explore the basics of sending different types of HTTP requests, and receiving and interpreting HTTP responses.
Read more >
OKHttpClient - recommended use of the client as static/single ...
I would like to see what memory difference the use of the static client will make - since at the end of the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found