Skip to content

Commit

Permalink
For #153 refactor spring namespace support.
Browse files Browse the repository at this point in the history
  • Loading branch information
haocao committed Nov 8, 2016
1 parent 85e50d4 commit a2f9aca
Show file tree
Hide file tree
Showing 10 changed files with 97 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,16 @@
<context:component-scan base-package="com.dangdang.ddframe.job.example" />
<context:property-placeholder location="classpath:conf/*.properties" />

<bean id="elasticJobLog" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="org.h2.Driver"/>
<property name="url" value="jdbc:h2:mem:job_event_storage"/>
<property name="username" value="sa"/>
<property name="password" value=""/>
</bean>

<reg:zookeeper id="regCenter" server-lists="${serverLists}" namespace="${namespace}" base-sleep-time-milliseconds="${baseSleepTimeMilliseconds}" max-sleep-time-milliseconds="${maxSleepTimeMilliseconds}" max-retries="${maxRetries}" />

<job:simple id="${simple.id}" class="${simple.class}" registry-center-ref="regCenter" sharding-total-count="${simple.shardingTotalCount}" cron="${simple.cron}" sharding-item-parameters="${simple.shardingItemParameters}" monitor-execution="${simple.monitorExecution}" monitor-port="${simple.monitorPort}" failover="${simple.failover}" description="${simple.description}" disabled="${simple.disabled}" overwrite="${simple.overwrite}">
<job:simple id="${simple.id}" class="${simple.class}" registry-center-ref="regCenter" sharding-total-count="${simple.shardingTotalCount}" cron="${simple.cron}" sharding-item-parameters="${simple.shardingItemParameters}" monitor-execution="${simple.monitorExecution}" monitor-port="${simple.monitorPort}" failover="${simple.failover}" description="${simple.description}" disabled="${simple.disabled}" overwrite="${simple.overwrite}" event-trace-data-source="elasticJobLog">
<job:listener class="${listener.simple}" />
<job:distributed-listener class="${listener.distributed}" started-timeout-milliseconds="${listener.distributed.startedTimeoutMilliseconds}" completed-timeout-milliseconds="${listener.distributed.completedTimeoutMilliseconds}" />
</job:simple>
Expand Down
5 changes: 5 additions & 0 deletions elastic-job-lite/elastic-job-lite-spring/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -59,5 +59,10 @@
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
</dependency>
<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,9 @@ public final class BaseJobBeanDefinitionParserTag {

public static final String DISTRIBUTED_LISTENER_COMPLETED_TIMEOUT_MILLISECONDS_ATTRIBUTE = "completed-timeout-milliseconds";

public static final String EXECUTOR_SERVICE_HANDLER = "executor-service-handler";
public static final String EXECUTOR_SERVICE_HANDLER_ATTRIBUTE = "executor-service-handler";

public static final String JOB_EXCEPTION_HANDLER = "job-exception-handler";
public static final String JOB_EXCEPTION_HANDLER_ATTRIBUTE = "job-exception-handler";

public static final String EVENT_TRACE_DATA_SOURCE_ATTRIBUTE = "event-trace-data-source";
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

package com.dangdang.ddframe.job.lite.spring.namespace.parser.common;

import com.dangdang.ddframe.job.event.rdb.JobEventRdbConfiguration;
import com.dangdang.ddframe.job.lite.spring.schedule.SpringJobScheduler;
import com.google.common.base.Strings;
import org.springframework.beans.factory.config.BeanDefinition;
Expand All @@ -37,9 +38,10 @@
import static com.dangdang.ddframe.job.lite.spring.namespace.constants.BaseJobBeanDefinitionParserTag.DISTRIBUTED_LISTENER_COMPLETED_TIMEOUT_MILLISECONDS_ATTRIBUTE;
import static com.dangdang.ddframe.job.lite.spring.namespace.constants.BaseJobBeanDefinitionParserTag.DISTRIBUTED_LISTENER_STARTED_TIMEOUT_MILLISECONDS_ATTRIBUTE;
import static com.dangdang.ddframe.job.lite.spring.namespace.constants.BaseJobBeanDefinitionParserTag.DISTRIBUTED_LISTENER_TAG;
import static com.dangdang.ddframe.job.lite.spring.namespace.constants.BaseJobBeanDefinitionParserTag.EXECUTOR_SERVICE_HANDLER;
import static com.dangdang.ddframe.job.lite.spring.namespace.constants.BaseJobBeanDefinitionParserTag.EVENT_TRACE_DATA_SOURCE_ATTRIBUTE;
import static com.dangdang.ddframe.job.lite.spring.namespace.constants.BaseJobBeanDefinitionParserTag.EXECUTOR_SERVICE_HANDLER_ATTRIBUTE;
import static com.dangdang.ddframe.job.lite.spring.namespace.constants.BaseJobBeanDefinitionParserTag.FAILOVER_ATTRIBUTE;
import static com.dangdang.ddframe.job.lite.spring.namespace.constants.BaseJobBeanDefinitionParserTag.JOB_EXCEPTION_HANDLER;
import static com.dangdang.ddframe.job.lite.spring.namespace.constants.BaseJobBeanDefinitionParserTag.JOB_EXCEPTION_HANDLER_ATTRIBUTE;
import static com.dangdang.ddframe.job.lite.spring.namespace.constants.BaseJobBeanDefinitionParserTag.JOB_PARAMETER_ATTRIBUTE;
import static com.dangdang.ddframe.job.lite.spring.namespace.constants.BaseJobBeanDefinitionParserTag.JOB_SHARDING_STRATEGY_CLASS_ATTRIBUTE;
import static com.dangdang.ddframe.job.lite.spring.namespace.constants.BaseJobBeanDefinitionParserTag.LISTENER_TAG;
Expand All @@ -62,15 +64,21 @@ public abstract class AbstractJobBeanDefinitionParser extends AbstractBeanDefini

@Override
protected AbstractBeanDefinition parseInternal(final Element element, final ParserContext parserContext) {

BeanDefinitionBuilder factory = BeanDefinitionBuilder.rootBeanDefinition(SpringJobScheduler.class);
factory.setInitMethodName("init");
factory.setDestroyMethodName("shutdown");
factory.addConstructorArgReference(element.getAttribute(REGISTRY_CENTER_REF_ATTRIBUTE));
factory.addConstructorArgValue(createJobConfiguration(element));
factory.addConstructorArgValue(createJobEventConfigs(element));
factory.addConstructorArgValue(createJobListeners(element));
return factory.getBeanDefinition();
}

protected abstract Class<? extends AbstractJobConfigurationDto> getJobConfigurationDTO();

protected abstract void setPropertiesValue(final Element element, final BeanDefinitionBuilder factory);

private BeanDefinition createJobConfiguration(final Element element) {
BeanDefinitionBuilder factory = BeanDefinitionBuilder.rootBeanDefinition(getJobConfigurationDTO());
String jobName = element.getAttribute(ID_ATTRIBUTE);
Expand All @@ -88,15 +96,22 @@ private BeanDefinition createJobConfiguration(final Element element) {
addPropertyValueIfNotEmpty(DESCRIPTION_ATTRIBUTE, "description", element, factory);
addPropertyValueIfNotEmpty(DISABLED_ATTRIBUTE, "disabled", element, factory);
addPropertyValueIfNotEmpty(OVERWRITE_ATTRIBUTE, "overwrite", element, factory);
addPropertyValueIfNotEmpty(EXECUTOR_SERVICE_HANDLER, "executorServiceHandler", element, factory);
addPropertyValueIfNotEmpty(JOB_EXCEPTION_HANDLER, "jobExceptionHandler", element, factory);
addPropertyValueIfNotEmpty(EXECUTOR_SERVICE_HANDLER_ATTRIBUTE, "executorServiceHandler", element, factory);
addPropertyValueIfNotEmpty(JOB_EXCEPTION_HANDLER_ATTRIBUTE, "jobExceptionHandler", element, factory);
setPropertiesValue(element, factory);
return factory.getBeanDefinition();
}

protected abstract Class<? extends AbstractJobConfigurationDto> getJobConfigurationDTO();

protected abstract void setPropertiesValue(final Element element, final BeanDefinitionBuilder factory);
private List<BeanDefinition> createJobEventConfigs(final Element element) {
List<BeanDefinition> result = new ManagedList<>();
String eventTraceRdbDs = element.getAttribute(EVENT_TRACE_DATA_SOURCE_ATTRIBUTE);
if (!Strings.isNullOrEmpty(eventTraceRdbDs)) {
BeanDefinitionBuilder factory = BeanDefinitionBuilder.rootBeanDefinition(JobEventRdbConfiguration.class);
factory.addConstructorArgReference(eventTraceRdbDs);
result.add(factory.getBeanDefinition());
}
return result;
}

private List<BeanDefinition> createJobListeners(final Element element) {
Element listenerElement = DomUtils.getChildElementByTagName(element, LISTENER_TAG);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,9 +110,6 @@ private LiteJobConfiguration buildLiteJobConfiguration(final JobCoreConfiguratio
if (null != overwrite) {
result.overwrite(overwrite);
}
if (null != overwrite) {
result.overwrite(overwrite);
}
return result.build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

package com.dangdang.ddframe.job.lite.spring.schedule;

import com.dangdang.ddframe.job.event.JobEventConfiguration;
import com.dangdang.ddframe.job.lite.api.JobScheduler;
import com.dangdang.ddframe.job.lite.api.listener.ElasticJobListener;
import com.dangdang.ddframe.job.lite.spring.namespace.parser.common.AbstractJobConfigurationDto;
Expand All @@ -25,6 +26,7 @@
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

import java.util.Collection;
import java.util.Properties;

/**
Expand All @@ -36,8 +38,9 @@ public class SpringJobScheduler extends JobScheduler implements ApplicationConte

private ApplicationContext applicationContext;

public SpringJobScheduler(final CoordinatorRegistryCenter regCenter, final AbstractJobConfigurationDto jobConfigDto, final ElasticJobListener[] elasticJobListeners) {
super(regCenter, jobConfigDto.toLiteJobConfiguration(), getTargetElasticJobListeners(elasticJobListeners));
public SpringJobScheduler(final CoordinatorRegistryCenter regCenter, final AbstractJobConfigurationDto jobConfigDto, final Collection<JobEventConfiguration> jobEventConfigs,
final ElasticJobListener[]elasticJobListeners) {
super(regCenter, jobConfigDto.toLiteJobConfiguration(), jobEventConfigs, getTargetElasticJobListeners(elasticJobListeners));
}

private static ElasticJobListener[] getTargetElasticJobListeners(final ElasticJobListener[] elasticJobListeners) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
<xsd:attribute name="overwrite" type="xsd:string" />
<xsd:attribute name="executor-service-handler" type="xsd:string" />
<xsd:attribute name="job-exception-handler" type="xsd:string" />
<xsd:attribute name="event-trace-data-source" type="xsd:string" />
</xsd:extension>
</xsd:complexContent>
</xsd:complexType>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@
JobSpringNamespaceWithJobPropertiesTest.class,
JobSpringNamespaceWithListenerTest.class,
JobSpringNamespaceWithListenerAndJdkDynamicProxyTest.class,
JobSpringNamespaceWithListenerAndCglibTest.class
JobSpringNamespaceWithListenerAndCglibTest.class,
JobSpringNamespaceWithEventTraceRdbTest.class
})
public class AllSpringIntegrateTests {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* 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.lite.spring.integrate;

import org.springframework.test.context.ContextConfiguration;

@ContextConfiguration(locations = "classpath:META-INF/job/withEventTraceRdb.xml")
public class JobSpringNamespaceWithEventTraceRdbTest extends AbstractJobSpringIntegrateTest {

public JobSpringNamespaceWithEventTraceRdbTest() {
super("simpleElasticJob_namespace_event_trace_rdb", "dataflowElasticJob_namespace_event_trace_rdb");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?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: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.dangdang.com/schema/ddframe/job
http://www.dangdang.com/schema/ddframe/job/job.xsd
">
<import resource="base.xml"/>

<bean id="elasticJobLog" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="org.h2.Driver"/>
<property name="url" value="jdbc:h2:mem:job_event_storage"/>
<property name="username" value="sa"/>
<property name="password" value=""/>
</bean>

<job:simple id="simpleElasticJob_namespace_event_trace_rdb" class="com.dangdang.ddframe.job.lite.fixture.FooSimpleElasticJob" registry-center-ref="regCenter" cron="${simpleJob.cron}" sharding-total-count="${simpleJob.shardingTotalCount}" sharding-item-parameters="${simpleJob.shardingItemParameters}" disabled="${simpleJob.disabled}" overwrite="${simpleJob.overwrite}" executor-service-handler="com.dangdang.ddframe.job.lite.fixture.handler.SimpleExecutorServiceHandler" event-trace-data-source="elasticJobLog"/>

<job:dataflow id="dataflowElasticJob_namespace_event_trace_rdb" class="com.dangdang.ddframe.job.lite.fixture.DataflowElasticJob" registry-center-ref="regCenter" cron="0/1 * * * * ?" sharding-total-count="3" sharding-item-parameters="0=A,1=B,2=C" description="中文描述" overwrite="true" job-exception-handler="com.dangdang.ddframe.job.lite.fixture.handler.SimpleJobExceptionHandler" />
</beans>

0 comments on commit a2f9aca

Please sign in to comment.