Twitter ?? Nashorn ????(??)
- by Homma
????
Nashorn ? Java ??????? Twitter ???????????????????? JavaFX ???????????????
?????
??? jlaskey ??? Nashorn Blog ????????????? https://blogs.oracle.com/nashorn/entry/nashorn_in_the_twitterverse_continued ????????
??
Twitter ???????????????????????? JavaFX ??????????????????????????????? Nashorn ?? JavaFX ??????????????JavaFX ???????????????????????????????????????Nashorn ? Java ????????????????????????????????????(JavaFX ?????????????????????)?
?????????????????????????????????????????????? Twitter ?????????????????????????
var twitter4j = Packages.twitter4j;
var TwitterFactory = twitter4j.TwitterFactory;
var Query = twitter4j.Query;
function getTrendingData() {
var twitter = new TwitterFactory().instance;
var query = new Query("nashorn OR nashornjs");
query.since("2012-11-21");
query.count = 100;
var data = {};
do {
var result = twitter.search(query);
var tweets = result.tweets;
for each (var tweet in tweets) {
var date = tweet.createdAt;
var key = (1900 + date.year) + "/" +
(1 + date.month) + "/" +
date.date;
data[key] = (data[key] || 0) + 1;
}
} while (query = result.nextQuery());
return data;
}
??????????????????getTrendingData() ??????????????(??????????Nashorn ???????? OpenJDK ?????? 2012 ? 11 ? 21 ???)???????????????????????????????????
????JavaFX ? BarChart ???????????
var javafx = Packages.javafx;
var Stage = javafx.stage.Stage
var Scene = javafx.scene.Scene;
var Group = javafx.scene.Group;
var Chart = javafx.scene.chart.Chart;
var FXCollections = javafx.collections.FXCollections;
var ObservableList = javafx.collections.ObservableList;
var CategoryAxis = javafx.scene.chart.CategoryAxis;
var NumberAxis = javafx.scene.chart.NumberAxis;
var BarChart = javafx.scene.chart.BarChart;
var XYChart = javafx.scene.chart.XYChart;
var Series = javafx.scene.chart.XYChart.Series;
var Data = javafx.scene.chart.XYChart.Data;
function graph(stage, data) {
var root = new Group();
stage.scene = new Scene(root);
var dates = Object.keys(data);
var xAxis = new CategoryAxis();
xAxis.categories = FXCollections.observableArrayList(dates);
var yAxis = new NumberAxis("Tweets", 0.0, 200.0, 50.0);
var series = FXCollections.observableArrayList();
for (var date in data) {
series.add(new Data(date, data[date]));
}
var tweets = new Series("Tweets", series);
var barChartData = FXCollections.observableArrayList(tweets);
var chart = new BarChart(xAxis, yAxis, barChartData, 25.0);
root.children.add(chart);
}
????????????????????????????????stage.scene = new Scene(root) ? stage.setScene(new Scene(root)) ????????????????????Nashorn ? stage ??????? scene ???????????????????(Dynalink ?????????)Java Beans ???????????????? (setScene()) ???????????????????????????????Nashorn ? FXCollections ??????????????????????????????observableArrayList(dates) ??????????Nashorn ? JavaScript ??? (dates) ? Java ???????????????????????????? JavaScript ?????????????????? Java ?????????????????????????????????????????????????????????????
????????????????????????????????? JavaFX ????????????????????????
JavaFX ??????????????javafx.application.Application ??????????????????????????? JavaFX ?????????????????????????????????????????????????
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import javafx.application.Application;
import javafx.stage.Stage;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
public class TrendingMain extends Application {
private static final ScriptEngineManager
MANAGER = new ScriptEngineManager();
private final ScriptEngine engine = MANAGER.getEngineByName("nashorn");
private Trending trending;
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage stage) throws Exception {
trending = (Trending) load("Trending.js");
trending.start(stage);
}
@Override
public void stop() throws Exception {
trending.stop();
}
private Object load(String script) throws IOException, ScriptException {
try (final InputStream is = TrendingMain.class.getResourceAsStream(script)) {
return engine.eval(new InputStreamReader(is, "utf-8"));
}
}
}
???? Nashorn ??????? JSR-223 ? javax.script ?????????
private static final ScriptEngineManager MANAGER = new ScriptEngineManager();
private final ScriptEngine engine = MANAGER.getEngineByName("nashorn");
????????? JavaScript ???????? Nashorn ????????????????????
load ???????????????????????engine ???????????????load ?????????????
???????????????Java ???????????????????????????????????????????????????? Java ????????????????JavaFX ???????? start ????? stop ??????????????????????????????????????
public interface Trending {
public void start(Stage stage) throws Exception;
public void stop() throws Exception;
}
??????????????????????????????
function newTrending() {
return new Packages.Trending() {
start: function(stage) {
var data = getTrendingData();
graph(stage, data);
stage.show();
},
stop: function() {
}
}
}
newTrending();
?????? Trending ?????????????????????start ????? stop ??????????????????????????????????? eval ???? Java ???????????????
trending = (Trending) load("Trending.js");
????????????????Trending.js ??????? getTrendingData ???????????? newTrending ????????????????????? Java ?????????newTrending ????????? eval ????????? Trending ?????????????????????????????????????????????????????????
trending.start(stage);
????????
????
Nashorn ????????? http://www.myexpospace.com/JavaOne2012/SessionFiles/CON5251_PDF_5251_0001.pdf ????????
Dynalink ??????? https://github.com/szegedi/dynalink ????????