@Override public void registerCallback(String functionName, GearmanFunction function) { for(GearmanWorkerThread t : threadSet) { t.getWorker().registerCallback(functionName, function); } }
@Override public void run() { worker.doWork(); } }
@Override public String submitJobInBackground(String callback, byte[] data) throws JobSubmissionException { return submitJobInBackground(callback, data, JobPriority.NORMAL); }
public static void main(String... args) { try { byte data[] = "This is a test".getBytes(); NetworkGearmanWorker worker = new NetworkGearmanWorker.Builder() .withConnection(new Connection("localhost", 4730)) .build(); worker.registerCallback("reverse", new ReverseFunction()); worker.doWork(); } catch (Exception e) { LOG.error("oops!"); } } }
private void initialize() { for(int i = 0; i < threadCount; i++) { NetworkGearmanWorker.Builder workerBuilder = new NetworkGearmanWorker.Builder(); for(Connection c : connectionList) { workerBuilder.withConnection(new Connection(c)); } NetworkGearmanWorker worker = workerBuilder.build(); threadSet.add(new GearmanWorkerThread(worker)); workerSet.add(worker); } }
@Override public void run() { NetworkGearmanWorker.Builder builder = new NetworkGearmanWorker.Builder(); for(Connection c : connections) { builder.withConnection(c); } worker = builder.build(); for(String function : functions.keySet()) { worker.registerCallback(function, functions.get(function)); } worker.doWork(); }
@Override public void run() { worker = new NetworkGearmanWorker.Builder() .withHostPort("localhost", SERVER_ONE_PORT) .build(); worker.registerCallback("reverse", new ReverseFunc()); worker.doWork(); }
NetworkGearmanClient client = new NetworkGearmanClient("localhost", 4730); client.addHostToList("localhost", 4731); client.registerEventListener(eventListener); byte[] result = client.submitJob("reverse", data, JobPriority.NORMAL); System.err.println("Result: " + new String(result)); } catch (WorkException e) {
public static void main(String... args) { try { byte data[] = "This is a test".getBytes(); NetworkGearmanWorkerPool workerPool = new NetworkGearmanWorkerPool.Builder() .threads(2) .withConnection(new Connection("localhost", 4730)) .build(); workerPool.registerCallback("reverse", new ReverseFunction()); workerPool.doWork(); } catch (Exception e) { LOG.error("Error: ", e); } } }
@Override public String submitFutureJob(String callback, byte[] data, Date whenToRun) throws NoServersAvailableException { String uniqueID = UUID.randomUUID().toString(); try { ServerResponse response = sendJobPacket(new SubmitJob(callback, uniqueID, data, whenToRun)); if(response != null) { LOG.debug("Sent future job request to " + response.getConnection()); // If we get back a JOB_CREATED packet, we can continue, // otherwise try the next job manager if (response.getPacket().getType() == PacketType.JOB_CREATED) { String jobHandle = ((JobCreated)response.getPacket()).getJobHandle(); LOG.debug("Created future job %s\n", jobHandle); return jobHandle; } } } catch (NoServersAvailableException nsae) { LOG.warn("No servers available to submit the job."); throw nsae; } return null; }
@Override public void doWork() { int counter = 0; for(GearmanWorkerThread t : threadSet) { LOG.debug("Starting worker #" + counter++); t.start(); } for(GearmanWorkerThread t : threadSet) { try { t.join(); LOG.debug("Thread " + t.getId() + " joined."); } catch (InterruptedException e) { LOG.error("Error waiting for worker thread: ", e); } } }
public void addHostToList(String host) throws IOException { this.addHostToList(host, 4730); }
@Override public byte[] submitJob(String callback, byte[] data) throws JobSubmissionException, WorkException { return submitJob(callback, data, JobPriority.NORMAL); }
public void testTwoNodeHazelcastCluster() throws InterruptedException, IOException, JobSubmissionException, WorkException { WorkerRunner workerRunner = new WorkerRunner(); Thread t3 = new Thread(workerRunner); t3.start(); byte[] jobData = {'H','E','L','L','O'}; byte[] reversedData = jobData.clone(); ArrayUtils.reverse(reversedData); GearmanClient client = new NetworkGearmanClient("localhost", SERVER_TWO_PORT); byte[] result = client.submitJob("reverse", jobData); Assert.assertArrayEquals(result, reversedData); LOG.debug(String.format("Result: %s", new String(result))); workerRunner.stop(); t3.join(); }
public Builder() { this.worker = new NetworkGearmanWorker(); }
public Builder() { this.pool = new NetworkGearmanWorkerPool(); }
@Override public void registerCallback(String method, GearmanFunction function) { callbacks.put(method, function); broadcastAbility(method); }
private ServerResponse sendJobPacket(SubmitJob jobPacket) throws NoServersAvailableException { Packet result; Connection connection; while ( (connection = connectionPool.getConnection()) != null) { try { connection.sendPacket(jobPacket); LOG.debug("Sent job request to " + connection.toString()); // We need to get back a JOB_CREATED packet result = connection.getNextPacket(); // If we get back a JOB_CREATED packet, we can continue // otherwise try the next job manager if (result != null && result.getType() == PacketType.JOB_CREATED) { LOG.debug("Created job " + ((JobCreated) result).getJobHandle()); return new ServerResponse(connection, result); } } catch (IOException ioe) { LOG.error("Connection to " + connection.toString() + " flaky, marking as bad."); } } // The only way we get here is if connection == null, by which point we would have been // thrown a NoServersAvailableException() throw new NoServersAvailableException(); }
public String submitJobInBackground(String callback, byte[] data, JobPriority priority) throws NoServersAvailableException { String jobid = UUID.randomUUID().toString(); try { ServerResponse response = sendJobPacket(new SubmitJob(callback, jobid, data, true, priority)); if(response != null) { LOG.debug("Sent background job request to " + response.getConnection()); // If we get back a JOB_CREATED packet, we can continue, // otherwise try the next job manager if (response.getPacket().getType() == PacketType.JOB_CREATED) { String jobHandle = ((JobCreated)response.getPacket()).getJobHandle(); LOG.debug(format("Created background job %s, with priority %s\n", jobHandle, priority.toString())); return jobHandle; } } } catch (NoServersAvailableException nsae) { LOG.warn("No servers available to submit the job."); throw nsae; } return null; }
public NetworkGearmanWorker build() { return new NetworkGearmanWorker(worker); }