// Path: projects/05_concurrency/managed-executor-service/src/main/java/org/jboss/as/quickstarts/managedexecutorservice/concurrency/DeleteTask.java
// public class DeleteTask implements Runnable {
// 
//     @Inject
//     private Logger log;
// 
//     @Inject
//     private EntityManager entityManager;
// 
//     @Inject
//     private UserTransaction userTransaction;
// 
//     @Override
//     public void run() {
//         try {
//             log.info("Begin transaction");
//             userTransaction.begin();
//             log.info("Deleting all products");
//             int qtd = entityManager.createQuery("DELETE FROM Product").executeUpdate();
//             log.info("Commit transaction. Products deleted: " + qtd);
//             userTransaction.commit();
//         } catch (Exception e) {
//             log.info("Exception: " + e.getMessage());
//             try {
//                 if (userTransaction != null) {
//                     log.info("Rollback transaction");
//                     userTransaction.rollback();
//                 }
//             } catch (SystemException e1) {
//                 log.info("Failed to rollback transaction: " + e1.getMessage());
//             }
//         }
// 
//     }
// 
// }
// 
// Path: projects/05_concurrency/managed-executor-service/src/main/java/org/jboss/as/quickstarts/managedexecutorservice/concurrency/LongRunningTask.java
// public class LongRunningTask implements Callable<Integer> {
// 
//     @Inject
//     private Logger log;
// 
//     @Inject
//     private EntityManager entityManager;
// 
//     @Override
//     @SuppressWarnings("unchecked")
//     public Integer call() throws Exception {
//         // here we simulate an access to Products listing
//         List<Product> products = (List<Product>) entityManager.createQuery("SELECT p FROM Product p").getResultList();
//         log.info("Starting a long running task");
//         for (Product product : products) {
//             log.info("Analysing " + product.getName());
//             // We simulate now a long running task
//             Thread.sleep(3000);
//         }
//         // after executing a long running task we return a result
//         return (int) (Math.random() * 100);
//     }
// 
// }
// 
// Path: projects/05_concurrency/managed-executor-service/src/main/java/org/jboss/as/quickstarts/managedexecutorservice/concurrency/PersitTask.java
// public class PersitTask implements Runnable {
// 
//     @Inject
//     private Logger log;
// 
//     @Inject
//     private EntityManager entityManager;
// 
//     @Inject
//     private UserTransaction userTransaction;
// 
//     private Product product;
// 
//     public void setProduct(Product product) {
//         this.product = product;
//     }
// 
//     @Override
//     public void run() {
//         try {
//             log.info("Begin transaction");
//             userTransaction.begin();
//             log.info("Persisting a new product");
//             entityManager.persist(product);
//             log.info("Commit transaction");
//             userTransaction.commit();
//         } catch (Exception e) {
//             log.info("Exception: " + e.getMessage());
//             try {
//                 if (userTransaction != null) {
//                     log.info("Rollback transaction");
//                     userTransaction.rollback();
//                 }
//             } catch (SystemException e1) {
//                 log.info("Failed to rollback transaction: " + e1.getMessage());
//             }
//         }
// 
//     }
// 
// }


import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.logging.Logger;
import javax.annotation.Resource;
import javax.enterprise.concurrent.ManagedExecutorService;
import javax.enterprise.inject.Instance;
import javax.inject.Inject;
import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import org.jboss.as.quickstarts.managedexecutorservice.concurrency.DeleteTask;
import org.jboss.as.quickstarts.managedexecutorservice.concurrency.LongRunningTask;
import org.jboss.as.quickstarts.managedexecutorservice.concurrency.PersitTask;
import org.jboss.as.quickstarts.managedexecutorservice.model.Product;
/*
 * JBoss, Home of Professional Open Source
 * Copyright 2015, Red Hat, Inc. and/or its affiliates, and individual
 * contributors by the @authors tag. See the copyright.txt in the
 * distribution for a full listing of individual contributors.
 *
 * Licensed 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
 * http://www.apache.org/licenses/LICENSE-2.0
 * 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.
 */
package org.jboss.as.quickstarts.managedexecutorservice.rest;




@Path("/products")
public class ProductResourceRESTService {

    @Inject
    private Logger log;

    @Resource
    private ManagedExecutorService managedExecutorService;

    // Here we use Instance so PersistTask can have CDI injections available
    @Inject
    private Instance<PersitTask> persisTaskInstance;

    // Here we use Instance so LongRunningTask can have CDI injections available
    @Inject
    private Instance<LongRunningTask> longRunningTaskIntance;

