This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package no.nilsapp.someapp.cronjobs; | |
import com.google.appengine.api.ThreadManager; | |
import java.io.IOException; | |
import java.util.List; | |
import java.util.concurrent.ExecutorService; | |
import java.util.concurrent.Executors; | |
import java.util.concurrent.ThreadFactory; | |
import java.util.concurrent.TimeUnit; | |
public class AppengineExecutor { | |
public void run() throws IOException { | |
ThreadFactory factory = ThreadManager.currentRequestThreadFactory(); | |
ExecutorService executor = Executors.newCachedThreadPool(factory); | |
List<Shipment> shipmentsToCheck = dataStorage.getShipmentsNotDelivered(); | |
System.out.println("Checking shipments " + shipmentsToCheck.size()); | |
for (Shipment shipment : shipmentsToCheck) { | |
// please note that "MyRunnable" implements Runnable and cannot directly extend Thread. | |
Runnable checkShipmentTask = ThreadManager.createThreadForCurrentRequest(new MyRunnable(shipment)); | |
executor.execute(checkShipmentTask); | |
} | |
System.out.println("Sent all jobs to execution."); | |
executor.shutdown(); | |
try { | |
executor.awaitTermination(10, TimeUnit.SECONDS); | |
} catch (InterruptedException e) { | |
e.printStackTrace(); | |
} | |
} | |
} |