Skip to content

Commit 6bb4d61

Browse files
authored
Merge pull request #766 from scalecube/cleanup-service-discovery-interface
Cleanup service discovery interface
2 parents 3c9f0ed + d575b4c commit 6bb4d61

File tree

37 files changed

+607
-318
lines changed

37 files changed

+607
-318
lines changed

services-api/src/main/java/io/scalecube/services/annotations/BeforeDestroy.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@
88
import java.lang.annotation.Target;
99

1010
/**
11-
* This annotation is used to mark the method which will be executed before shutdown of service
12-
* <br>
11+
* This annotation is used to mark the method which will be executed before shutdown of service <br>
1312
* Scalecube services doesn't support {@link javax.annotation.PreDestroy} since Java API *
1413
* Specification for it has strict limitation for annotated method.
1514
*/

services-api/src/main/java/io/scalecube/services/discovery/api/ServiceDiscovery.java

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,23 @@
11
package io.scalecube.services.discovery.api;
22

3-
import io.scalecube.net.Address;
4-
import io.scalecube.services.ServiceEndpoint;
53
import reactor.core.publisher.Flux;
64
import reactor.core.publisher.Mono;
75

86
public interface ServiceDiscovery {
97

10-
/**
11-
* Returns service discovery address.
12-
*
13-
* @return discovery address
14-
*/
15-
Address address();
16-
17-
/**
18-
* Returns service endpoint.
19-
*
20-
* @return service endpoint
21-
*/
22-
ServiceEndpoint serviceEndpoint();
23-
248
/**
259
* Function to subscribe and listen on {@code ServiceDiscoveryEvent} events.
2610
*
2711
* @return stream of {@code ServiceDiscoveryEvent} events
2812
*/
29-
Flux<ServiceDiscoveryEvent> listenDiscovery();
13+
Flux<ServiceDiscoveryEvent> listen();
3014

3115
/**
3216
* Starting this {@code ServiceDiscovery} instance.
3317
*
3418
* @return started {@code ServiceDiscovery} instance
3519
*/
36-
Mono<ServiceDiscovery> start();
20+
Mono<Void> start();
3721

3822
/**
3923
* Shutting down this {@code ServiceDiscovery} instance.
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package io.scalecube.services.discovery.api;
2+
3+
import io.scalecube.net.Address;
4+
import java.util.Objects;
5+
import java.util.StringJoiner;
6+
import reactor.core.publisher.Flux;
7+
8+
public final class ServiceDiscoveryContext {
9+
10+
private final String id;
11+
private final Address address;
12+
private final ServiceDiscovery discovery;
13+
14+
private ServiceDiscoveryContext(Builder builder) {
15+
this.id = Objects.requireNonNull(builder.id, "discoveryContext.id");
16+
this.address = Objects.requireNonNull(builder.address, "discoveryContext.address");
17+
this.discovery = Objects.requireNonNull(builder.discovery, "discoveryContext.discovery");
18+
}
19+
20+
public static Builder builder() {
21+
return new Builder();
22+
}
23+
24+
public String id() {
25+
return id;
26+
}
27+
28+
public Address address() {
29+
return address;
30+
}
31+
32+
public Flux<ServiceDiscoveryEvent> listen() {
33+
return discovery.listen();
34+
}
35+
36+
@Override
37+
public String toString() {
38+
return new StringJoiner(", ", ServiceDiscoveryContext.class.getSimpleName() + "[", "]")
39+
.add("id='" + id + "'")
40+
.add("address=" + address)
41+
.add("discovery=" + discovery)
42+
.toString();
43+
}
44+
45+
public static class Builder {
46+
47+
private String id;
48+
private Address address;
49+
private ServiceDiscovery discovery;
50+
51+
private Builder() {}
52+
53+
public Builder id(String id) {
54+
this.id = id;
55+
return this;
56+
}
57+
58+
public Builder address(Address address) {
59+
this.address = address;
60+
return this;
61+
}
62+
63+
public Builder discovery(ServiceDiscovery discovery) {
64+
this.discovery = discovery;
65+
return this;
66+
}
67+
68+
public ServiceDiscoveryContext build() {
69+
return new ServiceDiscoveryContext(this);
70+
}
71+
}
72+
}

services-api/src/main/java/io/scalecube/services/discovery/api/ServiceDiscoveryEvent.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import java.util.Objects;
55
import java.util.StringJoiner;
66

7-
public class ServiceDiscoveryEvent {
7+
public final class ServiceDiscoveryEvent {
88

99
public enum Type {
1010
ENDPOINT_ADDED, // service endpoint added
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package io.scalecube.services.discovery.api;
2+
3+
import io.scalecube.services.ServiceEndpoint;
4+
5+
@FunctionalInterface
6+
public interface ServiceDiscoveryFactory {
7+
8+
ServiceDiscovery createServiceDiscovery(ServiceEndpoint serviceEndpoint);
9+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package io.scalecube.services.discovery.api;
2+
3+
import io.scalecube.services.ServiceEndpoint;
4+
import java.util.StringJoiner;
5+
import java.util.UUID;
6+
import java.util.function.Consumer;
7+
8+
public final class ServiceDiscoveryOptions {
9+
10+
private String id = UUID.randomUUID().toString();
11+
private ServiceEndpoint serviceEndpoint;
12+
private ServiceDiscoveryFactory discoveryFactory;
13+
14+
public ServiceDiscoveryOptions() {}
15+
16+
/**
17+
* ServiceDiscoveryOptions copy constructor.
18+
*
19+
* @param other ServiceDiscoveryOptions to copy
20+
*/
21+
public ServiceDiscoveryOptions(ServiceDiscoveryOptions other) {
22+
this.id = other.id;
23+
this.serviceEndpoint = other.serviceEndpoint;
24+
this.discoveryFactory = other.discoveryFactory;
25+
}
26+
27+
private ServiceDiscoveryOptions set(Consumer<ServiceDiscoveryOptions> c) {
28+
ServiceDiscoveryOptions s = new ServiceDiscoveryOptions(this);
29+
c.accept(s);
30+
return s;
31+
}
32+
33+
public ServiceDiscoveryOptions id(String id) {
34+
return set(o -> o.id = id);
35+
}
36+
37+
public String id() {
38+
return id;
39+
}
40+
41+
public ServiceDiscoveryOptions serviceEndpoint(ServiceEndpoint serviceEndpoint) {
42+
return set(o -> o.serviceEndpoint = serviceEndpoint);
43+
}
44+
45+
public ServiceEndpoint serviceEndpoint() {
46+
return serviceEndpoint;
47+
}
48+
49+
public ServiceDiscoveryOptions discoveryFactory(ServiceDiscoveryFactory discoveryFactory) {
50+
return set(o -> o.discoveryFactory = discoveryFactory);
51+
}
52+
53+
public ServiceDiscoveryFactory discoveryFactory() {
54+
return discoveryFactory;
55+
}
56+
57+
@Override
58+
public String toString() {
59+
return new StringJoiner(", ", ServiceDiscoveryOptions.class.getSimpleName() + "[", "]")
60+
.add("id='" + id + "'")
61+
.add("serviceEndpoint=" + serviceEndpoint)
62+
.add("discoveryFactory=" + discoveryFactory)
63+
.toString();
64+
}
65+
}

