Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add client and worker demo #9

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
312 changes: 159 additions & 153 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -1,167 +1,173 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

<parent>
<groupId>org.sonatype.oss</groupId>
<artifactId>oss-parent</artifactId>
<version>7</version>
</parent>
<groupId>org.sonatype.oss</groupId>
<artifactId>oss-parent</artifactId>
<version>7</version>
</parent>

<name>Java Gearman Service</name>
<url>http://code.google.com/p/java-gearman-service/</url>
<description>
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.
</description>
<inceptionYear>2010</inceptionYear>
<organization>
<name>gearman</name>
<url>http://www.gearman.org</url>
</organization>
<scm>
<connection>scm:svn:http://java-gearman-service.googlecode.com/svn/trunk</connection>
<developerConnection>scm:svn:https://java-gearman-service.googlecode.com/svn/trunk</developerConnection>
<url>http://java-gearman-service.googlecode.com/svn/trunk</url>
</scm>
<modelVersion>4.0.0</modelVersion>
<groupId>org.gearman.jgs</groupId>
<artifactId>java-gearman-service</artifactId>
<version>0.7.0-SNAPSHOT</version>
<packaging>jar</packaging>
<licenses>
<name>gearman</name>
<url>http://www.gearman.org</url>
</organization>

<!--<scm>-->
<!--<connection>scm:svn:http://java-gearman-service.googlecode.com/svn/trunk</connection>-->
<!--<developerConnection>scm:svn:https://java-gearman-service.googlecode.com/svn/trunk</developerConnection>-->
<!--<url>http://java-gearman-service.googlecode.com/svn/trunk</url>-->
<!--</scm>-->

<modelVersion>4.0.0</modelVersion>
<groupId>org.gearman.jgs</groupId>
<artifactId>java-gearman-service</artifactId>
<version>0.7.0-SNAPSHOT</version>
<packaging>jar</packaging>

<licenses>
<license>
<name>BSD</name>
<url>http://opensource.org/licenses/BSD-2-Clause</url>
<distribution>repo</distribution>
</license>
</licenses>

<properties>
<slf4j.version>[1.6,2.0)</slf4j.version>
<junit.version>[4.0,5.0)</junit.version>

<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

<assembly.licence>License.txt</assembly.licence>
<assembly.licence.dir>src/main/resources</assembly.licence.dir>
<assembly.lib.dest>lib</assembly.lib.dest>

<scm.connection>scm:svn:http://java-gearman-service.googlecode.com/svn/trunk</scm.connection>
<scm.url>http://java-gearman-service.googlecode.com/svn/trunk</scm.url>
</properties>

<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>

<plugins>

<!-- Compile -->
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.0</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>

<!-- Javadocs (API) -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>2.9</version>
<configuration>
<excludePackageNames>org.gearman.*</excludePackageNames>
</configuration>
<executions>
<execution>
<id>javadoc</id>
<phase>package</phase>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>

<!-- Source -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>2.2.1</version>
<executions>
<execution>
<id>attach-sources</id>
<phase>package</phase>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>

<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.4</version>
<configuration>
<appendAssemblyId>true</appendAssemblyId>
<descriptors>
<descriptor>src/main/assembly/zip.xml</descriptor>
</descriptors>
</configuration>
<executions>
<execution>
<id>assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-scm-plugin</artifactId>
<version>1.8.1</version>
<configuration>
<connectionType>connection</connectionType>
</configuration>
</plugin>

</plugins>

</build>

<dependencies>

<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${slf4j.version}</version>
<scope>compile</scope>
</dependency>

<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>${slf4j.version}</version>
<scope>test</scope>
</dependency>

</dependencies>


<properties>
<slf4j.version>[1.6,2.0)</slf4j.version>
<junit.version>[4.0,5.0)</junit.version>

<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

<assembly.licence>License.txt</assembly.licence>
<assembly.licence.dir>src/main/resources</assembly.licence.dir>
<assembly.lib.dest>lib</assembly.lib.dest>

<!--<scm.connection>scm:svn:http://java-gearman-service.googlecode.com/svn/trunk</scm.connection>-->
<!--<scm.url>http://java-gearman-service.googlecode.com/svn/trunk</scm.url>-->
</properties>

<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>

<plugins>

<!-- Compile -->
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>

<!-- Javadocs (API) -->
<!--<plugin>-->
<!--<groupId>org.apache.maven.plugins</groupId>-->
<!--<artifactId>maven-javadoc-plugin</artifactId>-->
<!--<version>2.10.3</version>-->
<!--<configuration>-->
<!--<excludePackageNames>org.gearman.*</excludePackageNames>-->
<!--</configuration>-->
<!--<executions>-->
<!--<execution>-->
<!--<id>javadoc</id>-->
<!--<phase>package</phase>-->
<!--<goals>-->
<!--<goal>jar</goal>-->
<!--</goals>-->
<!--</execution>-->
<!--</executions>-->
<!--</plugin>-->

<!-- Source -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>3.0.0</version>
<executions>
<execution>
<id>attach-sources</id>
<phase>package</phase>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>

<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.5.3</version>
<configuration>
<appendAssemblyId>true</appendAssemblyId>
<descriptors>
<descriptor>src/main/assembly/zip.xml</descriptor>
</descriptors>
</configuration>
<executions>
<execution>
<id>assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>

<!--<plugin>-->
<!--<groupId>org.apache.maven.plugins</groupId>-->
<!--<artifactId>maven-scm-plugin</artifactId>-->
<!--<version>1.8.1</version>-->
<!--<configuration>-->
<!--<connectionType>connection</connectionType>-->
<!--</configuration>-->
<!--</plugin>-->

</plugins>

</build>

<dependencies>

<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${slf4j.version}</version>
<scope>compile</scope>
</dependency>

<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>${slf4j.version}</version>
<scope>test</scope>
</dependency>

</dependencies>


</project>
28 changes: 28 additions & 0 deletions src/test/java/org/gearman/test/pseudoserver/DemoClient.java
Original file line number Diff line number Diff line change
@@ -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());
}
}
39 changes: 39 additions & 0 deletions src/test/java/org/gearman/test/pseudoserver/DemoWorker.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
}