Skip to content

Commit

Permalink
fix #2
Browse files Browse the repository at this point in the history
  • Loading branch information
terrymanu committed Mar 8, 2016
1 parent 322ded1 commit 11dd433
Show file tree
Hide file tree
Showing 32 changed files with 1,330 additions and 65 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,27 +17,15 @@

package com.dangdang.ddframe.job.api;

import java.util.Date;
import java.util.List;
import java.util.Properties;

import org.quartz.CronScheduleBuilder;
import org.quartz.CronTrigger;
import org.quartz.JobBuilder;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.Trigger;
import org.quartz.TriggerBuilder;
import org.quartz.TriggerKey;
import org.quartz.impl.StdSchedulerFactory;

import com.dangdang.ddframe.job.api.listener.AbstractDistributeOnceElasticJobListener;
import com.dangdang.ddframe.job.api.listener.ElasticJobListener;
import com.dangdang.ddframe.job.exception.JobException;
import com.dangdang.ddframe.job.internal.config.ConfigurationService;
import com.dangdang.ddframe.job.internal.election.LeaderElectionService;
import com.dangdang.ddframe.job.internal.execution.ExecutionContextService;
import com.dangdang.ddframe.job.internal.execution.ExecutionService;
import com.dangdang.ddframe.job.internal.failover.FailoverService;
import com.dangdang.ddframe.job.internal.guarantee.GuaranteeService;
import com.dangdang.ddframe.job.internal.listener.ListenerManager;
import com.dangdang.ddframe.job.internal.monitor.MonitorService;
import com.dangdang.ddframe.job.internal.offset.OffsetService;
Expand All @@ -48,8 +36,22 @@
import com.dangdang.ddframe.job.internal.statistics.StatisticsService;
import com.dangdang.ddframe.reg.base.CoordinatorRegistryCenter;
import com.google.common.base.Joiner;

import lombok.extern.slf4j.Slf4j;
import org.quartz.CronScheduleBuilder;
import org.quartz.CronTrigger;
import org.quartz.JobBuilder;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.Trigger;
import org.quartz.TriggerBuilder;
import org.quartz.TriggerKey;
import org.quartz.impl.StdSchedulerFactory;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Properties;

/**
* 作业调度器.
Expand Down Expand Up @@ -89,15 +91,19 @@ public class JobScheduler {
private final OffsetService offsetService;

private final MonitorService monitorService;

private List<ElasticJobListener> elasticJobListeners;

private Scheduler scheduler;

private JobDetail jobDetail;

public JobScheduler(final CoordinatorRegistryCenter coordinatorRegistryCenter, final JobConfiguration jobConfiguration) {
public JobScheduler(final CoordinatorRegistryCenter coordinatorRegistryCenter, final JobConfiguration jobConfiguration, final ElasticJobListener... elasticJobListeners) {
this.jobConfiguration = jobConfiguration;
this.coordinatorRegistryCenter = coordinatorRegistryCenter;
listenerManager = new ListenerManager(coordinatorRegistryCenter, jobConfiguration);
this.elasticJobListeners = Arrays.asList(elasticJobListeners);
setGuaranteeServiceForElasticJobListeners();
listenerManager = new ListenerManager(coordinatorRegistryCenter, jobConfiguration, this.elasticJobListeners);
configService = new ConfigurationService(coordinatorRegistryCenter, jobConfiguration);
leaderElectionService = new LeaderElectionService(coordinatorRegistryCenter, jobConfiguration);
serverService = new ServerService(coordinatorRegistryCenter, jobConfiguration);
Expand All @@ -111,6 +117,15 @@ public JobScheduler(final CoordinatorRegistryCenter coordinatorRegistryCenter, f
jobDetail = JobBuilder.newJob(jobConfiguration.getJobClass()).withIdentity(jobConfiguration.getJobName()).build();
}

private void setGuaranteeServiceForElasticJobListeners() {
GuaranteeService guaranteeService = new GuaranteeService(coordinatorRegistryCenter, jobConfiguration);
for (ElasticJobListener each : elasticJobListeners) {
if (each instanceof AbstractDistributeOnceElasticJobListener) {
((AbstractDistributeOnceElasticJobListener) each).setGuaranteeService(guaranteeService);
}
}
}

/**
* 初始化作业.
*/
Expand Down Expand Up @@ -146,6 +161,7 @@ private void fillJobDetail() {
jobDetail.getJobDataMap().put("executionService", executionService);
jobDetail.getJobDataMap().put("failoverService", failoverService);
jobDetail.getJobDataMap().put("offsetService", offsetService);
jobDetail.getJobDataMap().put("elasticJobListeners", elasticJobListeners);
}