    // Here we use Instance so DeleteTask can have CDI injections available
    @Inject
// Path: projects/05_concurrency/managed-executor-service/src/main/java/org/jboss/as/quickstarts/managedexecutorservice/concurrency/DeleteTask.java
// public class DeleteTask implements Runnable {
// 
//     @Inject
//     private Logger log;
// 
//     @Inject
//     private EntityManager entityManager;
// 
//     @Inject
//     private UserTransaction userTransaction;
// 
//     @Override
//     public void run() {
//         try {
//             log.info("Begin transaction");
//             userTransaction.begin();
//             log.info("Deleting all products");
//             int qtd = entityManager.createQuery("DELETE FROM Product").executeUpdate();
//             log.info("Commit transaction. Products deleted: " + qtd);
//             userTransaction.commit();
//         } catch (Exception e) {
//             log.info("Exception: " + e.getMessage());
//             try {
//                 if (userTransaction != null) {
//                     log.info("Rollback transaction");
//                     userTransaction.rollback();
//                 }
//             } catch (SystemException e1) {
//                 log.info("Failed to rollback transaction: " + e1.getMessage());
//             }
//         }
// 
//     }
// 
// }
// 
// Path: projects/05_concurrency/managed-executor-service/src/main/java/org/jboss/as/quickstarts/managedexecutorservice/concurrency/LongRunningTask.java
// public class LongRunningTask implements Callable<Integer> {
// 
//     @Inject
//     private Logger log;
// 
//     @Inject
//     private EntityManager entityManager;
// 
//     @Override
//     @SuppressWarnings("unchecked")
//     public Integer call() throws Exception {
//         // here we simulate an access to Products listing
//         List<Product> products = (List<Product>) entityManager.createQuery("SELECT p FROM Product p").getResultList();
//         log.info("Starting a long running task");
//         for (Product product : products) {
//             log.info("Analysing " + product.getName());
//             // We simulate now a long running task
//             Thread.sleep(3000);
//         }
//         // after executing a long running task we return a result
//         return (int) (Math.random() * 100);
//     }
// 
// }
// 
// Path: projects/05_concurrency/managed-executor-service/src/main/java/org/jboss/as/quickstarts/managedexecutorservice/concurrency/PersitTask.java
// public class PersitTask implements Runnable {
// 
//     @Inject
//     private Logger log;
// 
//     @Inject
//     private EntityManager entityManager;
// 
//     @Inject
//     private UserTransaction userTransaction;
// 
//     private Product product;
// 
//     public void setProduct(Product product) {
//         this.product = product;
//     }
// 
//     @Override
//     public void run() {
//         try {
//             log.info("Begin transaction");
//             userTransaction.begin();
//             log.info("Persisting a new product");
//             entityManager.persist(product);
//             log.info("Commit transaction");
//             userTransaction.commit();
//         } catch (Exception e) {
//             log.info("Exception: " + e.getMessage());
//             try {
//                 if (userTransaction != null) {
//                     log.info("Rollback transaction");
//                     userTransaction.rollback();
//                 }
//             } catch (SystemException e1) {
//                 log.info("Failed to rollback transaction: " + e1.getMessage());
//             }
//         }
// 
//     }
// 
// }


// Path: projects/05_concurrency/managed-executor-service/src/main/java/org/jboss/as/quickstarts/managedexecutorservice/rest/ProductResourceRESTService.java
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.logging.Logger;
import javax.annotation.Resource;
import javax.enterprise.concurrent.ManagedExecutorService;
import javax.enterprise.inject.Instance;
import javax.inject.Inject;
import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import org.jboss.as.quickstarts.managedexecutorservice.concurrency.DeleteTask;
import org.jboss.as.quickstarts.managedexecutorservice.concurrency.LongRunningTask;
import org.jboss.as.quickstarts.managedexecutorservice.concurrency.PersitTask;
import org.jboss.as.quickstarts.managedexecutorservice.model.Product;

/*
 * JBoss, Home of Professional Open Source
 * Copyright 2015, Red Hat, Inc. and/or its affiliates, and individual
 * contributors by the @authors tag. See the copyright.txt in the
 * distribution for a full listing of individual contributors.
 *
 * Licensed 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
 * http://www.apache.org/licenses/LICENSE-2.0
 * 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.
 */
package org.jboss.as.quickstarts.managedexecutorservice.rest;




@Path("/products")
public class ProductResourceRESTService {

    @Inject
    private Logger log;

    @Resource
    private ManagedExecutorService managedExecutorService;

    // Here we use Instance so PersistTask can have CDI injections available
    @Inject
    private Instance<PersitTask> persisTaskInstance;

    // Here we use Instance so LongRunningTask can have CDI injections available
    @Inject
    private Instance<LongRunningTask> longRunningTaskIntance;

    // Here we use Instance so DeleteTask can have CDI injections available
    @Inject
    private Instance<DeleteTask> deleteTaskInstance;