/** * 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); }
/** * Generate a context from provided policy worker task data that can be used by this converter to reconstruct the * policy worker task when converting a result returned by another worker. * @param taskData Policy worker task data representing current processing progress. * @param codec Codec to use in serializing context. * @return Serialized context that converter will be able to rebuild the provided task data state from. * @throws CodecException If an exception occurs serializing the constructed context. */ default byte[] generatePolicyWorkerContext(final TaskData taskData, final Codec codec) throws CodecException { return codec.serialise(taskData); }
@Override public <T> T deserialiseData(Class<T> clazz, DecodeMethod decodeMethod) throws CodecException { final byte[] data = workerTask.getData(); return codec.deserialise(data, clazz, decodeMethod); }
public <T> byte[] serialiseResult(final T result) { try { return codec.serialise(result); } catch (final CodecException e) { throw new TaskFailedException("Failed to serialise result", e); } }
default <T> T deserialise(byte[] data, Class<T> clazz) throws CodecException { return deserialise(data, clazz, DecodeMethod.getDefault()); }
/** * Create task message. * * @param workerTask the worker task * @param taskId the task id * @return the task message * @throws CodecException the codec exception */ public TaskMessage create(final Object workerTask, TrackingInfo tracking, final String taskId) throws CodecException { Map<java.lang.String, byte[]> context = Collections.singletonMap(CONTEXT_KEY, CONTEXT_DATA); TaskMessage msg = new TaskMessage(taskId, workerName, apiVersion, codec.serialise(workerTask), TaskStatus.NEW_TASK, context, workerInputQueueName, tracking); return msg; } }
@Override public <T> T deserialiseData(Class<T> clazz) throws CodecException { final byte[] data = workerTask.getData(); return codec.deserialise(data, clazz); }
/** * Creates a context describing the policy worker state for this task data so that the task may be rebuilt for processing to continue * when a message is returned to a policy worker from an external destination. * @param classifierToSendTo The external destination that a task is to be sent to. * @param classifierVersion The version of the external destination. * @param taskData TaskData representing current state of policy worker processing. * @param codec Codec to use in serializing context. * @return Generated context to pass for this policy worker. */ public byte[] generatePolicyWorkerContext(final String classifierToSendTo, final int classifierVersion, final TaskData taskData, final Codec codec) throws CodecException { PolicyWorkerConverterInterface converter = workerInputConverters.get(classifierToSendTo); if(converter==null){ //there may not be a converter matching the handler used to build message e.g. GenericQueueHandler which is only //concerned with sending a message to a queue that will not be returned to a policy worker. return codec.serialise(taskData); } return converter.generatePolicyWorkerContext(taskData, codec); }
/** * 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()); }
/** * Publishes a TaskMessage to the InputQueue * * @param message the message to publish * @throws CodecException if TaskMessage fails to serialize * @throws IOException if channel fails to publish the message */ public void publishMessage(TaskMessage message) throws CodecException, IOException { if(Strings.isNullOrEmpty(publishQueue)){ LOGGER.warn("Attempted to publish message when publish queue is not set. Message not published."); return; } LOGGER.info("Publishing task message " + message.getTaskId()); byte[] data = codec.serialise(message); publisherChannel.basicPublish("", publishQueue, MessageProperties.TEXT_PLAIN, data); }
/** * 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; } }
/** * @param t the Throwable from the Worker * @return a byte array that is either the serialised Class of the Throwable or empty */ private byte[] getExceptionData(final Throwable t) { try { String exceptionDetail = buildExceptionStackTrace(t); return getCodec().serialise(exceptionDetail); } catch (CodecException e) { LOG.warn("Failed to serialise exception, continuing", e); return new byte[]{}; } }
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; }
public void publishDebugOutput(TaskMessage message) throws CodecException, IOException { byte[] data = workerServices.getCodec().serialise(message); debugConChan.basicPublish("", debugOutputQueueName, MessageProperties.TEXT_PLAIN, data); }
/** * 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; }
private void writeRawMessageFile(TaskMessage taskMessage, String messageOutputDir) throws IOException, CodecException { if (saveTaskDataOnly) { outputHelper.outputString(messageOutputDir, FileNameHelper.getRawTaskMessageFilename(), org.apache.commons.io.IOUtils.toString(codec.serialise(taskMessage.getTaskData()))); } else { outputHelper.outputString(messageOutputDir, FileNameHelper.getRawTaskMessageFilename(), org.apache.commons.io.IOUtils.toString(codec.serialise(taskMessage))); } }
@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()); } }
@Override public void reportUpdate(final String queueMsgId, final TaskMessage reportUpdateMessage) { Objects.requireNonNull(queueMsgId); Objects.requireNonNull(reportUpdateMessage); LOG.debug("Sending report updates to queue {})", reportUpdateMessage.getTo()); final byte[] output; try { output = codec.serialise(reportUpdateMessage); } catch (final CodecException ex) { throw new RuntimeException(ex); } final int priority = reportUpdateMessage.getPriority() == null ? 0 : reportUpdateMessage.getPriority(); try { workerQueue.publish("-1", output, reportUpdateMessage.getTo(), Collections.emptyMap(), priority); } catch (final QueueException ex) { throw new RuntimeException(ex); } }
/** * 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); } }
reportUpdatesTaskData = codec.serialise(trackingReportTask); } catch (final CodecException e) { LOG.error("Failed to serialise report update task data.");