Gearman框架Java demo

说明:官网gearman.org反转字符串demo的java版本

1.环境:JDK1.7

所需jar包:java-gearman-service-0.6.6.jar(最新的jar包,用此jar包要求jdk7)

               slf4j-api-1.6.4.jar

               slf4j-simple-1.6.4.jar

2.代码:

  //client端代码

package com.broada.gearman;

import org.gearman.Gearman;
import org.gearman.GearmanClient;
import org.gearman.GearmanJobEvent;
import org.gearman.GearmanJobReturn;
import org.gearman.GearmanServer;

public class EchoClient {

    public static void main(String... args) throws InterruptedException {

            //创建一个Gearman实例
            Gearman gearman = Gearman.createGearman();

            //创建一个Gearman client             
            GearmanClient client = gearman.createGearmanClient();

            /*
             * 创建一个jobserver
             * 
             * Parameter 1: job server的IP地址
             * Parameter 2: job server监听的端口
             * 
             *job server收到client的job,并将其分发给注册worker
             *
             */
            GearmanServer server = gearman.createGearmanServer(
                            EchoWorker.ECHO_HOST, EchoWorker.ECHO_PORT);

            
             // 告诉客户端,提交工作时它可以连接到该服务器
             
            client.addServer(server);

            /*
             * 向job server提交工作
             * 
             * Parameter 1: gearman function名字
             * Parameter 2: 传送给job server和worker的数据
             * 
             * GearmanJobReturn返回job发热结果
             */
            GearmanJobReturn jobReturn = client.submitJob(
                            EchoWorker.ECHO_FUNCTION_NAME, ("Hello World!").getBytes());
            

            //遍历作业事件,直到我们打到最后文件             
            while (!jobReturn.isEOF()) {

                    //下一个作业事件
                    GearmanJobEvent event = jobReturn.poll();

                    switch (event.getEventType()) {

                    case GEARMAN_JOB_SUCCESS:     //job执行成功
                            System.out.println(new String(event.getData()));
                            break;
                    case GEARMAN_SUBMIT_FAIL:     //job提交失败
                    	
                    case GEARMAN_JOB_FAIL:        //job执行失败
                            System.err.println(event.getEventType() + ": "
                                            + new String(event.getData()));
                    default:
                    }

            }

            //关闭
            gearman.shutdown();
    }
}

 

//worker端代码

package com.broada.gearman;

import org.gearman.Gearman;
import org.gearman.GearmanFunction;
import org.gearman.GearmanFunctionCallback;
import org.gearman.GearmanServer;
import org.gearman.GearmanWorker;

public class EchoWorker implements GearmanFunction {

        //function name
        public static final String ECHO_FUNCTION_NAME = "reverse";

        //job server地址
        public static final String ECHO_HOST = "192.168.125.131";

        //job server监听的端口
        public static final int ECHO_PORT = 4730;

        public static void main(String... args) {

        	    //创建一个Gearman实例
                Gearman gearman = Gearman.createGearman();

                /*
                 * 创建一个jobserver
                 * 
                 * Parameter 1: job server的IP地址
                 * Parameter 2: job server监听的端口
                 * 
                 *job server收到client的job,并将其分发给注册worker
                 *
                 */
                GearmanServer server = gearman.createGearmanServer(
                                EchoWorker.ECHO_HOST, EchoWorker.ECHO_PORT);

                //创建一个Gearman的worker
                GearmanWorker worker = gearman.createGearmanWorker();

                //告诉工人如何执行工作(主要实现了GearmanFunction接口)
                worker.addFunction(EchoWorker.ECHO_FUNCTION_NAME, new EchoWorker());

                //worker连接服务器
                worker.addServer(server);
        }

		@Override
        public byte[] work(String function, byte[] data,
                        GearmanFunctionCallback callback) throws Exception {

                //work方法实现了GearmanFunction接口中的work方法,本实例中进行了字符串的反写
        	    if(data != null){
        	    	String str = new String(data);
            	    StringBuffer sb = new StringBuffer(str);
            	    return sb.reverse().toString().getBytes();
        	    }else{
        	    	return "未接收到data".getBytes();
        	    }
                
        }

}

 

*ECHO_HOST = "192.168.125.131"为安装了Gearman并开启geramand服务的主机地址

 *int ECHO_PORT = 4730默认端口为4730

你可能感兴趣的:(gearman)