[ 
https://issues.apache.org/jira/browse/BROOKLYN-446?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=15964106#comment-15964106
 ] 

Aled Sage commented on BROOKLYN-446:
------------------------------------

I've deprecated the broken methods in 
https://github.com/apache/brooklyn-server/pull/627, in time for 0.11.0 
hopefully.

I agree that constructors taking a Runnable/Callable would be sensible. In the 
mean time, I've just pointed folk at the constructor that takes a task factory.

---
Longer term, I'd love to find the time to re-write our scheduled-executor 
stuff. I really do hate our {{ScheduledTask}}. Why did we invent a new (worse) 
api for scheduling jobs, rather than just re-using the very well known 
{{java.util.concurrent.ScheduledExecutorService}}? The answer I suspect is that 
in Brooklyn 0.1.0 we were too focused on a groovy API (not that I think it was 
a good groovy API either!).

That long-term aim makes me hesitant to tweak this stuff. If we are going to 
invest time in this, I'd prefer to do it better! (But adding the constructors 
is a couple of hours, versus a couple of days for the 
{{ScheduledExecutorService}} style of api.)

> Constructor ScheduledTask(Map, Task) is broken
> ----------------------------------------------
>
>                 Key: BROOKLYN-446
>                 URL: https://issues.apache.org/jira/browse/BROOKLYN-446
>             Project: Brooklyn
>          Issue Type: Bug
>    Affects Versions: 0.10.0
>            Reporter: Svetoslav Neykov
>
> The resulting task is executed only once, not rescheduled.
> A test case to show the behaviour:
> {noformat}
> import static org.testng.Assert.assertTrue;
> import java.util.Map;
> import java.util.concurrent.atomic.AtomicInteger;
> import org.apache.brooklyn.api.mgmt.Task;
> import org.apache.brooklyn.core.test.BrooklynMgmtUnitTestSupport;
> import org.apache.brooklyn.test.Asserts;
> import org.apache.brooklyn.util.collections.MutableMap;
> import org.apache.brooklyn.util.time.Duration;
> import org.testng.annotations.Test;
> import com.google.common.collect.ImmutableMap;
> import com.google.common.util.concurrent.Runnables;
> public class ScheduledTaskTest extends BrooklynMgmtUnitTestSupport {
>     /**
>      * Fails with:
>      * java.lang.UnsupportedOperationException
>      *     at 
> com.google.common.collect.ImmutableMap.remove(ImmutableMap.java:338)
>      *     at 
> org.apache.brooklyn.util.core.task.BasicTask.<init>(BasicTask.java:115)
>      *     at 
> org.apache.brooklyn.util.core.task.BasicTask.<init>(BasicTask.java:107)
>      *     at 
> org.apache.brooklyn.util.core.task.ScheduledTask.<init>(ScheduledTask.java:96)
>      *     at 
> org.apache.brooklyn.util.core.task.ScheduledTask.<init>(ScheduledTask.java:88)
>      *     at 
> org.apache.brooklyn.util.core.task.ScheduledTaskTest.testImmutableFlags(ScheduledTaskTest.java:43)
>      */
>     @Test
>     public void testImmutableFlags() {
>         Map<?, ?> flags = ImmutableMap.of(
>                 "period", Duration.ONE_SECOND,
>                 "delay", Duration.ONE_SECOND);
>         Task<?> task = new BasicTask<>(Runnables.doNothing());
>         new ScheduledTask(flags, task);
>     }
>     /**
>      * Fails with:
>      * java.lang.AssertionError: failed succeeds-eventually, 69 attempts, 
> 30005ms elapsed: AssertionError: expected [true] but found [false]
>      *     at 
> org.apache.brooklyn.test.Asserts.succeedsEventually(Asserts.java:1008)
>      *     at 
> org.apache.brooklyn.test.Asserts.succeedsEventually(Asserts.java:895)
>      *     at 
> org.apache.brooklyn.test.Asserts.succeedsEventually(Asserts.java:888)
>      *     at 
> org.apache.brooklyn.util.core.task.ScheduledTaskTest.testTaskConstructor(ScheduledTaskTest.java:60)
>      */
>     @Test
>     public void testTaskConstructor() {
>         final AtomicInteger cnt = new AtomicInteger();
>         Map<?, ?> flags = MutableMap.of(
>                 "period", Duration.ONE_SECOND,
>                 "delay", Duration.ONE_SECOND);
>         Task<?> task = new BasicTask<>(new Runnable() {
>             @Override
>             public void run() {
>                 cnt.incrementAndGet();
>             }
>         });
>         ScheduledTask scheduledTask = new ScheduledTask(flags, task);
>         mgmt.getExecutionManager().submit(scheduledTask);
>         Asserts.succeedsEventually(new Runnable() {
>             @Override
>             public void run() {
>                 assertTrue(cnt.get() > 1);
>             }
>         });
>     }
> }
> {noformat}
> The problem is that the task gets reused and on the second schedule attempt 
> it already has a future assigned so is not scheduled any more.
> The best solution seems to be to remove the constructor altogether. 
> Optionally providing a Runnable/Callable job constructor, wrapping the job in 
> a factory, returning a new task on each submission.



--
This message was sent by Atlassian JIRA
(v6.3.15#6346)

Reply via email to