Ejemplo n.º 1
0
 public int getSize() throws IOException {
   Reader reader = client.get("queues/" + name);
   Gson gson = new Gson();
   Info info = gson.fromJson(reader, Info.class);
   reader.close();
   return info.size;
 }
Ejemplo n.º 2
0
 /**
  * Retrieves Messages from the queue. If there are no items on the queue, an EmptyQueueException
  * is thrown.
  *
  * @param numberOfMessages The number of messages to receive. Max. is 100.
  * @param timeout timeout in seconds.
  * @throws EmptyQueueException If the queue is empty.
  * @throws HTTPException If the IronMQ service returns a status other than 200 OK.
  * @throws IOException If there is an error accessing the IronMQ server.
  */
 public Messages get(int numberOfMessages, int timeout) throws IOException {
   if (numberOfMessages < 0 || numberOfMessages > 100) {
     throw new IllegalArgumentException("numberOfMessages has to be within 1..100");
   }
   Reader reader =
       client.get("queues/" + name + "/messages?n=" + numberOfMessages + "&timeout=" + timeout);
   Gson gson = new Gson();
   Messages messages = gson.fromJson(reader, Messages.class);
   reader.close();
   return messages;
 }
  @Test
  public void testCopyHeadersRequest() {
    Map<String, String> transportHeaders = randomHeaders(randomIntBetween(0, 10));
    Map<String, String> restHeaders = randomHeaders(randomIntBetween(0, 10));
    Map<String, String> copiedHeaders = randomHeadersFrom(restHeaders);
    Set<String> usefulRestHeaders = new HashSet<>(copiedHeaders.keySet());
    usefulRestHeaders.addAll(randomMap(randomIntBetween(0, 10), "useful-").keySet());
    Map<String, String> restContext = randomContext(randomIntBetween(0, 10));
    Map<String, String> transportContext =
        Maps.difference(randomContext(randomIntBetween(0, 10)), restContext).entriesOnlyOnLeft();

    Map<String, String> expectedHeaders = new HashMap<>();
    expectedHeaders.putAll(transportHeaders);
    expectedHeaders.putAll(copiedHeaders);

    Map<String, String> expectedContext = new HashMap<>();
    expectedContext.putAll(transportContext);
    expectedContext.putAll(restContext);

    Client client =
        client(new NoOpClient(), new FakeRestRequest(restHeaders, restContext), usefulRestHeaders);

    SearchRequest searchRequest = Requests.searchRequest();
    putHeaders(searchRequest, transportHeaders);
    putContext(searchRequest, transportContext);
    assertHeaders(searchRequest, transportHeaders);
    client.search(searchRequest);
    assertHeaders(searchRequest, expectedHeaders);
    assertContext(searchRequest, expectedContext);

    GetRequest getRequest = Requests.getRequest("index");
    putHeaders(getRequest, transportHeaders);
    putContext(getRequest, transportContext);
    assertHeaders(getRequest, transportHeaders);
    client.get(getRequest);
    assertHeaders(getRequest, expectedHeaders);
    assertContext(getRequest, expectedContext);

    IndexRequest indexRequest = Requests.indexRequest();
    putHeaders(indexRequest, transportHeaders);
    putContext(indexRequest, transportContext);
    assertHeaders(indexRequest, transportHeaders);
    client.index(indexRequest);
    assertHeaders(indexRequest, expectedHeaders);
    assertContext(indexRequest, expectedContext);
  }