A recent reconstruction of an old project, using the Gearman Open source project, in short, this is an MQ-like asynchronous system, while distributing tasks, while processing tasks (there is similar to the MQ message sender and receiver), currently supports java,php and other languages, The disadvantage is that there is a single point of issue (the HA official of the server does not offer a solution and requires two development).
Website address: http://www.gearman.org
The following is an example of the Java language:
Note: There are several versions of the Java client instance of Gearman, the difference between the different versions is very large, it is recommended to use the latest official recommended version, the address is Https://github.com/gearman/java-service
One, spring configuration
1 <?XML version= "1.0" encoding= "UTF-8"?>2 <Beansxmlns= "Http://www.springframework.org/schema/beans"3 Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"4 xsi:schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd" >5 6 <BeanID= "Gearmanimpl"class= "Org.gearman.impl.GearmanImpl"></Bean>7 8 <BeanID= "Gearmanserver"class= "Org.gearman.impl.server.remote.GearmanServerRemote">9 <Constructor-argIndex= "0"ref= "Gearmanimpl"/>Ten <Constructor-argIndex= "1"> One <Beanclass= "Java.net.InetSocketAddress"> A <Constructor-argIndex= "0"value= "localhost"/> - <Constructor-argIndex= "1"value= "4730"/> - </Bean> the </Constructor-arg> - </Bean> - - <BeanID= "Gearmanclient"class= "Org.gearman.impl.client.ClientImpl"> + <Constructor-argIndex= "0"ref= "Gearmanimpl"/> - </Bean> + A <BeanID= "Gearmanworker"class= "Org.gearman.impl.worker.GearmanWorkerImpl"> at <Constructor-argIndex= "0"ref= "Gearmanimpl"/> - </Bean> - - - </Beans>
View Code
Ii. Task Sender (also known as client)
Import Org.gearman.gearmanjobevent;import Org.gearman.gearmanjobreturn;import Org.gearman.gearmanserver;import Org.gearman.impl.client.clientimpl;import Org.gearman.impl.server.remote.gearmanserverremote;import Org.springframework.context.support.classpathxmlapplicationcontext;import java.io.UnsupportedEncodingException /** * Created by Poplar under the Banyan tree on 6/12/16. */public class Democlient {public static void main (string[] args) throws Interruptedexception, Unsupportedencodingexce ption {Classpathxmlapplicationcontext ctx = new Classpathxmlapplicationcontext ("Spring-gearman-test.xml"); Gearmanserver gearmanserver = Ctx.getbean (Gearmanserverremote.class); Clientimpl client = Ctx.getbean (Clientimpl.class); Client.addserver (Gearmanserver); Client.submitbackgroundjob ("Demotask", "Jimmy1". GetBytes ());//asynchronous commit Gearmanjobreturn Jobreturn = Clie Nt.submitjob ("Demotask", "Jimmy2". GetBytes ());//synchronous Commit while (!jobreturn.iseof ()) {//next Job event Gearmanjobevent event = Jobreturn.poll (); Switch (Event.geteventtype ()) {case gearman_job_success://job execute SUCCESS Syst Em.out.println (New String (Event.getdata (), "Utf-8")); Break Case Gearman_submit_fail://job-SUBMIT fail case Gearman_job_fail://job Execute fail System.err.println (Event.geteventtype () + ":" + New String (Event.getdata (), "Utf-8")) ; Default:}} client.shutdown (); }}
Iii. task handlers (also known as worker)
Import Org.gearman.gearmanfunction;import Org.gearman.gearmanfunctioncallback;import org.gearman.GearmanServer; Import Org.gearman.gearmanworker;import Org.gearman.impl.server.remote.gearmanserverremote;import Org.gearman.impl.worker.gearmanworkerimpl;import Org.springframework.context.support.classpathxmlapplicationcontext;public class Demoworker Implements gearmanfunction {public static void main (string[] args) throws Interruptedexception {Classpathxmlapplicationco ntext CTX = new Classpathxmlapplicationcontext ("Spring-gearman-test.xml"); Gearmanserver gearmanserver = Ctx.getbean (Gearmanserverremote.class); Gearmanworker worker = Ctx.getbean (Gearmanworkerimpl.class); Worker.addfunction ("Demotask", New Demoworker ()); Worker.addserver (Gearmanserver); } @Override Public byte[] Work (String function, byte[] data, Gearmanfunctioncallback callback) throws Exception { if (data! = null) {string param = new String (data); System.out.println ("Demoworker = param:" + param); Return ("Return value:" + param). GetBytes ("Utf-8"); } else {return ' not receive data! '. GetBytes ("Utf-8"); } }}
Gearman Use Example