Skip to content

Commit

Permalink
fix #65.
Browse files Browse the repository at this point in the history
  • Loading branch information
haocao committed Mar 11, 2016
1 parent b6325d9 commit b6afa1b
Show file tree
Hide file tree
Showing 21 changed files with 446 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,22 +21,20 @@
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 long startedTimeoutMillseconds;

private final Object startedWait = new Object();

private final long completedTimeoutMills;
private final long completedTimeoutMillseconds;

private final Object completedWait = new Object();

Expand All @@ -45,6 +43,19 @@ public abstract class AbstractDistributeOnceElasticJobListener implements Elasti

private TimeService timeService = new TimeService();

public AbstractDistributeOnceElasticJobListener(final long startedTimeoutMillseconds, final long completedTimeoutMillseconds) {
if (startedTimeoutMillseconds <= 0L) {
this.startedTimeoutMillseconds = Long.MAX_VALUE;
} else {
this.startedTimeoutMillseconds = startedTimeoutMillseconds;
}
if (completedTimeoutMillseconds <= 0L) {
this.completedTimeoutMillseconds = Long.MAX_VALUE;
} else {
this.completedTimeoutMillseconds = completedTimeoutMillseconds;
}
}

@Override
public final void beforeJobExecuted(final JobExecutionMultipleShardingContext shardingContext) {
guaranteeService.registerStart(shardingContext.getShardingItems());
Expand All @@ -56,14 +67,14 @@ public final void beforeJobExecuted(final JobExecutionMultipleShardingContext sh
long before = timeService.getCurrentMillis();
try {
synchronized (startedWait) {
startedWait.wait(startedTimeoutMills);
startedWait.wait(startedTimeoutMillseconds);
}
} catch (final InterruptedException ex) {
Thread.interrupted();
}
if (timeService.getCurrentMillis() - before >= startedTimeoutMills) {
if (timeService.getCurrentMillis() - before >= startedTimeoutMillseconds) {
guaranteeService.clearAllStartedInfo();
throw new JobTimeoutException(startedTimeoutMills);
throw new JobTimeoutException(startedTimeoutMillseconds);
}
}

Expand All @@ -78,14 +89,14 @@ public final void afterJobExecuted(final JobExecutionMultipleShardingContext sha
long before = timeService.getCurrentMillis();
try {
synchronized (completedWait) {
completedWait.wait(completedTimeoutMills);
completedWait.wait(completedTimeoutMillseconds);
}
} catch (final InterruptedException ex) {
Thread.interrupted();
}
if (timeService.getCurrentMillis() - before >= completedTimeoutMills) {
if (timeService.getCurrentMillis() - before >= completedTimeoutMillseconds) {
guaranteeService.clearAllCompletedInfo();
throw new JobTimeoutException(completedTimeoutMills);
throw new JobTimeoutException(completedTimeoutMillseconds);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import com.dangdang.ddframe.job.api.ElasticJob;
import com.dangdang.ddframe.job.api.JobExecutionMultipleShardingContext;
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.execution.ExecutionContextService;
import com.dangdang.ddframe.job.internal.execution.ExecutionService;
Expand Down Expand Up @@ -86,9 +85,11 @@ public final void execute(final JobExecutionContext context) throws JobExecution
if (!elasticJobListeners.isEmpty()) {
for (ElasticJobListener each : elasticJobListeners) {
try {
each.beforeJobExecuted(shardingContext);
} catch (final JobException ex) {
handleJobExecutionException(new JobExecutionException(ex));
each.beforeJobExecuted(shardingContext);
//CHECKSTYLE:OFF
} catch (final Throwable cause) {
//CHECKSTYLE:ON
handleJobExecutionException(new JobExecutionException(cause));
}
}
}
Expand All @@ -107,8 +108,10 @@ public final void execute(final JobExecutionContext context) throws JobExecution
for (ElasticJobListener each : elasticJobListeners) {
try {
each.afterJobExecuted(shardingContext);
} catch (final JobException ex) {
handleJobExecutionException(new JobExecutionException(ex));
//CHECKSTYLE:OFF
} catch (final Throwable cause) {
//CHECKSTYLE:ON
handleJobExecutionException(new JobExecutionException(cause));
}
}
}
Expand All @@ -124,9 +127,9 @@ private void executeJobInternal(final JobExecutionMultipleShardingContext shardi
try {
executeJob(shardingContext);
//CHECKSTYLE:OFF
} catch (final Exception ex) {
} catch (final Throwable cause) {
//CHECKSTYLE:ON
handleJobExecutionException(new JobExecutionException(ex));
handleJobExecutionException(new JobExecutionException(cause));
} finally {
// TODO 考虑增加作业失败的状态,并且考虑如何处理作业失败的整体回路
executionService.registerJobCompleted(shardingContext);
Expand Down
1 change: 1 addition & 0 deletions elastic-job-doc/content/post/release_notes.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ weight=1
1. [ISSUE #2](https://github.com/dangdangdotcom/elastic-job/issues/2) 增加前置和后置任务
1. [ISSUE #60](https://github.com/dangdangdotcom/elastic-job/issues/60) 可于DataFlow类型作业定制化线程池配置
1. [ISSUE #62](https://github.com/dangdangdotcom/elastic-job/issues/61) 作业状态清理提速
1. [ISSUE #65](https://github.com/dangdangdotcom/elastic-job/issues/65) 增加前置和后置任务Spring命名空间支持

### 缺陷修正

Expand Down
17 changes: 17 additions & 0 deletions elastic-job-doc/content/post/user_guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,7 @@ public class JobMain {
### Spring命名空间配置

```xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
Expand All @@ -226,6 +227,12 @@ public class JobMain {

<!-- 配置作业B-->
<job:bean id="throughputDataFlow" class="xxx.MyThroughputDataFlowElasticJob" regCenter="regCenter" cron="0/10 * * * * ?" shardingTotalCount="3" shardingItemParameters="0=A,1=B,2=C" processCountIntervalSeconds="10" concurrentDataProcessThreadCount="10" />

<!-- 配置作业C-->
<job:bean id="listenerElasticJob" class="xxx.MySimpleListenerElasticJob" regCenter="regCenter" cron="0/10 * * * * ?" shardingTotalCount="3" shardingItemParameters="0=A,1=B,2=C">
<job:listener class="xx.MySimpleJobListener"/>
<job:listener class="xx.MyOnceSimpleJobListener" startedTimeoutMillseconds="1000" completedTimeoutMillseconds="2000" />
</job:bean>
</beans>
```

Expand Down Expand Up @@ -253,6 +260,16 @@ public class JobMain {
| disabled |boolean||false| 作业是否禁止启动<br />可用于部署作业时,先禁止启动,部署结束后统一启动 |
| overwrite |boolean||false| 本地配置是否可覆盖注册中心配置<br />如果可覆盖,每次启动作业都以本地配置为准 |

#### job:listener命名空间属性详细说明

job:listener必须配置为job:bean的子元素

| 属性名 | 类型 |是否必填|缺省值| 描述 |
| ------------------------------ |:------|:------|:----|:---------------------------------------------------------------------------|
|class |String |`` | | 前置后置任务监听实现类,需实现`ElasticJobListener`接口 |
|startedTimeoutMillseconds |int || | AbstractDistributeOnceElasticJobListener型监听器,最后一个作业执行前的执行方法的超时时间<br />单位:毫秒|
|completedTimeoutMillseconds |int || | AbstractDistributeOnceElasticJobListener型监听器,最后一个作业执行后的执行方法的超时时间<br />单位:毫秒|

#### reg:bean命名空间属性详细说明

| 属性名 |类型 |是否必填|缺省值|描述 |
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.dangdang.example.elasticjob.spring.job.listener;

public interface FooService {

String foo();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.dangdang.example.elasticjob.spring.job.listener;

import org.springframework.stereotype.Component;

@Component
public class FooServiceImpl implements FooService {

public String foo() {
return "this is foo.";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.dangdang.example.elasticjob.spring.job.listener;

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

public class SimpleListener implements ElasticJobListener {

@Override
public void beforeJobExecuted(final JobExecutionMultipleShardingContext shardingContext) {
System.out.println("beforeJobExecuted:" + shardingContext.getJobName());
}

@Override
public void afterJobExecuted(final JobExecutionMultipleShardingContext shardingContext) {
System.out.println("afterJobExecuted:" + shardingContext.getJobName());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* *
* * 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.example.elasticjob.spring.job.listener;

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

import javax.annotation.Resource;

public class SimpleOnceListener extends AbstractDistributeOnceElasticJobListener {

@Resource
private FooService fooService;

private final long startedTimeoutMillseconds;

private final long completedTimeoutMillseconds;

public SimpleOnceListener(final long startedTimeoutMillseconds, final long completedTimeoutMillseconds) {
super(startedTimeoutMillseconds, completedTimeoutMillseconds);
this.startedTimeoutMillseconds = startedTimeoutMillseconds;
this.completedTimeoutMillseconds = completedTimeoutMillseconds;
}

@Override
public void doBeforeJobExecutedAtLastStarted(final JobExecutionMultipleShardingContext shardingContext) {
System.out.println("doBeforeJobExecutedAtLastStarted:" + fooService.foo());
}

@Override
public void doAfterJobExecutedAtLastCompleted(final JobExecutionMultipleShardingContext shardingContext) {
System.out.println("doAfterJobExecutedAtLastCompleted:" + startedTimeoutMillseconds + "," + completedTimeoutMillseconds);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/**
* 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.example.elasticjob.spring.main;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public final class SpringJobMainWthNamespaceAndListener {

private SpringJobMainWthNamespaceAndListener() {
}

// CHECKSTYLE:OFF
@SuppressWarnings("resource")
public static void main(final String[] args) {
// CHECKSTYLE:ON
new ClassPathXmlApplicationContext("classpath:META-INF/withNamespaceAndListener.xml");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:reg="http://www.dangdang.com/schema/ddframe/reg"
xmlns:job="http://www.dangdang.com/schema/ddframe/job"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.dangdang.com/schema/ddframe/reg
http://www.dangdang.com/schema/ddframe/reg/reg.xsd
http://www.dangdang.com/schema/ddframe/job
http://www.dangdang.com/schema/ddframe/job/job.xsd
">
<context:component-scan base-package="com.dangdang.example.elasticjob" />
<context:property-placeholder location="classpath:conf/*.properties" />

<reg:zookeeper id="regCenter" serverLists="${serverLists}" namespace="${namespace}" baseSleepTimeMilliseconds="${baseSleepTimeMilliseconds}" maxSleepTimeMilliseconds="${maxSleepTimeMilliseconds}" maxRetries="${maxRetries}" nestedPort="${nestedPort}" nestedDataDir="${nestedDataDir}" />

<job:bean id="simpleElasticJob" class="com.dangdang.example.elasticjob.spring.job.SimpleJobDemo" regCenter="regCenter" shardingTotalCount="${simpleJob.shardingTotalCount}" cron="${simpleJob.cron}" shardingItemParameters="${simpleJob.shardingItemParameters}" monitorExecution="${simpleJob.monitorExecution}" monitorPort="${simpleJob.monitorPort}" failover="${simpleJob.failover}" description="${simpleJob.description}" disabled="${simpleJob.disabled}" overwrite="${simpleJob.overwrite}">
<job:listener class="com.dangdang.example.elasticjob.spring.job.listener.SimpleListener" />
<job:listener class="com.dangdang.example.elasticjob.spring.job.listener.SimpleOnceListener" startedTimeoutMillseconds="1000" completedTimeoutMillseconds="2000" />
</job:bean>
</beans>
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,21 @@

package com.dangdang.ddframe.job.spring.namespace;

import com.dangdang.ddframe.job.api.listener.AbstractDistributeOnceElasticJobListener;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.support.AbstractBeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.ManagedList;
import org.springframework.beans.factory.xml.AbstractBeanDefinitionParser;
import org.springframework.beans.factory.xml.ParserContext;
import org.springframework.util.xml.DomUtils;
import org.w3c.dom.Element;

import com.dangdang.ddframe.job.api.JobConfiguration;
import com.dangdang.ddframe.job.spring.schedule.SpringJobScheduler;
import com.google.common.base.Strings;

import java.util.List;

/**
* 分布式作业的命名空间解析器.
*
Expand All @@ -43,11 +48,12 @@ protected AbstractBeanDefinition parseInternal(final Element element, final Pars
factory.setDestroyMethodName("shutdown");
factory.addConstructorArgReference(element.getAttribute("regCenter"));
factory.addConstructorArgReference(createJobConfiguration(element, parserContext));
factory.addConstructorArgValue(createJobListeners(element));
return factory.getBeanDefinition();
}

private String createJobConfiguration(final Element element, final ParserContext parserContext) {
BeanDefinitionBuilder factory = BeanDefinitionBuilder.rootBeanDefinition(JobConfiguration.class);
BeanDefinitionBuilder factory = BeanDefinitionBuilder.rootBeanDefinition("com.dangdang.ddframe.job.api.JobConfiguration");
factory.addConstructorArgValue(element.getAttribute("id"));
factory.addConstructorArgValue(element.getAttribute("class"));
factory.addConstructorArgValue(element.getAttribute("shardingTotalCount"));
Expand All @@ -71,6 +77,27 @@ private String createJobConfiguration(final Element element, final ParserContext
return result;
}

private List<BeanDefinition> createJobListeners(final Element element) {
List<Element> listenerElements = DomUtils.getChildElementsByTagName(element, "listener");
List<BeanDefinition> result = new ManagedList<>(listenerElements.size());
for (Element each : listenerElements) {
String className = each.getAttribute("class");
BeanDefinitionBuilder factory = BeanDefinitionBuilder.rootBeanDefinition(className);
factory.setScope(BeanDefinition.SCOPE_PROTOTYPE);
try {
Class listenerClass = Class.forName(className);
if (AbstractDistributeOnceElasticJobListener.class.isAssignableFrom(listenerClass)) {
factory.addConstructorArgValue(each.getAttribute("startedTimeoutMillseconds"));
factory.addConstructorArgValue(each.getAttribute("completedTimeoutMillseconds"));
}
} catch (final ClassNotFoundException ex) {
throw new RuntimeException(ex);
}
result.add(factory.getBeanDefinition());
}
return result;
}

private void addPropertyValueIfNotEmpty(final String propertyName, final Element element, final BeanDefinitionBuilder factory) {
String propertyValue = element.getAttribute(propertyName);
if (!Strings.isNullOrEmpty(propertyValue)) {
Expand Down
Loading

0 comments on commit b6afa1b

Please sign in to comment.