Created
September 12, 2019 21:53
-
-
Save artem-v/32b3af36361f3c518109fdacaa74820e to your computer and use it in GitHub Desktop.
ClusterExampleRunner.java-Gitter-possible-issue-wtih-Metadta
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
import com.fasterxml.jackson.annotation.JsonAutoDetect; | |
import com.fasterxml.jackson.annotation.JsonInclude; | |
import com.fasterxml.jackson.annotation.JsonTypeInfo; | |
import com.fasterxml.jackson.annotation.PropertyAccessor; | |
import com.fasterxml.jackson.databind.DeserializationFeature; | |
import com.fasterxml.jackson.databind.ObjectMapper; | |
import com.fasterxml.jackson.databind.ObjectMapper.DefaultTyping; | |
import com.fasterxml.jackson.databind.SerializationFeature; | |
import io.scalecube.cluster.Cluster; | |
import io.scalecube.cluster.ClusterConfig; | |
import io.scalecube.cluster.ClusterImpl; | |
import io.scalecube.cluster.transport.api.Message; | |
import io.scalecube.cluster.transport.api.MessageCodec; | |
import java.io.InputStream; | |
import java.io.OutputStream; | |
import java.nio.ByteBuffer; | |
public class ClusterExampleRunner { | |
public static void main(String[] args) throws Exception { | |
ClusterConfig config = | |
new ClusterConfig() | |
.metadataEncoder(ClusterExampleRunner::encode) | |
.metadataDecoder(ClusterExampleRunner::decode) | |
.transport(t -> t.messageCodec(new MessageCodecImpl())); | |
Cluster cluster1 = new ClusterImpl(config.transport(t -> t.port(4800))).startAwait(); | |
System.out.println("cluster1.address: " + cluster1.address()); | |
System.out.println("cluster1.metadata: " + cluster1.metadata()); | |
System.out.println("cluster1.otherMembers: " + cluster1.otherMembers()); | |
Cluster cluster2 = | |
new ClusterImpl(config.membership(m -> m.seedMembers(cluster1.address()))).startAwait(); | |
System.out.println("cluster2.address: " + cluster2.address()); | |
System.out.println("cluster2.metadata: " + cluster2.metadata()); | |
System.out.println("cluster2.otherMembers: " + cluster2.otherMembers()); | |
System.out.println("cluster1.otherMembers: " + cluster1.otherMembers()); | |
Thread.currentThread().join(); | |
} | |
static Object decode(ByteBuffer buffer) { | |
return buffer; | |
} | |
static ByteBuffer encode(Object metadata) { | |
return ByteBuffer.allocate(0); | |
} | |
private static class MessageCodecImpl implements MessageCodec { | |
static final ObjectMapper OBJECT_MAPPER = initMapper(); | |
@Override | |
public Message deserialize(InputStream stream) throws Exception { | |
return OBJECT_MAPPER.readValue(stream, Message.class); | |
} | |
@Override | |
public void serialize(Message message, OutputStream stream) throws Exception { | |
OBJECT_MAPPER.writeValue(stream, message); | |
} | |
static ObjectMapper initMapper() { | |
ObjectMapper mapper = new ObjectMapper(); | |
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); | |
mapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false); | |
mapper.configure(DeserializationFeature.READ_UNKNOWN_ENUM_VALUES_AS_NULL, true); | |
mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false); | |
mapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY); | |
mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL); | |
mapper.configure(SerializationFeature.WRITE_ENUMS_USING_TO_STRING, true); | |
mapper.enableDefaultTyping(DefaultTyping.JAVA_LANG_OBJECT, JsonTypeInfo.As.PROPERTY); | |
return mapper; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment