Last active
December 12, 2017 23:06
-
-
Save gokhanoner/79bbddb82a183990a80a7c7b67b29e39 to your computer and use it in GitHub Desktop.
Serialization Demo
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
package test.ms; | |
import com.hazelcast.client.HazelcastClient; | |
import com.hazelcast.client.config.ClientConfig; | |
import com.hazelcast.config.SerializerConfig; | |
import com.hazelcast.core.HazelcastInstance; | |
import com.hazelcast.nio.ObjectDataInput; | |
import com.hazelcast.nio.ObjectDataOutput; | |
import com.hazelcast.nio.serialization.StreamSerializer; | |
import java.io.IOException; | |
import java.io.Serializable; | |
public class SerilCheck { | |
public static void main(String[] args) { | |
SerializerConfig serializerConfig = new SerializerConfig(); | |
serializerConfig.setTypeClass(MyData.class).setClass(MySerializer.class); | |
ClientConfig clientConfig = new ClientConfig(); | |
clientConfig.getSerializationConfig().addSerializerConfig(serializerConfig); | |
HazelcastInstance client = HazelcastClient.newHazelcastClient(clientConfig); | |
client.getMap("test").put(1, new MyData("Gokhan", 36)); | |
} | |
} | |
class MySerializer implements StreamSerializer<MyData> { | |
@Override | |
public void write(ObjectDataOutput out, MyData object) throws IOException { | |
System.out.println("Writing from " + this.getClass().getSimpleName()); | |
out.writeUTF(object.getName()); | |
out.writeInt(object.getAge()); | |
} | |
@Override | |
public MyData read(ObjectDataInput in) throws IOException { | |
System.out.println("Reading from " + this.getClass().getSimpleName()); | |
MyData myData = new MyData(); | |
myData.setName(in.readUTF()); | |
myData.setAge(in.readInt()); | |
return myData; | |
} | |
@Override | |
public int getTypeId() { | |
return 9; | |
} | |
@Override | |
public void destroy() { | |
} | |
} | |
class MyData implements Serializable { | |
public MyData(){} | |
public MyData(String name, int age) { | |
this.name = name; | |
this.age = age; | |
} | |
private String name; | |
private int age; | |
public String getName() { | |
return name; | |
} | |
public void setName(String name) { | |
this.name = name; | |
} | |
public int getAge() { | |
return age; | |
} | |
public void setAge(int age) { | |
this.age = age; | |
} | |
@Override | |
public String toString() { | |
return "MyData{" + | |
"name='" + name + '\'' + | |
", age=" + age + | |
'}'; | |
} | |
} |
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
package test.ms; | |
import com.hazelcast.core.EntryEvent; | |
import com.hazelcast.core.Hazelcast; | |
import com.hazelcast.core.HazelcastInstance; | |
import com.hazelcast.map.listener.EntryAddedListener; | |
public class SerilCheckInstance { | |
public static void main(String[] args) { | |
System.setProperty("hazelcast.phone.home.enabled", "false"); | |
HazelcastInstance hz = Hazelcast.newHazelcastInstance(); | |
hz.getMap("test").addEntryListener(new EntryAddedListener<Integer, MyData>() { | |
@Override | |
public void entryAdded(EntryEvent<Integer, MyData> event) { | |
try { | |
System.out.println(event.getKey()); | |
System.out.println(event.getValue()); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
} | |
}, true); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment