예제 #1
0
  @Test
  public void testPush() throws Exception {
    server.getJaxrsServer().getDeployment().getRegistry().addPerRequestResource(PushReceiver.class);
    ClientRequest request = new ClientRequest(generateURL("/topics/" + topicName));

    ClientResponse<?> response = request.head();
    response.releaseConnection();
    Assert.assertEquals(200, response.getStatus());
    Link consumers =
        MessageTestBase.getLinkByTitle(
            manager.getQueueManager().getLinkStrategy(), response, "push-subscriptions");
    System.out.println("push: " + consumers);

    PushTopicRegistration oneReg = new PushTopicRegistration();
    oneReg.setDurable(false);
    XmlLink target = new XmlLink();
    target.setMethod("post");
    target.setHref(generateURL("/push/one"));
    target.setType("application/xml");
    oneReg.setTarget(target);
    oneReg.setSelector("MyTag = '1'");
    response = consumers.request().body("application/xml", oneReg).post();
    response.releaseConnection();
    Link oneSubscription = response.getLocation();

    PushTopicRegistration twoReg = new PushTopicRegistration();
    twoReg.setDurable(false);
    target = new XmlLink();
    target.setMethod("post");
    target.setHref(generateURL("/push/two"));
    target.setType("application/xml");
    twoReg.setTarget(target);
    twoReg.setSelector("MyTag = '2'");
    response = consumers.request().body("application/xml", twoReg).post();
    response.releaseConnection();
    Link twoSubscription = response.getLocation();

    Order order = new Order();
    order.setName("1");
    order.setAmount("$5.00");
    publish(topicName, order, null, "1");
    Thread.sleep(200);
    Assert.assertEquals(order, PushReceiver.oneOrder);

    order.setName("2");
    publish(topicName, order, null, "2");
    Thread.sleep(200);
    Assert.assertEquals(order, PushReceiver.twoOrder);

    order.setName("3");
    publish(topicName, order, null, "2");
    Thread.sleep(200);
    Assert.assertEquals(order, PushReceiver.twoOrder);

    order.setName("4");
    publish(topicName, order, null, "1");
    Thread.sleep(200);
    Assert.assertEquals(order, PushReceiver.oneOrder);

    order.setName("5");
    publish(topicName, order, null, "1");
    Thread.sleep(200);
    Assert.assertEquals(order, PushReceiver.oneOrder);

    order.setName("6");
    publish(topicName, order, null, "1");
    Thread.sleep(200);
    Assert.assertEquals(order, PushReceiver.oneOrder);

    response = oneSubscription.request().delete();
    response.releaseConnection();
    response = twoSubscription.request().delete();
    response.releaseConnection();
  }
예제 #2
0
  @Override
  public boolean push(ClientMessage message) {
    ActiveMQRestLogger.LOGGER.debug("Pushing " + message);
    String uri = createUri(message);
    for (int i = 0; i < registration.getMaxRetries(); i++) {
      long wait = registration.getRetryWaitMillis();
      System.out.println("Creating request from " + uri);
      ClientRequest request = executor.createRequest(uri);
      request.followRedirects(false);
      ActiveMQRestLogger.LOGGER.debug("Created request " + request);

      for (XmlHttpHeader header : registration.getHeaders()) {
        ActiveMQRestLogger.LOGGER.debug(
            "Setting XmlHttpHeader: " + header.getName() + "=" + header.getValue());
        request.header(header.getName(), header.getValue());
      }
      HttpMessageHelper.buildMessage(message, request, contentType, jmsOptions);
      ClientResponse<?> res = null;
      try {
        ActiveMQRestLogger.LOGGER.debug(method + " " + uri);
        res = request.httpMethod(method);
        int status = res.getStatus();
        ActiveMQRestLogger.LOGGER.debug("Status of push: " + status);
        if (status == 503) {
          String retryAfter = res.getStringHeaders().getFirst("Retry-After");
          if (retryAfter != null) {
            wait = Long.parseLong(retryAfter) * 1000;
          }
        } else if (status == 307) {
          uri = res.getLocation().toString();
          wait = 0;
        } else if ((status >= 200 && status < 299) || status == 303 || status == 304) {
          ActiveMQRestLogger.LOGGER.debug("Success");
          return true;
        } else if (status >= 400) {
          switch (status) {
            case 400: // these usually mean the message you are trying to send is crap, let dead
                      // letter logic take over
            case 411:
            case 412:
            case 413:
            case 414:
            case 415:
            case 416:
              throw new RuntimeException(
                  "Something is wrong with the message, status returned: "
                      + status
                      + " for push registration of URI: "
                      + uri);
            case 401: // might as well consider these critical failures and abort.  Immediately
                      // signal to disable push registration depending on config
            case 402:
            case 403:
            case 405:
            case 406:
            case 407:
            case 417:
            case 505:
              return false;
            case 404: // request timeout, gone, and not found treat as a retry
            case 408:
            case 409:
            case 410:
              break;
            default: // all 50x requests just retry (except 505)
              break;
          }
        }
      } catch (Exception e) {
        ActiveMQRestLogger.LOGGER.debug("failed to push message to " + uri, e);
        e.printStackTrace();
        return false;
      } finally {
        if (res != null) res.releaseConnection();
      }
      try {
        if (wait > 0) Thread.sleep(wait);
      } catch (InterruptedException e) {
        throw new RuntimeException("Interrupted");
      }
    }
    return false;
  }