Package com.dianping.cat.message

Examples of com.dianping.cat.message.MessageProducer

or logs event or heartbeat in one shot, for example:

@author Frankie Wu

    int len = paths.size();

    if (len > 0) {
      Cat.setup("DumpUploader");

      MessageProducer cat = Cat.getProducer();
      String ip = NetworkInterfaceManager.INSTANCE.getLocalHostAddress();
      Transaction root = cat.newTransaction("System", "Dump-" + ip);

      Collections.sort(paths);

      root.addData("files", paths);
      root.setStatus(Message.SUCCESS);

      for (int i = 0; i < len; i++) {
        String path = paths.get(i);
        Transaction t = cat.newTransaction("System", "UploadDump");
        File file = new File(baseDir, path);

        t.addData("file", path);

        FSDataOutputStream fdos = null;
        FileInputStream fis = null;
        try {
          fdos = makeHdfsOutputStream(path);
          fis = new FileInputStream(file);

          long start = System.currentTimeMillis();

          Files.forIO().copy(fis, fdos, AutoClose.INPUT_OUTPUT);

          double sec = (System.currentTimeMillis() - start) / 1000d;
          String size = Formats.forNumber().format(file.length(), "0.#", "B");
          String speed = sec <= 0 ? "N/A" : Formats.forNumber().format(file.length() / sec, "0.0", "B/s");

          t.addData("size", size);
          t.addData("speed", speed);
          t.setStatus(Message.SUCCESS);

          if (!file.delete()) {
            m_logger.warn("Can't delete file: " + file);
          }
        } catch (AlreadyBeingCreatedException e) {
          Cat.logError(e);
          t.setStatus(e);

          m_logger.error(String.format("Already being created (%s)!", path), e);
        } catch (AccessControlException e) {
          cat.logError(e);
          t.setStatus(e);
          m_logger.error(String.format("No permission to create HDFS file(%s)!", path), e);
        } catch (Exception e) {
          cat.logError(e);
          t.setStatus(e);
          m_logger.error(String.format("Uploading file(%s) to HDFS(%s) failed!", file, path), e);
        } finally {
          try {
            if (fdos != null) {
View Full Code Here


  public MessageTree loadMessage(String messageId) throws IOException {
    if (!m_serverConfigManager.isHdfsOn()) {
      return null;
    }

    MessageProducer cat = Cat.getProducer();
    Transaction t = cat.newTransaction("BucketService", getClass().getSimpleName());

    t.setStatus(Message.SUCCESS);

    try {
      MessageId id = MessageId.parse(messageId);
      final String path = m_pathBuilder.getPath(new Date(id.getTimestamp()), "");
      final StringBuilder sb = new StringBuilder();
      FileSystem fs = m_manager.getFileSystem("dump", sb);

      sb.append('/').append(path);

      final String key = id.getDomain() + '-' + id.getIpAddress();
      final String str = sb.toString();
      final Path basePath = new Path(str);
      final List<String> paths = new ArrayList<String>();

      fs.listStatus(basePath, new PathFilter() {
        @Override
        public boolean accept(Path p) {
          String name = p.getName();

          if (name.contains(key) && !name.endsWith(".idx")) {
            paths.add(path + name);
          }
          return false;
        }
      });

      t.addData(paths.toString());
      for (String dataFile : paths) {
        try {
          Cat.getProducer().logEvent("HDFSBucket", dataFile);
          HdfsMessageBucket bucket = m_buckets.get(dataFile);

          if (bucket == null) {
            bucket = (HdfsMessageBucket) lookup(MessageBucket.class, HdfsMessageBucket.ID);
            bucket.initialize(dataFile);
            m_buckets.put(dataFile, bucket);
          }
          if (bucket != null) {
            MessageTree tree = bucket.findById(messageId);

            if (tree != null && tree.getMessageId().equals(messageId)) {
              t.addData("path", dataFile);
              return tree;
            }
          }
        } catch (Exception e) {
          t.setStatus(e);
          Cat.logError(e);
        }
      }

      return null;
    } catch (IOException e) {
      t.setStatus(e);
      cat.logError(e);
      throw e;
    } catch (RuntimeException e) {
      t.setStatus(e);
      cat.logError(e);
      throw e;
    } finally {
      t.complete();
    }
  }
View Full Code Here

import com.dianping.cat.message.Transaction;

public class CatEnvironmentTest {
  @Test
  public void testWithoutInitialize() throws InterruptedException {
    MessageProducer cat = Cat.getProducer();
    Transaction t = cat.newTransaction("TestType", "TestName");

    t.addData("data here");
    t.setStatus("TestStatus");
    t.complete();
View Full Code Here

  }

  @Test
  public void testWithInitialize() throws InterruptedException {
    Cat.initialize(new File("/data/appdatas/cat/client.xml"));
    MessageProducer cat = Cat.getProducer();
    Transaction t = cat.newTransaction("TestType", "TestName");

    t.addData("data here");
    t.setStatus("TestStatus");
    t.complete();
View Full Code Here

  }

  @Test
  public void testWithNoExistGlobalConfigInitialize() throws InterruptedException {
    Cat.initialize(new File("/data/appdatas/cat/clientNoExist.xml"));
    MessageProducer cat = Cat.getProducer();
    Transaction t = cat.newTransaction("TestType", "TestName");

    t.addData("data here");
    t.setStatus("TestStatus");
    t.complete();
View Full Code Here

    queue.initialize();
    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();
View Full Code Here

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

  @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();
View Full Code Here

    Date endDate = new Date(m_endTime - 1);

    m_logger.info(String.format("Finishing %s tasks in period [%s, %s]", m_tasks.size(), df.format(startDate),
          df.format(endDate)));

    MessageProducer cat = Cat.getProducer();
    Transaction t = cat.newTransaction("Checkpoint", "RealtimeConsumer");

    try {
      for (PeriodTask task : m_tasks) {
        task.finish();
      }

      t.setStatus(Message.SUCCESS);
    } catch (Throwable e) {
      cat.logError(e);
      t.setStatus(e);
    } finally {
      t.complete();

      m_logger.info(String.format("Finished %s tasks in period [%s, %s]", m_tasks.size(), df.format(startDate),
View Full Code Here

      logErrorInfo(tree);
    }
  }

  public void doCheckpoint() throws IOException {
    MessageProducer cat = Cat.getProducer();
    Transaction t = cat.newTransaction("Checkpoint", getClass().getSimpleName());

    try {
      long currentStartTime = getCurrentStartTime();
      Period period = m_periodManager.findPeriod(currentStartTime);

      for (MessageAnalyzer analyzer : period.getAnalzyers()) {
        try {
          analyzer.doCheckpoint(false);
        } catch (Exception e) {
          Cat.logError(e);
        }
      }

      try {
        // wait dump analyzer store completed
        Thread.sleep(10 * 1000);
      } catch (InterruptedException e) {
        // ignore
      }
      t.setStatus(Message.SUCCESS);
    } catch (RuntimeException e) {
      cat.logError(e);
      t.setStatus(e);
    } finally {
      t.complete();
    }
  }
View Full Code Here

  private void logError(LoggingEvent event) {
    ThrowableInformation info = event.getThrowableInformation();

    if (info != null) {
      MessageProducer cat = Cat.getProducer();
      Throwable exception = info.getThrowable();
      Object message = event.getMessage();
      MessageTree tree = Cat.getManager().getThreadLocalMessageTree();

      if (tree == null) {
        Transaction t = cat.newTransaction("System", "Log4jException");

        if (message != null) {
          cat.logError(String.valueOf(message), exception);
        } else {
          cat.logError(exception);
        }
        t.setStatus(Message.SUCCESS);
        t.complete();
      } else {
        cat.logError(exception);
      }
    }
  }
View Full Code Here

TOP

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

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.