Package com.dianping.cat.message

Examples of com.dianping.cat.message.Transaction


  }

  private class DependencyReloadTask implements Task {

    private void buildGraph(List<DependencyReport> reports) {
      Transaction t = Cat.newTransaction(DEPENDENCY, "BuildGraph");
      try {
        m_graphBuilder.getGraphs().clear();
        for (DependencyReport report : reports) {
          m_graphBuilder.visitDependencyReport(report);
        }
        Map<Long, TopologyGraph> graphs = m_graphBuilder.getGraphs();

        for (Entry<Long, TopologyGraph> entry : graphs.entrySet()) {
          m_topologyGraphs.put(entry.getKey(), entry.getValue());

          m_topologyGraphs.remove(entry.getKey() - TimeHelper.ONE_HOUR * 2);
        }
        t.setStatus(Message.SUCCESS);
      } catch (Exception e) {
        t.setStatus(e);
      } finally {
        t.complete();
      }
    }
View Full Code Here


    private List<DependencyReport> fetchReport(Collection<String> domains) {
      long current = System.currentTimeMillis();
      long currentHour = current - current % TimeHelper.ONE_HOUR;
      List<DependencyReport> reports = new ArrayList<DependencyReport>();
      Transaction t = Cat.newTransaction(DEPENDENCY, "FetchReport");

      try {
        for (String temp : domains) {
          try {
            ModelRequest request = new ModelRequest(temp, ModelPeriod.CURRENT.getStartTime()).setProperty("date",
                  String.valueOf(currentHour));
            if (m_service.isEligable(request)) {
              ModelResponse<DependencyReport> response = m_service.invoke(request);
              DependencyReport report = response.getModel();

              if (report != null) {
                reports.add(report);
              }
            } else {
              m_logger.warn(String.format("Can't get dependency report of %s", temp));
            }
          } catch (Exception e) {
            Cat.logError(e);
          }
        }
        t.setStatus(Message.SUCCESS);
      } catch (Exception e) {
        t.setStatus(e);
      } finally {
        t.complete();
      }
      return reports;
    }
View Full Code Here

    @Override
    public void run() {
      boolean active = true;

      while (active) {
        Transaction t = Cat.newTransaction(DEPENDENCY, "Reload");
        long current = System.currentTimeMillis();
        try {
          Collection<String> domains = queryAllDomains();

          buildGraph(fetchReport(domains));
          t.setStatus(Transaction.SUCCESS);
        } catch (Exception e) {
          m_logger.error(e.getMessage(), e);
          t.setStatus(e);
        } finally {
          t.complete();
        }
        long duration = System.currentTimeMillis() - current;

        try {
          int maxDuration = 60 * 1000;
 
View Full Code Here

    m_queue = Reflects.forField().getDeclaredFieldValue(queue.getClass(), "m_queue", queue);
  }

  public void testNormal() throws Exception {
    MessageProducer producer = Cat.getProducer();
    Transaction t = producer.newTransaction("URL", "MyPage");

    try {
      // do your business here
      t.addData("k1", "v1");
      t.addData("k2", "v2");
      t.addData("k3", "v3");

      Thread.sleep(20);

      producer.logEvent("URL", "Payload", Message.SUCCESS, "host=my-host&ip=127.0.0.1&agent=...");
      t.setStatus(Message.SUCCESS);
    } catch (Exception e) {
      t.setStatus(e);
    } finally {
      t.complete();
    }

    // please stop CAT server when you run this test case
    Assert.assertEquals("One message should be in the queue.", 1, m_queue.size());

    MessageTree tree = m_queue.poll();
    Message m = tree.getMessage();

    Assert.assertTrue(Transaction.class.isAssignableFrom(m.getClass()));

    Transaction trans = (Transaction) m;

    Assert.assertEquals("URL", trans.getType());
    Assert.assertEquals("MyPage", trans.getName());
    Assert.assertEquals("0", trans.getStatus());
    Assert.assertTrue(trans.getDurationInMillis() > 0);
    Assert.assertEquals("k1=v1&k2=v2&k3=v3", trans.getData().toString());

    Assert.assertEquals(1, trans.getChildren().size());

    Message c = trans.getChildren().get(0);

    Assert.assertEquals("URL", c.getType());
    Assert.assertEquals("Payload", c.getName());
    Assert.assertEquals("0", c.getStatus());
    Assert.assertEquals("host=my-host&ip=127.0.0.1&agent=...", c.getData().toString());
View Full Code Here

  }

  @Test
  public void testNormal() throws Exception {
    MessageProducer producer = Cat.getProducer();
    Transaction t = producer.newTransaction("URL", "MyPage");

    try {
      // do your business here
      t.addData("k1", "v1");
      t.addData("k2", "v2");
      t.addData("k3", "v3");

      Thread.sleep(20);

      producer.logEvent("URL", "Payload", Message.SUCCESS, "host=my-host&ip=127.0.0.1&agent=...");
      t.setStatus(Message.SUCCESS);
    } catch (Exception e) {
      t.setStatus(e);
    } finally {
      t.complete();
    }

    // please stop CAT server when you run this test case
    Assert.assertEquals("One message should be in the queue.", 1, m_queue.size());

    MessageTree tree = m_queue.poll();
    Message m = tree.getMessage();

    Assert.assertTrue(Transaction.class.isAssignableFrom(m.getClass()));

    Transaction trans = (Transaction) m;

    Assert.assertEquals("URL", trans.getType());
    Assert.assertEquals("MyPage", trans.getName());
    Assert.assertEquals("0", trans.getStatus());
    Assert.assertTrue(trans.getDurationInMillis() > 0);
    Assert.assertEquals("k1=v1&k2=v2&k3=v3", trans.getData().toString());

    Assert.assertEquals(1, trans.getChildren().size());

    Message c = trans.getChildren().get(0);

    Assert.assertEquals("URL", c.getType());
    Assert.assertEquals("Payload", c.getName());
    Assert.assertEquals("0", c.getStatus());
    Assert.assertEquals("host=my-host&ip=127.0.0.1&agent=...", c.getData().toString());
View Full Code Here

  @Test
  public void testNested() throws Exception {
    Stack<Transaction> stack = new Stack<Transaction>();

    for (int i = 0; i < 10; i++) {
      Transaction t = Cat.getProducer().newTransaction("Test", "TestName");

      t.addData("k1", "v1");
      t.addData("k2", "v2");
      t.addData("k3", "v3");

      stack.push(t);
    }

    while (!stack.isEmpty()) {
      Transaction t = stack.pop();

      t.setStatus(Message.SUCCESS);
      t.complete();
    }

    // please stop CAT server when you run this test case
    Assert.assertEquals("One message should be in the queue.", 1, m_queue.size());
View Full Code Here

    Thread.sleep(1000);
  }

  private MessageTree buildMessages() {
    Transaction t = Cat.newTransaction("type1", "name1\t\n\t\n\\");
    Transaction t2 = Cat.newTransaction("type2", "name\t\n\t\n2\\");
    Transaction t3 = Cat.newTransaction("type3", "name3\t\n\t\n\\");
    Transaction t4 = Cat.newTransaction("type4", "name4\t\n\t\n\\");
    Transaction t5 = Cat.newTransaction("type4", "name4\t\n\t\n\\");
    Transaction t6 = Cat.newTransaction("type4", "name4\t\n\t\n\\");
    Transaction t7 = Cat.newTransaction("type4", "name4\t\n\t\n\\");
    Transaction t8 = Cat.newTransaction("type4", "name4\t\n\t\n\\");

    Cat.logEvent("type1\t\n", "name\t\n", "sdfsdf\t\n", convertException(new NullPointerException()));
    Cat.logHeartbeat("type1\t\n", "name\t\n", "sdfsdf\t\n", convertException(new NullPointerException()));

    Cat.logError(new RuntimeException());

    for (int i = 0; i < 50; i++) {
      Cat.logEvent("type1\t\n", "name\t\n", "sdfsdf\t\n", "");
    }
    for (int i = 0; i < 10; i++) {
      Cat.logError(new RuntimeException());
    }

    t5.complete();
    t6.complete();
    t7.complete();
    t8.complete();
    t2.addData(convertException(new NullPointerException()));
    t2.setStatus(convertException(new NullPointerException()));
    t4.complete();
    t3.complete();
    t2.complete();
View Full Code Here

  }

  @Test
  public void testMode1() throws Exception {
    String url = "http://localhost:2282/mock/mode1";
    Transaction t = Cat.newTransaction("Mock", "testMode1");

    try {
      String childId = Cat.createMessageId();
      String id = Cat.getManager().getThreadLocalMessageTree().getMessageId();

      Cat.logEvent("RemoteCall", url, Message.SUCCESS, childId);

      InputStream in = Urls.forIO().connectTimeout(100) //
            .header("X-Cat-Id", childId) //
            .header("X-Cat-Parent-Id", id) //
            .header("X-Cat-Root-Id", id) //
            .openStream(url);
      String content = Files.forIO().readFrom(in, "utf-8");

      Assert.assertEquals("mock content here!", content);

      t.setStatus(Message.SUCCESS);
    } finally {
      t.complete();
    }

    TimeUnit.MILLISECONDS.sleep(100);
  }
View Full Code Here

    MessageTree tree = Cat.getManager().getThreadLocalMessageTree();

    tree.setMessageId(rootId);

    Transaction t = Cat.newTransaction("Mock", "testMode2");
    Cat.logEvent("RemoteCall", url, Message.SUCCESS, id);
    t.setStatus(Message.SUCCESS);
    t.complete();

    TimeUnit.MILLISECONDS.sleep(100);
  }
View Full Code Here

    private static final long serialVersionUID = 1L;

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
      PrintWriter writer = res.getWriter();
      Transaction t = Cat.newTransaction("Mock", req.getRequestURI());

      try {
        writer.write("mock content here!");

        // no status set by purpose
      } finally {
        t.complete();
      }
    }
View Full Code Here

TOP

Related Classes of com.dianping.cat.message.Transaction

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.