/** * Takes in a context previously generated by a converter and returns the policy worker task data it represents. * @param context Representation of the policy worker state that may be used in constructing a TaskData. * @param taskData The task data intended for this converter to convert which may contain information for use in constructing * the policy worker task data. * @param codec Codec to use in deserializing context. * @return The constructed policy worker task data from the parameters provided. * @throws CodecException If an exception occurs during operations using provided codec. */ default TaskData convertPolicyWorkerContext(final byte[] context, final byte[] taskData, final Codec codec) throws CodecException { return codec.deserialise(context, TaskData.class); }
default <T> T deserialise(byte[] data, Class<T> clazz) throws CodecException { return deserialise(data, clazz, DecodeMethod.getDefault()); }
@Override public <T> T deserialiseData(Class<T> clazz, DecodeMethod decodeMethod) throws CodecException { final byte[] data = workerTask.getData(); return codec.deserialise(data, clazz, decodeMethod); }
@Override public <T> T deserialiseData(Class<T> clazz) throws CodecException { final byte[] data = workerTask.getData(); return codec.deserialise(data, clazz); }
/** * Deserialise the given data into the specified class using the default decode method. * * @param stream the serialised data as a stream * @param clazz the class the serialised data represents * @param <T> the class the serialised data represents * @return an instance of the class specified represented by the data * @throws CodecException if the data could not be deserialised */ @Override default <T> T deserialise(InputStream stream, Class<T> clazz) throws CodecException { return deserialise(stream, clazz, DecodeMethod.getDefault()); }
/** * Deserialise the given data into the specified class, and validate that any constraints specified have been met. */ @Nonnull public static <T> T deserialiseAndValidateTask(final Codec codec, final Class<T> taskType, final byte[] data) throws InvalidTaskException { final T documentWorkerTask; try { documentWorkerTask = codec.deserialise(data, taskType, DecodeMethod.STRICT); } catch (final CodecException e) { throw new InvalidTaskException("Invalid input message", e); } if (documentWorkerTask == null) { throw new InvalidTaskException("Invalid input message: no result from deserialisation"); } final Set<ConstraintViolation<T>> violations = validator.validate(documentWorkerTask); if (violations.size() > 0) { LOG.error("Task of type {} failed validation due to: {}", taskType.getSimpleName(), violations); throw new InvalidTaskException("Task failed validation"); } return documentWorkerTask; } }
public static TaskMessage getTaskMessageFromDelivery(Delivery deliveredResult) { //check that response received Assert.assertNotNull(deliveredResult); final Codec codec = new JsonCodec(); TaskMessage resultWrapper; try { resultWrapper = codec.deserialise(deliveredResult.getMessageData(), TaskMessage.class); } catch (CodecException ex) { throw new RuntimeException("Failed to turn message delivery into a TaskMessage: ", ex); } return resultWrapper; }
/** * Deserialize message to the worker-under-test result using configured {@link Codec} implementation. * * @param message the message * @param resultClass the result class * @return the t result * @throws CodecException the codec exception */ protected TResult deserializeMessage(TaskMessage message, Class<TResult> resultClass) throws CodecException { if (message.getTaskStatus() != TaskStatus.RESULT_SUCCESS && message.getTaskStatus() != TaskStatus.RESULT_FAILURE) { throw new AssertionError("Task status was failure."); } TResult workerResult = codec.deserialise(message.getTaskData(), resultClass); return workerResult; }
@Override public void processDelivery(Delivery delivery) { System.out.print("New delivery"); try { TaskMessage taskMessage = codec.deserialise(delivery.getMessageData(), TaskMessage.class, DecodeMethod.LENIENT); System.out.println(taskMessage.getTaskId() + ", status: " + taskMessage.getTaskStatus()); synchronized (syncLock) { resultHandler.handleResult(taskMessage); } } catch (CodecException e) { e.printStackTrace(); throw new AssertionError("Failed: " + e.getMessage()); } catch (Exception e) { e.printStackTrace(); throw new AssertionError("Failed: " + e.getMessage()); } }
/** * Handles a delivery and passes to message handler. * * @param delivery the newly arrived message including metadata */ @Override public void processDelivery(Delivery delivery) { try { TaskMessage taskMessage = codec.deserialise(delivery.getMessageData(), TaskMessage.class); synchronized (syncLock) { messageHandler.handleMessage(taskMessage,delivery.getEnvelope().getRoutingKey()); } } catch (CodecException e) { LOGGER.error("Error encountered trying to process delivery from queue.", e); } }
@Override public <T> T getObject(final String ref, final Class<T> clazz) throws DataSourceException { try (QuietResource<InputStream> qr = new QuietResource<>(getStream(ref))) { try { return codec.deserialise(qr.get(), clazz); } catch (CodecException e) { throw new DataSourceException("Could not deserialise stream", e); } } }
private void writeMessageWithTaskGrouping(TaskMessage taskMessage, String messageAsStr) throws IOException, CodecException { //if the task data represents type 'SharedDocument' then additional files should be output SharedDocument document = null; try{ document = codec.deserialise(taskMessage.getTaskData(), SharedDocument.class); } catch(CodecException e){ LOGGER.debug("Error deserializing task data to SharedDocument. Avoiding output of document specific files."); } String folderName = null; if(document!=null) { folderName = outputHelper.createFolderFromDocument(document, outputDirectory); } else{ folderName = outputHelper.createFolderFromTaskMessageId(taskMessage, outputDirectory); } writeMessageDetails(taskMessage, messageAsStr, folderName); if(document!=null){ writeSharedDocumentDetails(taskMessage, messageAsStr, document, folderName); } }
TaskResponse taskResponse = codec.deserialise(returnedTaskData, TaskResponse.class); Assert.assertNotNull(taskResponse); Collection<com.github.cafdataprocessing.worker.policy.shared.ClassifyDocumentResult> classifyResultCollection = taskResponse.getClassifiedDocuments();
TaskResponse taskResponse = codec.deserialise(returnedTaskData, TaskResponse.class); Assert.assertNotNull(taskResponse); Collection<com.github.cafdataprocessing.worker.policy.shared.ClassifyDocumentResult> classifyResultCollection = taskResponse.getClassifiedDocuments();
TaskResponse taskResponse = codec.deserialise(returnedTaskData, TaskResponse.class); Assert.assertNotNull(taskResponse); Collection<com.github.cafdataprocessing.worker.policy.shared.ClassifyDocumentResult> classifyResultCollection = taskResponse.getClassifiedDocuments();
/** * Verify that the specified worker task has the right type and is a version that can be handled. */ private T verifyWorkerTask( final String classifier, final int version, final byte[] data ) throws TaskRejectedException, InvalidTaskException { // Reject tasks of the wrong type and tasks that require a newer version final String workerName = getWorkerName(); if (!workerName.equals(classifier)) { throw new InvalidTaskException("Task of type " + classifier + " found on queue for " + getWorkerName()); } final int workerApiVersion = getWorkerApiVersion(); if (workerApiVersion < version) { throw new TaskRejectedException("Found task version " + version + ", which is newer than " + workerApiVersion); } if (data == null) { throw new InvalidTaskException("Invalid input message: task not specified"); } try { return codec.deserialise(data, taskClass, DecodeMethod.LENIENT); } catch (CodecException e) { throw new InvalidTaskException("Invalid input message", e); } }
checkTaskMessageReturned(resultWrapper, taskId); TaskResponse taskResponse = codec.deserialise(returnedTaskData, TaskResponse.class); Assert.assertNotNull(taskResponse); Collection<com.github.cafdataprocessing.worker.policy.shared.ClassifyDocumentResult> classifyResultCollection = taskResponse.getClassifiedDocuments();
TaskResponse taskResponse = codec.deserialise(returnedTaskData, TaskResponse.class); Assert.assertNotNull(taskResponse); Collection<com.github.cafdataprocessing.worker.policy.shared.ClassifyDocumentResult> classifyResultCollection = taskResponse.getClassifiedDocuments();
private void registerNewTaskImpl(final String queueMsgId, final byte[] taskMessage, Map<String, Object> headers) throws InvalidTaskException, TaskRejectedException { try { TaskMessage tm = codec.deserialise(taskMessage, TaskMessage.class, DecodeMethod.LENIENT); LOG.debug("Received task {} (message id: {})", tm.getTaskId(), queueMsgId); boolean poison = isTaskPoisoned(headers); validateTaskMessage(tm); boolean taskIsActive = checkStatus(tm); if (taskIsActive) { if (tm.getTo() != null && tm.getTo().equalsIgnoreCase(workerQueue.getInputQueue())) { LOG.debug("Task {} (message id: {}) on input queue {} {}", tm.getTaskId(), queueMsgId, workerQueue.getInputQueue(), (tm.getTo() != null) ? "is intended for this worker" : "has no explicit destination, therefore assuming it is intended for this worker"); executor.executeTask(tm, queueMsgId, poison, headers, codec); } else { LOG.debug("Task {} (message id: {}) is not intended for this worker: input queue {} does not match message destination queue {}", tm.getTaskId(), queueMsgId, workerQueue.getInputQueue(), tm.getTo()); executor.forwardTask(tm, queueMsgId, headers); } } else { LOG.debug("Task {} is no longer active. The task message (message id: {}) will not be executed", tm.getTaskId(), queueMsgId); executor.discardTask(tm, queueMsgId); } } catch (CodecException e) { throw new InvalidTaskException("Queue data did not deserialise to a TaskMessage", e); } catch (InvalidJobTaskIdException ijte) { throw new InvalidTaskException("TaskMessage contains an invalid job task identifier", ijte); } }
return converter.convertPolicyWorkerContext(context, workerTask.getData(), codec); return codec.deserialise(context, TaskData.class);