Search in sources :

Example 6 with ManagedContext

use of com.hazelcast.core.ManagedContext in project hazelcast by hazelcast.

the class DurableExecutorServiceTest method testManagedContextAndLocal.

/* ############ submit runnable ############ */
@Test
public void testManagedContextAndLocal() throws Exception {
    Config config = new Config();
    config.addDurableExecutorConfig(new DurableExecutorConfig("test").setPoolSize(1));
    final AtomicBoolean initialized = new AtomicBoolean();
    config.setManagedContext(new ManagedContext() {

        @Override
        public Object initialize(Object obj) {
            if (obj instanceof RunnableWithManagedContext) {
                initialized.set(true);
            }
            return obj;
        }
    });
    HazelcastInstance instance = createHazelcastInstance(config);
    DurableExecutorService executor = instance.getDurableExecutorService("test");
    RunnableWithManagedContext task = new RunnableWithManagedContext();
    executor.submit(task).get();
    assertTrue("The task should have been initialized by the ManagedContext", initialized.get());
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) HazelcastInstance(com.hazelcast.core.HazelcastInstance) Config(com.hazelcast.config.Config) DurableExecutorConfig(com.hazelcast.config.DurableExecutorConfig) DurableExecutorConfig(com.hazelcast.config.DurableExecutorConfig) ManagedContext(com.hazelcast.core.ManagedContext) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Example 7 with ManagedContext

use of com.hazelcast.core.ManagedContext in project hazelcast by hazelcast.

the class MultipleEntryOperation method innerBeforeRun.

@Override
public void innerBeforeRun() throws Exception {
    super.innerBeforeRun();
    final SerializationService serializationService = getNodeEngine().getSerializationService();
    final ManagedContext managedContext = serializationService.getManagedContext();
    managedContext.initialize(entryProcessor);
}
Also used : SerializationService(com.hazelcast.spi.serialization.SerializationService) ManagedContext(com.hazelcast.core.ManagedContext)

Example 8 with ManagedContext

use of com.hazelcast.core.ManagedContext in project hazelcast by hazelcast.

the class AbstractCallableTaskOperation method beforeRun.

@Override
public final void beforeRun() throws Exception {
    returnsResponse = false;
    callable = getCallable();
    ManagedContext managedContext = getManagedContext();
    if (callable instanceof RunnableAdapter) {
        RunnableAdapter adapter = (RunnableAdapter) callable;
        Runnable runnable = (Runnable) managedContext.initialize(adapter.getRunnable());
        adapter.setRunnable(runnable);
    } else {
        callable = (Callable) managedContext.initialize(callable);
    }
}
Also used : RunnableAdapter(com.hazelcast.executor.impl.RunnableAdapter) ManagedContext(com.hazelcast.core.ManagedContext)

Example 9 with ManagedContext

use of com.hazelcast.core.ManagedContext in project hazelcast by hazelcast.

the class SpecificSetupTest method managedContext_mustInitializeRunnable.

@Test
public void managedContext_mustInitializeRunnable() throws Exception {
    final AtomicBoolean initialized = new AtomicBoolean();
    Config config = new Config().addExecutorConfig(new ExecutorConfig("test", 1)).setManagedContext(new ManagedContext() {

        @Override
        public Object initialize(Object obj) {
            if (obj instanceof RunnableWithManagedContext) {
                initialized.set(true);
            }
            return obj;
        }
    });
    IExecutorService executor = createHazelcastInstance(config).getExecutorService("test");
    executor.submit(new RunnableWithManagedContext()).get();
    assertTrue("The task should have been initialized by the ManagedContext", initialized.get());
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ExecutorConfig(com.hazelcast.config.ExecutorConfig) Config(com.hazelcast.config.Config) ManagedContext(com.hazelcast.core.ManagedContext) IExecutorService(com.hazelcast.core.IExecutorService) ExecutorConfig(com.hazelcast.config.ExecutorConfig) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Aggregations

ManagedContext (com.hazelcast.core.ManagedContext)9 Config (com.hazelcast.config.Config)4 ParallelTest (com.hazelcast.test.annotation.ParallelTest)4 QuickTest (com.hazelcast.test.annotation.QuickTest)4 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)4 Test (org.junit.Test)4 SerializationService (com.hazelcast.spi.serialization.SerializationService)3 DurableExecutorConfig (com.hazelcast.config.DurableExecutorConfig)2 ExecutorConfig (com.hazelcast.config.ExecutorConfig)2 HazelcastInstance (com.hazelcast.core.HazelcastInstance)2 IExecutorService (com.hazelcast.core.IExecutorService)2 RunnableAdapter (com.hazelcast.executor.impl.RunnableAdapter)1 InternalSerializationService (com.hazelcast.internal.serialization.InternalSerializationService)1 Portable (com.hazelcast.nio.serialization.Portable)1