public static int getValueInt(String name) {
    Configuration root = Configuration.root();

    if (root.getInt(name) == null) {
      return -1;
    }

    int value = root.getInt(name);
    Logger.info(name + ":" + value);

    return value;
  }
Exemple #2
0
  private static void startStream(List<String> terms) throws TwitterException {
    if (twitter != null) {
      twitter.cleanUp();
    }
    if (esClient != null) {
      esClient.close();
      esClient = null;
    }

    play.Configuration pconf = Play.application().configuration();
    String elasticSearchCluster = pconf.getString("tweet.elasticsearch.cluster.name");
    if (elasticSearchCluster != null) {
      Logger.info("Configuring ElasticSearch...");
      Settings settings =
          ImmutableSettings.settingsBuilder().put("cluster.name", elasticSearchCluster).build();

      esClient =
          new TransportClient(settings)
              .addTransportAddress(
                  new InetSocketTransportAddress(
                      pconf.getString("tweet.elasticsearch.transport.host"),
                      pconf.getInt("tweet.elasticsearch.transport.port")));
    } else {
      esClient = null;
    }

    twitter4j.conf.Configuration tconf = Application.getTwitterConfiguration();
    TwitterStreamFactory tf = new TwitterStreamFactory(tconf);
    twitter = tf.getInstance();
    StatusListener l =
        new TweetListener(
            terms,
            esClient,
            pconf.getString("tweet.elasticsearch.index"),
            pconf.getString("tweet.elasticsearch.type"));
    twitter.addListener(l);

    String[] tracks = new String[terms.size()];
    StringBuffer termsString = new StringBuffer();
    for (int i = 0; i < terms.size(); i++) {
      tracks[i] = terms.get(i);
      if (i != 0) termsString.append(",");
      termsString.append(terms.get(i));
    }
    FilterQuery q = new FilterQuery().track(tracks);
    twitter.filter(q);
    Logger.info("Starting listening for tweets using terms " + termsString.toString() + "...");
  }
  @Inject
  public MorphiumConnection(ApplicationLifecycle lifecycle, Configuration configuration) {
    final String dbName = configuration.getString("mongodb.name");
    final String dbHostName = configuration.getString("mongodb.host.name");
    final Integer dbHostPort = configuration.getInt("mongodb.host.port");

    final String username = configuration.getString("mongodb.username");
    final String password = configuration.getString("mongodb.password");

    Logger.info(
        "Mong db name: "
            + dbName
            + ", host : "
            + dbHostName
            + ", port : "
            + dbHostPort
            + ", username:"******", passsword:"
            + password);

    cfg = new MorphiumConfig();
    cfg.setDatabase(dbName);
    try {
      cfg.addHost(dbHostName, dbHostPort);
      if (StringUtils.isNotEmpty(username)) {
        cfg.setMongoLogin(username);
      }
      if (StringUtils.isNotEmpty(password)) {
        cfg.setMongoPassword(password);
      }
      m = new Morphium(cfg);

    } catch (UnknownHostException e) {
      e.printStackTrace();
      m = null;
      cfg = null;
    }

    lifecycle.addStopHook(
        () -> {
          if (m != null) {
            m.close();
          }
          return F.Promise.pure(null);
        });
  }