コード例 #1
0
  private Client build(
      String name, ExecutorService threadPool, ObjectMapper objectMapper, Validator validator) {
    final Client client =
        ClientBuilder.newClient(buildConfig(name, threadPool, objectMapper, validator));
    client.register(new JerseyIgnoreRequestUserAgentHeaderFilter());

    // Tie the client to server lifecycle
    if (environment != null) {
      environment
          .lifecycle()
          .manage(
              new Managed() {
                @Override
                public void start() throws Exception {}

                @Override
                public void stop() throws Exception {
                  client.close();
                }
              });
    }
    if (configuration.isGzipEnabled()) {
      client.register(new GZipDecoder());
      client.register(new ConfiguredGZipEncoder(configuration.isGzipEnabledForRequests()));
    }

    return client;
  }
コード例 #2
0
  /**
   * Builds the {@link Client} instance.
   *
   * @return a fully-configured {@link Client}
   */
  public Client build(String name) {
    if ((environment == null) && ((executorService == null) || (objectMapper == null))) {
      throw new IllegalStateException(
          "Must have either an environment or both " + "an executor service and an object mapper");
    }

    if (executorService == null && environment != null) {
      executorService =
          environment
              .lifecycle()
              .executorService("jersey-client-" + name + "-%d")
              .minThreads(configuration.getMinThreads())
              .maxThreads(configuration.getMaxThreads())
              .workQueue(new ArrayBlockingQueue<Runnable>(configuration.getWorkQueueSize()))
              .build();
    }

    if (objectMapper == null && environment != null) {
      objectMapper = environment.getObjectMapper();
    }

    if (environment != null) {
      validator = environment.getValidator();
    }

    return build(name, executorService, objectMapper, validator);
  }
コード例 #3
0
  private Client build(ExecutorService threadPool, ObjectMapper objectMapper) {
    final Client client = new ApacheHttpClient4(buildHandler(), buildConfig(objectMapper));
    client.setExecutorService(threadPool);

    if (configuration.isGzipEnabled()) {
      client.addFilter(new GZIPContentEncodingFilter(configuration.isGzipEnabledForRequests()));
    }

    return client;
  }
コード例 #4
0
  /**
   * Builds the {@link Client} instance.
   *
   * @return a fully-configured {@link Client}
   */
  public Client build() {
    if ((environment == null) && (executorService == null) && (objectMapper == null)) {
      throw new IllegalStateException(
          "Must have either an environment or both " + "an executor service and an object mapper");
    }

    if (environment == null) {
      return build(executorService, objectMapper);
    }

    return build(
        environment.managedExecutorService(
            "jersey-client-%d",
            configuration.getMinThreads(), configuration.getMaxThreads(), 60, TimeUnit.SECONDS),
        environment.getObjectMapperFactory().build());
  }
コード例 #5
0
  @Before
  public void setup() throws Exception {
    JerseyClientConfiguration clientConfiguration = new JerseyClientConfiguration();
    clientConfiguration.setConnectionTimeout(Duration.milliseconds(SLEEP_TIME_IN_MILLIS / 2));
    clientConfiguration.setTimeout(Duration.milliseconds(DEFAULT_CONNECT_TIMEOUT_IN_MILLIS));

    environment =
        new Environment(
            "test-dropwizard-apache-connector",
            Jackson.newObjectMapper(),
            Validators.newValidator(),
            new MetricRegistry(),
            getClass().getClassLoader());
    client =
        (JerseyClient)
            new JerseyClientBuilder(environment).using(clientConfiguration).build("test");
    for (LifeCycle lifeCycle : environment.lifecycle().getManagedObjects()) {
      lifeCycle.start();
    }
  }
コード例 #6
0
  private Configuration buildConfig(
      final String name,
      final ExecutorService threadPool,
      final ObjectMapper objectMapper,
      final Validator validator) {
    final ClientConfig config = new ClientConfig();

    for (Object singleton : this.singletons) {
      config.register(singleton);
    }

    for (Class<?> provider : this.providers) {
      config.register(provider);
    }

    config.register(new JacksonMessageBodyProvider(objectMapper));
    config.register(new HibernateValidationFeature(validator));

    for (Map.Entry<String, Object> property : this.properties.entrySet()) {
      config.property(property.getKey(), property.getValue());
    }

    config.register(new DropwizardExecutorProvider(threadPool));
    if (connectorProvider == null) {
      final ConfiguredCloseableHttpClient apacheHttpClient =
          apacheHttpClientBuilder.buildWithDefaultRequestConfiguration(name);
      connectorProvider =
          (client, runtimeConfig) ->
              new DropwizardApacheConnector(
                  apacheHttpClient.getClient(),
                  apacheHttpClient.getDefaultRequestConfig(),
                  configuration.isChunkedEncodingEnabled());
    }
    config.connectorProvider(connectorProvider);

    return config;
  }