Java Gearman Service
- Project Page: https://github.com/gearman/java-service
The Java Gearman Service provides a complete gearman implementation in java. This includes the client, worker, and job server.
Standalone Server
The Java Gearman Service jar file doubles as a fully featured gearman server.
Usage:
java [jvm options] -jar java-gearman-service-X.Y.Z.jar [server options]
Server Options
The server options will specify the server’s system variables and/or tell the server what actions to take. The following is a list of the available options.
Short Name | Long Name | Description |
---|---|---|
-p |
--port=PORT |
Defines what port number the server will listen on (Default: 4730) |
-v |
--version |
Display the version of java gearman service and exit |
-? |
--help |
Print the help menu and exit |
JVM Options
The following is a list of some applicable jvm options.
Option Name | Description |
---|---|
-server |
Use Server HotSpot VM. Must be first option (HotSpot Only) |
Gearman
org.gearman.Gearman
objects define gearman systems and creates gearman
services. These services include GearmanWorker
s, GearmanClient
s,
and GearmanServer
s. All services created by the same Gearman
object
are said to be in the same system, and all services in the same system
share system wide thread resources.
Example
The following example shows how to create a gearman system using 8 threads and how to create the three gearman services:
GearmanWorker
org.gearman.GearmanWorker
objects receive GearmanJob
s and distributes them
to registered GearmanFunction
s.
Adding Job Servers
The purpose of a GearmanWorker
is to grab jobs from a job servers implementing
the gearman protocol. For this to work, the user
will need to specify what job servers the GearmanWorker
may grab from.
Adding GearmanServers
JGS provides an API for creating and managing job servers in the local address
space, the GearmanServer
API. This makes it possible for services to
communicate directly with a local job server.
The following example adds a running GearmanServer
to the GearmanWorker
allowing for direct communication (without a TCP socket):
Adding Remote Servers
A GearmanWorker
can connect to any job server implementing the
gearman protocol (See the
download page for more job server
implementations). This allows the user to build a very scalable
multilingual distributed system.
The following example adds a remote job server that implements the gearman protocol:
Register GearmanFunctions
The user will also need provide a mapping from function names to
GearmanFunction
objects. This will tell the worker what job queues to grab
from and what GearmanFunction
to invoke when a job is received.
GearmanWorker Example
The following example sets up a simple GearmanWorker
. It shows how to create
a GearmanWorker
, register a GearmanFunction
, and add a job server.
GearmanClient
org.gearman.GearmanClient
objects dispatch GearmanJob
s to the job servers.
GearmanServer
org.gearman.GearmanServer