├── Dockerfile ├── LICENSE ├── README.md ├── pom.xml └── src └── main ├── java └── com │ └── wordpress │ └── abhirockzz │ └── cdi │ └── concutils │ ├── BackgroundTask.java │ ├── JAXRSConfig.java │ ├── TaskStore.java │ ├── TasksResource.java │ └── pojo │ ├── AllTasks.java │ ├── Task.java │ └── TaskState.java └── webapp └── WEB-INF └── beans.xml /Dockerfile: -------------------------------------------------------------------------------- 1 | FROM airhacks/payara 2 | ENV WAR javaee-cdi-concurrency-utils.war 3 | COPY target/${WAR} ${DEPLOYMENT_DIR} -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2017 Abhishek Gupta 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ## To run on Docker 2 | 3 | - Shortcut 4 | - `docker pull abhirockzz/javaee-cdi-concurrency-utils` 5 | - `docker run -it -p 8080:8080 abhirockzz/javaee-cdi-concurrency-utils` 6 | 7 | - Build it yourself using the [`Dockerfile`](https://github.com/abhirockzz/javaee-cdi-concurrency-utils/blob/master/Dockerfile) in the project (it [uses this Payara image](https://hub.docker.com/r/airhacks/payara/) as the base) 8 | - `docker build -t .` 9 | - `docker run -it -p 8080:8080 ` 10 | 11 | ## To run 12 | 13 | - `git clone` the project and execute `mvn clean install` 14 | - Deploy `javaee-cdi-concurrency-utils.war` in `target` directory to any of the [Java EE 7 containers](http://www.oracle.com/technetwork/java/javaee/overview/compatibility-jsp-136984.html) 15 | 16 | ## To check 17 | 18 | - execute a HTTP `POST` to `http://localhost:8080/javaee-cdi-concurrency-utils/tasks/` - [this initiates a task](https://github.com/abhirockzz/javaee-cdi-concurrency-utils/blob/master/src/main/java/com/wordpress/abhirockzz/cdi/concutils/TasksResource.java#L27) and returns the task ID 19 | 20 | ![](https://abhirockzz.files.wordpress.com/2017/04/1.jpg) 21 | 22 | - execute a HTTP `GET` to `http://localhost:8080/javaee-cdi-concurrency-utils/tasks/` - [it will give you a JSON payload](https://github.com/abhirockzz/javaee-cdi-concurrency-utils/blob/master/src/main/java/com/wordpress/abhirockzz/cdi/concutils/TasksResource.java#L40) with the task details 23 | 24 | ![](https://abhirockzz.files.wordpress.com/2017/04/2.jpg) 25 | 26 | - `POST` a few more tasks.... 27 | - execute a HTTP `GET` to `http://localhost:8080/javaee-cdi-concurrency-utils/tasks/all` [to get a JSON payload of all the tasks](https://github.com/abhirockzz/javaee-cdi-concurrency-utils/blob/master/src/main/java/com/wordpress/abhirockzz/cdi/concutils/TasksResource.java#L50) 28 | 29 | ![](https://abhirockzz.files.wordpress.com/2017/04/3.jpg) 30 | -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4.0.0 4 | 5 | com.wordpress.abhirockzz 6 | javaee-cdi-concurrency-utils 7 | 1.0 8 | war 9 | 10 | javaee-cdi-concurrency-utils 11 | 12 | 13 | ${project.build.directory}/endorsed 14 | UTF-8 15 | 16 | 17 | 18 | 19 | javax 20 | javaee-api 21 | 7.0 22 | provided 23 | 24 | 25 | 26 | 27 | javaee-cdi-concurrency-utils 28 | 29 | 30 | org.apache.maven.plugins 31 | maven-compiler-plugin 32 | 3.1 33 | 34 | 1.7 35 | 1.7 36 | 37 | ${endorsed.dir} 38 | 39 | 40 | 41 | 42 | org.apache.maven.plugins 43 | maven-war-plugin 44 | 2.3 45 | 46 | false 47 | 48 | 49 | 50 | org.apache.maven.plugins 51 | maven-dependency-plugin 52 | 2.6 53 | 54 | 55 | validate 56 | 57 | copy 58 | 59 | 60 | ${endorsed.dir} 61 | true 62 | 63 | 64 | javax 65 | javaee-endorsed-api 66 | 7.0 67 | jar 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | -------------------------------------------------------------------------------- /src/main/java/com/wordpress/abhirockzz/cdi/concutils/BackgroundTask.java: -------------------------------------------------------------------------------- 1 | package com.wordpress.abhirockzz.cdi.concutils; 2 | 3 | import com.wordpress.abhirockzz.cdi.concutils.pojo.TaskState; 4 | import com.wordpress.abhirockzz.cdi.concutils.pojo.Task; 5 | import java.util.Date; 6 | import java.util.Random; 7 | import javax.enterprise.context.Dependent; 8 | import javax.inject.Inject; 9 | 10 | @Dependent 11 | public class BackgroundTask implements Runnable { 12 | 13 | String taskid; 14 | 15 | public void setTaskID(String taskid) { 16 | this.taskid = taskid; 17 | } 18 | 19 | @Inject 20 | TaskStore ts; 21 | 22 | @Override 23 | public void run() { 24 | try { 25 | System.out.println("Running in thread " + Thread.currentThread().getName()); 26 | Task task = new Task(taskid, TaskState.IN_PROGRESS, new Date(), null); 27 | ts.addTask(taskid, task); 28 | long sleep = new Random().nextInt(10000) + 5000; 29 | System.out.println("sleeping for " + sleep); 30 | Thread.sleep(sleep); 31 | System.out.println("Task " + taskid + " completed ......"); 32 | ts.markComplete(taskid); 33 | } catch (Exception e) { 34 | System.out.println("Task " + taskid + " failed ......"); 35 | ts.markFailed(taskid); 36 | } 37 | 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /src/main/java/com/wordpress/abhirockzz/cdi/concutils/JAXRSConfig.java: -------------------------------------------------------------------------------- 1 | package com.wordpress.abhirockzz.cdi.concutils; 2 | 3 | import javax.ws.rs.ApplicationPath; 4 | import javax.ws.rs.core.Application; 5 | 6 | @ApplicationPath("") 7 | public class JAXRSConfig extends Application{ 8 | 9 | } 10 | -------------------------------------------------------------------------------- /src/main/java/com/wordpress/abhirockzz/cdi/concutils/TaskStore.java: -------------------------------------------------------------------------------- 1 | package com.wordpress.abhirockzz.cdi.concutils; 2 | 3 | import com.wordpress.abhirockzz.cdi.concutils.pojo.TaskState; 4 | import com.wordpress.abhirockzz.cdi.concutils.pojo.Task; 5 | import java.util.ArrayList; 6 | import java.util.Date; 7 | import java.util.List; 8 | import java.util.Map; 9 | import java.util.concurrent.ConcurrentHashMap; 10 | import javax.ejb.Lock; 11 | import javax.ejb.LockType; 12 | import javax.inject.Singleton; 13 | 14 | @Singleton 15 | @Lock(LockType.READ) //change the default concurrency semantic 16 | public class TaskStore { 17 | 18 | private Map idToTaskMapping = new ConcurrentHashMap<>(); 19 | 20 | public void addTask(String taskid, Task task) { 21 | idToTaskMapping.put(taskid, task); 22 | } 23 | 24 | public Task getTask(String taskid){ 25 | return idToTaskMapping.get(taskid); 26 | } 27 | public void markComplete(String taskid){ 28 | Task task = idToTaskMapping.get(taskid); 29 | task.setEnd(new Date()); 30 | task.setState(TaskState.COMPLETED); 31 | } 32 | 33 | public void markFailed(String taskid){ 34 | Task task = idToTaskMapping.get(taskid); 35 | task.setEnd(new Date()); 36 | task.setState(TaskState.FAILED); 37 | } 38 | 39 | public List getAllTasks() { 40 | List tasks = new ArrayList<>(); 41 | for(Task task : idToTaskMapping.values()){ 42 | tasks.add(task); 43 | } 44 | return tasks; 45 | 46 | } 47 | 48 | public TaskState getStatusForTask(String taskid) { 49 | return idToTaskMapping.get(taskid).getState(); 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /src/main/java/com/wordpress/abhirockzz/cdi/concutils/TasksResource.java: -------------------------------------------------------------------------------- 1 | package com.wordpress.abhirockzz.cdi.concutils; 2 | 3 | import com.wordpress.abhirockzz.cdi.concutils.pojo.AllTasks; 4 | import java.util.UUID; 5 | import javax.annotation.Resource; 6 | import javax.enterprise.concurrent.ManagedExecutorService; 7 | import javax.inject.Inject; 8 | import javax.ws.rs.GET; 9 | import javax.ws.rs.POST; 10 | import javax.ws.rs.Path; 11 | import javax.ws.rs.PathParam; 12 | import javax.ws.rs.Produces; 13 | import javax.ws.rs.container.AsyncResponse; 14 | import javax.ws.rs.container.Suspended; 15 | import javax.ws.rs.core.MediaType; 16 | import javax.ws.rs.core.Response; 17 | 18 | @Path("tasks") 19 | public class TasksResource { 20 | 21 | @Resource 22 | private ManagedExecutorService mes; 23 | 24 | @Inject 25 | private BackgroundTask ht; 26 | 27 | @POST 28 | public void postTask(@Suspended final AsyncResponse resp) { 29 | String taskid = UUID.randomUUID().toString(); 30 | ht.setTaskID(taskid); 31 | 32 | mes.submit(ht); 33 | System.out.println("submitted task " + taskid); 34 | resp.resume(Response.accepted(taskid).build()); 35 | } 36 | 37 | @Inject 38 | private TaskStore ts; 39 | 40 | @Path("{taskid}") 41 | @GET 42 | @Produces(MediaType.APPLICATION_JSON) 43 | public Response getTask(@PathParam("taskid") String taskid) { 44 | return Response.ok(ts.getTask(taskid)).build(); 45 | } 46 | 47 | @GET 48 | @Path("all") 49 | @Produces(MediaType.APPLICATION_JSON) 50 | public Response getAllTasks() { 51 | return Response.ok(new AllTasks(ts.getAllTasks())).build(); 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /src/main/java/com/wordpress/abhirockzz/cdi/concutils/pojo/AllTasks.java: -------------------------------------------------------------------------------- 1 | package com.wordpress.abhirockzz.cdi.concutils.pojo; 2 | 3 | import java.util.List; 4 | import javax.xml.bind.annotation.XmlAccessType; 5 | import javax.xml.bind.annotation.XmlAccessorType; 6 | import javax.xml.bind.annotation.XmlRootElement; 7 | 8 | @XmlRootElement 9 | @XmlAccessorType(XmlAccessType.FIELD) 10 | public class AllTasks { 11 | List tasks; 12 | 13 | public AllTasks() { 14 | 15 | } 16 | 17 | public AllTasks(List tasks) { 18 | this.tasks = tasks; 19 | } 20 | 21 | 22 | } 23 | -------------------------------------------------------------------------------- /src/main/java/com/wordpress/abhirockzz/cdi/concutils/pojo/Task.java: -------------------------------------------------------------------------------- 1 | package com.wordpress.abhirockzz.cdi.concutils.pojo; 2 | 3 | import java.util.Date; 4 | import javax.xml.bind.annotation.XmlAccessType; 5 | import javax.xml.bind.annotation.XmlAccessorType; 6 | import javax.xml.bind.annotation.XmlRootElement; 7 | 8 | @XmlRootElement 9 | @XmlAccessorType(XmlAccessType.FIELD) 10 | public class Task { 11 | private String taskid; 12 | private TaskState state; 13 | private Date start; 14 | private Date end; 15 | 16 | public Task() { 17 | } 18 | 19 | 20 | public Task(String taskid, TaskState state, Date start, Date end) { 21 | this.taskid = taskid; 22 | this.state = state; 23 | this.start = start; 24 | this.end = end; 25 | } 26 | 27 | @Override 28 | public String toString() { 29 | return "Task{" + "taskid=" + taskid + ", state=" + state + ", start=" + start + ", end=" + end + '}'; 30 | } 31 | 32 | public void setEnd(Date end) { 33 | this.end = end; 34 | } 35 | 36 | public TaskState getState() { 37 | return state; 38 | } 39 | 40 | public void setState(TaskState state) { 41 | this.state = state; 42 | } 43 | 44 | 45 | 46 | } 47 | -------------------------------------------------------------------------------- /src/main/java/com/wordpress/abhirockzz/cdi/concutils/pojo/TaskState.java: -------------------------------------------------------------------------------- 1 | package com.wordpress.abhirockzz.cdi.concutils.pojo; 2 | 3 | public enum TaskState { 4 | COMPLETED, IN_PROGRESS, FAILED; 5 | } 6 | -------------------------------------------------------------------------------- /src/main/webapp/WEB-INF/beans.xml: -------------------------------------------------------------------------------- 1 | 2 | 6 | 7 | --------------------------------------------------------------------------------