private Scheduler initializeScheduler(final String jobName) throws SchedulerException {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
/**
* Copyright 1999-2015 dangdang.com.
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* </p>
*/

package com.dangdang.ddframe.job.api.listener;

import com.dangdang.ddframe.job.api.JobExecutionMultipleShardingContext;
import com.dangdang.ddframe.job.exception.JobTimeoutException;
import com.dangdang.ddframe.job.internal.env.TimeService;
import com.dangdang.ddframe.job.internal.guarantee.GuaranteeService;
import lombok.RequiredArgsConstructor;
import lombok.Setter;

/**
* 在分布式作业中只执行一次的监听器.
*
* @author zhangliang
*/
@RequiredArgsConstructor
public abstract class AbstractDistributeOnceElasticJobListener implements ElasticJobListener {

private final long startedTimeoutMills;

private final Object startedWait = new Object();

private final long completedTimeoutMills;

private final Object completedWait = new Object();

@Setter
private GuaranteeService guaranteeService;

private TimeService timeService = new TimeService();

@Override
public final void beforeJobExecuted(final JobExecutionMultipleShardingContext shardingContext) {
guaranteeService.registerStart(shardingContext.getShardingItems());
if (guaranteeService.isAllStarted()) {
doBeforeJobExecutedAtLastStarted(shardingContext);
guaranteeService.clearAllStartedInfo();
return;
}
long before = timeService.getCurrentMillis();
try {
synchronized (startedWait) {
startedWait.wait(startedTimeoutMills);
}
} catch (final InterruptedException ex) {
Thread.interrupted();
}
if (timeService.getCurrentMillis() - before >= startedTimeoutMills) {
guaranteeService.clearAllStartedInfo();
throw new JobTimeoutException(startedTimeoutMills);
}
}

@Override
public final void afterJobExecuted(final JobExecutionMultipleShardingContext shardingContext) {
guaranteeService.registerComplete(shardingContext.getShardingItems());
if (guaranteeService.isAllCompleted()) {
doAfterJobExecutedAtLastCompleted(shardingContext);
guaranteeService.clearAllCompletedInfo();
return;
}
long before = timeService.getCurrentMillis();
try {
synchronized (completedWait) {
completedWait.wait(completedTimeoutMills);
}
} catch (final InterruptedException ex) {
Thread.interrupted();
}
if (timeService.getCurrentMillis() - before >= completedTimeoutMills) {
guaranteeService.clearAllCompletedInfo();
throw new JobTimeoutException(completedTimeoutMills);
}
}

/**
* 分布式环境中最后一个作业执行前的执行的方法.
*
* @param shardingContext 分片上下文
*/
public abstract void doBeforeJobExecutedAtLastStarted(final JobExecutionMultipleShardingContext shardingContext);

/**
* 分布式环境中最后一个作业执行后的执行的方法.
*
* @param shardingContext 分片上下文
*/
public abstract void doAfterJobExecutedAtLastCompleted(final JobExecutionMultipleShardingContext shardingContext);

/**
* 通知任务开始.
*/
public void notifyWaitingTaskStart() {
synchronized (startedWait) {
startedWait.notifyAll();
}
}

/**
* 通知任务结束.
*/
public void notifyWaitingTaskComplete() {
synchronized (completedWait) {
completedWait.notifyAll();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/**
* Copyright 1999-2015 dangdang.com.
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* </p>
*/

package com.dangdang.ddframe.job.api.listener;

import com.dangdang.ddframe.job.api.JobExecutionMultipleShardingContext;

/**
* 弹性化分布式作业监听器接口.
*
* @author zhangliang
*/
public interface ElasticJobListener {

/**
* 作业执行前的执行的方法.
*
* @param shardingContext 分片上下文
*/
void beforeJobExecuted(final JobExecutionMultipleShardingContext shardingContext);

/**
* 作业执行后的执行的方法.
*
* @param shardingContext 分片上下文
*/
void afterJobExecuted(final JobExecutionMultipleShardingContext shardingContext);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/**
* Copyright 1999-2015 dangdang.com.
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* </p>
*/

package com.dangdang.ddframe.job.exception;

/**
* 作业超时抛出的异常.
*
* @author zhangliang
*/
public final class JobTimeoutException extends JobException {

/**
* 作业超时抛出的异常.
*
* @param timeoutMills 超时毫秒数
*/
public JobTimeoutException(final long timeoutMills) {
super("Job timeout. timeout mills is %s.", timeoutMills);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
import com.dangdang.ddframe.job.exception.JobException;

/**
* 获取真实本机网络的实现类.
* 获取真实本机网络的服务.
*
* @author zhangliang
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/**
* Copyright 1999-2015 dangdang.com.
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* </p>
*/

package com.dangdang.ddframe.job.internal.env;

/**
* 获取时间的服务.
*
* @author zhangliang
*/
public class TimeService {

/**
* 获取当前时间的毫秒数.
*
* @return 当前时间的毫秒数
*/
public long getCurrentMillis() {
return System.currentTimeMillis();
}
}
Loading

0 comments on commit 11dd433

Please sign in to comment.