This repository was archived by the owner on Jan 28, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 101
Expand file tree
/
Copy pathbatch-processing003.html
More file actions
307 lines (286 loc) · 9.47 KB
/
batch-processing003.html
File metadata and controls
307 lines (286 loc) · 9.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8"/>
<title>Simple Use Case</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href="css/style.css" rel="stylesheet">
<script src="https://use.fontawesome.com/96c4d89611.js"></script>
</head>
<body>
<table id="doc-title" cellspacing="0" cellpadding="0">
<tr>
<td align="left" valign="top">
<b>Java Platform, Enterprise Edition (Java EE) 8</b><br />
<b>The Java EE Tutorial</b>
<!-- <p class="beta">Beta Draft (Pre-General Availability)</p> -->
</td>
</tr>
</table>
<hr />
<table width="90%" id="top-nav" cellspacing="0" cellpadding="0">
<colgroup>
<col width="12%"/>
<col width="12%"/>
<col width="*"/>
</colgroup>
<tr>
<td align="left">
<a href="batch-processing002.html">
<span class="vector-font"><i class="fa fa-arrow-circle-left" aria-hidden="true"></i></span>
<span style="position:relative;top:-2px;">Previous</span>
</a>
</td>
<td align="left">
<a href="batch-processing004.html">
<span class=" vector-font"><i class="fa fa-arrow-circle-right vector-font" aria-hidden="true"></i></span>
<span style="position:relative;top:-2px;">Next</span>
</a>
</td>
<td align="right">
<a href="toc.html">
<span class=" vector-font"><i class="fa fa-list vector-font" aria-hidden="true"></i></span>
<span style="position:relative;top:-2px;">Contents</span>
</a>
</td>
</tr>
</table>
<div id="preamble">
<div class="sectionbody">
<div class="paragraph">
<p><a id="BCGHBJIG"></a><a id="simple-use-case"></a></p>
</div>
</div>
</div>
<div class="sect1">
<h2 id="_simple_use_case">Simple Use Case</h2>
<div class="sectionbody">
<div class="paragraph">
<p>This section demonstrates how to define a simple job using the Job
Specification Language (JSL) and how to implement the corresponding
batch artifacts. Refer to the rest of the sections in this chapter for
detailed descriptions of the elements in the batch framework.</p>
</div>
<div class="paragraph">
<p>The following job definition specifies a chunk step and a task step as
follows:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="prettyprint highlight"><code class="language-oac_no_warn" data-lang="oac_no_warn"><?xml version="1.0" encoding="UTF-8"?>
<job id="simplejob" xmlns="http://xmlns.jcp.org/xml/ns/javaee"
version="1.0">
<properties>
<property name="input_file" value="input.txt"/>
<property name="output_file" value="output.txt"/>
</properties>
<step id="mychunk" next="mytask">
<chunk>
<reader ref="MyReader"></reader>
<processor ref="MyProcessor"></processor>
<writer ref="MyWriter"></writer>
</chunk>
</step>
<step id="mytask">
<batchlet ref="MyBatchlet"></batchlet>
<end on="COMPLETED"/>
</step>
</job></code></pre>
</div>
</div>
<div class="paragraph">
<p><a id="sthref268"></a><a id="chunk-step"></a></p>
</div>
<div class="sect2">
<h3 id="_chunk_step">Chunk Step</h3>
<div class="paragraph">
<p>In most cases, you have to implement a checkpoint class for
chunk-oriented steps. The following class just keeps track of the line
number in a text file:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="prettyprint highlight"><code class="language-oac_no_warn" data-lang="oac_no_warn">public class MyCheckpoint implements Serializable {
private long lineNum = 0;
public void increase() { lineNum++; }
public long getLineNum() { return lineNum; }
}</code></pre>
</div>
</div>
<div class="paragraph">
<p>The following item reader implementation continues reading the input
file from the provided checkpoint if the job was restarted. The items
consist of each line in the text file (in more complex scenarios, the
items are custom Java types and the input source can be a database):</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="prettyprint highlight"><code class="language-oac_no_warn" data-lang="oac_no_warn">@Dependent
@Named("MyReader")
public class MyReader implements javax.batch.api.chunk.ItemReader {
private MyCheckpoint checkpoint;
private BufferedReader breader;
@Inject
JobContext jobCtx;
public MyReader() {}
@Override
public void open(Serializable ckpt) throws Exception {
if (ckpt == null)
checkpoint = new MyCheckpoint();
else
checkpoint = (MyCheckpoint) ckpt;
String fileName = jobCtx.getProperties()
.getProperty("input_file");
breader = new BufferedReader(new FileReader(fileName));
for (long i = 0; i < checkpoint.getLineNum(); i++)
breader.readLine();
}
@Override
public void close() throws Exception {
breader.close();
}
@Override
public Object readItem() throws Exception {
String line = breader.readLine();
return line;
}
}</code></pre>
</div>
</div>
<div class="paragraph">
<p>In the following case, the item processor only converts the line to
uppercase. More complex examples can process items in different ways or
transform them into custom output Java types:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="prettyprint highlight"><code class="language-oac_no_warn" data-lang="oac_no_warn">@Dependent
@Named("MyProcessor")
public class MyProcessor implements javax.batch.api.chunk.ItemProcessor {
public MyProcessor() {}
@Override
public Object processItem(Object obj) throws Exception {
String line = (String) obj;
return line.toUpperCase();
}
}</code></pre>
</div>
</div>
<table class="tableblock frame-all grid-all spread">
<colgroup>
<col style="width: 100%;">
</colgroup>
<tbody>
<tr>
<td class="tableblock halign-left valign-top"><div><div class="paragraph">
<p>Note:</p>
</div>
<div class="paragraph">
<p>The batch processing API does not support generics. In most cases, you
need to cast items to their specific type before processing them.</p>
</div></div></td>
</tr>
</tbody>
</table>
<div class="paragraph">
<p>The item writer writes the processed items to the output file. It
overwrites the output file if no checkpoint is provided; otherwise, it
resumes writing at the end of the file. Items are written in chunks:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="prettyprint highlight"><code class="language-oac_no_warn" data-lang="oac_no_warn">@Dependent
@Named("MyWriter")
public class MyWriter implements javax.batch.api.chunk.ItemWriter {
private BufferedWriter bwriter;
@Inject
private JobContext jobCtx;
@Override
public void open(Serializable ckpt) throws Exception {
String fileName = jobCtx.getProperties()
.getProperty("output_file");
bwriter = new BufferedWriter(new FileWriter(fileName,
(ckpt != null)));
}
@Override
public void writeItems(List<Object> items) throws Exception {
for (int i = 0; i < items.size(); i++) {
String line = (String) items.get(i);
bwriter.write(line);
bwriter.newLine();
}
}
@Override
public Serializable checkpointInfo() throws Exception {
return new MyCheckpoint();
}</code></pre>
</div>
</div>
<div class="paragraph">
<p><a id="sthref269"></a><a id="task-step"></a></p>
</div>
</div>
<div class="sect2">
<h3 id="_task_step">Task Step</h3>
<div class="paragraph">
<p>The task step displays the length of the output file. In more complex
scenarios, task steps perform any task that does not fit the chunk
processing programming model:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="prettyprint highlight"><code class="language-oac_no_warn" data-lang="oac_no_warn">@Dependent
@Named("MyBatchlet")
public class MyBatchlet implements javax.batch.api.chunk.Batchlet {
@Inject
private JobContext jobCtx;
@Override
public String process() throws Exception {
String fileName = jobCtx.getProperties()
.getProperty("output_file");
System.out.println(""+(new File(fileName)).length());
return "COMPLETED";
}
}</code></pre>
</div>
</div>
</div>
</div>
</div>
<hr />
<table width="90%" id="bottom-nav" cellspacing="0" cellpadding="0">
<colgroup>
<col width="12%"/>
<col width="12%"/>
<col width="*"/>
</colgroup>
<tr>
<td align="left">
<a href="batch-processing002.html">
<span class=" vector-font"><i class="fa fa-arrow-circle-left" aria-hidden="true"></i></span>
<span style="position:relative;top:-2px;">Previous</span>
</a>
</td>
<td align="left">
<a href="batch-processing004.html">
<span class="vector-font"><i class="fa fa-arrow-circle-right vector-font" aria-hidden="true"></i></span>
<span style="position:relative;top:-2px;">Next</span>
</a>
</td>
<td align="right">
<a href="toc.html">
<span class="vector-font"><i class="fa fa-list vector-font" aria-hidden="true"></i></span>
<span style="position:relative;top:-2px;">Contents</span>
</a>
</td>
</tr>
</table>
<span id="copyright">
<a href="img/cpyr.adoc">
<img src="img/oracle.gif" height="10px" alt="Oracle Logo" />
<span>Copyright © 2017, Oracle and/or its affiliates. All rights reserved.</span>
</a>
</span>
<!--<p align="center" class="beta">Beta Draft (Pre-General Availability)</p> -->
</body>
</html>