services/src/main/java/io/scalecube/services/gateway/GatewayOptions.java renamed to services-api/src/main/java/io/scalecube/services/gateway/GatewayOptions.java

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
package io.scalecube.services.gateway;
22

33
import io.scalecube.services.ServiceCall;
4+
import java.util.StringJoiner;
45
import java.util.concurrent.Executor;
56
import java.util.function.Consumer;
67

78
public class GatewayOptions {
89

9-
private Executor workerPool;
10-
private ServiceCall call;
1110
private String id;
1211
private int port = 0;
12+
private Executor workerPool;
13+
private ServiceCall call;
1314

1415
public GatewayOptions() {}
1516

@@ -62,4 +63,14 @@ public GatewayOptions call(ServiceCall call) {
6263
public ServiceCall call() {
6364
return call;
6465
}
66+
67+
@Override
68+
public String toString() {
69+
return new StringJoiner(", ", GatewayOptions.class.getSimpleName() + "[", "]")
70+
.add("id='" + id + "'")
71+
.add("port=" + port)
72+
.add("workerPool=" + workerPool)
73+
.add("call=" + call)
74+
.toString();
75+
}
6576
}

services-api/src/main/java/io/scalecube/services/transport/api/JdkCodec.java

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,8 @@
1313
import java.util.Map;
1414
import java.util.Map.Entry;
1515

16-
/** Simple headers and data codec based on JDK only. */
1716
public class JdkCodec implements DataCodec, HeadersCodec {
1817

19-
/**
20-
* {@inheritDoc}
21-
*/
2218
@Override
2319
public String contentType() {
2420
return "application/octet-stream";
@@ -37,9 +33,6 @@ public void encode(OutputStream stream, Object value) throws IOException {
3733
}
3834
}
3935

40-
/**
41-
* {@inheritDoc}
42-
*/
4336
@Override
4437
public void encode(OutputStream stream, Map<String, String> headers) throws IOException {
4538
if (headers.isEmpty()) {
@@ -70,9 +63,6 @@ public Object decode(InputStream stream, Type type) throws IOException {
7063
}
7164
}
7265

73-
/**
74-
* {@inheritDoc}
75-
*/
7666
@Override
7767
public Map<String, String> decode(InputStream stream) throws IOException {
7868
if (stream.available() < 1) {

services-api/src/test/java/io/scalecube/services/transport/api/JdkCodecTest.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,5 +66,4 @@ public int hashCode() {
6666
return Objects.hash(name);
6767
}
6868
}
69-
7069
}

0 commit comments

Comments
 (0)