/** * 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); }
public <T> byte[] serialiseResult(final T result) { try { return codec.serialise(result); } catch (final CodecException e) { throw new TaskFailedException("Failed to serialise result", e); } }
/** * 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; } }
/** * 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); }
/** * 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); }
/** * @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[]{}; } }
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))); } }
public void publishDebugOutput(TaskMessage message) throws CodecException, IOException { byte[] data = workerServices.getCodec().serialise(message); debugConChan.basicPublish("", debugOutputQueueName, MessageProperties.TEXT_PLAIN, data); }
@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); } }
reportUpdatesTaskData = codec.serialise(trackingReportTask); } catch (final CodecException e) { LOG.error("Failed to serialise report update task data.");
@Override public void send(final String queueMsgId, final TaskMessage responseMessage) { Objects.requireNonNull(queueMsgId); Objects.requireNonNull(responseMessage); LOG.debug("Sending task {} complete (message id: {})", responseMessage.getTaskId(), queueMsgId); final String queue = responseMessage.getTo(); checkForTrackingTermination(queueMsgId, queue, responseMessage); final byte[] output; try { output = codec.serialise(responseMessage); } catch (final CodecException ex) { throw new RuntimeException(ex); } final int priority = responseMessage.getPriority() == null ? 0 : responseMessage.getPriority(); try { workerQueue.publish("-1", output, queue, Collections.emptyMap(), priority); } catch (final QueueException ex) { throw new RuntimeException(ex); } }
public void publish(TaskMessage message) throws CodecException, IOException { byte[] data = workerServices.getCodec().serialise(message); pubChan.basicPublish("", queueServices.getWorkerInputQueue(), MessageProperties.TEXT_PLAIN, data); if (debugEnabled) { debugPubChan.basicPublish("", debugInputQueueName, MessageProperties.TEXT_PLAIN, data); } }
@Override public void forward(String queueMsgId, String queue, TaskMessage forwardedMessage, Map<String, Object> headers) { Objects.requireNonNull(queueMsgId); Objects.requireNonNull(forwardedMessage); // queue can be null for a dead end worker LOG.debug("Task {} (message id: {}) being forwarded to queue {}", forwardedMessage.getTaskId(), queueMsgId, queue); checkForTrackingTermination(queueMsgId, queue, forwardedMessage); try { // If the queue is null, acknowledge the task rather than forwarding it if (queue == null) { workerQueue.acknowledgeTask(queueMsgId); } else { // Else forward the task byte[] output = codec.serialise(forwardedMessage); workerQueue.publish(queueMsgId, output, queue, headers, forwardedMessage.getPriority() == null ? 0 : forwardedMessage.getPriority()); stats.incrementTasksForwarded(); //TODO - I'm guessing this stat should not be updated for forwarded messages: // stats.getOutputSizes().update(output.length); } } catch (CodecException | QueueException e) { LOG.error("Cannot publish data for forwarded task {}, rejecting", forwardedMessage.getTaskId(), e); abandon(queueMsgId, e); } }
/** * Utility method for creating a WorkerReponse that represents a failed result with context data. * * @param result the result from the Worker * @param context the context entries to add to the published message * @return a WorkerResponse that represents a failed result containing the specified task-specific serialised message */ protected final WorkerResponse createFailureResult(final V result, final byte[] context) { try { byte[] data = (result != null ? getCodec().serialise(result) : new byte[]{}); return new WorkerResponse(getResultQueue(), TaskStatus.RESULT_FAILURE, data, getWorkerIdentifier(), getWorkerApiVersion(), context); } catch (CodecException e) { throw new TaskFailedException("Failed to serialise result", e); } }
/** * Utility method for creating a WorkerReponse that represents a successful result with context data. * * @param result the result from the Worker * @param context the context entries to add to the published message * @return a WorkerResponse that represents a successful result containing the specified task-specific serialised message */ protected final WorkerResponse createSuccessResult(final V result, final byte[] context) { try { byte[] data = (result != null ? getCodec().serialise(result) : new byte[]{}); return new WorkerResponse(getResultQueue(), TaskStatus.RESULT_SUCCESS, data, getWorkerIdentifier(), getWorkerApiVersion(), context); } catch (CodecException e) { throw new TaskFailedException("Failed to serialise result", e); } }
private static TaskMessage buildTaskMessage(Document document, long workflowId, String projectId, String storageDirectory) throws CodecException { TaskData taskData = new TaskData(); taskData.setDocument(document); taskData.setOutputPartialReference(storageDirectory); taskData.setWorkflowId(String.valueOf(workflowId)); taskData.setExecutePolicyOnClassifiedDocuments(true); taskData.setProjectId(projectId); byte[] serializedTaskData = codec.serialise(taskData); TaskMessage taskMessage = new TaskMessage(); taskMessage.setTaskData(serializedTaskData); taskMessage.setContext(new HashMap<>()); taskMessage.setTaskId(String.valueOf(messageCount.addAndGet(1)) + "-" + document.getReference()); taskMessage.setTaskClassifier("PolicyWorker"); taskMessage.setTaskApiVersion(1); taskMessage.setTaskStatus(TaskStatus.NEW_TASK); taskMessage.setTo(destinationQueue); return taskMessage; } }
/** * Utility method for creating a WorkerReponse that represents a successful result and reports complete. * * @param result the result from the Worker * @return a WorkerResponse that represents a successful result containing the specified task-specific serialised message */ protected final WorkerResponse createSuccessAndCompleteResponse(final V result) { final String outputQueue = getResultQueue(); try { final byte[] data = (result != null ? getCodec().serialise(result) : new byte[]{}); return new WorkerResponse(outputQueue, TaskStatus.RESULT_SUCCESS, data, getWorkerIdentifier(), getWorkerApiVersion(), null, outputQueue); } catch (CodecException e) { throw new TaskFailedException("Failed to serialise result", e); } }
byte[] output = codec.serialise(responseMessage); workerQueue.publish(queueMsgId, output, queue, Collections.emptyMap(), responseMessage.getPriority() == null ? 0 : responseMessage.getPriority()); stats.getOutputSizes().update(output.length);
workerResponse = new WorkerResponse(workerHandlerResponse.getQueueReference(), workerHandlerResponse.getTaskStatus(), codec.serialise(workerHandlerResponse.getData()), workerHandlerResponse.getMessageType(), workerHandlerResponse.getApiVersion(),