Skip to content

Commit

Permalink
Added isXXXEnabled for logging statements
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
mdeinum authored and fmbenhassine committed Mar 17, 2021
1 parent 718837f commit 737f6ac
Show file tree
Hide file tree
Showing 30 changed files with 175 additions and 102 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2006-2007 the original author or authors.
* Copyright 2006-2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -54,7 +54,9 @@ public void setJobRegistry(JobRegistry jobRegistry) {
* @throws Exception if there is a problem
*/
public void bind(JobFactory jobFactory, Map<String, ?> params) throws Exception {
logger.info("Binding JobFactory: " + jobFactory.getJobName());
if (logger.isInfoEnabled()) {
logger.info("Binding JobFactory: " + jobFactory.getJobName());
}
jobRegistry.register(jobFactory);
}

Expand All @@ -66,7 +68,9 @@ public void bind(JobFactory jobFactory, Map<String, ?> params) throws Exception
* @throws Exception if there is a problem
*/
public void unbind(JobFactory jobFactory, Map<String, ?> params) throws Exception {
logger.info("Unbinding JobFactory: " + jobFactory.getJobName());
if (logger.isInfoEnabled()) {
logger.info("Unbinding JobFactory: " + jobFactory.getJobName());
}
jobRegistry.unregister(jobFactory.getJobName());
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2006-2020 the original author or authors.
* Copyright 2006-2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -337,8 +337,10 @@ public final void execute(JobExecution execution) {
}

} catch (JobInterruptedException e) {
logger.info("Encountered interruption executing job: "
+ e.getMessage());
if (logger.isInfoEnabled()) {
logger.info("Encountered interruption executing job: "
+ e.getMessage());
}
if (logger.isDebugEnabled()) {
logger.debug("Full exception", e);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2006-2014 the original author or authors.
* Copyright 2006-2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -116,9 +116,11 @@ public StepExecution handleStep(Step step, JobExecution execution) throws JobInt
if (stepExecutionPartOfExistingJobExecution(execution, lastStepExecution)) {
// If the last execution of this step was in the same job, it's
// probably intentional so we want to run it again...
logger.info(String.format("Duplicate step [%s] detected in execution of job=[%s]. "
+ "If either step fails, both will be executed again on restart.", step.getName(), jobInstance
.getJobName()));
if (logger.isInfoEnabled()) {
logger.info(String.format("Duplicate step [%s] detected in execution of job=[%s]. "
+ "If either step fails, both will be executed again on restart.", step.getName(), jobInstance
.getJobName()));
}
lastStepExecution = null;
}
StepExecution currentStepExecution = lastStepExecution;
Expand All @@ -143,7 +145,9 @@ public StepExecution handleStep(Step step, JobExecution execution) throws JobInt

jobRepository.add(currentStepExecution);

logger.info("Executing step: [" + step.getName() + "]");
if (logger.isInfoEnabled()) {
logger.info("Executing step: [" + step.getName() + "]");
}
try {
step.execute(currentStepExecution);
currentStepExecution.getExecutionContext().put("batch.executed", true);
Expand Down Expand Up @@ -215,7 +219,9 @@ protected boolean shouldStart(StepExecution lastStepExecution, JobExecution jobE
|| stepStatus == BatchStatus.ABANDONED) {
// step is complete, false should be returned, indicating that the
// step should not be started
logger.info("Step already complete or not restartable, so no action to execute: " + lastStepExecution);
if (logger.isInfoEnabled()) {
logger.info("Step already complete or not restartable, so no action to execute: " + lastStepExecution);
}
return false;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2013 the original author or authors.
* Copyright 2013-2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -71,7 +71,9 @@ private void parseRefElements(Element element,
if(!registry.containsBeanDefinition(beanName)) {
registry.registerBeanDefinition(beanName, beanDefinition);
} else {
logger.info("Ignoring batch.xml bean definition for " + beanName + " because another bean of the same name has been registered");
if (logger.isInfoEnabled()) {
logger.info("Ignoring batch.xml bean definition for " + beanName + " because another bean of the same name has been registered");
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2013-2018 the original author or authors.
* Copyright 2013-2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -200,7 +200,7 @@ private String resolveValue(String value) {

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

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

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2014 the original author or authors.
* Copyright 2014-2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -93,7 +93,9 @@ protected boolean shouldStart(StepExecution lastStepExecution, JobExecution jobE

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

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2013 the original author or authors.
* Copyright 2013-2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -94,8 +94,10 @@ public Step build() {
name = getStep().getName();
}
catch (Exception e) {
logger.info("Ignored exception from step asking for name and allowStartIfComplete flag. "
+ "Using default from enclosing PartitionStep (" + name + "," + allowStartIfComplete + ").");
if (logger.isInfoEnabled()) {
logger.info("Ignored exception from step asking for name and allowStartIfComplete flag. "
+ "Using default from enclosing PartitionStep (" + name + "," + allowStartIfComplete + ").");
}
}
}
SimpleStepExecutionSplitter splitter = new SimpleStepExecutionSplitter();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2006-2013 the original author or authors.
* Copyright 2006-2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -131,7 +131,9 @@ private void register(String[] paths) throws DuplicateJobException, IOException
for (int j = 0; j < resources.length; j++) {

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

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

logger.info("Starting job registry in parent context from XML at: [" + args[0] + "]");

if (logger.isInfoEnabled()) {
logger.info("Starting job registry in parent context from XML at: [" + args[0] + "]");
}
new Thread(new Runnable() {
@Override
public void run() {
Expand All @@ -221,7 +224,9 @@ public void run() {

synchronized (errors) {
if (!errors.isEmpty()) {
logger.info(errors.size() + " errors detected on startup of parent context. Rethrowing.");
if (logger.isInfoEnabled()) {
logger.info(errors.size() + " errors detected on startup of parent context. Rethrowing.");
}
throw errors.get(0);
}
}
Expand All @@ -231,7 +236,9 @@ public void run() {
final String[] paths = new String[args.length - 1];
System.arraycopy(args, 1, paths, 0, paths.length);

logger.info("Parent context started. Registering jobs from paths: " + Arrays.asList(paths));
if (logger.isInfoEnabled()) {
logger.info("Parent context started. Registering jobs from paths: " + Arrays.asList(paths));
}
launcher.register(paths);

if (System.getProperty(EMBEDDED) != null) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2006-2019 the original author or authors.
* Copyright 2006-2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -142,18 +142,24 @@ public JobExecution run(final Job job, final JobParameters jobParameters)
@Override
public void run() {
try {
logger.info("Job: [" + job + "] launched with the following parameters: [" + jobParameters
+ "]");
if (logger.isInfoEnabled()) {
logger.info("Job: [" + job + "] launched with the following parameters: [" + jobParameters
+ "]");
}
job.execute(jobExecution);
Duration jobExecutionDuration = BatchMetrics.calculateDuration(jobExecution.getStartTime(), jobExecution.getEndTime());
logger.info("Job: [" + job + "] completed with the following parameters: [" + jobParameters
+ "] and the following status: [" + jobExecution.getStatus() + "]"
+ (jobExecutionDuration == null ? "" : " in " + BatchMetrics.formatDuration(jobExecutionDuration)));
if (logger.isInfoEnabled()) {
Duration jobExecutionDuration = BatchMetrics.calculateDuration(jobExecution.getStartTime(), jobExecution.getEndTime());
logger.info("Job: [" + job + "] completed with the following parameters: [" + jobParameters
+ "] and the following status: [" + jobExecution.getStatus() + "]"
+ (jobExecutionDuration == null ? "" : " in " + BatchMetrics.formatDuration(jobExecutionDuration)));
}
}
catch (Throwable t) {
logger.info("Job: [" + job
+ "] failed unexpectedly and fatally with the following parameters: [" + jobParameters
+ "]", t);
if (logger.isInfoEnabled()) {
logger.info("Job: [" + job
+ "] failed unexpectedly and fatally with the following parameters: [" + jobParameters
+ "]", t);
}
rethrow(t);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2006-2018 the original author or authors.
* Copyright 2006-2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -267,15 +267,18 @@ public String getSummary(long executionId) throws NoSuchJobExecutionException {
@Override
public Long restart(long executionId) throws JobInstanceAlreadyCompleteException, NoSuchJobExecutionException, NoSuchJobException, JobRestartException, JobParametersInvalidException {

logger.info("Checking status of job execution with id=" + executionId);

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

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

logger.info(String.format("Attempting to resume job with name=%s and parameters=%s", jobName, parameters));
if (logger.isInfoEnabled()) {
logger.info(String.format("Attempting to resume job with name=%s and parameters=%s", jobName, parameters));
}
try {
return jobLauncher.run(job, parameters).getId();
}
Expand All @@ -295,8 +298,9 @@ public Long restart(long executionId) throws JobInstanceAlreadyCompleteException
*/
@Override
public Long start(String jobName, String parameters) throws NoSuchJobException, JobInstanceAlreadyExistsException, JobParametersInvalidException {

logger.info("Checking status of job with name=" + jobName);
if (logger.isInfoEnabled()) {
logger.info("Checking status of job with name=" + jobName);
}

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

Job job = jobRegistry.getJob(jobName);

logger.info(String.format("Attempting to launch job with name=%s and parameters=%s", jobName, parameters));
if (logger.isInfoEnabled()) {
logger.info(String.format("Attempting to launch job with name=%s and parameters=%s", jobName, parameters));
}
try {
return jobLauncher.run(job, jobParameters).getId();
}
Expand All @@ -336,15 +341,17 @@ public Long start(String jobName, String parameters) throws NoSuchJobException,
@Override
public Long startNextInstance(String jobName) throws NoSuchJobException,
UnexpectedJobExecutionException, JobParametersInvalidException {

logger.info("Locating parameters for next instance of job with name=" + jobName);
if (logger.isInfoEnabled()) {
logger.info("Locating parameters for next instance of job with name=" + jobName);
}

Job job = jobRegistry.getJob(jobName);
JobParameters parameters = new JobParametersBuilder(jobExplorer)
.getNextJobParameters(job)
.toJobParameters();

logger.info(String.format("Attempting to launch job with name=%s and parameters=%s", jobName, parameters));
if (logger.isInfoEnabled()) {
logger.info(String.format("Attempting to launch job with name=%s and parameters=%s", jobName, parameters));
}
try {
return jobLauncher.run(job, parameters).getId();
}
Expand Down Expand Up @@ -424,8 +431,9 @@ public JobExecution abandon(long jobExecutionId) throws NoSuchJobExecutionExcept
throw new JobExecutionAlreadyRunningException(
"JobExecution is running or complete and therefore cannot be aborted");
}

logger.info("Aborting job execution: " + jobExecution);
if (logger.isInfoEnabled()) {
logger.info("Aborting job execution: " + jobExecution);
}
jobExecution.upgradeStatus(BatchStatus.ABANDONED);
jobExecution.setEndTime(new Date());
jobRepository.update(jobExecution);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2014 the original author or authors.
* Copyright 2002-2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -181,7 +181,9 @@ public void afterPropertiesSet() throws Exception {

if (databaseType == null) {
databaseType = DatabaseType.fromMetaData(dataSource).name();
logger.info("No database type set, using meta data indicating: " + databaseType);
if (logger.isInfoEnabled()) {
logger.info("No database type set, using meta data indicating: " + databaseType);
}
}

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

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

Expand Down
Loading

0 comments on commit 737f6ac

Please sign in to comment.