Revision 46424
Added by Michele Artini over 7 years ago
modules/dnet-springboot-apps/trunk/dnet-common-utils/src/main/java/eu/dnetlib/clients/msro/MsroClient.java | ||
---|---|---|
1 | 1 |
package eu.dnetlib.clients.msro; |
2 | 2 |
|
3 |
import java.util.Map; |
|
4 |
|
|
5 |
import org.springframework.beans.factory.annotation.Autowired; |
|
3 | 6 |
import org.springframework.context.annotation.Scope; |
4 | 7 |
import org.springframework.stereotype.Component; |
5 | 8 |
|
9 |
import eu.dnetlib.clients.BaseServiceClient; |
|
6 | 10 |
import eu.dnetlib.enabling.annotations.DnetServiceClient; |
7 | 11 |
import eu.dnetlib.enabling.annotations.DnetServiceType; |
12 |
import eu.dnetlib.services.async.AsyncClientCallback; |
|
13 |
import eu.dnetlib.services.async.AsyncClientUtils; |
|
8 | 14 |
|
9 | 15 |
@Component |
10 | 16 |
@Scope("prototype") |
11 | 17 |
@DnetServiceClient(DnetServiceType.msro) |
12 |
public class MsroClient { |
|
18 |
public class MsroClient extends BaseServiceClient {
|
|
13 | 19 |
|
20 |
@Autowired |
|
21 |
private AsyncClientUtils asyncClientUtils; |
|
22 |
|
|
23 |
public void startWorkflow(final String wfId, final String parent, final AsyncClientCallback callback) { |
|
24 |
final WorkflowDesc wf = new WorkflowDesc(); |
|
25 |
wf.setId(wfId); |
|
26 |
wf.setParent(parent); |
|
27 |
asyncClientUtils.invokeRemoteMethod(getBaseUrl(), "startWorkflow", wf, callback); |
|
28 |
} |
|
29 |
|
|
30 |
public void startWorkflowTemplate(final String wfTemplateId, final String parent, final Map<String, String> params, final AsyncClientCallback callback) { |
|
31 |
final WorkflowDesc wf = new WorkflowDesc(); |
|
32 |
wf.setId(wfTemplateId); |
|
33 |
wf.setParent(parent); |
|
34 |
wf.setTemplate(true); |
|
35 |
wf.setParams(params); |
|
36 |
|
|
37 |
asyncClientUtils.invokeRemoteMethod(getBaseUrl(), "startWorkflow", wf, callback); |
|
38 |
} |
|
14 | 39 |
} |
modules/dnet-springboot-apps/trunk/dnet-common-utils/src/main/java/eu/dnetlib/clients/msro/WorkflowDesc.java | ||
---|---|---|
1 |
package eu.dnetlib.clients.msro; |
|
2 |
|
|
3 |
import java.util.HashMap; |
|
4 |
import java.util.Map; |
|
5 |
|
|
6 |
public class WorkflowDesc { |
|
7 |
|
|
8 |
private String id; |
|
9 |
private String parent; |
|
10 |
private boolean template = false; |
|
11 |
private Map<String, String> params = new HashMap<>(); |
|
12 |
private String family; |
|
13 |
private int priority = 50; |
|
14 |
private String dsId; |
|
15 |
private String iface; |
|
16 |
|
|
17 |
public String getId() { |
|
18 |
return id; |
|
19 |
} |
|
20 |
|
|
21 |
public void setId(final String id) { |
|
22 |
this.id = id; |
|
23 |
} |
|
24 |
|
|
25 |
public String getParent() { |
|
26 |
return parent; |
|
27 |
} |
|
28 |
|
|
29 |
public void setParent(final String parent) { |
|
30 |
this.parent = parent; |
|
31 |
} |
|
32 |
|
|
33 |
public boolean isTemplate() { |
|
34 |
return template; |
|
35 |
} |
|
36 |
|
|
37 |
public void setTemplate(final boolean template) { |
|
38 |
this.template = template; |
|
39 |
} |
|
40 |
|
|
41 |
public Map<String, String> getParams() { |
|
42 |
return params; |
|
43 |
} |
|
44 |
|
|
45 |
public void setParams(final Map<String, String> params) { |
|
46 |
this.params = params; |
|
47 |
} |
|
48 |
|
|
49 |
public String getFamily() { |
|
50 |
return family; |
|
51 |
} |
|
52 |
|
|
53 |
public void setFamily(final String family) { |
|
54 |
this.family = family; |
|
55 |
} |
|
56 |
|
|
57 |
public int getPriority() { |
|
58 |
return priority; |
|
59 |
} |
|
60 |
|
|
61 |
public void setPriority(final int priority) { |
|
62 |
this.priority = priority; |
|
63 |
} |
|
64 |
|
|
65 |
public String getDsId() { |
|
66 |
return dsId; |
|
67 |
} |
|
68 |
|
|
69 |
public void setDsId(final String dsId) { |
|
70 |
this.dsId = dsId; |
|
71 |
} |
|
72 |
|
|
73 |
public String getIface() { |
|
74 |
return iface; |
|
75 |
} |
|
76 |
|
|
77 |
public void setIface(final String iface) { |
|
78 |
this.iface = iface; |
|
79 |
} |
|
80 |
|
|
81 |
} |
modules/dnet-springboot-apps/trunk/dnet-common-utils/src/main/java/eu/dnetlib/services/HCMController.java | ||
---|---|---|
1 |
package eu.dnetlib.services; |
|
2 |
|
|
3 |
import java.io.IOException; |
|
4 |
import java.lang.management.ManagementFactory; |
|
5 |
import java.lang.management.RuntimeMXBean; |
|
6 |
import java.util.ArrayList; |
|
7 |
import java.util.Arrays; |
|
8 |
import java.util.Iterator; |
|
9 |
import java.util.List; |
|
10 |
import java.util.Map; |
|
11 |
import java.util.TreeMap; |
|
12 |
import java.util.UUID; |
|
13 |
import java.util.stream.Collectors; |
|
14 |
|
|
15 |
import org.apache.commons.logging.Log; |
|
16 |
import org.apache.commons.logging.LogFactory; |
|
17 |
import org.apache.maven.model.Model; |
|
18 |
import org.apache.maven.model.Parent; |
|
19 |
import org.apache.maven.model.io.xpp3.MavenXpp3Reader; |
|
20 |
import org.dom4j.DocumentHelper; |
|
21 |
import org.dom4j.Element; |
|
22 |
import org.springframework.beans.factory.annotation.Autowired; |
|
23 |
import org.springframework.context.ResourceLoaderAware; |
|
24 |
import org.springframework.core.env.AbstractEnvironment; |
|
25 |
import org.springframework.core.env.Environment; |
|
26 |
import org.springframework.core.env.MapPropertySource; |
|
27 |
import org.springframework.core.env.PropertySource; |
|
28 |
import org.springframework.core.io.Resource; |
|
29 |
import org.springframework.core.io.ResourceLoader; |
|
30 |
import org.springframework.core.io.support.ResourcePatternUtils; |
|
31 |
import org.springframework.web.bind.annotation.RequestMapping; |
|
32 |
import org.springframework.web.bind.annotation.RequestMethod; |
|
33 |
import org.springframework.web.bind.annotation.RestController; |
|
34 |
|
|
35 |
import com.google.common.collect.Maps; |
|
36 |
|
|
37 |
import eu.dnetlib.conf.DnetGenericApplicationProperties; |
|
38 |
import eu.dnetlib.enabling.annotations.DnetService; |
|
39 |
import eu.dnetlib.enabling.annotations.DnetServiceType; |
|
40 |
import eu.dnetlib.miscutils.datetime.DateUtils; |
|
41 |
import eu.dnetlib.miscutils.datetime.HumanTime; |
|
42 |
import eu.dnetlib.miscutils.streams.DnetStreamSupport; |
|
43 |
|
|
44 |
@RestController |
|
45 |
@RequestMapping(value = "/hcm", method = RequestMethod.GET) |
|
46 |
@DnetService(DnetServiceType.hcm) |
|
47 |
public class HCMController extends BaseService implements ResourceLoaderAware { |
|
48 |
|
|
49 |
private static final Log log = LogFactory.getLog(HCMController.class); |
|
50 |
|
|
51 |
@Autowired |
|
52 |
private Environment env; |
|
53 |
|
|
54 |
@Autowired |
|
55 |
private DnetGenericApplicationProperties containerConfiguration; |
|
56 |
|
|
57 |
private ResourceLoader resourceLoader; |
|
58 |
|
|
59 |
@RequestMapping("properties") |
|
60 |
public Map<String, Object> listProperties() { |
|
61 |
|
|
62 |
final Iterator<PropertySource<?>> iter = ((AbstractEnvironment) env).getPropertySources().iterator(); |
|
63 |
return DnetStreamSupport.generateStreamFromIterator(iter) |
|
64 |
.filter(ps -> ps instanceof MapPropertySource) |
|
65 |
.map(ps -> (MapPropertySource) ps) |
|
66 |
.collect(Collectors.toMap(MapPropertySource::getName, MapPropertySource::getSource)); |
|
67 |
|
|
68 |
} |
|
69 |
|
|
70 |
@RequestMapping("info") |
|
71 |
public Map<String, Object> info() { |
|
72 |
final RuntimeMXBean mxbean = ManagementFactory.getRuntimeMXBean(); |
|
73 |
final Map<String, Object> genInfo = Maps.newLinkedHashMap(); |
|
74 |
genInfo.put("Hostname", containerConfiguration.getHost()); |
|
75 |
genInfo.put("Port", containerConfiguration.getPort()); |
|
76 |
genInfo.put("Uptime", HumanTime.exactly(mxbean.getUptime())); |
|
77 |
genInfo.put("Start Time", DateUtils.calculate_ISO8601(mxbean.getStartTime())); |
|
78 |
return genInfo; |
|
79 |
} |
|
80 |
|
|
81 |
@RequestMapping("jvm") |
|
82 |
public Map<String, String> jvmInfo() { |
|
83 |
final RuntimeMXBean mxbean = ManagementFactory.getRuntimeMXBean(); |
|
84 |
final Map<String, String> jvmInfo = Maps.newLinkedHashMap(); |
|
85 |
jvmInfo.put("JVM Name", mxbean.getVmName()); |
|
86 |
jvmInfo.put("JVM Vendor", mxbean.getVmVendor()); |
|
87 |
jvmInfo.put("JVM Version", mxbean.getVmVersion()); |
|
88 |
jvmInfo.put("JVM Spec Name", mxbean.getSpecName()); |
|
89 |
jvmInfo.put("JVM Spec Vendor", mxbean.getSpecVendor()); |
|
90 |
jvmInfo.put("JVM Spec Version", mxbean.getSpecVersion()); |
|
91 |
jvmInfo.put("Running JVM Name", mxbean.getName()); |
|
92 |
jvmInfo.put("Management Spec Version", mxbean.getManagementSpecVersion()); |
|
93 |
return jvmInfo; |
|
94 |
} |
|
95 |
|
|
96 |
@RequestMapping("libs") |
|
97 |
public Map<String, String> libInfo() { |
|
98 |
final RuntimeMXBean mxbean = ManagementFactory.getRuntimeMXBean(); |
|
99 |
final Map<String, String> libInfo = Maps.newLinkedHashMap(); |
|
100 |
libInfo.put("Classpath", mxbean.getClassPath().replaceAll(":", " : ")); |
|
101 |
libInfo.put("Boot ClassPath", mxbean.getBootClassPath().replaceAll(":", " : ")); |
|
102 |
libInfo.put("Input arguments", mxbean.getInputArguments().toString()); |
|
103 |
libInfo.put("Library Path", mxbean.getLibraryPath().replaceAll(":", " : ")); |
|
104 |
return libInfo; |
|
105 |
} |
|
106 |
|
|
107 |
@RequestMapping("maven") |
|
108 |
private Map<String, Map<String, Map<String, List<String>>>> mavenModules() throws IOException { |
|
109 |
final Map<String, Map<String, Map<String, List<String>>>> modules = new TreeMap<>(); |
|
110 |
|
|
111 |
final MavenXpp3Reader reader = new MavenXpp3Reader(); |
|
112 |
for (final Resource res : ResourcePatternUtils.getResourcePatternResolver(resourceLoader).getResources("classpath*:/META-INF/**/pom.xml")) { |
|
113 |
try { |
|
114 |
final Model model = reader.read(res.getInputStream()); |
|
115 |
|
|
116 |
final String name = model.getArtifactId(); |
|
117 |
|
|
118 |
String groupId = model.getGroupId(); |
|
119 |
for (Parent parent = model.getParent(); (groupId == null) && (model.getParent() != null); parent = model.getParent()) { |
|
120 |
groupId = parent.getGroupId(); |
|
121 |
} |
|
122 |
|
|
123 |
String version = model.getVersion(); |
|
124 |
for (Parent parent = model.getParent(); (version == null) && (model.getParent() != null); parent = model.getParent()) { |
|
125 |
version = parent.getVersion(); |
|
126 |
} |
|
127 |
|
|
128 |
if (!modules.containsKey(groupId)) { |
|
129 |
modules.put(groupId, new TreeMap<>()); |
|
130 |
} |
|
131 |
if (!modules.get(groupId).containsKey(name)) { |
|
132 |
modules.get(groupId).put(name, new TreeMap<>()); |
|
133 |
} |
|
134 |
if (!modules.get(groupId).get(name).containsKey(version)) { |
|
135 |
modules.get(groupId).get(name).put(version, new ArrayList<>()); |
|
136 |
} |
|
137 |
|
|
138 |
modules.get(groupId).get(name).get(version).add(res.getURI().toString()); |
|
139 |
} catch (final Exception e) { |
|
140 |
log.warn("Error evaluating pom: " + res.getURI()); |
|
141 |
log.debug("-- ERROR --", e); |
|
142 |
} |
|
143 |
} |
|
144 |
|
|
145 |
return modules; |
|
146 |
} |
|
147 |
|
|
148 |
@Override |
|
149 |
public void setResourceLoader(final ResourceLoader resourceLoader) { |
|
150 |
this.resourceLoader = resourceLoader; |
|
151 |
} |
|
152 |
|
|
153 |
@Override |
|
154 |
public List<Element> geXmlProfileSections() { |
|
155 |
final Element elem = DocumentHelper.createElement("HCM_SESSION_ID"); |
|
156 |
elem.addAttribute("value", "session-" + UUID.randomUUID()); |
|
157 |
return Arrays.asList(elem); |
|
158 |
} |
|
159 |
|
|
160 |
} |
modules/dnet-springboot-apps/trunk/dnet-common-utils/src/main/java/eu/dnetlib/services/hcm/HCM.java | ||
---|---|---|
1 |
package eu.dnetlib.services.hcm; |
|
2 |
|
|
3 |
import java.io.IOException; |
|
4 |
import java.lang.management.ManagementFactory; |
|
5 |
import java.lang.management.RuntimeMXBean; |
|
6 |
import java.util.ArrayList; |
|
7 |
import java.util.Arrays; |
|
8 |
import java.util.Iterator; |
|
9 |
import java.util.List; |
|
10 |
import java.util.Map; |
|
11 |
import java.util.TreeMap; |
|
12 |
import java.util.UUID; |
|
13 |
import java.util.stream.Collectors; |
|
14 |
|
|
15 |
import org.apache.commons.logging.Log; |
|
16 |
import org.apache.commons.logging.LogFactory; |
|
17 |
import org.apache.maven.model.Model; |
|
18 |
import org.apache.maven.model.Parent; |
|
19 |
import org.apache.maven.model.io.xpp3.MavenXpp3Reader; |
|
20 |
import org.dom4j.DocumentHelper; |
|
21 |
import org.dom4j.Element; |
|
22 |
import org.springframework.beans.factory.annotation.Autowired; |
|
23 |
import org.springframework.core.env.AbstractEnvironment; |
|
24 |
import org.springframework.core.env.Environment; |
|
25 |
import org.springframework.core.env.MapPropertySource; |
|
26 |
import org.springframework.core.env.PropertySource; |
|
27 |
import org.springframework.core.io.Resource; |
|
28 |
import org.springframework.core.io.ResourceLoader; |
|
29 |
import org.springframework.core.io.support.ResourcePatternUtils; |
|
30 |
import org.springframework.web.bind.annotation.RequestMapping; |
|
31 |
import org.springframework.web.bind.annotation.RequestMethod; |
|
32 |
import org.springframework.web.bind.annotation.RestController; |
|
33 |
|
|
34 |
import com.google.common.collect.Maps; |
|
35 |
|
|
36 |
import eu.dnetlib.conf.DnetGenericApplicationProperties; |
|
37 |
import eu.dnetlib.enabling.annotations.DnetService; |
|
38 |
import eu.dnetlib.enabling.annotations.DnetServiceType; |
|
39 |
import eu.dnetlib.miscutils.datetime.DateUtils; |
|
40 |
import eu.dnetlib.miscutils.datetime.HumanTime; |
|
41 |
import eu.dnetlib.miscutils.streams.DnetStreamSupport; |
|
42 |
import eu.dnetlib.services.BaseService; |
|
43 |
|
|
44 |
@RestController |
|
45 |
@RequestMapping(value = "/hcm", method = RequestMethod.GET) |
|
46 |
@DnetService(DnetServiceType.hcm) |
|
47 |
public class HCM extends BaseService { |
|
48 |
|
|
49 |
private static final Log log = LogFactory.getLog(HCM.class); |
|
50 |
|
|
51 |
@Autowired |
|
52 |
private Environment env; |
|
53 |
|
|
54 |
@Autowired |
|
55 |
private DnetGenericApplicationProperties containerConfiguration; |
|
56 |
|
|
57 |
@Autowired |
|
58 |
private ResourceLoader resourceLoader; |
|
59 |
|
|
60 |
@RequestMapping(value = "properties", method = RequestMethod.GET) |
|
61 |
public Map<String, Object> listProperties() { |
|
62 |
|
|
63 |
final Iterator<PropertySource<?>> iter = ((AbstractEnvironment) env).getPropertySources().iterator(); |
|
64 |
return DnetStreamSupport.generateStreamFromIterator(iter) |
|
65 |
.filter(ps -> ps instanceof MapPropertySource) |
|
66 |
.map(ps -> (MapPropertySource) ps) |
|
67 |
.collect(Collectors.toMap(MapPropertySource::getName, MapPropertySource::getSource)); |
|
68 |
|
|
69 |
} |
|
70 |
|
|
71 |
@RequestMapping(value = "info", method = RequestMethod.GET) |
|
72 |
public Map<String, Object> info() { |
|
73 |
final RuntimeMXBean mxbean = ManagementFactory.getRuntimeMXBean(); |
|
74 |
final Map<String, Object> genInfo = Maps.newLinkedHashMap(); |
|
75 |
genInfo.put("Hostname", containerConfiguration.getHost()); |
|
76 |
genInfo.put("Port", containerConfiguration.getPort()); |
|
77 |
genInfo.put("Uptime", HumanTime.exactly(mxbean.getUptime())); |
|
78 |
genInfo.put("Start Time", DateUtils.calculate_ISO8601(mxbean.getStartTime())); |
|
79 |
return genInfo; |
|
80 |
} |
|
81 |
|
|
82 |
@RequestMapping(value = "jvm", method = RequestMethod.GET) |
|
83 |
public Map<String, String> jvmInfo() { |
|
84 |
final RuntimeMXBean mxbean = ManagementFactory.getRuntimeMXBean(); |
|
85 |
final Map<String, String> jvmInfo = Maps.newLinkedHashMap(); |
|
86 |
jvmInfo.put("JVM Name", mxbean.getVmName()); |
|
87 |
jvmInfo.put("JVM Vendor", mxbean.getVmVendor()); |
|
88 |
jvmInfo.put("JVM Version", mxbean.getVmVersion()); |
|
89 |
jvmInfo.put("JVM Spec Name", mxbean.getSpecName()); |
|
90 |
jvmInfo.put("JVM Spec Vendor", mxbean.getSpecVendor()); |
|
91 |
jvmInfo.put("JVM Spec Version", mxbean.getSpecVersion()); |
|
92 |
jvmInfo.put("Running JVM Name", mxbean.getName()); |
|
93 |
jvmInfo.put("Management Spec Version", mxbean.getManagementSpecVersion()); |
|
94 |
return jvmInfo; |
|
95 |
} |
|
96 |
|
|
97 |
@RequestMapping(value = "libs", method = RequestMethod.GET) |
|
98 |
public Map<String, String> libInfo() { |
|
99 |
final RuntimeMXBean mxbean = ManagementFactory.getRuntimeMXBean(); |
|
100 |
final Map<String, String> libInfo = Maps.newLinkedHashMap(); |
|
101 |
libInfo.put("Classpath", mxbean.getClassPath().replaceAll(":", " : ")); |
|
102 |
libInfo.put("Boot ClassPath", mxbean.getBootClassPath().replaceAll(":", " : ")); |
|
103 |
libInfo.put("Input arguments", mxbean.getInputArguments().toString()); |
|
104 |
libInfo.put("Library Path", mxbean.getLibraryPath().replaceAll(":", " : ")); |
|
105 |
return libInfo; |
|
106 |
} |
|
107 |
|
|
108 |
@RequestMapping(value = "maven", method = RequestMethod.GET) |
|
109 |
private Map<String, Map<String, Map<String, List<String>>>> mavenModules() throws IOException { |
|
110 |
final Map<String, Map<String, Map<String, List<String>>>> modules = new TreeMap<>(); |
|
111 |
|
|
112 |
final MavenXpp3Reader reader = new MavenXpp3Reader(); |
|
113 |
for (final Resource res : ResourcePatternUtils.getResourcePatternResolver(resourceLoader).getResources("classpath*:/META-INF/**/pom.xml")) { |
|
114 |
try { |
|
115 |
final Model model = reader.read(res.getInputStream()); |
|
116 |
|
|
117 |
final String name = model.getArtifactId(); |
|
118 |
|
|
119 |
String groupId = model.getGroupId(); |
|
120 |
for (Parent parent = model.getParent(); (groupId == null) && (model.getParent() != null); parent = model.getParent()) { |
|
121 |
groupId = parent.getGroupId(); |
|
122 |
} |
|
123 |
|
|
124 |
String version = model.getVersion(); |
|
125 |
for (Parent parent = model.getParent(); (version == null) && (model.getParent() != null); parent = model.getParent()) { |
|
126 |
version = parent.getVersion(); |
|
127 |
} |
|
128 |
|
|
129 |
if (!modules.containsKey(groupId)) { |
|
130 |
modules.put(groupId, new TreeMap<>()); |
|
131 |
} |
|
132 |
if (!modules.get(groupId).containsKey(name)) { |
|
133 |
modules.get(groupId).put(name, new TreeMap<>()); |
|
134 |
} |
|
135 |
if (!modules.get(groupId).get(name).containsKey(version)) { |
|
136 |
modules.get(groupId).get(name).put(version, new ArrayList<>()); |
|
137 |
} |
|
138 |
|
|
139 |
modules.get(groupId).get(name).get(version).add(res.getURI().toString()); |
|
140 |
} catch (final Exception e) { |
|
141 |
log.warn("Error evaluating pom: " + res.getURI()); |
|
142 |
log.debug("-- ERROR --", e); |
|
143 |
} |
|
144 |
} |
|
145 |
|
|
146 |
return modules; |
|
147 |
} |
|
148 |
|
|
149 |
@Override |
|
150 |
public List<Element> geXmlProfileSections() { |
|
151 |
final Element elem = DocumentHelper.createElement("HCM_SESSION_ID"); |
|
152 |
elem.addAttribute("value", "session-" + UUID.randomUUID()); |
|
153 |
return Arrays.asList(elem); |
|
154 |
} |
|
155 |
|
|
156 |
} |
modules/dnet-springboot-apps/trunk/dnet-common-utils/src/main/java/eu/dnetlib/services/BaseService.java | ||
---|---|---|
6 | 6 |
import java.util.HashMap; |
7 | 7 |
import java.util.List; |
8 | 8 |
import java.util.Map; |
9 |
import java.util.UUID; |
|
9 | 10 |
|
10 | 11 |
import javax.annotation.PostConstruct; |
11 | 12 |
|
... | ... | |
14 | 15 |
import org.dom4j.Element; |
15 | 16 |
import org.springframework.beans.factory.annotation.Autowired; |
16 | 17 |
import org.springframework.context.Lifecycle; |
18 |
import org.springframework.web.bind.annotation.PathVariable; |
|
19 |
import org.springframework.web.bind.annotation.RequestBody; |
|
17 | 20 |
import org.springframework.web.bind.annotation.RequestMapping; |
18 | 21 |
import org.springframework.web.bind.annotation.RequestMethod; |
22 |
import org.springframework.web.bind.annotation.RequestParam; |
|
19 | 23 |
import org.springframework.web.bind.annotation.RestController; |
24 |
import org.springframework.web.client.RestTemplate; |
|
20 | 25 |
|
21 | 26 |
import eu.dnetlib.conf.DnetGenericApplicationProperties; |
22 | 27 |
import eu.dnetlib.enabling.annotations.DnetService; |
23 | 28 |
import eu.dnetlib.enabling.annotations.DnetServiceType; |
24 | 29 |
import eu.dnetlib.exceptions.DnetGenericException; |
30 |
import eu.dnetlib.services.async.AsyncClientUtils; |
|
31 |
import eu.dnetlib.services.async.AsyncMethodNotFoundException; |
|
32 |
import eu.dnetlib.services.async.AsyncResponse; |
|
33 |
import eu.dnetlib.services.async.HasAsyncMethods; |
|
34 |
import eu.dnetlib.services.async.ResponseAck; |
|
35 |
import eu.dnetlib.services.async.ResponseAck.ResponseAckStatus; |
|
25 | 36 |
|
26 | 37 |
public abstract class BaseService implements Lifecycle { |
27 | 38 |
|
... | ... | |
42 | 53 |
@Autowired |
43 | 54 |
private DnetGenericApplicationProperties containerConfiguration; |
44 | 55 |
|
56 |
@Autowired |
|
57 |
private AsyncClientUtils asyncClientUtils; |
|
58 |
|
|
45 | 59 |
@PostConstruct |
46 | 60 |
public void init() throws DnetGenericException { |
47 | 61 |
|
... | ... | |
91 | 105 |
return ServiceRunningInstance.newInstance(profileId, baseUrl, baseDir()); |
92 | 106 |
} |
93 | 107 |
|
108 |
@RequestMapping(value = "async/response/{id}", method = RequestMethod.POST) |
|
109 |
public final ResponseAck asyncResponse(@PathVariable final String id, @RequestBody final AsyncResponse response) { |
|
110 |
return asyncClientUtils.processResponse(id, response) ? ResponseAck.OK : ResponseAck.IGNORED; |
|
111 |
} |
|
112 |
|
|
113 |
@RequestMapping(value = "async/method/{method}", method = RequestMethod.POST) |
|
114 |
public final String asyncMethod(@PathVariable final String method, @RequestParam final String clientBaseUrl, @RequestParam final String jsonParams) |
|
115 |
throws DnetGenericException { |
|
116 |
|
|
117 |
if (this instanceof HasAsyncMethods) { |
|
118 |
|
|
119 |
try { |
|
120 |
final String id = "invocation-" + UUID.randomUUID(); |
|
121 |
final String url = clientBaseUrl + "/async/response/" + id; |
|
122 |
final RestTemplate restTemplate = new RestTemplate(); |
|
123 |
|
|
124 |
((HasAsyncMethods) this).processMethod(method, jsonParams, res -> restTemplate.postForObject(url, res, ResponseAckStatus.class)); |
|
125 |
|
|
126 |
return id; |
|
127 |
} catch (final AsyncMethodNotFoundException e) { |
|
128 |
log.error("Async method " + method + " not found"); |
|
129 |
throw e; |
|
130 |
} |
|
131 |
|
|
132 |
} else { |
|
133 |
log.error("Service " + getServiceType() + " does not implement HasAsyncMethods interface"); |
|
134 |
throw new DnetGenericException("Service " + getServiceType() + " does not implement HasAsyncMethods interface"); |
|
135 |
} |
|
136 |
|
|
137 |
} |
|
138 |
|
|
94 | 139 |
protected String baseDir() { |
95 | 140 |
return containerConfiguration.getBaseDir(); |
96 | 141 |
} |
modules/dnet-springboot-apps/trunk/dnet-common-utils/src/main/java/eu/dnetlib/services/async/AsyncServerCallback.java | ||
---|---|---|
1 |
package eu.dnetlib.services.async; |
|
2 |
|
|
3 |
public interface AsyncServerCallback { |
|
4 |
|
|
5 |
void notify(AsyncResponse res); |
|
6 |
|
|
7 |
} |
modules/dnet-springboot-apps/trunk/dnet-common-utils/src/main/java/eu/dnetlib/services/async/AsyncMethodStatus.java | ||
---|---|---|
1 |
package eu.dnetlib.services.async; |
|
2 |
|
|
3 |
public enum AsyncMethodStatus { |
|
4 |
ASSIGNED, SUCCESS, FAILED, RUNNING |
|
5 |
} |
modules/dnet-springboot-apps/trunk/dnet-common-utils/src/main/java/eu/dnetlib/services/async/AsyncClientCallback.java | ||
---|---|---|
1 |
package eu.dnetlib.services.async; |
|
2 |
|
|
3 |
import java.util.Date; |
|
4 |
|
|
5 |
public abstract class AsyncClientCallback { |
|
6 |
|
|
7 |
public Date lastInvocation = new Date(); |
|
8 |
|
|
9 |
abstract public void onDone(AsyncResponse res); |
|
10 |
|
|
11 |
abstract public void onFailed(AsyncResponse res); |
|
12 |
|
|
13 |
abstract public void onGoing(AsyncResponse res); |
|
14 |
|
|
15 |
public Date getLastInvocation() { |
|
16 |
return lastInvocation; |
|
17 |
} |
|
18 |
|
|
19 |
public void setLastInvocation(final Date lastInvocation) { |
|
20 |
this.lastInvocation = lastInvocation; |
|
21 |
} |
|
22 |
|
|
23 |
public void updateLastInvocation() { |
|
24 |
lastInvocation = new Date(); |
|
25 |
} |
|
26 |
|
|
27 |
} |
modules/dnet-springboot-apps/trunk/dnet-common-utils/src/main/java/eu/dnetlib/services/async/AsyncMethodNotFoundException.java | ||
---|---|---|
1 |
package eu.dnetlib.services.async; |
|
2 |
|
|
3 |
import eu.dnetlib.exceptions.DnetGenericException; |
|
4 |
|
|
5 |
public class AsyncMethodNotFoundException extends DnetGenericException { |
|
6 |
|
|
7 |
/** |
|
8 |
* |
|
9 |
*/ |
|
10 |
private static final long serialVersionUID = 4505152782783864469L; |
|
11 |
|
|
12 |
public AsyncMethodNotFoundException() { |
|
13 |
super(); |
|
14 |
} |
|
15 |
|
|
16 |
public AsyncMethodNotFoundException(final String message, final Throwable cause) { |
|
17 |
super(message, cause); |
|
18 |
} |
|
19 |
|
|
20 |
public AsyncMethodNotFoundException(final String message) { |
|
21 |
super(message); |
|
22 |
} |
|
23 |
|
|
24 |
public AsyncMethodNotFoundException(final Throwable cause) { |
|
25 |
super(cause); |
|
26 |
} |
|
27 |
|
|
28 |
} |
modules/dnet-springboot-apps/trunk/dnet-common-utils/src/main/java/eu/dnetlib/services/async/AsyncResponse.java | ||
---|---|---|
1 |
package eu.dnetlib.services.async; |
|
2 |
|
|
3 |
import com.fasterxml.jackson.databind.ObjectMapper; |
|
4 |
|
|
5 |
public class AsyncResponse { |
|
6 |
|
|
7 |
private String responseJson; |
|
8 |
private AsyncMethodStatus status; |
|
9 |
|
|
10 |
public AsyncResponse() {} |
|
11 |
|
|
12 |
public AsyncResponse(final String responseJson, final AsyncMethodStatus status) { |
|
13 |
this.responseJson = responseJson; |
|
14 |
this.status = status; |
|
15 |
} |
|
16 |
|
|
17 |
public String getResponseJson() { |
|
18 |
return responseJson; |
|
19 |
} |
|
20 |
|
|
21 |
public void setResponseJson(final String responseJson) { |
|
22 |
this.responseJson = responseJson; |
|
23 |
} |
|
24 |
|
|
25 |
public AsyncMethodStatus getStatus() { |
|
26 |
return status; |
|
27 |
} |
|
28 |
|
|
29 |
public void setStatus(final AsyncMethodStatus status) { |
|
30 |
this.status = status; |
|
31 |
} |
|
32 |
|
|
33 |
public <K> K parseResponse(final Class<K> clazz) throws Exception { |
|
34 |
return (new ObjectMapper()).readValue(responseJson, clazz); |
|
35 |
} |
|
36 |
|
|
37 |
} |
modules/dnet-springboot-apps/trunk/dnet-common-utils/src/main/java/eu/dnetlib/services/async/AsyncClientUtils.java | ||
---|---|---|
1 |
package eu.dnetlib.services.async; |
|
2 |
|
|
3 |
import java.util.HashMap; |
|
4 |
import java.util.Map; |
|
5 |
|
|
6 |
import org.apache.commons.logging.Log; |
|
7 |
import org.apache.commons.logging.LogFactory; |
|
8 |
import org.springframework.stereotype.Component; |
|
9 |
import org.springframework.web.client.RestTemplate; |
|
10 |
|
|
11 |
@Component |
|
12 |
public class AsyncClientUtils { |
|
13 |
|
|
14 |
private static final Log log = LogFactory.getLog(AsyncClientUtils.class); |
|
15 |
|
|
16 |
private static final Map<String, AsyncClientCallback> activeRemoteCalls = new HashMap<>(); |
|
17 |
|
|
18 |
public void invokeRemoteMethod(final String remoteBaseUrl, final String remoteMethod, final Object o, final AsyncClientCallback callback) { |
|
19 |
final String url = remoteBaseUrl + "/async/method/" + remoteMethod; |
|
20 |
final String id = (new RestTemplate()).postForObject(url, o, String.class); |
|
21 |
activeRemoteCalls.put(id, callback); |
|
22 |
} |
|
23 |
|
|
24 |
public boolean processResponse(final String id, final AsyncResponse response) { |
|
25 |
if (activeRemoteCalls.containsKey(id)) { |
|
26 |
final AsyncClientCallback callback = activeRemoteCalls.get(id); |
|
27 |
|
|
28 |
callback.updateLastInvocation(); |
|
29 |
|
|
30 |
switch (response.getStatus()) { |
|
31 |
case ASSIGNED: |
|
32 |
case RUNNING: |
|
33 |
callback.onGoing(response); |
|
34 |
break; |
|
35 |
case SUCCESS: |
|
36 |
callback.onDone(response); |
|
37 |
activeRemoteCalls.remove(id); |
|
38 |
break; |
|
39 |
case FAILED: |
|
40 |
callback.onFailed(response); |
|
41 |
activeRemoteCalls.remove(id); |
|
42 |
break; |
|
43 |
default: |
|
44 |
log.warn("Status " + response.getStatus() + " not managed"); |
|
45 |
break; |
|
46 |
} |
|
47 |
return true; |
|
48 |
} else { |
|
49 |
log.warn("Unexpected message: " + id); |
|
50 |
return false; |
|
51 |
} |
|
52 |
} |
|
53 |
|
|
54 |
public void clearRemoteCalls() { |
|
55 |
activeRemoteCalls.clear(); |
|
56 |
} |
|
57 |
|
|
58 |
public void deleteRemoteCall(final String id) { |
|
59 |
activeRemoteCalls.remove(id); |
|
60 |
} |
|
61 |
|
|
62 |
public static Map<String, AsyncClientCallback> getActiveremotecalls() { |
|
63 |
return activeRemoteCalls; |
|
64 |
} |
|
65 |
|
|
66 |
} |
modules/dnet-springboot-apps/trunk/dnet-common-utils/src/main/java/eu/dnetlib/services/async/HasAsyncMethods.java | ||
---|---|---|
1 |
package eu.dnetlib.services.async; |
|
2 |
|
|
3 |
public interface HasAsyncMethods { |
|
4 |
|
|
5 |
void processMethod(String method, String jsonParams, AsyncServerCallback callback) throws AsyncMethodNotFoundException; |
|
6 |
|
|
7 |
} |
modules/dnet-springboot-apps/trunk/dnet-common-utils/src/main/java/eu/dnetlib/services/async/ResponseAck.java | ||
---|---|---|
1 |
package eu.dnetlib.services.async; |
|
2 |
|
|
3 |
public class ResponseAck { |
|
4 |
|
|
5 |
public enum ResponseAckStatus { |
|
6 |
OK, IGNORED |
|
7 |
} |
|
8 |
|
|
9 |
public static final ResponseAck OK = new ResponseAck(ResponseAckStatus.OK); |
|
10 |
public static final ResponseAck IGNORED = new ResponseAck(ResponseAckStatus.IGNORED); |
|
11 |
|
|
12 |
private ResponseAckStatus status; |
|
13 |
|
|
14 |
public ResponseAck() {} |
|
15 |
|
|
16 |
private ResponseAck(final ResponseAckStatus status) { |
|
17 |
this.status = status; |
|
18 |
} |
|
19 |
|
|
20 |
public ResponseAckStatus getStatus() { |
|
21 |
return status; |
|
22 |
} |
|
23 |
|
|
24 |
public void setStatus(final ResponseAckStatus status) { |
|
25 |
this.status = status; |
|
26 |
} |
|
27 |
|
|
28 |
} |
Also available in: Unified diff
async communication