質問編集履歴
2
解決方法について記述します
test
CHANGED
File without changes
|
test
CHANGED
@@ -72,9 +72,7 @@
|
|
72
72
|
|
73
73
|
|
74
74
|
|
75
|
-
@Bean
|
76
|
-
|
77
|
-
//
|
75
|
+
@Bean(name = "job1")//beanアノテーションにname属性を追加しました。※修正後追記
|
78
76
|
|
79
77
|
public Job job(JobBuilderFactory jobs, Step s) {
|
80
78
|
|
@@ -112,6 +110,8 @@
|
|
112
110
|
|
113
111
|
```html
|
114
112
|
|
113
|
+
|
114
|
+
|
115
115
|
<!DOCTYPE html>
|
116
116
|
|
117
117
|
<html lang="en" xmlns:th="http://www.thymeleaf.org">
|
@@ -142,6 +142,102 @@
|
|
142
142
|
|
143
143
|
```
|
144
144
|
|
145
|
+
```java
|
146
|
+
|
147
|
+
//コントローラー加筆※修正後追加
|
148
|
+
|
149
|
+
package jp.co.sample.sampleTraining.web.download;
|
150
|
+
|
151
|
+
|
152
|
+
|
153
|
+
import java.util.HashMap;
|
154
|
+
|
155
|
+
import java.util.Map;
|
156
|
+
|
157
|
+
|
158
|
+
|
159
|
+
import javax.batch.operations.JobRestartException;
|
160
|
+
|
161
|
+
|
162
|
+
|
163
|
+
import org.springframework.batch.core.Job;
|
164
|
+
|
165
|
+
import org.springframework.batch.core.JobParameter;
|
166
|
+
|
167
|
+
import org.springframework.batch.core.JobParameters;
|
168
|
+
|
169
|
+
import org.springframework.batch.core.JobParametersInvalidException;
|
170
|
+
|
171
|
+
import org.springframework.batch.core.launch.JobLauncher;
|
172
|
+
|
173
|
+
import org.springframework.batch.core.repository.JobExecutionAlreadyRunningException;
|
174
|
+
|
175
|
+
import org.springframework.batch.core.repository.JobInstanceAlreadyCompleteException;
|
176
|
+
|
177
|
+
import org.springframework.beans.factory.annotation.Autowired;
|
178
|
+
|
179
|
+
import org.springframework.stereotype.Controller;
|
180
|
+
|
181
|
+
import org.springframework.web.bind.annotation.RequestMapping;
|
182
|
+
|
183
|
+
|
184
|
+
|
185
|
+
@Controller
|
186
|
+
|
187
|
+
@RequestMapping("/download")
|
188
|
+
|
189
|
+
public class DownloadController {
|
190
|
+
|
191
|
+
|
192
|
+
|
193
|
+
@Autowired
|
194
|
+
|
195
|
+
JobLauncher jobLauncher;
|
196
|
+
|
197
|
+
|
198
|
+
|
199
|
+
@Autowired
|
200
|
+
|
201
|
+
Job job1;
|
202
|
+
|
203
|
+
|
204
|
+
|
205
|
+
@RequestMapping("/csvDownload")
|
206
|
+
|
207
|
+
public String csvDownload()
|
208
|
+
|
209
|
+
throws JobExecutionAlreadyRunningException, JobRestartException, JobInstanceAlreadyCompleteException,
|
210
|
+
|
211
|
+
JobParametersInvalidException, org.springframework.batch.core.repository.JobRestartException {
|
212
|
+
|
213
|
+
jobLauncher.run(job1, createInitialJobParameterMap());
|
214
|
+
|
215
|
+
return "/item/downloadPage";
|
216
|
+
|
217
|
+
}
|
218
|
+
|
219
|
+
|
220
|
+
|
221
|
+
private JobParameters createInitialJobParameterMap() {
|
222
|
+
|
223
|
+
Map<String, JobParameter> m = new HashMap<>();
|
224
|
+
|
225
|
+
m.put("time", new JobParameter(System.currentTimeMillis()));
|
226
|
+
|
227
|
+
JobParameters p = new JobParameters(m);
|
228
|
+
|
229
|
+
return p;
|
230
|
+
|
231
|
+
}
|
232
|
+
|
233
|
+
|
234
|
+
|
235
|
+
}
|
236
|
+
|
237
|
+
|
238
|
+
|
239
|
+
```
|
240
|
+
|
145
241
|
###試したこと
|
146
242
|
|
147
243
|
ネットで調べたのですが、中々それらしい記事は見つかりませんでした。
|
@@ -158,6 +254,20 @@
|
|
158
254
|
|
159
255
|
|
160
256
|
|
257
|
+
<以降解決後追記>
|
258
|
+
|
259
|
+
まず、jobのbeanアノテーションにnameをつけました。
|
260
|
+
|
261
|
+
そしてリンクに飛ぶようrequestmappingアノテーションをつけたcontrollerクラスの
|
262
|
+
|
263
|
+
メソッドにjobLuncherクラスのrunメソッドを作成し、第一引数にjobのbeanアノテーションにつけた
|
264
|
+
|
265
|
+
nameを入れたところ、実行タイミングを変える事が出来ました。
|
266
|
+
|
267
|
+
念のため修正後のコードも記載しておきます。
|
268
|
+
|
269
|
+
|
270
|
+
|
161
271
|
###補足情報(言語/FW/ツール等のバージョンなど)
|
162
272
|
|
163
273
|
java/spring mvc/sts
|
1
ご指摘を頂いた為修正致しました。【試したこと】の欄に追記させて頂きました。コードも少し削らさせて頂きました。(今後質問を投稿する際は気をつけます。)
test
CHANGED
File without changes
|
test
CHANGED
@@ -8,7 +8,9 @@
|
|
8
8
|
|
9
9
|
|
10
10
|
|
11
|
-
プログラムを実行した際にjobも一緒に実行されてしまい
|
11
|
+
プログラムを実行した際にjobも一緒に実行されてしまい、
|
12
|
+
|
13
|
+
リンクを押下した時のみ実行されるように試行錯誤しています。
|
12
14
|
|
13
15
|
|
14
16
|
|
@@ -40,19 +42,19 @@
|
|
40
42
|
|
41
43
|
import org.springframework.context.annotation.Configuration;
|
42
44
|
|
43
|
-
import org.springframework.web.bind.annotation.RequestMapping;
|
44
45
|
|
45
46
|
|
47
|
+
//import org.springframework.web.bind.annotation.RequestMapping;
|
46
48
|
|
47
|
-
import jp.co.sample.sampleTraining.service.ItemService;
|
48
|
-
|
49
|
-
import
|
49
|
+
//import org.springframework.stereotype.Controller;
|
50
50
|
|
51
51
|
|
52
52
|
|
53
53
|
@Configuration
|
54
54
|
|
55
55
|
@EnableBatchProcessing
|
56
|
+
|
57
|
+
//ここに実験的に@Controller, @RequestMapping をつけたものの実行のタイミングは変わりませんでした。
|
56
58
|
|
57
59
|
public class SampleBatchApplication {
|
58
60
|
|
@@ -68,17 +70,13 @@
|
|
68
70
|
|
69
71
|
private StepBuilderFactory steps;
|
70
72
|
|
71
|
-
|
72
|
-
|
73
|
-
@Autowired
|
74
|
-
|
75
|
-
private ItemService service;
|
76
|
-
|
77
73
|
|
78
74
|
|
79
75
|
@Bean
|
80
76
|
|
77
|
+
//ここに実験的に@RequestMapping をつけたものの実行のタイミングは変わりませんでした。
|
78
|
+
|
81
|
-
public Job job(JobBuilderFactory jobs, Step s
|
79
|
+
public Job job(JobBuilderFactory jobs, Step s) {
|
82
80
|
|
83
81
|
return jobs
|
84
82
|
|
@@ -86,9 +84,7 @@
|
|
86
84
|
|
87
85
|
.incrementer(new RunIdIncrementer())
|
88
86
|
|
89
|
-
.start(s
|
87
|
+
.start(s)
|
90
|
-
|
91
|
-
.next(s2)
|
92
88
|
|
93
89
|
.build();
|
94
90
|
|
@@ -96,31 +92,13 @@
|
|
96
92
|
|
97
93
|
|
98
94
|
|
99
|
-
@Bean(name = "s
|
95
|
+
@Bean(name = "s")
|
100
96
|
|
101
|
-
public Step step
|
97
|
+
public Step step() {
|
102
98
|
|
103
|
-
return steps.get("step
|
99
|
+
return steps.get("step").tasklet((stepContribution, chunkContext) -> {
|
104
100
|
|
105
|
-
|
106
|
-
|
107
|
-
return RepeatStatus.FINISHED;
|
108
|
-
|
109
|
-
}).build();
|
110
|
-
|
111
|
-
}
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
@Bean(name = "s2")
|
116
|
-
|
117
|
-
public Step step2() {
|
118
|
-
|
119
|
-
return steps.get("step2").tasklet((stepContribution, chunkContext) -> {
|
120
|
-
|
121
|
-
Integer totalTime = service.totalTime();
|
122
|
-
|
123
|
-
|
101
|
+
System.out.println("実行の確認");
|
124
102
|
|
125
103
|
return RepeatStatus.FINISHED;
|
126
104
|
|
@@ -172,6 +150,14 @@
|
|
172
150
|
|
173
151
|
|
174
152
|
|
153
|
+
<以降追記部分>
|
154
|
+
|
155
|
+
違うだろうなと思いつつアノテーション(@RequestMapping)などをつけてみたりしていました。
|
156
|
+
|
157
|
+
あとは何かspring batchに実行条件を変える設定等があるのではないかと考え調べています。
|
158
|
+
|
159
|
+
|
160
|
+
|
175
161
|
###補足情報(言語/FW/ツール等のバージョンなど)
|
176
162
|
|
177
163
|
java/spring mvc/sts
|