add example java client

Create a gearman example client to show how to initiate a build
with the jenkins gearman plugin.

Change-Id: I62e8688ea5aada5a4211e651509b3263b1b9c449
This commit is contained in:
zaro0508 2013-03-19 10:56:41 -07:00
parent c461e204f2
commit 6162acb78b
1 changed files with 81 additions and 0 deletions

View File

@ -0,0 +1,81 @@
/*
*
* Copyright 2013 Hewlett-Packard Development Company, L.P.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package hudson.plugins.gearman.example;
import java.util.UUID;
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.common.GearmanNIOJobServerConnection;
import org.gearman.util.ByteUtils;
/**
* A java example of how to start a jenkins job using a Gearman client
*
* @author Khai Do
*/
public class StartJobClient {
/**
* @param args
*/
public static void main(String[] args) {
// setup connection settings
String host = "127.0.0.1";
int port = 4730;
// setup job parameters
String function = "build:pep8:precise";
String uniqueId = UUID.randomUUID().toString();
String params = "{param1:red, param2:white, param3:blue}";
// connect client to server
GearmanClient client = new GearmanClientImpl();
GearmanNIOJobServerConnection conn = new GearmanNIOJobServerConnection(host, port);
client.addJobServer(conn);
// send job request
byte[] data = ByteUtils.toUTF8Bytes(params);
GearmanJob job = GearmanJobImpl.createJob(function, data, uniqueId);
client.submit(job);
// get results
String value = "";
GearmanJobResult res = null;
try {
res = job.get();
value = ByteUtils.fromUTF8Bytes(res.getResults());
System.out.println("Job Result: "+value);
} catch (Exception e) {
e.printStackTrace();
}
// close client
client.shutdown();
}
}