JobClientAutoConfiguration.java
2.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package com.github.ltsopensource.spring.boot;
import com.github.ltsopensource.core.cluster.AbstractJobNode;
import com.github.ltsopensource.core.cluster.NodeType;
import com.github.ltsopensource.core.commons.utils.CollectionUtils;
import com.github.ltsopensource.jobclient.JobClient;
import com.github.ltsopensource.jobclient.JobClientBuilder;
import com.github.ltsopensource.jobclient.support.JobCompletedHandler;
import com.github.ltsopensource.spring.boot.annotation.EnableJobClient;
import com.github.ltsopensource.spring.boot.annotation.JobCompletedHandler4JobClient;
import com.github.ltsopensource.spring.boot.properties.JobClientProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.util.Map;
/**
* @author Robert HG (254963746@qq.com) on 4/9/16.
*/
@Configuration
@ConditionalOnBean(annotation = EnableJobClient.class)
@EnableConfigurationProperties(JobClientProperties.class)
public class JobClientAutoConfiguration extends AbstractAutoConfiguration {
@Autowired(required = false)
private JobClientProperties properties;
private JobClient jobClient;
@Bean
public JobClient jobClient() {
return jobClient;
}
@Override
protected void initJobNode() {
jobClient = JobClientBuilder.buildByProperties(properties);
Map<String, Object> handlers = applicationContext.getBeansWithAnnotation(JobCompletedHandler4JobClient.class);
if (CollectionUtils.isNotEmpty(handlers)) {
if (handlers.size() > 1) {
throw new IllegalArgumentException("annotation @" + JobCompletedHandler4JobClient.class.getSimpleName() + " only should have one");
}
for (Map.Entry<String, Object> entry : handlers.entrySet()) {
Object handler = entry.getValue();
if (handler instanceof JobCompletedHandler) {
jobClient.setJobCompletedHandler((JobCompletedHandler) entry.getValue());
} else {
throw new IllegalArgumentException(entry.getKey() + " is not instance of " + JobCompletedHandler.class.getName());
}
}
}
}
@Override
protected NodeType nodeType() {
return NodeType.JOB_CLIENT;
}
@Override
protected AbstractJobNode getJobNode() {
return jobClient;
}
}