How to cancel execution by observable

17 views
Skip to first unread message

Anurag Laddha

unread,
Dec 30, 2017, 9:24:00 PM12/30/17
to HystrixOSS
Hello 


//Hystrix command
public class CommandHelloWorld extends HystrixCommand<String> {

        private final String name;

        public CommandHelloWorld(String name) {
            super(HystrixCommandGroupKey.Factory.asKey("ExampleGroup"));
            this.name = name;
        }

        @Override
        protected String run() {
            try {
                //long running task
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            log.debug("Done producing value");
            return "Hello " + name + "!";
        }
    }

@Test
    public void testAsync() throws Exception{

        Observable<String> cold = new CommandHelloWorld("sample1").toObservable();
        Subscription subscription = cold.subscribe((n) -> {
                log.info("Received next: " + n);
            },
            (error) -> log.error("Exception occurred ", error),
            () -> {
                log.debug("Finished executing");
            });

        Thread.sleep(500);
        subscription.unsubscribe(); //How can i cancel the long running task here?
       Thread.sleep(2000);
    }

When i unsubscribe the observer (observable had only one subscription), the observable still continues with execution.
Is there any way to cancel the execution done by observable?

Thanks!

Anurag Laddha

unread,
Jan 3, 2018, 11:45:07 PM1/3/18
to HystrixOSS
Any update?
Reply all
Reply to author
Forward
0 new messages