Issue
I was working Spring Batch for inactive accounts. But the return type doesn't work. In inactiveMemberProcessor
log.info("InactiveMemberProcessor execution")
doesn't work.
Not only the log but also the code cannot be executed because the return is not possible.
Why does this happen?
package com.capston.chatting.config.batch;
import com.capston.chatting.entity.Member;
import com.capston.chatting.enums.MemberStatus;
import com.capston.chatting.repository.MemberRepository;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.item.ItemProcessor;
import org.springframework.batch.item.ItemWriter;
import org.springframework.batch.item.support.ListItemReader;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.time.LocalDateTime;
import java.util.List;
@Slf4j
@RequiredArgsConstructor
@Configuration
public class InactiveMemberJobConfig {
private final MemberRepository memberRepository;
private final JobBuilderFactory jobBuilderFactory;
private final StepBuilderFactory stepBuilderFactory;
@Bean
public Job inactiveMemberJob() {
return jobBuilderFactory.get("inactiveMemberJob3")
.start(inactiveJobStep())
.build();
}
@Bean
public Step inactiveJobStep() {
return stepBuilderFactory.get("inactiveMemberStep")
.<Member, Member>chunk(10)
.reader(inactiveMemberReader())
.processor(this.inactiveMemberProcessor())
.writer(this.inactiveMemberWriter())
.build();
}
@Bean
public ListItemReader<Member> inactiveMemberReader() {
log.info("InactiveMemberReader execution");
List<Member> oldMembers = memberRepository
.findByUpdateDateBeforeAndStatusEquals(LocalDateTime.now().minusYears(1), MemberStatus.ACTIVE);
for (Member oldMember : oldMembers) {
log.info("oldMembers = {}", oldMember.getName());
}
return new ListItemReader<>(oldMembers);
}
@Bean
public ItemProcessor<Member, Member> inactiveMemberProcessor() {
log.info("test");
return new ItemProcessor<Member, Member>() {
@Override
public Member process(Member member) throws Exception {
log.info("InactiveMemberProcessor execution");
return member.setInactive();
}
};
// return member -> {
// log.info("InactiveMemberProcessor execution");
// return member.setInactive();
// };
}
@Bean
public ItemWriter<Member> inactiveMemberWriter() {
log.info("InactiveMemberWriter execution");
return ((List<? extends Member> members) -> {
memberRepository.saveAll(members);
log.info("Members : {}", members);
});
}
}
Output
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.7.2)
2022-08-21 20:31:27.400 INFO 10044 --- [ restartedMain] c.capston.chatting.ChattingApplication : Starting ChattingApplication using Java 11.0.12 on DESKTOP-SHB62PK with PID 10044 (D:\chatting\chatting\build\classes\java\main started by user in D:\chatting\chatting)
2022-08-21 20:31:27.401 INFO 10044 --- [ restartedMain] c.capston.chatting.ChattingApplication : The following 4 profiles are active: "google", "naver", "kakao", "local"
2022-08-21 20:31:27.423 INFO 10044 --- [ restartedMain] .e.DevToolsPropertyDefaultsPostProcessor : Devtools property defaults active! Set 'spring.devtools.add-properties' to 'false' to disable
2022-08-21 20:31:27.423 INFO 10044 --- [ restartedMain] .e.DevToolsPropertyDefaultsPostProcessor : For additional web related logging consider setting the 'logging.level.web' property to 'DEBUG'
2022-08-21 20:31:27.799 INFO 10044 --- [ restartedMain] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data JPA repositories in DEFAULT mode.
2022-08-21 20:31:27.832 INFO 10044 --- [ restartedMain] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 29 ms. Found 4 JPA repository interfaces.
2022-08-21 20:31:28.160 INFO 10044 --- [ restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
2022-08-21 20:31:28.165 INFO 10044 --- [ restartedMain] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2022-08-21 20:31:28.165 INFO 10044 --- [ restartedMain] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.65]
2022-08-21 20:31:28.239 INFO 10044 --- [ restartedMain] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2022-08-21 20:31:28.239 INFO 10044 --- [ restartedMain] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 816 ms
2022-08-21 20:31:28.287 INFO 10044 --- [ restartedMain] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Starting...
2022-08-21 20:31:28.445 INFO 10044 --- [ restartedMain] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Start completed.
2022-08-21 20:31:28.507 INFO 10044 --- [ restartedMain] o.hibernate.jpa.internal.util.LogHelper : HHH000204: Processing PersistenceUnitInfo [name: default]
2022-08-21 20:31:28.529 INFO 10044 --- [ restartedMain] org.hibernate.Version : HHH000412: Hibernate ORM core version 5.6.10.Final
2022-08-21 20:31:28.597 INFO 10044 --- [ restartedMain] o.hibernate.annotations.common.Version : HCANN000001: Hibernate Commons Annotations {5.1.2.Final}
2022-08-21 20:31:28.649 INFO 10044 --- [ restartedMain] org.hibernate.dialect.Dialect : HHH000400: Using dialect: org.hibernate.dialect.MySQL8Dialect
2022-08-21 20:31:28.900 INFO 10044 --- [ restartedMain] o.h.e.t.j.p.i.JtaPlatformInitiator : HHH000490: Using JtaPlatform implementation: [org.hibernate.engine.transaction.jta.platform.internal.NoJtaPlatform]
2022-08-21 20:31:28.904 INFO 10044 --- [ restartedMain] j.LocalContainerEntityManagerFactoryBean : Initialized JPA EntityManagerFactory for persistence unit 'default'
2022-08-21 20:31:29.331 INFO 10044 --- [ restartedMain] o.s.s.web.DefaultSecurityFilterChain : Will secure any request with [org.springframework.security.web.session.DisableEncodeUrlFilter@e2a9a2e, org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter@8f52ad, org.springframework.security.web.context.SecurityContextPersistenceFilter@5d28bf88, org.springframework.security.web.header.HeaderWriterFilter@682aa17e, org.springframework.security.web.authentication.logout.LogoutFilter@4b700064, org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter@5266a5db, org.springframework.security.web.session.ConcurrentSessionFilter@5f76bf5b, org.springframework.security.web.savedrequest.RequestCacheAwareFilter@4a39d06c, org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter@6cc78b9b, org.springframework.security.web.authentication.AnonymousAuthenticationFilter@4bed4ca2, org.springframework.security.web.session.SessionManagementFilter@34652a4a, org.springframework.security.web.access.ExceptionTranslationFilter@33fe6437, org.springframework.security.web.access.intercept.FilterSecurityInterceptor@5e47964]
2022-08-21 20:31:29.338 INFO 10044 --- [ restartedMain] c.c.c.c.batch.InactiveMemberJobConfig : InactiveMemberReader execution
2022-08-21 20:31:29.355 DEBUG 10044 --- [ restartedMain] org.hibernate.SQL :
select
member0_.member_id as member_i1_3_,
member0_.created_date as created_2_3_,
member0_.login_id as login_id3_3_,
member0_.login_pw as login_pw4_3_,
member0_.name as name5_3_,
member0_.role as role6_3_,
member0_.score as score7_3_,
member0_.status as status8_3_,
member0_.update_date as update_d9_3_
from
member member0_
where
member0_.update_date<?
and member0_.status=?
2022-08-21 20:31:29.397 INFO 10044 --- [ restartedMain] c.c.c.c.batch.InactiveMemberJobConfig : oldMembers = resultMember
2022-08-21 20:31:29.398 INFO 10044 --- [ restartedMain] c.c.c.c.batch.InactiveMemberJobConfig : test
2022-08-21 20:31:29.399 INFO 10044 --- [ restartedMain] c.c.c.c.batch.InactiveMemberJobConfig : InactiveMemberWriter execution
2022-08-21 20:31:29.518 WARN 10044 --- [ restartedMain] o.s.b.a.batch.JpaBatchConfigurer : JPA does not support custom isolation levels, so locks may not be taken when launching Jobs. To silence this warning, set 'spring.batch.jdbc.isolation-level-for-create' to 'default'.
2022-08-21 20:31:29.520 INFO 10044 --- [ restartedMain] o.s.b.c.r.s.JobRepositoryFactoryBean : No database type set, using meta data indicating: MYSQL
2022-08-21 20:31:29.529 INFO 10044 --- [ restartedMain] o.s.b.c.l.support.SimpleJobLauncher : No TaskExecutor has been set, defaulting to synchronous executor.
2022-08-21 20:31:29.608 INFO 10044 --- [ restartedMain] o.s.b.d.a.OptionalLiveReloadServer : LiveReload server is running on port 35729
2022-08-21 20:31:29.628 INFO 10044 --- [ restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
2022-08-21 20:31:29.629 INFO 10044 --- [ restartedMain] o.s.m.s.b.SimpleBrokerMessageHandler : Starting...
2022-08-21 20:31:29.630 INFO 10044 --- [ restartedMain] o.s.m.s.b.SimpleBrokerMessageHandler : BrokerAvailabilityEvent[available=true, SimpleBrokerMessageHandler [org.springframework.messaging.simp.broker.DefaultSubscriptionRegistry@62c1be01]]
2022-08-21 20:31:29.630 INFO 10044 --- [ restartedMain] o.s.m.s.b.SimpleBrokerMessageHandler : Started.
2022-08-21 20:31:29.635 INFO 10044 --- [ restartedMain] c.capston.chatting.ChattingApplication : Started ChattingApplication in 2.448 seconds (JVM running for 3.017)
2022-08-21 20:31:29.637 INFO 10044 --- [ restartedMain] o.s.b.a.b.JobLauncherApplicationRunner : Running default command line with: []
2022-08-21 20:31:30.054 INFO 10044 --- [ restartedMain] o.s.b.c.l.support.SimpleJobLauncher : Job: [SimpleJob: [name=inactiveMemberJob3]] launched with the following parameters: [{}]
2022-08-21 20:31:30.189 INFO 10044 --- [ restartedMain] o.s.batch.core.job.SimpleStepHandler : Step already complete or not restartable, so no action to execute: StepExecution: id=2, version=3, name=inactiveMemberStep, status=COMPLETED, exitStatus=COMPLETED, readCount=0, filterCount=0, writeCount=0 readSkipCount=0, writeSkipCount=0, processSkipCount=0, commitCount=1, rollbackCount=0, exitDescription=
2022-08-21 20:31:30.255 INFO 10044 --- [ restartedMain] o.s.b.c.l.support.SimpleJobLauncher : Job: [SimpleJob: [name=inactiveMemberJob3]] completed with the following parameters: [{}] and the following status: [COMPLETED] in 128ms
2022-08-21 20:31:30.634 INFO 10044 --- [ restartedMain] o.s.b.c.l.support.SimpleJobLauncher : Job: [SimpleJob: [name=simpleJob]] launched with the following parameters: [{}]
2022-08-21 20:31:30.759 INFO 10044 --- [ restartedMain] o.s.batch.core.job.SimpleStepHandler : Step already complete or not restartable, so no action to execute: StepExecution: id=1, version=3, name=simpleStep1, status=COMPLETED, exitStatus=COMPLETED, readCount=0, filterCount=0, writeCount=0 readSkipCount=0, writeSkipCount=0, processSkipCount=0, commitCount=1, rollbackCount=0, exitDescription=
2022-08-21 20:31:30.824 INFO 10044 --- [ restartedMain] o.s.b.c.l.support.SimpleJobLauncher : Job: [SimpleJob: [name=simpleJob]] completed with the following parameters: [{}] and the following status: [COMPLETED] in 125ms
Solution
According to the log message Step already complete or not restartable, so no action to execute
, your step does not seem to be executed. So probably that is the reason you are not seeing your log message.
You can set allowStartIfComplete
to true
on your step and that should work.
Answered By - Mahmoud Ben Hassine
Answer Checked By - David Goodson (JavaFixing Volunteer)