Search in sources :

Example 1 with SocketStreamer

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());
        }
    }
}
Also used : AffinityUuid(org.apache.ignite.cache.affinity.AffinityUuid) UnsupportedEncodingException(java.io.UnsupportedEncodingException) IgniteException(org.apache.ignite.IgniteException) SocketStreamer(org.apache.ignite.stream.socket.SocketStreamer) Ignite(org.apache.ignite.Ignite) InetAddress(java.net.InetAddress)

Aggregations

UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 InetAddress (java.net.InetAddress)1 Ignite (org.apache.ignite.Ignite)1 IgniteException (org.apache.ignite.IgniteException)1 AffinityUuid (org.apache.ignite.cache.affinity.AffinityUuid)1 SocketStreamer (org.apache.ignite.stream.socket.SocketStreamer)1