Skip to content

Commit 737f6ac

Browse files
mdeinumfmbenhassine
authored andcommitted
Added isXXXEnabled for logging statements
Guarded the logging statements which do concatenation of strings or calling toString on objects. When a log level isn't enabled this still would produce garbage that would need to be collected. Guarded all logging up to info, warn and error can be assumed to be enabled on a production system.
1 parent 718837f commit 737f6ac

File tree

30 files changed

+175
-102
lines changed

30 files changed

+175
-102
lines changed

spring-batch-core/src/main/java/org/springframework/batch/core/configuration/support/JobFactoryRegistrationListener.java

+7-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2006-2007 the original author or authors.
2+
* Copyright 2006-2021 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -54,7 +54,9 @@ public void setJobRegistry(JobRegistry jobRegistry) {
5454
* @throws Exception if there is a problem
5555
*/
5656
public void bind(JobFactory jobFactory, Map<String, ?> params) throws Exception {
57-
logger.info("Binding JobFactory: " + jobFactory.getJobName());
57+
if (logger.isInfoEnabled()) {
58+
logger.info("Binding JobFactory: " + jobFactory.getJobName());
59+
}
5860
jobRegistry.register(jobFactory);
5961
}
6062

@@ -66,7 +68,9 @@ public void bind(JobFactory jobFactory, Map<String, ?> params) throws Exception
6668
* @throws Exception if there is a problem
6769
*/
6870
public void unbind(JobFactory jobFactory, Map<String, ?> params) throws Exception {
69-
logger.info("Unbinding JobFactory: " + jobFactory.getJobName());
71+
if (logger.isInfoEnabled()) {
72+
logger.info("Unbinding JobFactory: " + jobFactory.getJobName());
73+
}
7074
jobRegistry.unregister(jobFactory.getJobName());
7175
}
7276

spring-batch-core/src/main/java/org/springframework/batch/core/job/AbstractJob.java

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2006-2020 the original author or authors.
2+
* Copyright 2006-2021 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -337,8 +337,10 @@ public final void execute(JobExecution execution) {
337337
}
338338

339339
} catch (JobInterruptedException e) {
340-
logger.info("Encountered interruption executing job: "
341-
+ e.getMessage());
340+
if (logger.isInfoEnabled()) {
341+
logger.info("Encountered interruption executing job: "
342+
+ e.getMessage());
343+
}
342344
if (logger.isDebugEnabled()) {
343345
logger.debug("Full exception", e);
344346
}

spring-batch-core/src/main/java/org/springframework/batch/core/job/SimpleStepHandler.java

+12-6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2006-2014 the original author or authors.
2+
* Copyright 2006-2021 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -116,9 +116,11 @@ public StepExecution handleStep(Step step, JobExecution execution) throws JobInt
116116
if (stepExecutionPartOfExistingJobExecution(execution, lastStepExecution)) {
117117
// If the last execution of this step was in the same job, it's
118118
// probably intentional so we want to run it again...
119-
logger.info(String.format("Duplicate step [%s] detected in execution of job=[%s]. "
120-
+ "If either step fails, both will be executed again on restart.", step.getName(), jobInstance
121-
.getJobName()));
119+
if (logger.isInfoEnabled()) {
120+
logger.info(String.format("Duplicate step [%s] detected in execution of job=[%s]. "
121+
+ "If either step fails, both will be executed again on restart.", step.getName(), jobInstance
122+
.getJobName()));
123+
}
122124
lastStepExecution = null;
123125
}
124126
StepExecution currentStepExecution = lastStepExecution;
@@ -143,7 +145,9 @@ public StepExecution handleStep(Step step, JobExecution execution) throws JobInt
143145

144146
jobRepository.add(currentStepExecution);
145147

146-
logger.info("Executing step: [" + step.getName() + "]");
148+
if (logger.isInfoEnabled()) {
149+
logger.info("Executing step: [" + step.getName() + "]");
150+
}
147151
try {
148152
step.execute(currentStepExecution);
149153
currentStepExecution.getExecutionContext().put("batch.executed", true);
@@ -215,7 +219,9 @@ protected boolean shouldStart(StepExecution lastStepExecution, JobExecution jobE
215219
|| stepStatus == BatchStatus.ABANDONED) {
216220
// step is complete, false should be returned, indicating that the
217221
// step should not be started
218-
logger.info("Step already complete or not restartable, so no action to execute: " + lastStepExecution);
222+
if (logger.isInfoEnabled()) {
223+
logger.info("Step already complete or not restartable, so no action to execute: " + lastStepExecution);
224+
}
219225
return false;
220226
}
221227

spring-batch-core/src/main/java/org/springframework/batch/core/jsr/configuration/xml/BatchParser.java

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2013 the original author or authors.
2+
* Copyright 2013-2021 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -71,7 +71,9 @@ private void parseRefElements(Element element,
7171
if(!registry.containsBeanDefinition(beanName)) {
7272
registry.registerBeanDefinition(beanName, beanDefinition);
7373
} else {
74-
logger.info("Ignoring batch.xml bean definition for " + beanName + " because another bean of the same name has been registered");
74+
if (logger.isInfoEnabled()) {
75+
logger.info("Ignoring batch.xml bean definition for " + beanName + " because another bean of the same name has been registered");
76+
}
7577
}
7678
}
7779
}

spring-batch-core/src/main/java/org/springframework/batch/core/jsr/configuration/xml/JsrBeanDefinitionDocumentReader.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2013-2018 the original author or authors.
2+
* Copyright 2013-2021 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -200,7 +200,7 @@ private String resolveValue(String value) {
200200

201201
String resolvedProperty = properties.getProperty(extractedProperty, NULL);
202202

203-
if (NULL.equals(resolvedProperty)) {
203+
if (NULL.equals(resolvedProperty) && LOG.isInfoEnabled()) {
204204
LOG.info(propertyType + " with key of: " + extractedProperty + " could not be resolved. Possible configuration error?");
205205
}
206206

spring-batch-core/src/main/java/org/springframework/batch/core/jsr/job/JsrStepHandler.java

+7-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2014 the original author or authors.
2+
* Copyright 2014-2021 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -93,7 +93,9 @@ protected boolean shouldStart(StepExecution lastStepExecution, JobExecution jobE
9393

9494
if(CollectionUtils.isEmpty(jobExecution.getStepExecutions()) && lastJobExecution.getStatus() == BatchStatus.STOPPED && StringUtils.hasText(restartStep)) {
9595
if(!restartStep.equals(step.getName()) && !jobExecution.getExecutionContext().containsKey("batch.startedStep")) {
96-
logger.info("Job was stopped and should restart at step " + restartStep + ". The current step is " + step.getName());
96+
if (logger.isInfoEnabled()) {
97+
logger.info("Job was stopped and should restart at step " + restartStep + ". The current step is " + step.getName());
98+
}
9799
return false;
98100
} else {
99101
// Indicates the starting point for execution evaluation per JSR-352
@@ -113,7 +115,9 @@ protected boolean shouldStart(StepExecution lastStepExecution, JobExecution jobE
113115
|| stepStatus == BatchStatus.ABANDONED) {
114116
// step is complete, false should be returned, indicating that the
115117
// step should not be started
116-
logger.info("Step already complete or not restartable, so no action to execute: " + lastStepExecution);
118+
if (logger.isInfoEnabled()) {
119+
logger.info("Step already complete or not restartable, so no action to execute: " + lastStepExecution);
120+
}
117121
return false;
118122
}
119123

spring-batch-core/src/main/java/org/springframework/batch/core/jsr/step/builder/JsrPartitionStepBuilder.java

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2013 the original author or authors.
2+
* Copyright 2013-2021 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -94,8 +94,10 @@ public Step build() {
9494
name = getStep().getName();
9595
}
9696
catch (Exception e) {
97-
logger.info("Ignored exception from step asking for name and allowStartIfComplete flag. "
98-
+ "Using default from enclosing PartitionStep (" + name + "," + allowStartIfComplete + ").");
97+
if (logger.isInfoEnabled()) {
98+
logger.info("Ignored exception from step asking for name and allowStartIfComplete flag. "
99+
+ "Using default from enclosing PartitionStep (" + name + "," + allowStartIfComplete + ").");
100+
}
99101
}
100102
}
101103
SimpleStepExecutionSplitter splitter = new SimpleStepExecutionSplitter();

spring-batch-core/src/main/java/org/springframework/batch/core/launch/support/JobRegistryBackgroundJobRunner.java

+13-6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2006-2013 the original author or authors.
2+
* Copyright 2006-2021 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -131,7 +131,9 @@ private void register(String[] paths) throws DuplicateJobException, IOException
131131
for (int j = 0; j < resources.length; j++) {
132132

133133
Resource path = resources[j];
134-
logger.info("Registering Job definitions from " + Arrays.toString(resources));
134+
if (logger.isInfoEnabled()) {
135+
logger.info("Registering Job definitions from " + Arrays.toString(resources));
136+
}
135137

136138
GenericApplicationContextFactory factory = new GenericApplicationContextFactory(path);
137139
factory.setApplicationContext(parentContext);
@@ -199,8 +201,9 @@ public static void main(String... args) throws Exception {
199201
final JobRegistryBackgroundJobRunner launcher = new JobRegistryBackgroundJobRunner(args[0]);
200202
errors.clear();
201203

202-
logger.info("Starting job registry in parent context from XML at: [" + args[0] + "]");
203-
204+
if (logger.isInfoEnabled()) {
205+
logger.info("Starting job registry in parent context from XML at: [" + args[0] + "]");
206+
}
204207
new Thread(new Runnable() {
205208
@Override
206209
public void run() {
@@ -221,7 +224,9 @@ public void run() {
221224

222225
synchronized (errors) {
223226
if (!errors.isEmpty()) {
224-
logger.info(errors.size() + " errors detected on startup of parent context. Rethrowing.");
227+
if (logger.isInfoEnabled()) {
228+
logger.info(errors.size() + " errors detected on startup of parent context. Rethrowing.");
229+
}
225230
throw errors.get(0);
226231
}
227232
}
@@ -231,7 +236,9 @@ public void run() {
231236
final String[] paths = new String[args.length - 1];
232237
System.arraycopy(args, 1, paths, 0, paths.length);
233238

234-
logger.info("Parent context started. Registering jobs from paths: " + Arrays.asList(paths));
239+
if (logger.isInfoEnabled()) {
240+
logger.info("Parent context started. Registering jobs from paths: " + Arrays.asList(paths));
241+
}
235242
launcher.register(paths);
236243

237244
if (System.getProperty(EMBEDDED) != null) {

spring-batch-core/src/main/java/org/springframework/batch/core/launch/support/SimpleJobLauncher.java

+16-10
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2006-2019 the original author or authors.
2+
* Copyright 2006-2021 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -142,18 +142,24 @@ public JobExecution run(final Job job, final JobParameters jobParameters)
142142
@Override
143143
public void run() {
144144
try {
145-
logger.info("Job: [" + job + "] launched with the following parameters: [" + jobParameters
146-
+ "]");
145+
if (logger.isInfoEnabled()) {
146+
logger.info("Job: [" + job + "] launched with the following parameters: [" + jobParameters
147+
+ "]");
148+
}
147149
job.execute(jobExecution);
148-
Duration jobExecutionDuration = BatchMetrics.calculateDuration(jobExecution.getStartTime(), jobExecution.getEndTime());
149-
logger.info("Job: [" + job + "] completed with the following parameters: [" + jobParameters
150-
+ "] and the following status: [" + jobExecution.getStatus() + "]"
151-
+ (jobExecutionDuration == null ? "" : " in " + BatchMetrics.formatDuration(jobExecutionDuration)));
150+
if (logger.isInfoEnabled()) {
151+
Duration jobExecutionDuration = BatchMetrics.calculateDuration(jobExecution.getStartTime(), jobExecution.getEndTime());
152+
logger.info("Job: [" + job + "] completed with the following parameters: [" + jobParameters
153+
+ "] and the following status: [" + jobExecution.getStatus() + "]"
154+
+ (jobExecutionDuration == null ? "" : " in " + BatchMetrics.formatDuration(jobExecutionDuration)));
155+
}
152156
}
153157
catch (Throwable t) {
154-
logger.info("Job: [" + job
155-
+ "] failed unexpectedly and fatally with the following parameters: [" + jobParameters
156-
+ "]", t);
158+
if (logger.isInfoEnabled()) {
159+
logger.info("Job: [" + job
160+
+ "] failed unexpectedly and fatally with the following parameters: [" + jobParameters
161+
+ "]", t);
162+
}
157163
rethrow(t);
158164
}
159165
}

spring-batch-core/src/main/java/org/springframework/batch/core/launch/support/SimpleJobOperator.java

+22-14
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2006-2018 the original author or authors.
2+
* Copyright 2006-2021 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -267,15 +267,18 @@ public String getSummary(long executionId) throws NoSuchJobExecutionException {
267267
@Override
268268
public Long restart(long executionId) throws JobInstanceAlreadyCompleteException, NoSuchJobExecutionException, NoSuchJobException, JobRestartException, JobParametersInvalidException {
269269

270-
logger.info("Checking status of job execution with id=" + executionId);
271-
270+
if (logger.isInfoEnabled()) {
271+
logger.info("Checking status of job execution with id=" + executionId);
272+
}
272273
JobExecution jobExecution = findExecutionById(executionId);
273274

274275
String jobName = jobExecution.getJobInstance().getJobName();
275276
Job job = jobRegistry.getJob(jobName);
276277
JobParameters parameters = jobExecution.getJobParameters();
277278

278-
logger.info(String.format("Attempting to resume job with name=%s and parameters=%s", jobName, parameters));
279+
if (logger.isInfoEnabled()) {
280+
logger.info(String.format("Attempting to resume job with name=%s and parameters=%s", jobName, parameters));
281+
}
279282
try {
280283
return jobLauncher.run(job, parameters).getId();
281284
}
@@ -295,8 +298,9 @@ public Long restart(long executionId) throws JobInstanceAlreadyCompleteException
295298
*/
296299
@Override
297300
public Long start(String jobName, String parameters) throws NoSuchJobException, JobInstanceAlreadyExistsException, JobParametersInvalidException {
298-
299-
logger.info("Checking status of job with name=" + jobName);
301+
if (logger.isInfoEnabled()) {
302+
logger.info("Checking status of job with name=" + jobName);
303+
}
300304

301305
JobParameters jobParameters = jobParametersConverter.getJobParameters(PropertiesConverter
302306
.stringToProperties(parameters));
@@ -308,8 +312,9 @@ public Long start(String jobName, String parameters) throws NoSuchJobException,
308312
}
309313

310314
Job job = jobRegistry.getJob(jobName);
311-
312-
logger.info(String.format("Attempting to launch job with name=%s and parameters=%s", jobName, parameters));
315+
if (logger.isInfoEnabled()) {
316+
logger.info(String.format("Attempting to launch job with name=%s and parameters=%s", jobName, parameters));
317+
}
313318
try {
314319
return jobLauncher.run(job, jobParameters).getId();
315320
}
@@ -336,15 +341,17 @@ public Long start(String jobName, String parameters) throws NoSuchJobException,
336341
@Override
337342
public Long startNextInstance(String jobName) throws NoSuchJobException,
338343
UnexpectedJobExecutionException, JobParametersInvalidException {
339-
340-
logger.info("Locating parameters for next instance of job with name=" + jobName);
344+
if (logger.isInfoEnabled()) {
345+
logger.info("Locating parameters for next instance of job with name=" + jobName);
346+
}
341347

342348
Job job = jobRegistry.getJob(jobName);
343349
JobParameters parameters = new JobParametersBuilder(jobExplorer)
344350
.getNextJobParameters(job)
345351
.toJobParameters();
346-
347-
logger.info(String.format("Attempting to launch job with name=%s and parameters=%s", jobName, parameters));
352+
if (logger.isInfoEnabled()) {
353+
logger.info(String.format("Attempting to launch job with name=%s and parameters=%s", jobName, parameters));
354+
}
348355
try {
349356
return jobLauncher.run(job, parameters).getId();
350357
}
@@ -424,8 +431,9 @@ public JobExecution abandon(long jobExecutionId) throws NoSuchJobExecutionExcept
424431
throw new JobExecutionAlreadyRunningException(
425432
"JobExecution is running or complete and therefore cannot be aborted");
426433
}
427-
428-
logger.info("Aborting job execution: " + jobExecution);
434+
if (logger.isInfoEnabled()) {
435+
logger.info("Aborting job execution: " + jobExecution);
436+
}
429437
jobExecution.upgradeStatus(BatchStatus.ABANDONED);
430438
jobExecution.setEndTime(new Date());
431439
jobRepository.update(jobExecution);

spring-batch-core/src/main/java/org/springframework/batch/core/repository/support/JobRepositoryFactoryBean.java

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2014 the original author or authors.
2+
* Copyright 2002-2021 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -181,7 +181,9 @@ public void afterPropertiesSet() throws Exception {
181181

182182
if (databaseType == null) {
183183
databaseType = DatabaseType.fromMetaData(dataSource).name();
184-
logger.info("No database type set, using meta data indicating: " + databaseType);
184+
if (logger.isInfoEnabled()) {
185+
logger.info("No database type set, using meta data indicating: " + databaseType);
186+
}
185187
}
186188

187189
if (lobHandler == null && databaseType.equalsIgnoreCase(DatabaseType.ORACLE.toString())) {
@@ -194,7 +196,7 @@ public void afterPropertiesSet() throws Exception {
194196
serializer = defaultSerializer;
195197
}
196198

197-
Assert.isTrue(incrementerFactory.isSupportedIncrementerType(databaseType), "'" + databaseType
199+
Assert.isTrue(incrementerFactory.isSupportedIncrementerType(databaseType), () -> "'" + databaseType
198200
+ "' is an unsupported database type. The supported database types are "
199201
+ StringUtils.arrayToCommaDelimitedString(incrementerFactory.getSupportedIncrementerTypes()));
200202

0 commit comments

Comments
 (0)