how do you add a constructor to a GearmanFunction?

Asked by Khai Do

Hello. Could you please explain how to add a constructor to my gearman function? I just want to simply add variables to my gearman function and initialize them when I instantiate the function however I can't seem to just extend AbstractGearmanFunction and overload the constructor with my own.

Initially I create my function without a constructor and use the DefaultGearmanFunctionFactory to instantiate MyTestFunction() like so..

GearmanWorker worker = new GearmanWorkerImpl();
worker.addServer(new GearmanNIOJobServerConnection("localhost",4730));
worker.registerFunctionFactory(new DefaultGearmanFunctionFactory("test", MyTestFunction.class.getName()));
worker.work();

Now what I want to store data in my test function and tried to do it like so..

public class MyTestFunction extends AbstractGearmanFunction {

 String id;

 public MyTestFunction() {
  this(null);
 }
 public MyTestFunction(String name) {
  this(name,"");
 }
 public MyTestFunction(String name, String id) {
  super(name);
  this.id = id;
 }

 @Override
 public GearmanJobResult executeFunction() {

  System.out.println(id);

        StringBuffer sb = new StringBuffer(ByteUtils.fromUTF8Bytes((byte[]) this.data));
        GearmanJobResult gjr = new GearmanJobResultImpl(this.jobHandle,
                true, sb.toString().getBytes(),
                new byte[0], new byte[0], 0, 0);
        return gjr;
 }

}

However this doesn't work and it also doesn't seem correct. Could you please help?

Question information

Language:
English Edit question
Status:
Solved
For:
Gearman Java Edit question
Assignee:
No assignee Edit question
Solved by:
Khai Do
Solved:
Last query:
Last reply:
Revision history for this message
Eric Lambert (elambert) said :
#1

Hi Khai:

I believe The problem is that DefaultGearmanFunctionFactory calls the no-arg constructor on the function when it creates a new instance of the function so I suspect this is why it is not working(I am not near my computer at the moment so I can't confirm this).

You could create your own impl of a GearmanFunctionFactory (which could extend DefaultGearmanFunctionFactory) that calls the right constructor.

Eric
On Jan 7, 2013, at 8:50 PM, Khai Do <email address hidden> wrote:

> New question #218597 on Gearman Java:
> https://answers.launchpad.net/gearman-java/+question/218597
>
> Hello. Could you please explain how to add a constructor to my gearman function? I just want to simply add variables to my gearman function and initialize them when I instantiate the function however I can't seem to just extend AbstractGearmanFunction and overload the constructor with my own.
>
> Initially I create my function without a constructor and use the DefaultGearmanFunctionFactory to instantiate MyTestFunction() like so..
>
> GearmanWorker worker = new GearmanWorkerImpl();
> worker.addServer(new GearmanNIOJobServerConnection("localhost",4730));
> worker.registerFunctionFactory(new DefaultGearmanFunctionFactory("test", MyTestFunction.class.getName()));
> worker.work();
>
> Now what I want to store data in my test function and tried to do it like so..
>
> public class MyTestFunction extends AbstractGearmanFunction {
>
> String id;
>
> public MyTestFunction() {
> this(null);
> }
> public MyTestFunction(String name) {
> this(name,"");
> }
> public MyTestFunction(String name, String id) {
> super(name);
> this.id = id;
> }
>
> @Override
> public GearmanJobResult executeFunction() {
>
> System.out.println(id);
>
> StringBuffer sb = new StringBuffer(ByteUtils.fromUTF8Bytes((byte[]) this.data));
> GearmanJobResult gjr = new GearmanJobResultImpl(this.jobHandle,
> true, sb.toString().getBytes(),
> new byte[0], new byte[0], 0, 0);
> return gjr;
> }
>
> }
>
> However this doesn't work and it also doesn't seem correct. Could you please help?
>
>
>
> --
> You received this question notification because you are an answer
> contact for Gearman Java.

Revision history for this message
Khai Do (zaro0508) said :
#2

Thanks for the suggestion. I have it working now.