Article directory
1. Reproduce the error
Today, I am writing and using Quartz
the method of executing timed tasks, as shown in the following code:
public class QuartzTest {
public static void main(String[] args) throws SchedulerException {
// 1、创建Scheduler(调度器)
SchedulerFactory schedulerFactory = new StdSchedulerFactory();
Scheduler scheduler = schedulerFactory.getScheduler();
// 2、创建JobDetail实例,并与SimpleJob类绑定
JobDetail jobDetail = JobBuilder.newJob(SimpleJob.class)
.withIdentity("job-1", "job-group").build();
// 3、构建Trigger(触发器),定义执行频率和时长
CronTrigger cronTrigger = TriggerBuilder.newTrigger()
.withIdentity("trigger-1", "trigger-group")
.startNow() //立即生效
.withSchedule(CronScheduleBuilder.cronSchedule("0/5 * * * * ? "))
.build();
//4、执行
scheduler.scheduleJob(jobDetail, cronTrigger);
scheduler.start();
}
class SimpleJob implements Job {
@Override
public void execute(JobExecutionContext jobExecutionContext) {
// 创建一个事件,下面仅创建一个输出语句作演示
final SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println(
Thread.currentThread().getName() + "--" + simpleDateFormat.format(new Date()));
}
}
}
When starting, the following error is reported:
Right nowSLF4J: Defaulting to no-operation (NOP) logger implementation
2. Analysis errors
First, find this sentence in the picture above SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
.
Translate it into Chinese yes SLF4J:有关更多详细信息,请参阅 http://www.slf4j.org/codes.html#StaticLoggerBinder。
.
So, click on the link https://www.slf4j.org/codes.html#StaticLoggerBinder , as shown in the figure below:
However, this is all in English, so we might as well translate it into Chinese, as follows:
Could not load class org.slf4j.impl.StaticLoggerBinder
This warning message is reported when a
org.slf4j.impl.StaticLoggerBinder
class cannot be loaded into memory.SLF4J
This happens when an appropriate binding cannot be found on the classpath . Putting one (and only one) on the classpathslf4j-nop.jar slf4j-simple.jar,slf4j-log4j12.jar,slf4j-jdk14.jar或logback-classic.jar
should do the trick.
Note thatslf4j-api
versions2.0.x
and higher useServiceLoader
the mechanism. Targetedslf4j-api 2.x
backends (such aslogback 1.3
and later) are notorg.slf4j.impl.StaticLoggerBinder
shipped with it. If placing a orientedslf4j-api 2.0.x
logging backend, it needs to be on the classpathslf4j-api-2.x.jar
. See also the related FAQ entry. Since
version 1, will default to a no-op( ) logger implementation in the absence of a binding . If you are responsible for packaging your application and don't care about logging, putting this on the application's classpath will get rid of this warning message. Note that embedded components such as libraries or frameworks should not declare a dependency on any bindings, but only on . When a library declares a compile-time dependency on a binding, it imposes that binding on the end user, thereby negating the purpose of .1.6.0
SLF4J
1.6
SLF4J
NOP
slf4j-nop.jar
SLF4J
slf4j-api
SLF4J
SLF4J
slf4j-log4j12.jar
In general, this dependency is missing .
So, looking for my project, there is indeed a missing slf4j-log4j12.jar
dependency.
3. Fix bugs
Since my project lacks slf4j-log4j12.jar
dependencies, pom.xml
I can add the following in :
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>2.0.7</version>
<scope>compile</scope>
</dependency>
Restart the project and it will work fine.
4. Other ways to resolve this error
Because my mistake is missing slf4j-log4j12
dependencies in the project, pom.xml
just add the dependencies in .
If you have added slf4j-log4j12
dependencies to the project, but still report an error, you can refer to the following solutions.
Check scope
if range() is set to test
, as shown in the following code:
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>2.0.7</version>
<scope>test</scope>
</dependency>
If scope ( scope
) is set to test
, that is to say only in the test environment, it can be used.
And I main
run it directly in the method, so it does not belong to the test environment, so this package is equivalent to not adding dependencies.
The scope ( ) can be scope
modified compile
as shown in the following figure: