我试图解决akka-streams的问题。我希望我的初始生产者不断获取队列/ mongodb集合并在配置的时间间隔内完成。实现它的常见做法是什么?
答案 0 :(得分:4)
你的意思是固定的时间间隔?
从JsonObjectRequest jsonObjectRequest1 = new JsonObjectRequest(Request.Method.GET, URL_1_CURRENCY, (JSONObject) null,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
JSONObject ratesJsonObj = response.getJSONObject("rates");
String cursRon = ratesJsonObj.getString("RON");
String cursEuro = ratesJsonObj.getString("EUR");
String cursDolar = ratesJsonObj.getString("USD");
model.setCurrency(cursRon);
TXT.setText(cursRon);
Toast.makeText(MainActivity.this, "RON: " + cursRon, Toast.LENGTH_SHORT).show();
JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(Request.Method.GET, URL_2_COUNTRY, (JSONObject) null, new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
try {
for (int i = 179; i < 185 ; i++) {
JSONObject jsonObjectCountry = response.getJSONObject(i);
String countryROM = jsonObjectCountry.getString("name");
String countryCODE = jsonObjectCountry.getString("code");
model.setCountry(countryROM);
model.setCountryCode(countryCODE);
Log.v(TAG, countryROM + " " + countryCODE);
//move bucketCurrency.add(model); to here
bucketCurrency.add(model);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
});
AppController.getInstance().addToRequestQueue(jsonArrayRequest);
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
error.getMessage();
}
});
AppController.getInstance().addToRequestQueue(jsonObjectRequest1);
开始?
放入conflate that drops ticks以防止比下游更频繁地查询数据库,如果这是你想要的。
答案 1 :(得分:1)
此外,还有一个名为油门的概念。它可以使用如下:
Source(...).throttle(50, 1.second, 1, ThrottleMode.Shaping)
它允许您指定一个提取间隔,例如每秒超过50个项目。可以在此处的文档中找到一个示例:http://doc.akka.io/docs/akka/current/scala/stream/stream-quickstart.html#Time-Based_Processing