private void addNewJob(GearmanSessionEvent event) {
   byte[] handle, data, functionNameBytes, unique;
   GearmanPacket p = event.getPacket();
   GearmanJobServerSession sess = event.getSession();
   String functionName;
   handle = p.getDataComponentValue(GearmanPacket.DataComponentName.JOB_HANDLE);
   functionNameBytes = p.getDataComponentValue(GearmanPacket.DataComponentName.FUNCTION_NAME);
   data = p.getDataComponentValue(GearmanPacket.DataComponentName.DATA);
   unique = p.getDataComponentValue(DataComponentName.UNIQUE_ID);
   functionName = ByteUtils.fromUTF8Bytes(functionNameBytes);
   FunctionDefinition def = functionMap.get(functionName);
   if (def == null) {
     GearmanTask gsr =
         new GearmanTask(
             new GearmanPacketImpl(GearmanPacketMagic.REQ, GearmanPacketType.WORK_FAIL, handle));
     sess.submitTask(gsr);
   } else {
     GearmanFunction function = def.getFactory().getFunction();
     function.setData(data);
     function.setJobHandle(handle);
     function.registerEventListener(sess);
     if (unique != null && unique.length > 0) {
       function.setUniqueId(unique);
     }
     functionList.add(function);
   }
 }
  public void handleSessionEvent(GearmanSessionEvent event)
      throws IllegalArgumentException, IllegalStateException {
    GearmanPacket p = event.getPacket();
    GearmanJobServerSession s = event.getSession();
    GearmanPacketType t = p.getPacketType();
    Map<JobHandle, GearmanJobImpl> jobsMaps = sessionJobsMap.get(s);

    switch (t) {
      case JOB_CREATED:
        if (jobAwatingCreation == null) {
          throw new IllegalStateException(
              "Recevied job creation " + "message but have not job awaiting submission.");
        }
        if (!jobAwatingCreation.isBackgroundJob()) {
          jobsMaps.put(new JobHandle(jobAwatingCreation), jobAwatingCreation);
        }
        break;
      case WORK_DATA:
      case WORK_STATUS:
      case WORK_WARNING:
      case WORK_COMPLETE:
      case WORK_FAIL:
      case WORK_EXCEPTION:
        JobHandle handle =
            new JobHandle(p.getDataComponentValue(GearmanPacket.DataComponentName.JOB_HANDLE));
        GearmanJobImpl job = jobsMaps.get(handle);
        if (job == null) {
          LOG.warn(
              "Client received packet from server"
                  + " for unknown job ( job_handle = "
                  + handle
                  + " packet = "
                  + t
                  + " )");
        } else {
          job.handleEvent(p);
          if (job.isDone()) {
            jobsMaps.remove(handle);
          }
        }
        break;
      case ERROR:
        String errCode =
            ByteUtils.fromUTF8Bytes(
                p.getDataComponentValue(GearmanPacket.DataComponentName.ERROR_CODE));
        String errMsg =
            ByteUtils.fromUTF8Bytes(
                p.getDataComponentValue(GearmanPacket.DataComponentName.ERROR_TEXT));
        LOG.warn(
            "Received error code "
                + errCode
                + "( "
                + errMsg
                + " )"
                + " from session "
                + s
                + ". Shutting session down");
        shutDownSession(s);
        if (sessionsMap.isEmpty()) {
          shutdown();
        }
        break;
      default:
        LOG.warn(
            "received un-expected packet from Job"
                + " Server Session: "
                + p
                + ". Shutting down session");
        shutDownSession(s);
        if (sessionsMap.isEmpty()) {
          shutdown();
        }
    }
  }