use of org.apache.ignite.stream.socket.SocketStreamer in project ignite by apache.
the class WordsSocketStreamerServer method main.
/**
* Starts socket streaming server.
*
* @param args Command line arguments (none required).
* @throws Exception If failed.
*/
public static void main(String[] args) throws Exception {
// Mark this cluster member as client.
Ignition.setClientMode(true);
CacheConfiguration<AffinityUuid, String> cfg = CacheConfig.wordCache();
try (Ignite ignite = Ignition.start("examples/config/example-ignite.xml")) {
if (!ExamplesUtils.hasServerNodes(ignite))
return;
// The cache is configured with sliding window holding 1 second of the streaming data.
try (IgniteCache<AffinityUuid, String> stmCache = ignite.getOrCreateCache(cfg)) {
IgniteDataStreamer<AffinityUuid, String> stmr = ignite.dataStreamer(stmCache.getName());
InetAddress addr = InetAddress.getLocalHost();
// Configure socket streamer
SocketStreamer<String, AffinityUuid, String> sockStmr = new SocketStreamer<>();
sockStmr.setAddr(addr);
sockStmr.setPort(PORT);
sockStmr.setDelimiter(DELIM);
sockStmr.setIgnite(ignite);
sockStmr.setStreamer(stmr);
// Converter from zero-terminated string to Java strings.
sockStmr.setConverter(new SocketMessageConverter<String>() {
@Override
public String convert(byte[] msg) {
try {
return new String(msg, "ASCII");
} catch (UnsupportedEncodingException e) {
throw new IgniteException(e);
}
}
});
sockStmr.setSingleTupleExtractor(new StreamSingleTupleExtractor<String, AffinityUuid, String>() {
@Override
public Map.Entry<AffinityUuid, String> extract(String word) {
// words are processed on the same cluster node.
return new IgniteBiTuple<>(new AffinityUuid(word), word);
}
});
sockStmr.start();
} catch (IgniteException e) {
System.err.println("Streaming server didn't start due to an error: ");
e.printStackTrace();
} finally {
// Distributed cache could be removed from cluster only by #destroyCache() call.
ignite.destroyCache(cfg.getName());
}
}
}
Aggregations