gpt4 book ai didi

java - Quartz在集群环境下无法正常工作

转载 作者:行者123 更新时间:2023-11-30 05:55:53 27 4
gpt4 key购买 nike

我在集群环境中的 Websphere 8.5.5 上使用 quartz-2.2.3,其中我有 2 个节点,每个节点上有 3 个 JVM。

我正在应用程序启动时配置作业。

问题是该作业在每个节点上配置一次,而我期望的是它只会在两个节点上配置一次,而不是每个节点上配置一次。

我的配置如下:

quartzConfig.properties:

#============================================================================
# Configure Main Scheduler Properties
#============================================================================

org.quartz.scheduler.instanceName = MyJobScheduler
org.quartz.scheduler.instanceId = AUTO
#org.quartz.scheduler.threadsInheritContextClassLoaderOfInitializer = true

#============================================================================
# Configure ThreadPool
#============================================================================

org.quartz.threadPool.class = org.quartz.simpl.SimpleThreadPool
org.quartz.threadPool.threadCount = 25
org.quartz.threadPool.threadPriority = 5

#============================================================================
# Configure JobStore
#============================================================================

org.quartz.jobStore.misfireThreshold = 60000

org.quartz.jobStore.class = org.quartz.impl.jdbcjobstore.JobStoreTX
org.quartz.jobStore.driverDelegateClass = org.quartz.impl.jdbcjobstore.MSSQLDelegate
org.quartz.jobStore.useProperties = false
org.quartz.jobStore.dataSource = myDS
org.quartz.jobStore.tablePrefix = QRTZ_

org.quartz.jobStore.isClustered = true
org.quartz.jobStore.clusterCheckinInterval = 20000

#============================================================================
# Configure Datasources
#============================================================================

org.quartz.dataSource.myDS.driver = com.microsoft.sqlserver.jdbc.SQLServerDriver
org.quartz.dataSource.myDS.URL = jdbc:sqlserver://mydbserver:51803;databaseName=quartz
org.quartz.dataSource.myDS.user = quartz
org.quartz.dataSource.myDS.password = quartz
org.quartz.dataSource.myDS.maxConnections = 5
org.quartz.dataSource.myDS.validationQuery=select 1

#============================================================================
# Configure Shutdown Plugin
#============================================================================

org.quartz.threadPool.makeThreadsDaemons=true
org.quartz.scheduler.makeSchedulerThreadDaemon=true
org.quartz.scheduler.interruptJobsOnShutdown=true
org.quartz.plugin.shutdownhook.class = org.quartz.plugins.management.ShutdownHookPlugin
org.quartz.plugin.shutdownhook.cleanShutdown = true

ApplicationContextListener:

public class ApplicationContextListener implements ServletContextListener {

private StdSchedulerFactory factoryMyAppJob;

@Override
public void contextInitialized(ServletContextEvent event) {
configureQuartzJobs(event.getServletContext());
}

@Override
public void contextDestroyed(ServletContextEvent arg0) {
try {
factoryMyAppJob.getScheduler().shutdown(false);
} catch (SchedulerException e) {
AppLogger.log(e);
}
}

private void configureQuartzJobs(ServletContext servletContext) {

String currentTime = new SimpleDateFormat("dd_MM_yyyy_HH").format(new Date());
String myAppGroup = "myAppGroup";
String myAppJob = "myAppJob";


try {



JobDetail job = JobBuilder.newJob(myAppJob.class).withIdentity(myAppJob, myAppGroup).build();

Trigger triggerMyApp = TriggerBuilder.newTrigger().withIdentity(myAppJob, myAppGroup)
.withSchedule(simpleSchedule()
.withIntervalInMinutes(3).repeatForever())
.build();

Properties propsMyAppJob = new Properties();

boolean production = ConfigurationUtils.isProductionEnvironment();

if (production) {
propsMyAppJob.load(this.getClass().getClassLoader().getResourceAsStream("quartzConfig.properties"));
factoryMyAppJob = new StdSchedulerFactory(propsMyAppJob);
} else {
factoryMyAppJob = new StdSchedulerFactory();
}
Scheduler scheduler = factoryMyAppJob.getScheduler();

if (scheduler.checkExists(job.getKey())) {
scheduler.deleteJob(job.getKey());
}

scheduler.scheduleJob(job, triggerMyApp);

scheduler.start();


} catch (ObjectAlreadyExistsException oae) {

} catch (Exception e) {
AppLogger.log(e);
}
}

}

最佳答案

造成这种行为的原因有很多。配置看起来是正确的。

Quartz Documentation提到以下内容:

Never run clustering on separate machines, unless their clocks are synchronized using some form of time-sync service (daemon) that runs very regularly (the clocks must be within a second of each other)

还有一个:

Never start (scheduler.start()) a non-clustered instance against the same set of database tables that any other instance is running (start()ed) against. You may get serious data corruption, and will definitely experience erratic behavior.

IMO 这两者都可以成为集群无法正常运行的合理原因。

关于java - Quartz在集群环境下无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53203389/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com