From ea4b8bac9cd00be18be202285732d12cc7d0520e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=A8=E4=BF=8A=E6=98=8E?= Date: Sat, 25 Jun 2016 10:25:43 +0800 Subject: [PATCH 1/2] add client and worker demo --- pom.xml | 312 +++++++++++++++++++++++++++++--------------------------- 1 file changed, 159 insertions(+), 153 deletions(-) diff --git a/pom.xml b/pom.xml index f75f362..4e7dbf6 100644 --- a/pom.xml +++ b/pom.xml @@ -1,167 +1,173 @@ - - + + - org.sonatype.oss - oss-parent - 7 - - + org.sonatype.oss + oss-parent + 7 + + Java Gearman Service http://code.google.com/p/java-gearman-service/ - Java Gearman Service is an easy-to-use distributed network application framework implementing the gearman protocol used to farm out work to other machines or processes that are better suited to do the work. It allows you to do work in parallel, to load balance processing, and to call functions between languages. It can be used in a variety of applications, from high-availability web sites to the transport of database replication events. In other words, it is the nervous system for how distributed processing communicates. + Java Gearman Service is an easy-to-use distributed network application framework implementing the gearman + protocol used to farm out work to other machines or processes that are better suited to do the work. It allows + you to do work in parallel, to load balance processing, and to call functions between languages. It can be used + in a variety of applications, from high-availability web sites to the transport of database replication events. + In other words, it is the nervous system for how distributed processing communicates. 2010 - gearman - http://www.gearman.org - - - - scm:svn:http://java-gearman-service.googlecode.com/svn/trunk - scm:svn:https://java-gearman-service.googlecode.com/svn/trunk - http://java-gearman-service.googlecode.com/svn/trunk - - - 4.0.0 - org.gearman.jgs - java-gearman-service - 0.7.0-SNAPSHOT - jar - - + gearman + http://www.gearman.org + + + + + + + + + 4.0.0 + org.gearman.jgs + java-gearman-service + 0.7.0-SNAPSHOT + jar + + BSD http://opensource.org/licenses/BSD-2-Clause repo - - - [1.6,2.0) - [4.0,5.0) - - UTF-8 - - License.txt - src/main/resources - lib - - scm:svn:http://java-gearman-service.googlecode.com/svn/trunk - http://java-gearman-service.googlecode.com/svn/trunk - - - - - - src/main/resources - true - - - - - - - - maven-compiler-plugin - 3.0 - - 1.7 - 1.7 - - - - - - org.apache.maven.plugins - maven-javadoc-plugin - 2.9 - - org.gearman.* - - - - javadoc - package - - jar - - - - - - - - org.apache.maven.plugins - maven-source-plugin - 2.2.1 - - - attach-sources - package - - jar - - - - - - - maven-assembly-plugin - 2.4 - - true - - src/main/assembly/zip.xml - - - - - assembly - package - - single - - - - - - - org.apache.maven.plugins - maven-scm-plugin - 1.8.1 - - connection - - - - - - - - - - - org.slf4j - slf4j-api - ${slf4j.version} - compile - - - - junit - junit - ${junit.version} - test - - - - org.slf4j - slf4j-simple - ${slf4j.version} - test - - - - + + + [1.6,2.0) + [4.0,5.0) + + UTF-8 + + License.txt + src/main/resources + lib + + + + + + + + + src/main/resources + true + + + + + + + + maven-compiler-plugin + 3.3 + + 1.8 + 1.8 + + + + + + + + + + + + + + + + + + + + + + + + + org.apache.maven.plugins + maven-source-plugin + 3.0.0 + + + attach-sources + package + + jar + + + + + + + maven-assembly-plugin + 2.5.3 + + true + + src/main/assembly/zip.xml + + + + + assembly + package + + single + + + + + + + + + + + + + + + + + + + + + + org.slf4j + slf4j-api + ${slf4j.version} + compile + + + + junit + junit + ${junit.version} + test + + + + org.slf4j + slf4j-simple + ${slf4j.version} + test + + + + + \ No newline at end of file From 30a4b8d8306798e3154c1ea001d5ffbf9f66cb8a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=A8=E4=BF=8A=E6=98=8E?= Date: Sat, 25 Jun 2016 10:29:40 +0800 Subject: [PATCH 2/2] add client and worker demo --- .../gearman/test/pseudoserver/DemoClient.java | 28 +++++++++++++ .../gearman/test/pseudoserver/DemoWorker.java | 39 +++++++++++++++++++ 2 files changed, 67 insertions(+) create mode 100644 src/test/java/org/gearman/test/pseudoserver/DemoClient.java create mode 100644 src/test/java/org/gearman/test/pseudoserver/DemoWorker.java diff --git a/src/test/java/org/gearman/test/pseudoserver/DemoClient.java b/src/test/java/org/gearman/test/pseudoserver/DemoClient.java new file mode 100644 index 0000000..a54c59d --- /dev/null +++ b/src/test/java/org/gearman/test/pseudoserver/DemoClient.java @@ -0,0 +1,28 @@ +package org.gearman.test.pseudoserver; + +import org.gearman.GearmanServer; +import org.gearman.impl.GearmanImpl; +import org.gearman.impl.client.ClientImpl; +import org.gearman.impl.server.remote.GearmanServerRemote; + +import java.io.IOException; +import java.net.InetSocketAddress; + +/** + * Created by yangjunming on 6/25/16. + */ +public class DemoClient { + + public static void main(String[] args) throws IOException, InterruptedException { + GearmanImpl gearmanImpl = new GearmanImpl(); + GearmanServer gearmanServer = new GearmanServerRemote(gearmanImpl, new InetSocketAddress("localhost", 4730)); + ClientImpl client = new ClientImpl(gearmanImpl); + client.addServer(gearmanServer); + + //异步方式派发任务 + client.submitBackgroundJob("demoTask", "jimmy".getBytes()); + + //同步方式派发任务 + client.submitJob("anotherTask", "mike".getBytes()); + } +} diff --git a/src/test/java/org/gearman/test/pseudoserver/DemoWorker.java b/src/test/java/org/gearman/test/pseudoserver/DemoWorker.java new file mode 100644 index 0000000..c5f0e5e --- /dev/null +++ b/src/test/java/org/gearman/test/pseudoserver/DemoWorker.java @@ -0,0 +1,39 @@ +package org.gearman.test.pseudoserver; + +import org.gearman.GearmanServer; +import org.gearman.GearmanWorker; +import org.gearman.impl.GearmanImpl; +import org.gearman.impl.server.remote.GearmanServerRemote; +import org.gearman.impl.worker.GearmanWorkerImpl; + +import java.io.IOException; +import java.net.InetSocketAddress; + +/** + * Created by yangjunming on 6/25/16. + */ +public class DemoWorker { + + public static void main(String[] args) throws IOException, InterruptedException { + GearmanImpl gearmanImpl = new GearmanImpl(); + GearmanWorker worker = new GearmanWorkerImpl(gearmanImpl); + GearmanServer gearmanServer = new GearmanServerRemote(gearmanImpl, new InetSocketAddress("localhost", 4730)); + worker.addServer(gearmanServer); + + worker.addFunction("demoTask", (function, data, callback) -> { + String param = new String(data, "utf-8"); + System.out.println("demoTask => param:" + param); + return ("demoTask => hello," + param).getBytes(); + }); + + worker.addFunction("anotherTask", (function, data, callback) -> { + String param = new String(data, "utf-8"); + System.out.println("anotherTask => param:" + param); + return ("anotherTask => hello," + param).getBytes(); + }); + + while (true) { + Thread.sleep(100); + } + } +}