/// <summary>Submit tasks using a JPPFExecutorService</summary> /// <param name="client">The JPPF client connected to the server</param> public static void SubmitWithExecutor(JPPFClient client) { JPPFExecutorService executor = new JPPFExecutorService(client); // send tasks 1 at a time executor.setBatchSize(1); IList <Future> futures = new List <Future>(); for (int i = 0; i < 3; i++) { futures.Add(executor.Submit(new MyDotnetTask(100))); } // process the results in the order the tasks were submitted foreach (Future future in futures) { // 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(); }
/// <summary>Register a listener for jobs life cycle notifications emitted by the server</summary> /// <param name="client">The JPPF client from which to get a JMX connection</param> public static void RegisterJobNotificationListener(JPPFClient client) { JMXDriverConnectionWrapper jmx = GetJMXConnection(client); AbstractMBeanStaticProxy jobProxy = new DriverJobManagementMBeanStaticProxy(jmx); jobProxy.AddNotificationListener(new MyJobNotificationListener(), "job notification"); }
/// <summary>Print the number of nodes connected to the server</summary> /// <param name="client">The JPPF client connected to the server</param> public static void PrintNbNodes(JPPFClient client) { JMXDriverConnectionWrapper jmx = GetJMXConnection(client); java.lang.Integer n = jmx.nbNodes(); int nbNodes = (n == null) ? 0 : n.intValue(); Console.WriteLine("there are " + nbNodes + " nodes"); }
/// <summary>Obtain a JMX connection from a JPPF client</summary> /// <param name="client">The client to get the JMX connection from</param> /// <returns>An instance of <code>JMXDriverConnectionWrapper</code></returns> public static JMXDriverConnectionWrapper GetJMXConnection(JPPFClient client) { if (jmx == null) { // wait for a connection pool to be ready JPPFConnectionPool pool = client.awaitWorkingConnectionPool(); // wait until at least one JMX connection is available from the pool java.util.List list = pool.awaitJMXConnections(Operator.AT_LEAST, 1, true); // return the first available JMX connection jmx = (JMXDriverConnectionWrapper)list.get(0); } return(jmx); }
/// <summary>Register a listener for tasks completion notifications emitted by all the nodes</summary> /// <param name="client">The JPPF client from which to get a JMX connection</param> /// <returns>An id string assigned to the registered listener</returns> public static string RegisterTaskNotificationListener(JPPFClient client) { // name of the node task monitor MBean string mbeanName = JPPFNodeTaskMonitorMBeanStaticProxy.getMBeanName(); // only receive notifications from .Net-capable nodes ExecutionPolicy dotnetPolicy = new Equal("jppf.dotnet.bridge.initialized", true); NodeSelector selector = new ExecutionPolicySelector(dotnetPolicy); // register the forwarding listener with the driver JMXDriverConnectionWrapper jmx = GetJMXConnection(client); string listenerId = jmx.RegisterFowrwardingNotificationListener(selector, mbeanName, new MyTaskNotificationListener(), "task notification"); Console.WriteLine("registered task notifications listener with listenerId = " + listenerId); // return the listener id so it can be eventually removed later return(listenerId); }
/// <summary>Provision the specified number of slave nodes. /// This method forwards the provisioning request to all relevant nodes via the driver</summary> /// <param name="client">The JPPF client connected to the server</param> /// <param name="nbNodes">The number of slave nodes to provision</param> public static void ProvisionNodes(JPPFClient client, int nbNodes) { // we are invoking the remote provisioning mbeans string mbeanName = JPPFNodeProvisioningMBeanStaticProxy.getMBeanName(); // policy applied on master nodes with a .Net bridge initialized ExecutionPolicy masterPolicy = new Equal("jppf.node.provisioning.master", true).and(new Equal("jppf.dotnet.bridge.initialized", true)); NodeSelector masterSelector = new ExecutionPolicySelector(masterPolicy); // parameters to the MBean method java.lang.Object[] parameters = { new java.lang.Integer(nbNodes), null }; // Java signature of the remote MBean method to invoke java.lang.String[] signature = { "int", "org.jppf.utils.TypedProperties" }; // send the request via the forwarding mbean JMXDriverConnectionWrapper jmx = GetJMXConnection(client); JPPFNodeForwardingMBeanStaticProxy proxy = new JPPFNodeForwardingMBeanStaticProxy(jmx); proxy.forwardInvoke(masterSelector, mbeanName, "provisionSlaveNodes", parameters, signature); }
/// <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(); }
static void Main(string[] args) { JPPFClient client = null; TopologyManager manager = null; JobMonitor jobMonitor = null; try { // initialize the .Net bridge with verbose/quiet mode JPPFDotnet.Init(false); // initialize a topology manager and register a listener for topology events manager = new DotnetTopologyManager(new MyTopologyListener()); // initialize a topology manager and register a listener for topology events jobMonitor = new DotnetJobMonitor(manager, new MyJobMonitoringListener()); // wait until the topology manager and job monitor are fully initialized while (jobMonitor.getJobDrivers().size() <= 0) { Thread.Sleep(10); } JobDriver jobDriver = jobMonitor.getJobDrivers().get(0) as JobDriver; while (jobDriver.getTopologyDriver().getChildCount() <= 0) { Thread.Sleep(10); } // initialize the JPPF client client = manager.getJPPFClient(); // print the number of nodes connected to the server PrintNbNodes(client); // provision a slave node for each .Net-capable master node //ProvisionNodes(client, 1); // subscribe to job notifications emitted by the JPPF server //RegisterJobNotificationListener(client); // subscribe to task completion notifications emitted by the JPPF nodes RegisterTaskNotificationListener(client); // uncomment to test an executor service //SubmitWithExecutor(client); // uncomment to test a completion service //SubmitWithCompletionService(client); JPPFJob job = new JPPFJob(); job.setName(".NET job"); // execute the job only on nodes which successfully initialized the .Net bridge job.getSLA().setExecutionPolicy(new Equal("jppf.dotnet.bridge.initialized", true)); int n = 5; for (int i = 0; i < n; i++) { job.add(new MyDotnetTask(1000)).setId("task " + (i + 1)); } MyDotnetTask myTask = new MyDotnetTask(3000); // this .Net task will time out after 1.5 second myTask.TimeoutSchedule = new JPPFSchedule(1500); job.add(myTask).setId("task " + (n + 1)); // alternatively: job.add(new MyDotnetTask(3000)).setTimeoutSchedule(new JPPFSchedule(1500)); // add a job listner that prints job events to the console job.addJobListener(new MyJobListener()); Console.WriteLine("created job"); // submit the job to the grid and get the execution results java.util.List results = client.submitJob(job); Console.WriteLine("got job results"); for (int i = 0; i < results.size(); i++) { Task task = (Task)results.get(i); //BaseDotnetTask dotnetTask = job.asBaseDotnetTask(task); BaseDotnetTask dotnetTask = task.AsBaseDotnetTask(); if (dotnetTask != null) // if .Net task { if (dotnetTask.Exception != null) { Console.WriteLine("got exception for task " + dotnetTask + " : " + dotnetTask.Exception); Console.WriteLine(dotnetTask.Exception.StackTrace); } else if (dotnetTask.Result != null) { Console.WriteLine("got result for task " + dotnetTask + " : " + dotnetTask.Result); } else { Console.WriteLine("no result or exception for task " + dotnetTask); } } } } catch (Exception e) { Console.WriteLine(e.ToString()); } if (!Console.IsInputRedirected && !Console.IsOutputRedirected) { Console.WriteLine("Please press ESC to terminate"); do { while (!Console.KeyAvailable) { } } while (Console.ReadKey(true).Key != ConsoleKey.Escape); } else { Console.WriteLine("Waiting 5 seconds ..."); Thread.Sleep(5000); } if (client != null) { client.close(); } }
/// <summary>Initialize this topology manager with the specified intervals, client and listeners</summary> /// <param name="topologyRefreshInterval">the interval in millis between refreshes of the topology</param> /// <param name="jvmHealthRefreshInterval">the interval in millis between refreshes of the JVM health data</param> /// <param name="client">the JPPF client used internally</param> /// <param name="listeners">the listeners to register</param> public DotnetTopologyManager(long topologyRefreshInterval, long jvmHealthRefreshInterval, JPPFClient client, params BaseDotnetTopologyListener[] listeners) : base(topologyRefreshInterval, jvmHealthRefreshInterval, client, ToJavaListeners(listeners)) { }
/// <summary>Initialize this topology manager with the specified client and listeners. /// The refresh intervals are determined from the configuration, or take a default value of 1000L if they are not configured</summary> /// <param name="client">the JPPF client used internally</param> /// <param name="listeners">the listeners to register</param> public DotnetTopologyManager(JPPFClient client, params BaseDotnetTopologyListener[] listeners) : this(Config.getLong("jppf.admin.refresh.interval.topology", 1000L), Config.getLong("jppf.admin.refresh.interval.health", 1000L), client, listeners) { }