/// <summary>Submit a .Net task to the specified completion service</summary> /// <param name="executor">The compeletion service to submit the task to</param> /// <param name="task">The task to submit</param> /// <returns>A <c>Future</c> instance</returns> public static Future Submit(this JPPFCompletionService completionService, BaseDotnetTask task) { DotnetSerializer ser = new DotnetSerializer(); byte[] bytes = ser.Serialize(task); DotnetTaskWrapper dtw = new DotnetTaskWrapper(bytes); //if (task.TimeoutSchedule != null) dtw.setTimeoutSchedule(task.TimeoutSchedule); dtw.setLoggingEnabled(false); return(completionService.submit(dtw, dtw)); }
/// <summary>Submit tasks using a JPPFCompletionService</summary> /// <param name="client">The JPPF client connected to the server</param> public static void SubmitWithCompletionService(JPPFClient client) { JPPFExecutorService executor = new JPPFExecutorService(client); JPPFCompletionService completionService = new JPPFCompletionService(executor); // send tasks 3 at a time executor.setBatchSize(3); for (int i = 0; i < 3; i++) { completionService.Submit(new MyDotnetTask(100)); } // process the results in the order in which they arrive int count = 0; while (count < 3) { // get the next completed task Future future = completionService.poll(); count++; // future.get() returns the value of myTask.Result after execution // or throws an eventual exception that was raised try { object result = future.get(); if (result != null) { Console.WriteLine("[executor service] got result = " + result); } else { Console.WriteLine("[executor service] no result or exception"); } } catch (Exception e) { Console.WriteLine("[executor service] exception during execution: " + e.ToString()); } } executor.shutdownNow(); }