Java Reference
In-Depth Information
batching framework API gives you many possibilities without too many complications in
the area of job management.
Creating a job-based batch step
Our chunk-based job was great to process big data sets. However, what if we only want to
perform a specific task? Besides creating chunks, we can also define steps that will simply
call a process method of a specific class. These kinds of classes must implement the
Batchlet interface (or extend the AbstractBatchlet class).
In our sample, let's try to contact an external API to ask about the current Bitcoin ex-
change rate (a decentralized, virtual currency). Then, we will store the current prices of
our tickets in a simple flat file. Our batchlet would be as follows:
@Named
public class BitcoinTask extends AbstractBatchlet {
private static final String EXTERNAL_API =
"https://api.bitcoinaverage.com/exchanges/USD";
public static final String FILENAME_PARAM =
"bitcoinFile";
@Inject
private SeatTypeDao seatTypeDao;
@Inject
private JobContext jobContext;
@Override
public String process() throws Exception { // [1]
WebTarget api =
ClientBuilder.newClient().target(EXTERNAL_API);
Response response = api.request().get();
JsonObject entity =
response.readEntity(JsonObject.class); // [2]
double averageValue =
entity.getJsonObject("btce").getJsonObject("rates").getJsonNumber("bid").doubleValue();
Search WWH ::




Custom Search