# ExecutionManager (variable scope in runnable)

**URL:** <https://forum.inductiveautomation.com/t/executionmanager-variable-scope-in-runnable/15591>\
**Category:** Module Development\
**Created:** [October 6, 2017, 9:18am UTC](https://forum.inductiveautomation.com/t/executionmanager-variable-scope-in-runnable/15591 "2017-10-06T09:18:53Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![r.lebohec](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/r.lebohec/32/3704_2.png) [@r.lebohec](https://forum.inductiveautomation.com/u/r.lebohec)\
**Post date:** [October 6, 2017, 9:18am UTC](https://forum.inductiveautomation.com/t/executionmanager-variable-scope-in-runnable/15591/1 "2017-10-06T09:18:53Z")

</div>

Hi @pturmel,

Sorry If it was a very trivial question but I don’t understand my mistake in this context.  
As you can see below, In my module development I have multiple instance of this abstract ‘Sample’ class.  
For each instance, I register my runnable ‘TimeoutManager’ with an uniqueID with an initial delay.  
Before the runnable execution, I update an atomic Long with ‘doSomething()’ function. Unfortunately when my runnable ‘TimeoutManager’ run the atomicLong is stay with the initial Value.  
The execution manager don’t take the context (variables) during execution runnable.  
How can I have the right value of my AtomicLong during TimeoutManager.run()?

```auto
import com.inductiveautomation.ignition.common.execution.ExecutionManager;
import com.inductiveautomation.ignition.gateway.model.GatewayContext;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicLong;

public abstract class Sample{

    private final Logger logger = LoggerFactory.getLogger(getClass());
    private final ExecutionManager execm;
    private final TimeoutManager timeoutManager = new TimeoutManager();
    private AtomicLong myLastAccessTime = new AtomicLong(System.currentTimeMillis());

    public Sample(GatewayContext _context){
        this.execm = _context.getExecutionManager();
    }

    public void registerRun() {
        execm.registerWithInitialDelay("UUID", TimeoutManager.class.getName(), timeoutManager, 30000, TimeUnit.MILLISECONDS, 10000);
logger.debug("registerRun()");
    }

    public void unRegisterRun() {
        execm.unRegister("UUID",TimeoutManager.class.getName());

    }

    public void doSomething(){
        //Do something Here
        myLastAccessTime.set(System.currentTimeMillis());
logger.debug("doSomething() > LastAccessTime:[{}]",getLastAccessTime());
    }

    public Long getLastAccessTime(){
        return myLastAccessTime.get();
    }

    private class TimeoutManager implements Runnable{

    public TimeoutManager(){
 logger.debug("TimeoutManager > LastAccessTime:{}",getLastAccessTime());
}
        @Override
        public void run() {
            long currentTime = System.currentTimeMillis();
            long lastCommTime = getLastAccessTime();

            if ((currentTime - lastCommTime)> 30000){
                logger.debug("TimeoutManager.run()> You haven't done Something() is in timeout Delay:[{}]ms LastAccessTime:[{}]",(currentTime-lastCommTime),lastCommTime);
            } else {
                logger.debug("TimeoutManager.run()> You have done Something");
         }
        }
    }
}
```

```auto
registerRun()
TimeoutManager > LastAccessTime:1507275293767
doSomething() > LastAccessTime:[1507275293867]
doSomething() > LastAccessTime:[1507275293892]
TimeoutManager.run()> You haven't done Something() is in timeout Delay:[30000]ms LastAccessTime:[1507275293767]
```

Thanks.

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [October 6, 2017, 12:33pm UTC](https://forum.inductiveautomation.com/t/executionmanager-variable-scope-in-runnable/15591/2 "2017-10-06T12:33:32Z")

</div>

Not entirely sure. But look at “Delay:[30]ms” in your output. That suggests that the initial delay didn’t work as advertised.

---

<div class="post-metadata">

**Author:** ![r.lebohec](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/r.lebohec/32/3704_2.png) [@r.lebohec](https://forum.inductiveautomation.com/u/r.lebohec)\
**Post date:** [October 6, 2017, 1:27pm UTC](https://forum.inductiveautomation.com/t/executionmanager-variable-scope-in-runnable/15591/3 "2017-10-06T13:27:21Z")

</div>

Sorry I correct my Sample class example to illustrate the case

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [October 6, 2017, 2:22pm UTC](https://forum.inductiveautomation.com/t/executionmanager-variable-scope-in-runnable/15591/4 "2017-10-06T14:22:05Z")

</div>

Hmm. I still don’t see any obvious problem. Consider using a volatile long instead of an AtomicLong – you only have one writer, the doSomething method, and replacing the value of a volatile long is [guaranteed to be atomic in Java](http://docs.oracle.com/javase/specs/jls/se8/html/jls-17.html#jls-17.7). That chapter of the Java spec discusses a number of gotcha’s that might be involved.

---

<div class="post-metadata">

**Author:** ![Kevin.Herron](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/kevin.herron/32/7259_2.png) [@Kevin.Herron](https://forum.inductiveautomation.com/u/Kevin.Herron)\
**Post date:** [October 6, 2017, 4:30pm UTC](https://forum.inductiveautomation.com/t/executionmanager-variable-scope-in-runnable/15591/5 "2017-10-06T16:30:44Z")

</div>

The code sample you present looks fine… I suspect the problem is in the context surrounding how you’re using it.

Do you have more than one instance of `Sample` and are you calling `doSomething()` on a different one than you think?

Another thing I find strange, your logs have these messages:

```auto
registerRun()
TimeoutManager > LastAccessTime:1507275293767

```

Given the code you’ve supplied, that order should be reversed, or there’s more than one instance logging.

---

<div class="post-metadata">

**Author:** ![r.lebohec](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/r.lebohec/32/3704_2.png) [@r.lebohec](https://forum.inductiveautomation.com/u/r.lebohec)\
**Post date:** [October 6, 2017, 6:57pm UTC](https://forum.inductiveautomation.com/t/executionmanager-variable-scope-in-runnable/15591/6 "2017-10-06T18:57:21Z")

</div>

I try your volatile suggestion but it s still the same issue.

---

<div class="post-metadata">

**Author:** ![r.lebohec](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/r.lebohec/32/3704_2.png) [@r.lebohec](https://forum.inductiveautomation.com/u/r.lebohec)\
**Post date:** [October 6, 2017, 7:11pm UTC](https://forum.inductiveautomation.com/t/executionmanager-variable-scope-in-runnable/15591/7 "2017-10-06T19:11:06Z")

</div>

Interesting suggestions. I don’t systematically uninstall my module between each test on my plateform. The context is maybe wrong.

Finally, there will have more than one instance of sample class. (The original code are here: [https://github.com/IgnitionModuleDevelopmentCommunity/OpcUaFctAddon/blob/master/OpcUaFctAddon-gateway/src/main/java/com/bouyguesenergiesservices/opcuafctaddon/gateway/fct/GatewayFct.java](https://github.com/IgnitionModuleDevelopmentCommunity/OpcUaFctAddon/blob/master/OpcUaFctAddon-gateway/src/main/java/com/bouyguesenergiesservices/opcuafctaddon/gateway/fct/GatewayFct.java))

I will made more tests on monday!

Thank you @Kevin.Herron.

---

<div class="post-metadata">

**Author:** ![r.lebohec](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/r.lebohec/32/3704_2.png) [@r.lebohec](https://forum.inductiveautomation.com/u/r.lebohec)\
**Post date:** [October 10, 2017, 12:26pm UTC](https://forum.inductiveautomation.com/t/executionmanager-variable-scope-in-runnable/15591/8 "2017-10-10T12:26:29Z")

</div>

> [@Kevin.Herron](#):
>
> Do you have more than one instance of Sample and are you calling doSomething() on a different one than you think?

My issue was that I create more than one instance with a quasi similar UUID (In logs viewer there is just 1 digit who changed ). I will doSomething() in one and check in an other if there is a Timeout.

Moreover as @pturmel said, I add AtomicLong consideration in my code implementation.

As usual, It was an efficient intervention. Thanks @Kevin.Herron and @pturmel.
