-
-
Save seyfullahdemir/3520121fc38811ed0c48 to your computer and use it in GitHub Desktop.
Example Code for Deserialization Problem
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 com.myproject; | |
import com.hazelcast.core.HazelcastInstance; | |
import com.hazelcast.core.IMap; | |
import com.hazelcast.core.TransactionalMap; | |
import com.hazelcast.transaction.TransactionException; | |
import com.hazelcast.transaction.TransactionalTask; | |
import com.hazelcast.transaction.TransactionalTaskContext; | |
import java.io.Serializable; | |
import java.util.Map; | |
import java.util.Set; | |
public class TransactionalTaskExample { | |
private IHazelcastClientFactory hazelcastClientFactory; | |
public TransactionalTaskExample(final IHazelcastClientFactory hazelcastClientFactory){ | |
this.hazelcastClientFactory = hazelcastClientFactory; | |
} | |
public void storeSentenceWithWordFrequencies(final String key, final Sentence sentence) { | |
final HazelcastInstance hazelcastClient = hazelcastClientFactory.getHazelcastClient(); | |
hazelcastClient.executeTransaction(new TransactionalTask() { | |
@Override | |
public Object execute(final TransactionalTaskContext context) throws TransactionException { | |
final TransactionalMap<String, Sentence> sentences = context.getMap("sentences"); | |
final TransactionalMap<String, WordFrequency> wordFrequencies = context.getMap("wordFrequencies"); | |
//so I use a secondary IMap reference to same map for get and contains methods | |
final IMap<String, WordFrequency> wordFrequenciesAsMap = hazelcastClient.getMap("wordFrequencies"); | |
sentences.put(key, sentence); | |
final String[] words = sentence.getContent().split(" "); | |
for (final String word : words) { | |
if(! wordFrequenciesAsMap.containsKey(word)){ | |
wordFrequencies.put(word, new WordFrequency(1)); | |
} else{ | |
final WordFrequency wordFrequency = wordFrequenciesAsMap.get(word); | |
wordFrequency.increaseFrequency(); | |
//This line updates the existing wordFrequency entry, when this line is executed; | |
//same deserialization exception is throwed, | |
//maybe because it internally does a get (or contains or some reading operation) call to map for the existing entry??? | |
wordFrequencies.put(word, wordFrequency); | |
} | |
} | |
return null; | |
} | |
}); | |
} | |
public void saveOrUpdateSentence(final String key, final Sentence sentence) { | |
final HazelcastInstance hazelcastClient = hazelcastClientFactory.getHazelcastClient(); | |
hazelcastClient.executeTransaction(new TransactionalTask() { | |
@Override | |
public Object execute(final TransactionalTaskContext context) throws TransactionException { | |
final TransactionalMap<String, Sentence> sentences = context.getMap("sentences"); | |
sentences.put(key, sentence); | |
return null; | |
} | |
}); | |
} | |
public Sentence getSentence(final String key) { | |
final HazelcastInstance hazelcastClient = hazelcastClientFactory.getHazelcastClient(); | |
final IMap<String, Sentence> sentencesAsMap = hazelcastClient.getMap("sentences"); | |
return sentencesAsMap.get(key); | |
} | |
public void dumpAll(){ | |
final HazelcastInstance hazelcastClient = hazelcastClientFactory.getHazelcastClient(); | |
final IMap<String, Sentence> sentencesAsMap = hazelcastClient.getMap("sentences"); | |
final IMap<String, Sentence> wordFrequenciesAsMap = hazelcastClient.getMap("wordFrequencies"); | |
final Set<Map.Entry<String, Sentence>> sentenceEntries = sentencesAsMap.entrySet(); | |
System.out.println("Sentences"); | |
for (final Map.Entry<String, Sentence> entry : sentenceEntries) { | |
System.out.println("key: " + entry.getKey() + " value: " + entry.getValue()); | |
} | |
System.out.println(); | |
System.out.println("Word Frequencies"); | |
final Set<Map.Entry<String, Sentence>> wordFrequencyEntries = wordFrequenciesAsMap.entrySet(); | |
for (final Map.Entry<String, Sentence> entry : wordFrequencyEntries) { | |
System.out.println("key: " + entry.getKey() + " value: " + entry.getValue()); | |
} | |
} | |
public static class Sentence implements Serializable { | |
private static final long serialVersionUID = 1L; | |
private String content; | |
private String authorsName; | |
public Sentence(final String content, final String authorsName){ | |
this.content = content; | |
this.authorsName = authorsName; | |
} | |
public String getContent(){ | |
return content; | |
} | |
public String getAuthorsName(){ | |
return authorsName; | |
} | |
public void setAuthorsName(final String authorsName){ | |
this.authorsName = authorsName; | |
} | |
@Override | |
public boolean equals(final Object o) { | |
if (this == o) { | |
return true; | |
} | |
if (!(o instanceof Sentence)) { | |
return false; | |
} | |
final Sentence sentence = (Sentence) o; | |
if (!authorsName.equals(sentence.authorsName)) { | |
return false; | |
} | |
if (!content.equals(sentence.content)) { | |
return false; | |
} | |
return true; | |
} | |
@Override | |
public int hashCode() { | |
int result = content.hashCode(); | |
result = 31 * result + authorsName.hashCode(); | |
return result; | |
} | |
@Override | |
public String toString() { | |
return "Sentence{" + | |
"content='" + content + '\'' + | |
", authorsName='" + authorsName + '\'' + | |
'}'; | |
} | |
} | |
public static class WordFrequency implements Serializable { | |
private static final long serialVersionUID = 1L; | |
private int frequency; | |
public WordFrequency(final int frequency){ | |
this.frequency = frequency; | |
} | |
public void increaseFrequency(){ | |
frequency += 1; | |
} | |
@Override | |
public String toString() { | |
return "WordFrequency{" + | |
"frequency=" + frequency + | |
'}'; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment