summaryrefslogtreecommitdiffstats
path: root/gearman/controllerWorker/ControllerWorker/SomePingWorker.java
diff options
context:
space:
mode:
authorBjörn Geiger2011-07-26 15:02:30 +0200
committerBjörn Geiger2011-07-26 15:02:30 +0200
commit64b2ead01f1543c5e94228893754a417174652af (patch)
tree90340cf42c3931893237a615438bb28608540b06 /gearman/controllerWorker/ControllerWorker/SomePingWorker.java
parentminor (diff)
downloadpoolctrl-64b2ead01f1543c5e94228893754a417174652af.tar.gz
poolctrl-64b2ead01f1543c5e94228893754a417174652af.tar.xz
poolctrl-64b2ead01f1543c5e94228893754a417174652af.zip
ordner java in controllerWorker unbenannt
Diffstat (limited to 'gearman/controllerWorker/ControllerWorker/SomePingWorker.java')
-rw-r--r--gearman/controllerWorker/ControllerWorker/SomePingWorker.java95
1 files changed, 95 insertions, 0 deletions
diff --git a/gearman/controllerWorker/ControllerWorker/SomePingWorker.java b/gearman/controllerWorker/ControllerWorker/SomePingWorker.java
new file mode 100644
index 0000000..d37745c
--- /dev/null
+++ b/gearman/controllerWorker/ControllerWorker/SomePingWorker.java
@@ -0,0 +1,95 @@
+package ControllerWorker;
+
+import java.util.*;
+import java.util.concurrent.ExecutionException;
+
+import org.gearman.client.GearmanClient;
+import org.gearman.client.GearmanClientImpl;
+import org.gearman.client.GearmanJob;
+import org.gearman.client.GearmanJobImpl;
+import org.gearman.client.GearmanJobResult;
+import org.gearman.client.GearmanJobResultImpl;
+import org.gearman.common.GearmanJobServerConnection;
+import org.gearman.common.GearmanNIOJobServerConnection;
+import org.gearman.util.ByteUtils;
+import org.gearman.worker.AbstractGearmanFunction;
+
+import org.json.simple.JSONArray;
+import org.json.simple.JSONValue;
+
+public class SomePingWorker extends AbstractGearmanFunction {
+ @Override
+ public String getName() {
+ return "somePing";
+ }
+
+ @Override
+ public GearmanJobResult executeFunction() {
+ final GearmanJobServerConnection connection = new GearmanNIOJobServerConnection(
+ "127.0.0.1", 4730);
+ GearmanClient client = new GearmanClientImpl();
+ client.addJobServer(connection);
+ String data = ByteUtils.fromUTF8Bytes((byte[]) this.data);
+ JSONArray jsonArray = (JSONArray) JSONValue.parse(data);
+ int count = 0;
+ String res = "";
+
+ List<GearmanJob> jobs = new ArrayList<GearmanJob>();
+
+ for (Object ipObj : jsonArray) {
+ String ip = ipObj.toString();
+ System.out.println("Ping " + ip);
+ GearmanJob job = GearmanJobImpl.createJob("ping", ip.getBytes(),
+ "pingJob");
+ client.submit(job);
+ jobs.add(job);
+ count++;
+ }
+
+ Boolean exit=false;
+ while (!exit) {
+ ListIterator<GearmanJob> it = jobs.listIterator();
+ while (it.hasNext()) {
+
+ GearmanJob j = it.next();
+ if (j.isDone()) {
+ System.out.println("job done..");
+ GearmanJobResult jobRes;
+ try {
+ jobRes = j.get();
+ if (count == 0) {
+ res += ByteUtils.fromUTF8Bytes(jobRes.getResults());
+ } else {
+ res += "; " + ByteUtils.fromUTF8Bytes(jobRes.getResults());
+ }
+ } catch (InterruptedException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ } catch (ExecutionException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ }
+ } else {
+ System.out.println("job not yet done ....");
+ }
+/* try {
+ GearmanJobResult jobRes = j.get();
+ if (count == 0) {
+ res += ByteUtils.fromUTF8Bytes(jobRes.getResults());
+ } else {
+ res += "; " + ByteUtils.fromUTF8Bytes(jobRes.getResults());
+ }
+ } catch (Exception e) {
+ e.printStackTrace();
+ }*/
+ }
+ }
+ byte[] warnings = new byte[0];
+ byte[] exceptions = new byte[0];
+ int numerator = 0;
+ int denominator = 0;
+ GearmanJobResult gjr = new GearmanJobResultImpl(this.jobHandle, true,
+ res.getBytes(), warnings, exceptions, numerator, denominator);
+ return gjr;
+ }
+} \ No newline at end of file