Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class WorkerThread extends Thread {
- String operation = "";
- private Tuple incomingTuple;
- int result = 0;
- @Override
- public void run() {
- while(true) {
- try {
- // Sleep for 100 milliseconds. This is just an example.
- // You can do something else instead of sleeping.
- Thread.sleep(100);
- } catch (Exception ex) {
- ;
- }
- if (incomingTuple == null) {
- continue;
- }
- if (operation.equalsIgnoreCase("add")) {
- result = incomingTuple.getInt("x") + incomingTuple.getInt("y");
- } else if (operation.equalsIgnoreCase("subtract")) {
- result = incomingTuple.getInt("x") - incomingTuple.getInt("y");
- } else if (operation.equalsIgnoreCase("multiply")) {
- result = incomingTuple.getInt("x") * incomingTuple.getInt("y");
- } else if (operation.equalsIgnoreCase("divide")) {
- result = incomingTuple.getInt("x") / incomingTuple.getInt("y");
- } else {
- System.out.println("Invalid operation.");
- continue;
- }
- StreamingOutput<OutputTuple> outStream = getOutput(0);
- OutputTuple outTuple = outStream.newTuple();
- outTuple.setInt("x", incomingTuple.getInt("x"));
- outTuple.setInt("y", incomingTuple.getInt("y"));
- outTuple.setInt("result", result);
- outTuple.setString("operation", operation);
- try {
- outStream.submit(outTuple);
- } catch(Exception ex) {
- // Do something with this exception if you want to.
- }
- incomingTuple = null;
- }
- }
- void setOperation(String op) {
- operation = op;
- }
- void setIncomingTuple(Tuple x) {
- incomingTuple = x;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement