Search in sources :

Example 1 with SinkConnectionsStatus

use of io.mantisrx.client.SinkConnectionsStatus in project mantis by Netflix.

the class ConnectToNamedJob method main.

public static void main(String[] args) {
    // SinkParameters params = new SinkParameters.Builder().withParameter("filter", "windows8").build();
    final AtomicLong eventCounter = new AtomicLong(0L);
    System.setProperty("log4j.logger.io", "DEBUG");
    try {
        Args.parse(ConnectToNamedJob.class, args);
    } catch (IllegalArgumentException e) {
        Args.usage(SubmitEphemeralJob.class);
        System.exit(1);
    }
    Properties properties = new Properties();
    System.out.println("propfile=" + propFile);
    try (InputStream inputStream = new FileInputStream(propFile)) {
        properties.load(inputStream);
    } catch (IOException e) {
        e.printStackTrace();
    }
    final CountDownLatch latch = new CountDownLatch(1);
    MantisSSEJob job = null;
    try {
        job = new MantisSSEJob.Builder(properties).name(jobName).onConnectionReset(new Action1<Throwable>() {

            @Override
            public void call(Throwable throwable) {
                System.err.println("Reconnecting due to error: " + throwable.getMessage());
            }
        }).sinkConnectionsStatusObserver(new Observer<SinkConnectionsStatus>() {

            @Override
            public void onCompleted() {
                System.out.println("ConnectionStatusObserver completed");
            }

            @Override
            public void onError(Throwable e) {
                System.err.println("ConnectionStatusObserver error: " + e.getMessage());
            }

            @Override
            public void onNext(SinkConnectionsStatus status) {
                System.out.println("ConnectionStatusObserver: receiving from " + status.getRecevingDataFrom() + ", connected to " + status.getNumConnected() + " of " + status.getTotal());
            }
        }).sinkDataRecvTimeoutSecs(11).buildJobConnector();
    } catch (Exception e) {
        e.printStackTrace();
    }
    // try{Thread.sleep(3000);}catch(InterruptedException ie){}
    System.out.println("Subscribing now");
    Subscription subscription = job.connectAndGet().doOnNext(new Action1<Observable<MantisServerSentEvent>>() {

        @Override
        public void call(Observable<MantisServerSentEvent> o) {
            o.doOnNext(new Action1<MantisServerSentEvent>() {

                @Override
                public void call(MantisServerSentEvent data) {
                    logger.info("Got event:  + " + data);
                    latch.countDown();
                // if(eventCounter.incrementAndGet()>4)
                // throw new RuntimeException("Test exception");
                }
            }).subscribe();
        }
    }).doOnError(new Action1<Throwable>() {

        @Override
        public void call(Throwable throwable) {
            logger.error(throwable.getMessage());
        }
    }).doOnCompleted(new Action0() {

        @Override
        public void call() {
            System.out.println("Completed");
            System.exit(0);
        }
    }).subscribe();
    // .subscribe();
    try {
        boolean await = latch.await(30, TimeUnit.SECONDS);
        if (await)
            System.out.println("PASSED");
        else
            System.err.println("FAILED!");
        Thread.sleep(5000000);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    subscription.unsubscribe();
    System.out.println("Unsubscribed");
    try {
        Thread.sleep(80000);
    } catch (InterruptedException ie) {
    }
    System.exit(0);
}
Also used : Action0(rx.functions.Action0) Action1(rx.functions.Action1) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) IOException(java.io.IOException) Properties(java.util.Properties) CountDownLatch(java.util.concurrent.CountDownLatch) FileInputStream(java.io.FileInputStream) IOException(java.io.IOException) Observable(rx.Observable) AtomicLong(java.util.concurrent.atomic.AtomicLong) MantisServerSentEvent(io.mantisrx.common.MantisServerSentEvent) SinkConnectionsStatus(io.mantisrx.client.SinkConnectionsStatus) Observer(rx.Observer) Subscription(rx.Subscription) MantisSSEJob(io.mantisrx.client.MantisSSEJob)

Example 2 with SinkConnectionsStatus

use of io.mantisrx.client.SinkConnectionsStatus in project mantis by Netflix.

the class MantisSSEJobTest method submitAndConnectTest.

// @Test
public void submitAndConnectTest() {
    CountDownLatch latch = new CountDownLatch(2);
    CountDownLatch connectionStatusReceived = new CountDownLatch(1);
    MantisSSEJob job = new MantisSSEJob.Builder(zkProps).name("mantis-examples-sine-function").sinkConnectionsStatusObserver(new Observer<SinkConnectionsStatus>() {

        @Override
        public void onCompleted() {
        // TODO Auto-generated method stub
        }

        @Override
        public void onError(Throwable e) {
        // TODO Auto-generated method stub
        }

        @Override
        public void onNext(SinkConnectionsStatus t) {
            connectionStatusReceived.countDown();
        }
    }).onConnectionReset(new Action1<Throwable>() {

        @Override
        public void call(Throwable throwable) {
            System.err.println("Reconnecting due to error: " + throwable.getMessage());
        }
    }).onCloseKillJob().buildJobSubmitter();
    job.submitAndGet().flatMap((t) -> {
        return t.map((mmsse) -> {
            return mmsse.getEventAsString();
        });
    }).take(2).doOnNext((d) -> {
        latch.countDown();
    }).toBlocking().subscribe((data) -> {
        System.out.println("Got data -> " + data);
    });
    ;
    try {
        latch.await(20, TimeUnit.SECONDS);
        connectionStatusReceived.await(10, TimeUnit.SECONDS);
    } catch (InterruptedException e) {
        fail();
    }
}
Also used : SinkConnectionsStatus(io.mantisrx.client.SinkConnectionsStatus) Observer(rx.Observer) CountDownLatch(java.util.concurrent.CountDownLatch) MantisSSEJob(io.mantisrx.client.MantisSSEJob)

Example 3 with SinkConnectionsStatus

use of io.mantisrx.client.SinkConnectionsStatus in project mantis by Netflix.

the class MantisSSEJobTest method connectToJobTest.

// @Test
public void connectToJobTest() {
    CountDownLatch latch = new CountDownLatch(5);
    CountDownLatch connectionStatusReceived = new CountDownLatch(1);
    MantisSSEJob job = new MantisSSEJob.Builder(zkProps).name("GroupByIP").sinkConnectionsStatusObserver(new Observer<SinkConnectionsStatus>() {

        @Override
        public void onCompleted() {
        // TODO Auto-generated method stub
        }

        @Override
        public void onError(Throwable e) {
        // TODO Auto-generated method stub
        }

        @Override
        public void onNext(SinkConnectionsStatus t) {
            connectionStatusReceived.countDown();
        }
    }).onConnectionReset(new Action1<Throwable>() {

        @Override
        public void call(Throwable throwable) {
            System.err.println("Reconnecting due to error: " + throwable.getMessage());
        }
    }).buildJobConnector();
    job.connectAndGet().flatMap((t) -> {
        return t.map((mmsse) -> {
            return mmsse.getEventAsString();
        });
    }).take(5).doOnNext((d) -> {
        latch.countDown();
    }).toBlocking().subscribe((data) -> {
        System.out.println("Got data -> " + data);
    });
    ;
    try {
        latch.await(10, TimeUnit.SECONDS);
        connectionStatusReceived.await(10, TimeUnit.SECONDS);
    } catch (InterruptedException e) {
        fail();
    }
}
Also used : Action1(rx.functions.Action1) SinkConnectionsStatus(io.mantisrx.client.SinkConnectionsStatus) CountDownLatch(java.util.concurrent.CountDownLatch) MantisSSEJob(io.mantisrx.client.MantisSSEJob)

Aggregations

MantisSSEJob (io.mantisrx.client.MantisSSEJob)3 SinkConnectionsStatus (io.mantisrx.client.SinkConnectionsStatus)3 CountDownLatch (java.util.concurrent.CountDownLatch)3 Observer (rx.Observer)2 Action1 (rx.functions.Action1)2 MantisServerSentEvent (io.mantisrx.common.MantisServerSentEvent)1 FileInputStream (java.io.FileInputStream)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 Properties (java.util.Properties)1 AtomicLong (java.util.concurrent.atomic.AtomicLong)1 Observable (rx.Observable)1 Subscription (rx.Subscription)1 Action0 (rx.functions.Action0)1