Skip to content

Commit 6ab7be7

Browse files
authored
Merge pull request aol#233 from aol/app-reg-manifest
Application Registry enhancements
2 parents a8d3365 + 39360bc commit 6ab7be7

File tree

7 files changed

+323
-186
lines changed

7 files changed

+323
-186
lines changed

micro-application-register/src/main/java/com/aol/micro/server/application/registry/ApplicationRegisterImpl.java

Lines changed: 45 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@
66
import java.util.stream.Collectors;
77
import java.util.stream.Stream;
88

9-
import lombok.Getter;
10-
119
import org.slf4j.Logger;
1210
import org.slf4j.LoggerFactory;
1311
import org.springframework.beans.factory.annotation.Autowired;
@@ -18,41 +16,51 @@
1816
import com.aol.micro.server.servers.ApplicationRegister;
1917
import com.aol.micro.server.servers.model.ServerData;
2018

19+
import lombok.Getter;
20+
2121
@Component
2222
public class ApplicationRegisterImpl implements ApplicationRegister {
23-
private final Logger logger = LoggerFactory.getLogger(getClass());
24-
@Getter
25-
private volatile Application application;
26-
27-
28-
private final String customHostname;
29-
private final String targetEndpoint;
30-
31-
@Autowired
32-
public ApplicationRegisterImpl(@Value("${host.address:#{null}}")String customHostname,
33-
@Value("${target.endpoint:#{null}}")String targetEndpoint){
34-
35-
this.customHostname=customHostname;
36-
this.targetEndpoint = targetEndpoint;
37-
38-
}
39-
40-
public ApplicationRegisterImpl() {
41-
this(null,null);
42-
}
43-
44-
@Override
45-
public void register(ServerData[] data) {
46-
47-
try {
48-
final String hostname = Optional.ofNullable(customHostname).orElse( InetAddress.getLocalHost().getHostName());
49-
application = new Application(Stream
50-
.of(data)
51-
.map(next -> new RegisterEntry(next.getPort(), hostname, next.getModule().getContext(), next
52-
.getModule().getContext(), null,targetEndpoint)).collect(Collectors.toList()));
53-
logger.info("Registered application {} ", application);
54-
} catch (UnknownHostException e) {
55-
throw ExceptionSoftener.throwSoftenedException(e);
56-
}
57-
}
23+
private final Logger logger = LoggerFactory.getLogger(getClass());
24+
@Getter
25+
private volatile Application application;
26+
27+
private final String customHostname;
28+
private final String targetEndpoint;
29+
30+
@Autowired
31+
public ApplicationRegisterImpl(@Value("${host.address:#{null}}") String customHostname,
32+
@Value("${target.endpoint:#{null}}") String targetEndpoint) {
33+
34+
this.customHostname = customHostname;
35+
this.targetEndpoint = targetEndpoint;
36+
37+
}
38+
39+
public ApplicationRegisterImpl() {
40+
this(
41+
null, null);
42+
}
43+
44+
@Override
45+
public void register(ServerData[] data) {
46+
47+
try {
48+
final String hostname = Optional.ofNullable(customHostname)
49+
.orElse(InetAddress.getLocalHost()
50+
.getHostName());
51+
application = new Application(
52+
Stream.of(data)
53+
.map(next -> new RegisterEntry(
54+
next.getPort(), hostname,
55+
next.getModule()
56+
.getContext(),
57+
next.getModule()
58+
.getContext(),
59+
null, targetEndpoint))
60+
.collect(Collectors.toList()));
61+
logger.info("Registered application {} ", application);
62+
} catch (UnknownHostException e) {
63+
throw ExceptionSoftener.throwSoftenedException(e);
64+
}
65+
}
5866
}

micro-application-register/src/main/java/com/aol/micro/server/application/registry/Job.java

Lines changed: 46 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import java.util.Date;
44
import java.util.UUID;
55

