3

Feign スレッドセーフのインスタンスはありますか? これをサポートするドキュメントは見つかりませんでした。そうでないと思う人はいますか?

これは、Feign の github リポジトリに投稿された標準的な例です...

interface GitHub {
  @RequestLine("GET /repos/{owner}/{repo}/contributors")
  List<Contributor> contributors(@Param("owner") String owner, @Param("repo") String repo);
}

static class Contributor {
  String login;
  int contributions;
}

public static void main(String... args) {
  GitHub github = Feign.builder()
                       .decoder(new GsonDecoder())
                       .target(GitHub.class, "https://api.github.com");

  // Fetch and print a list of the contributors to this library.
  List<Contributor> contributors = github.contributors("netflix", "feign");
  for (Contributor contributor : contributors) {
    System.out.println(contributor.login + " (" + contributor.contributions + ")");
  }
}

これを次のように変更する必要があります...スレッドセーフですか...?

interface GitHub {
  @RequestLine("GET /repos/{owner}/{repo}/contributors")
  List<Contributor> contributors(@Param("owner") String owner, @Param("repo") String repo);
}

static class Contributor {
  String login;
  int contributions;
}

@Component
public class GithubService {

  GitHub github = null;

  @PostConstruct
  public void postConstruct() {
    github = Feign.builder()
                .decoder(new GsonDecoder())
                .target(GitHub.class, "https://api.github.com");
  }

  public void callMeForEveryRequest() {
    github.contributors... // Is this thread-safe...?
  }
}

上記の例では... スプリング ベースのコンポーネントを使用してシングルトンを強調表示しました。前もって感謝します...

4

4 に答える 4