-
-
Notifications
You must be signed in to change notification settings - Fork 107
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #726 from scalecube/feature/issue-724-codec-zepo-3…
…dparty ISSUE-724 Add Jdk Data Codec
- Loading branch information
Showing
7 changed files
with
166 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
...es-api/src/main/resources/META-INF/services/io.scalecube.services.transport.api.DataCodec
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
io.scalecube.services.transport.api.JdkCodec |
70 changes: 70 additions & 0 deletions
70
services-api/src/test/java/io/scalecube/services/transport/api/JdkCodecTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package io.scalecube.services.transport.api; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
import java.io.ByteArrayInputStream; | ||
import java.io.ByteArrayOutputStream; | ||
import java.io.IOException; | ||
import java.io.Serializable; | ||
import java.util.Arrays; | ||
import java.util.Objects; | ||
import java.util.stream.Stream; | ||
import org.junit.jupiter.params.ParameterizedTest; | ||
import org.junit.jupiter.params.provider.MethodSource; | ||
|
||
class JdkCodecTest { | ||
|
||
private final DataCodec codec = new JdkCodec(); | ||
|
||
@ParameterizedTest | ||
@MethodSource("provider") | ||
void test(Object body) throws IOException { | ||
Object decoded = writeAndRead(body); | ||
assertEquals(body, decoded); | ||
} | ||
|
||
static Stream<Object> provider() { | ||
return Stream.of("hello", Arrays.<Object>asList(1, 2, 3), new Greeting("joe")); | ||
} | ||
|
||
private Object writeAndRead(Object body) throws IOException { | ||
try (ByteArrayOutputStream os = new ByteArrayOutputStream()) { | ||
codec.encode(os, body); | ||
byte[] bytes = os.toByteArray(); | ||
try (ByteArrayInputStream is = new ByteArrayInputStream(bytes)) { | ||
return codec.decode(is, body.getClass()); | ||
} | ||
} | ||
} | ||
|
||
static class Greeting implements Serializable { | ||
|
||
private final String name; | ||
|
||
Greeting(String name) { | ||
this.name = name; | ||
} | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (this == o) { | ||
return true; | ||
} | ||
if (o == null || getClass() != o.getClass()) { | ||
return false; | ||
} | ||
Greeting greeting = (Greeting) o; | ||
return Objects.equals(name, greeting.name); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(name); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
...arent/services-examples/src/main/java/io/scalecube/services/examples/codecs/Example2.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package io.scalecube.services.examples.codecs; | ||
|
||
import io.scalecube.net.Address; | ||
import io.scalecube.services.Microservices; | ||
import io.scalecube.services.discovery.ScalecubeServiceDiscovery; | ||
import io.scalecube.services.examples.helloworld.service.GreetingServiceImpl; | ||
import io.scalecube.services.examples.helloworld.service.api.GreetingsService; | ||
import io.scalecube.services.transport.rsocket.RSocketServiceTransport; | ||
|
||
public class Example2 { | ||
|
||
public static final String CONTENT_TYPE = "application/octet-stream"; | ||
|
||
/** | ||
* Start the example. | ||
* | ||
* @param args ignored | ||
*/ | ||
public static void main(String[] args) { | ||
// ScaleCube Node node with no members | ||
Microservices seed = | ||
Microservices.builder() | ||
.discovery(ScalecubeServiceDiscovery::new) | ||
.transport(RSocketServiceTransport::new) | ||
.defaultContentType(CONTENT_TYPE) // need to send with non-default data format | ||
.startAwait(); | ||
|
||
final Address seedAddress = seed.discovery().address(); | ||
|
||
// Construct a ScaleCube node which joins the cluster hosting the Greeting Service | ||
Microservices ms = | ||
Microservices.builder() | ||
.discovery( | ||
endpoint -> | ||
new ScalecubeServiceDiscovery(endpoint) | ||
.membership(cfg -> cfg.seedMembers(seedAddress))) | ||
.transport(RSocketServiceTransport::new) | ||
.services(new GreetingServiceImpl()) | ||
.startAwait(); | ||
|
||
// Create service proxy | ||
GreetingsService service = seed.call().api(GreetingsService.class); | ||
|
||
// Execute the services and subscribe to service events | ||
service | ||
.sayHello("joe") | ||
.subscribe(consumer -> System.out.println(consumer.message())); | ||
|
||
seed.onShutdown().block(); | ||
ms.onShutdown().block(); | ||
} | ||
|
||
} |
4 changes: 3 additions & 1 deletion
4
...xamples/src/main/java/io/scalecube/services/examples/helloworld/service/api/Greeting.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters