How can I get the gearman client's sumbitted function name?

Asked by Khai Do on 2013-01-10

Hello. Could you please tell me whether the function name from the client gets passed to the worker?

Assume I have registered my ReverseFunction with the following function names: 'reverse', 'reverse_a_string' and 'reverse_me'. My gearman client submits a job like so.. client.submit_job('reverse_me', "foo").

Now in the worker I want determine which function name was submitted by the client. How can I get the function name that was requested by the client which in this case is 'reverse_me'?

public class ReverseFunction extends AbstractGearmanFunction {

    public GearmanJobResult executeFunction() {

       // Can I get the function name that was submitted by the client?

        StringBuffer sb = new StringBuffer(ByteUtils.fromUTF8Bytes((byte[]);

        GearmanJobResult gjr = new GearmanJobResultImpl(this.jobHandle,
                true, sb.reverse().toString().getBytes(),
                new byte[0], new byte[0], 0, 0);
        return gjr;

Question information

English Edit question
Gearman Java Edit question
No assignee Edit question
Solved by:
Khai Do
Last query:
Last reply:
Eric Lambert (elambert) said : #1

Hmm ...I think you maybe mixing up terms here (or perhaps I dont understand what you are asking).

So the Worker is the class which receives job requests from the server and then dispatches them to the appropriate Function for execution. So it is required to receive the function name from the server otherwise it would not know which function should fulfill the request.

So, for example, if the client requests a job called "reverse_me" and the Worker has registered with the server that it can execute "reverse_me" jobs. Then when the server has a reverse_me job, it will send a message to the Worker saying something along the lines of "please execute the function 'reverse_me'". At this point the worker looks up what Function handles "reverse_me" and creates a new instance of it.

But I think maybe you are asking me if the Function itself knows the name which was used by the client? If your worker is using DefaultGearmanFunctionFactory to create new instances of the Function, then the Function will not be aware of the name used by the client because the Default factory does not pass in the new-name/alias to the Function's constructor when creating the Function. But if I recall, you are now using your own impl of GearmanFunctionFactory which does pass in the new-name/alias to the Functions constructor. In which case the Function's name should be the name specified by the client and a call to getName() would return the name specified by the client.

Khai Do (zaro0508) said : #2

Sorry i wasn't real clear, but yes, my question was whether the Function itself knows the name which was used by the client. I see that I can just pass the functionName along to the gearman function from my factory Class. I just didn't know whether it was already 'built into' the current implementation. Thanks.