TOMEE-2301 - Start with scheduled executor part Signed-off-by: brunobat <[email protected]>
Project: http://git-wip-us.apache.org/repos/asf/tomee/repo Commit: http://git-wip-us.apache.org/repos/asf/tomee/commit/fe7bc8ff Tree: http://git-wip-us.apache.org/repos/asf/tomee/tree/fe7bc8ff Diff: http://git-wip-us.apache.org/repos/asf/tomee/diff/fe7bc8ff Branch: refs/heads/master Commit: fe7bc8ff6e0cfd8110ef687ca46afedbbcaeb8cf Parents: de075f8 Author: brunobat <[email protected]> Authored: Mon Dec 3 18:25:25 2018 +0000 Committer: brunobat <[email protected]> Committed: Wed Dec 5 15:27:04 2018 +0000 ---------------------------------------------------------------------- .../executor/ManagedScheduledService.java | 83 ++++++++++++++++++++ .../executor/ManagedScheduledServiceTest.java | 59 ++++++++++++++ 2 files changed, 142 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/tomee/blob/fe7bc8ff/examples/concurrency-utils/src/main/java/org/superbiz/executor/ManagedScheduledService.java ---------------------------------------------------------------------- diff --git a/examples/concurrency-utils/src/main/java/org/superbiz/executor/ManagedScheduledService.java b/examples/concurrency-utils/src/main/java/org/superbiz/executor/ManagedScheduledService.java new file mode 100644 index 0000000..1032f51 --- /dev/null +++ b/examples/concurrency-utils/src/main/java/org/superbiz/executor/ManagedScheduledService.java @@ -0,0 +1,83 @@ +package org.superbiz.executor; + +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * <p> + * http://www.apache.org/licenses/LICENSE-2.0 + * <p> + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import javax.annotation.Resource; +import javax.enterprise.concurrent.ManagedScheduledExecutorService; +import javax.enterprise.context.RequestScoped; +import java.util.concurrent.Callable; +import java.util.concurrent.Future; +import java.util.concurrent.ScheduledFuture; +import java.util.concurrent.TimeUnit; + +import static java.util.Objects.nonNull; + + +@RequestScoped +public class ManagedScheduledService { + + @Resource + private ManagedScheduledExecutorService executor; + + public Future<Integer> singleFixedDelayTask(final int value) { + System.out.println("longCallableTask scheduled"); + return executor.schedule(longCallableTask(value, 10, null), 100, TimeUnit.MILLISECONDS); + } + + public ScheduledFuture<?> periodicFixedDelayTask(final int value) { + return executor.scheduleAtFixedRate(longRunnableTask(value, 10, null), 0, 100, TimeUnit.MILLISECONDS); + } + + private Runnable longRunnableTask(final int value, + final int taskDurationMs, + final String errorMessage) { + return () -> { + if (nonNull(errorMessage)) { + System.out.println("Exception will be thrown"); + throw new RuntimeException(errorMessage); + } + + Integer result = value + 1; + System.out.println("longRunnableTask complete. Value is " + result); + // Cannot return result with a Runnable. + }; + } + + private Callable<Integer> longCallableTask(final int value, + final int taskDurationMs, + final String errorMessage) { + return () -> { + System.out.println("longCallableTask start"); + if (nonNull(errorMessage)) { + System.out.println("Exception will be thrown"); + throw new RuntimeException(errorMessage); + } + + try { + // Simulate a long processing task using TimeUnit to sleep. + TimeUnit.MILLISECONDS.sleep(taskDurationMs); + } catch (InterruptedException e) { + throw new RuntimeException("Problem while waiting"); + } + System.out.println("longCallableTask complete"); + // We can return a result with a Callable. + return value + 1; + }; + } + +} http://git-wip-us.apache.org/repos/asf/tomee/blob/fe7bc8ff/examples/concurrency-utils/src/test/java/org/superbiz/executor/ManagedScheduledServiceTest.java ---------------------------------------------------------------------- diff --git a/examples/concurrency-utils/src/test/java/org/superbiz/executor/ManagedScheduledServiceTest.java b/examples/concurrency-utils/src/test/java/org/superbiz/executor/ManagedScheduledServiceTest.java new file mode 100644 index 0000000..b4e3807 --- /dev/null +++ b/examples/concurrency-utils/src/test/java/org/superbiz/executor/ManagedScheduledServiceTest.java @@ -0,0 +1,59 @@ +package org.superbiz.executor; +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * <p> + * http://www.apache.org/licenses/LICENSE-2.0 + * <p> + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import org.jboss.arquillian.container.test.api.Deployment; +import org.jboss.arquillian.junit.Arquillian; +import org.jboss.shrinkwrap.api.ShrinkWrap; +import org.jboss.shrinkwrap.api.asset.EmptyAsset; +import org.jboss.shrinkwrap.api.spec.WebArchive; +import org.junit.Test; +import org.junit.runner.RunWith; + +import javax.inject.Inject; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.Future; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.TimeoutException; + +import static org.junit.Assert.assertEquals; + +@RunWith(Arquillian.class) +public class ManagedScheduledServiceTest { + + @Inject + private ManagedScheduledService scheduledService; + + @Deployment() + public static final WebArchive app() { + return ShrinkWrap.create(WebArchive.class, "example.war") + .addClasses(ManagedScheduledService.class) + .addAsWebInfResource(EmptyAsset.INSTANCE, "beans.xml"); + } + + + @Test + public void singleFixedDelayTask() throws InterruptedException, ExecutionException, TimeoutException { + final Future<Integer> future = scheduledService.singleFixedDelayTask(1); + assertEquals(2, future.get(200, TimeUnit.MILLISECONDS).intValue()); + + } + + @Test + public void periodicFixedDelayTask() { + } +} \ No newline at end of file
