-
Notifications
You must be signed in to change notification settings - Fork 3.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
32 changed files
with
1,330 additions
and
65 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
123 changes: 123 additions & 0 deletions
123
.../java/com/dangdang/ddframe/job/api/listener/AbstractDistributeOnceElasticJobListener.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
elastic-job-core/src/main/java/com/dangdang/ddframe/job/api/listener/ElasticJobListener.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
35 changes: 35 additions & 0 deletions
35
elastic-job-core/src/main/java/com/dangdang/ddframe/job/exception/JobTimeoutException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
elastic-job-core/src/main/java/com/dangdang/ddframe/job/internal/env/TimeService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
Oops, something went wrong.