PageRenderTime 2787ms CodeModel.GetById 30ms RepoModel.GetById 0ms app.codeStats 0ms

/htest/flumetest/src/main/java/com/zdingke/flume/gangliasink/GangliaSink.java

https://gitlab.com/zhengdingke/htest
Java | 58 lines | 48 code | 9 blank | 1 comment | 2 complexity | c0dae8fcda57b97f601d851781231176 MD5 | raw file
  1. package com.zdingke.flume.gangliasink;
  2. import org.apache.commons.logging.Log;
  3. import org.apache.commons.logging.LogFactory;
  4. import org.apache.flume.Channel;
  5. import org.apache.flume.Context;
  6. import org.apache.flume.Event;
  7. import org.apache.flume.EventDeliveryException;
  8. import org.apache.flume.Transaction;
  9. import org.apache.flume.conf.Configurable;
  10. import org.apache.flume.sink.AbstractSink;
  11. import com.alibaba.fastjson.JSON;
  12. import com.zdingke.flume.ganglia.metric.GangliaWriter;
  13. public class GangliaSink extends AbstractSink implements Configurable {
  14. private static final Log log = LogFactory.getLog(GangliaSink.class);
  15. @Override
  16. public Status process() throws EventDeliveryException {
  17. Channel ch = getChannel();
  18. Transaction txn = ch.getTransaction();
  19. try {
  20. txn.begin();
  21. Event e = getChannel().take();
  22. if (e == null) {
  23. txn.rollback();
  24. return Status.BACKOFF;
  25. }
  26. String data = new String(e.getBody());
  27. sendDataToGanglia(data);
  28. txn.commit();
  29. return Status.READY;
  30. } catch (Exception ex) {
  31. txn.rollback();
  32. log.error(ex.getMessage(), ex);
  33. return Status.BACKOFF;
  34. } finally {
  35. txn.close();
  36. }
  37. }
  38. private void sendDataToGanglia(String data) {
  39. MetricInfo info = JSON.parseObject(data,MetricInfo.class);
  40. String []strs= info.getGmetadInfo().split(":");
  41. GangliaWriter writer = GangliaWriter.builder().setHost(strs[0]).setPort(Integer.parseInt(strs[1])).setAddressingMode(null).setDmax(null).setTmax(null)
  42. .setUnits("km/h").setSlope(null).setTtl(null).setV31(null).setGroupName(info.getServiceName()).build();
  43. writer.doWrite(info.getMap());
  44. }
  45. @Override
  46. public void configure(Context context) {
  47. // TODO Auto-generated method stub
  48. }
  49. }