Guest User

HystrixCommand send http error

a guest
Jul 28th, 2014
470
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import com.netflix.niws.client.http.HttpClientRequest;
  2. import com.netflix.niws.client.http.HttpClientRequest.Verb;
  3. import com.netflix.niws.client.http.HttpClientResponse;
  4. import com.netflix.niws.client.http.RestClient;
  5. import org.apache.http.entity.StringEntity;
  6. import org.apache.http.entity.ContentType;
  7.  
  8. public class AddRSSCommand extends HystrixCommand<String> {
  9.  
  10.     private final String url;
  11.  
  12.     public AddRSSCommand() {
  13.         super (Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey(RSSConstants.HYSTRIX_RSS_MUTATIONS_GROUP))
  14.                 .andCommandKey(HystrixCommandKey.Factory.asKey(RSSConstants.HYSTRIX_RSS_ADD_COMMAND_KEY))
  15.                 .andThreadPoolKey(HystrixThreadPoolKey.Factory.asKey(RSSConstants.HYSTRIX_RSS_THREAD_POOL)));
  16.     }
  17.  
  18.     @Override
  19.     protected String run() {
  20.         String jsonString = "{username: 'Hermione', password: 'Granger'}"
  21.         StringEntity se = new StringEntity(jsonString, ContentType.APPLICATION_JSON);
  22.  
  23.         try {
  24.             RestClient client = (RestClient) ClientFactory.getNamedClient(RSSConstants.MIDDLETIER_REST_CLIENT);
  25.  
  26.             HttpClientRequest request = HttpClientRequest
  27.                     .newBuilder()
  28.                     .setVerb(Verb.POST)
  29.                     .setUri(new URI("/"
  30.                             + RSSConstants.MIDDLETIER_WEB_RESOURCE_ROOT_PATH
  31.                             + RSSConstants.RSS_ENTRY_POINT
  32.                             + "?url=" + url))
  33.                     .setEntity(se);
  34.                     .build();
  35.  
  36.             HttpClientResponse response = client.executeWithLoadBalancer(request);
  37.             return IOUtils.toString(response.getRawEntity(), Charsets.UTF_8);
  38.         } catch (Exception exc) {
  39.             throw new RuntimeException("Exception occurred when adding a RSS feed", exc);
  40.         }
  41.     }
  42.  
  43. }
RAW Paste Data