6+
import javax.annotation.PostConstruct;
7+
68
import org.slf4j.Logger;
79
import org.slf4j.LoggerFactory;
810
import org.springframework.beans.factory.annotation.Autowired;
@@ -16,39 +18,48 @@
1618
@Component
1719
public class Job {
1820

19-
private final Logger logger = LoggerFactory.getLogger(getClass());
20-
private final AsyncRestClient rest = new AsyncRestClient(100,2000);
21-
private final String apiUrl;
22-
private final ApplicationRegisterImpl app;
23-
private final String uuid = UUID.randomUUID().toString();
24-
private final String resourcePath;
25-
26-
@Autowired
27-
public Job(@Value("${service.registry.url:null}") String apiUrl, ApplicationRegisterImpl app,
28-
@Value("${resource.path:/service-registry/register}") String resourcePath) {
29-
30-
this.apiUrl = apiUrl;
31-
this.app = app;
32-
this.resourcePath = resourcePath;
33-
34-
}
35-
36-
@Scheduled(fixedDelayString = "${service.registry.delay:300000}")
37-
public synchronized void schedule() {
38-
if(app.getApplication()!=null && apiUrl!=null)
39-
app.getApplication().forEach(moduleEntry -> sendPing(moduleEntry));
40-
}
41-
42-
private void sendPing(RegisterEntry moduleEntry) {
43-
final RegisterEntry entry = moduleEntry.withTime(new Date())
44-
.withUuid(uuid);
45-
try {
46-
47-
logger.info("Posting {} to " + apiUrl + resourcePath, JacksonUtil.serializeToJson(entry));
48-
rest.post(apiUrl + resourcePath, JacksonUtil.serializeToJson(entry)).join();
49-
} catch (Exception e) {
50-
logger.warn("Failed posting {} to {}"+resourcePath, JacksonUtil.serializeToJson(entry), apiUrl);
51-
52-
}
53-
}
21+
private final Logger logger = LoggerFactory.getLogger(getClass());
22+
private final AsyncRestClient rest = new AsyncRestClient(
23+
100, 2000);
24+
private final String apiUrl;
25+
private final ApplicationRegisterImpl app;
26+
private final String uuid = UUID.randomUUID()
27+
.toString();
28+
private final String resourcePath;
29+
30+
@Autowired
31+
public Job(@Value("${service.registry.url:null}") String apiUrl, ApplicationRegisterImpl app,
32+
@Value("${resource.path:/service-registry/register}") String resourcePath) {
33+
34+
this.apiUrl = apiUrl;
35+
this.app = app;
36+
this.resourcePath = resourcePath;
37+
38+
}
39+
40+
@PostConstruct
41+
@Scheduled(fixedDelayString = "${service.registry.delay:300000}")
42+
public synchronized void schedule() {
43+
try {
44+
if (app.getApplication() != null && apiUrl != null)
45+
app.getApplication()
46+
.forEach(moduleEntry -> sendPing(moduleEntry));
47+
} catch (Exception e) {
48+
logger.error("Failed to register services due to exception {}", e.getMessage(), e);
49+
}
50+
}
51+
52+
private void sendPing(RegisterEntry moduleEntry) {
53+
final RegisterEntry entry = moduleEntry.withTime(new Date())
54+
.withUuid(uuid);
55+
try {
56+
57+
logger.info("Posting {} to " + apiUrl + resourcePath, JacksonUtil.serializeToJson(entry));
58+
rest.post(apiUrl + resourcePath, JacksonUtil.serializeToJson(entry))
59+
.join();
60+
} catch (Exception e) {
61+
logger.warn("Failed posting {} to {}" + resourcePath, JacksonUtil.serializeToJson(entry), apiUrl);
62+
63+
}
64+
}
5465
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package com.aol.micro.server.application.registry;
2+
3+
import java.io.InputStream;
4+
import java.util.HashMap;
5+
import java.util.Map;
6+
import java.util.function.Supplier;
7+
import java.util.jar.Attributes;
8+
import java.util.jar.Manifest;
9+
10+
import org.slf4j.Logger;
11+
import org.slf4j.LoggerFactory;
12+
13+
import com.aol.cyclops.control.FluentFunctions;
14+
import com.aol.cyclops.control.ReactiveSeq;
15+
16+
import lombok.AccessLevel;
17+
import lombok.NoArgsConstructor;
18+
19+
@NoArgsConstructor(access = AccessLevel.PRIVATE)
20+
public class ManifestLoader {
21+
private final Logger logger = LoggerFactory.getLogger(getClass());
22+
23+
public final static ManifestLoader instance = new ManifestLoader();
24+
Supplier<Map<String, String>> fn = FluentFunctions.of(this::manifest)
25+
.memoize();
26+
27+
public Map<String, String> getManifest() {
28+
return fn.get();
29+
}
30+
31+
private Map<String, String> manifest() {
32+
33+
try {
34+
return ReactiveSeq.of("META-INF/MANIFEST.MF")
35+
.map(url -> this.getClass()
36+
.getClassLoader()
37+
.getResourceAsStream(url))
38+
.map(this::getManifest)
39+
.singleOptional()
40+
.orElse(null);
41+
} catch (Exception e) {
42+
logger.warn("Warning : can't load manifest due to exception {}", e.getMessage());
43+
}
44+
return null;
45+
46+
}
47+
48+
public Map<String, String> getManifest(final InputStream input) {
49+
50+
final Map<String, String> retMap = new HashMap<String, String>();
51+
try {
52+
Manifest manifest = new Manifest();
53+
manifest.read(input);
54+
final Attributes attributes = manifest.getMainAttributes();
55+
for (final Map.Entry attribute : attributes.entrySet()) {
56+
retMap.put(attribute.getKey()
57+
.toString(),
58+
attribute.getValue()
59+
.toString());
60+
}
61+
} catch (final Exception ex) {
62+
logger.error("Failed to load manifest ", ex);
63+
}
64+
65+
return retMap;
66+
}
67+
}
Lines changed: 57 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package com.aol.micro.server.application.registry;
22

3+
import java.text.SimpleDateFormat;
34
import java.util.Date;
5+
import java.util.Map;
46
import java.util.UUID;
57

68
import javax.xml.bind.annotation.XmlAccessType;
@@ -23,31 +25,59 @@
2325
@Builder
2426
public class RegisterEntry {
2527

26-
int port;
27-
String hostname;
28-
String module;
29-
String context;
30-
Date time;
31-
String uuid;
32-
String target;
33-
34-
public RegisterEntry() {
35-
this(-1, null, null, null, null, null, null);
36-
}
37-
38-
public RegisterEntry(int port, String hostname, String module, String context, Date time,
39-
String uuid, String target) {
40-
this.port = port;
41-
this.hostname = hostname;
42-
this.module = module;
43-
this.context = context;
44-
this.time = time;
45-
this.uuid = uuid;
46-
this.target = target;
47-
}
48-
49-
public RegisterEntry(int port, String hostname, String module, String context,
50-
Date time,String target) {
51-
this(port, hostname, module, context, time, UUID.randomUUID().toString(),target);
52-
}
28+
private static SimpleDateFormat f = new SimpleDateFormat(
29+
"EEE, d MMM yyyy HH:mm:ss Z");
30+
@Wither
31+
int port;
32+
@Wither
33+
String hostname;
34+
@Wither
35+
String module;
36+
@Wither
37+
String context;
38+
Date time;
39+
@Wither
40+
String uuid;
41+
@Wither
42+
String target;
43+
String formattedDate;
44+
Map<String, String> manifest = ManifestLoader.instance.getManifest();
45+
46+
public RegisterEntry() {
47+
this(
48+
-1, null, null, null, null, null, null);
49+
}
50+
51+
public RegisterEntry(int port, String hostname, String module, String context, Date time, String uuid,
52+
String target) {
53+
this(
54+
port, hostname, module, context, time, UUID.randomUUID()
55+
.toString(),
56+
target, null);
57+
}
58+
59+
private RegisterEntry(int port, String hostname, String module, String context, Date time, String uuid,
60+
String target, String ignoreDate) {
61+
this.port = port;
62+
this.hostname = hostname;
63+
this.module = module;
64+
this.context = context;
65+
this.time = time;
66+
this.uuid = uuid;
67+
this.target = target;
68+
69+
if (time != null)
70+
this.formattedDate = f.format(this.time);
71+
else
72+
this.formattedDate = null;
73+
74+
}
75+
76+
public RegisterEntry(int port, String hostname, String module, String context, Date time, String target) {
77+
this(
78+
port, hostname, module, context, time, UUID.randomUUID()
79+
.toString(),
80+
target);
81+
}
82+
5383
}

0 commit comments

Comments
 (0)