0

i'm using Gearman to distribute different tasks and therefore i'm using java-gearman-service for implementing clients and workers.

however, i'm not able to figure out the data i receive in

GearmanJobEventCallback<T>::onEvent:

how can i convert the event.getData() byte array to the data i need? e.g. status or returned data? when i send status(3,10) it returns a byte array [51,0,49,48] - well that's not very useful to my client. unserializing with ObjectInputStream doesn't seem to be successful.

Same with the returned data of the work method, how can i "decode" that?

how can i "decode" the data argument (byte array) from the work method in GearmanFunction ?

Any input will be greatly appreciated!

Stefan
  • 2,028
  • 2
  • 36
  • 53
  • Can you use `java-gearman-service` to implement jobs in Java, not python? – raffian Oct 08 '13 at 23:19
  • @raffian: i don't understand your question. `java-gearman-service` is used to create or consume jobs implemented in Java. You can, however, create jobs in java and consume them in any other language. – Stefan Oct 09 '13 at 16:07
  • I'm new to gearman. We have lots of POJO services in Java; we want to add these to gearman so we can queue jobs against those services; is that possible? – raffian Oct 09 '13 at 16:21

2 Answers2

1

OK, it seems that the creators of this library seem to leave the user a bit much to handle.. :D

I've found a way to solve this, hopefully it helps anyone, who comes across this problem.

String numeratorStr = "";
String denominatorStr = "";
boolean writeNumerator = true;
for (byte b : event.getData()) {
    if (b == 0) {
        writeNumerator = false;
        continue;
    }
    if (writeNumerator) {
        numeratorStr += (char) b;
    } else {
        denominatorStr += (char) b;
    }
}
int numerator = Integer.parseInt(numeratorStr);
int denominator = Integer.parseInt(denominatorStr);

It seems that the byte array values are entries from an ASCII table and put together in a byte array like so: http://code.google.com/p/java-gearman-service/source/search?q=GEARMAN_JOB_STATUS&origq=GEARMAN_JOB_STATUS&btnG=Search+Trunk

hope this helps :)

Stefan
  • 2,028
  • 2
  • 36
  • 53
1

Your solution looks good for decoding the "status" callback. The status packet is a bit of an odd ball case and does not fit the API very well, and unfortunately there is not much documentation on how to handle it.

Another idea might be to use the method GearmanFunctionCallback#sendData(byte[]). Using the sendData method, you can send any type of data in any format and allow the client to decode the value. If you're already using the "data" channel, you could also use the "warning" channel to send untyped data back to the client.

Isaiah van der Elst
  • 1,435
  • 9
  • 14