Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

IScheduledExecutorService appears not to use HazelcastInstanceAware on local operations #9675

Closed
stevebarham opened this issue Jan 17, 2017 · 2 comments

Comments

Projects
None yet
3 participants
@stevebarham
Copy link

commented Jan 17, 2017

Version: 3.8 EA

I've been using the IScheduledExecutorService which was added in Hazelcast 3.8. It appears that when operations are scheduled and executed on a single local node, that the HazelcastInstance does not get injected. An instance is injected if the task is submitted and executed on a remote node.

Sample reproduction case; console contains:

Testing via ExecutorService
Injected state: true
Testing via ScheduledExecutorService
Injected state: false

Expected:

Testing via ExecutorService
Injected state: true
Testing via ScheduledExecutorService
Injected state: true

Test case:

import com.hazelcast.core.Hazelcast;
import com.hazelcast.core.HazelcastInstance;
import com.hazelcast.core.HazelcastInstanceAware;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.Serializable;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;

import static org.junit.Assert.assertTrue;

public class HazelcastInstanceAwareTest {
    HazelcastInstance instance;

    @Before
    public void setup() {
        instance = Hazelcast.newHazelcastInstance();
    }

    @After
    public void teardown() throws Exception {
        instance.shutdown();
    }

    @Test
    public void test() throws ExecutionException, InterruptedException {
        System.err.println("Testing via ExecutorService");
        assertTrue(instance.getExecutorService("foo").submit((Callable<Boolean>) new AwareRunnable()).get());

        System.err.println("Testing via ScheduledExecutorService");
        instance.getScheduledExecutorService("bar").scheduleAtFixedRate(new AwareRunnable(), 0, 5, TimeUnit.SECONDS);

        Thread.sleep(TimeUnit.SECONDS.toMillis(10));
    }

    static class AwareRunnable implements Runnable, Callable<Boolean>, HazelcastInstanceAware, Serializable {
        private volatile HazelcastInstance instance;

        @Override
        public void setHazelcastInstance(final HazelcastInstance instance) {
            this.instance = instance;
        }

        @Override
        public void run() {
            call();
        }

        @Override
        public Boolean call() {
            final boolean injected = (instance != null);
            System.err.println("Injected state: " + injected);
            return injected;
        }
    }
}

@jerrinot jerrinot added this to the 3.8 milestone Jan 17, 2017

@jerrinot

This comment has been minimized.

Copy link
Contributor

commented Jan 17, 2017

@stevebarham: good one, thanks for reporting it!

@stevebarham

This comment has been minimized.

Copy link
Author

commented Jan 19, 2017

Nice one, thanks all!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.