@Override public GenericDeadlineMessage<T> andMetaData(Map<String, ?> additionalMetaData) { return new GenericDeadlineMessage<>( deadlineName, getDelegate().andMetaData(additionalMetaData), this::getTimestamp ); }
@Override public GenericDeadlineMessage<T> withMetaData(Map<String, ?> metaData) { return new GenericDeadlineMessage<>(deadlineName, getDelegate().withMetaData(metaData), this::getTimestamp); }
/** * Returns the given {@code deadlineName} and {@code messageOrPayload} as a DeadlineMessage. If the * {@code messageOrPayload} parameter is of type {@link Message}, a new DeadlineMessage will be created using the * payload and meta data of the given deadline. * Otherwise, the given {@code messageOrPayload} is wrapped into a GenericDeadlineMessage as its payload. * * @param deadlineName A {@link String} denoting the deadline's name * @param messageOrPayload A {@link Message} or payload to wrap as a DeadlineMessage * @param <T> The generic type of the expected payload of the resulting object * @return a DeadlineMessage using the {@code deadlineName} as its deadline name and containing the given * {@code messageOrPayload} as the payload */ @SuppressWarnings("unchecked") public static <T> DeadlineMessage<T> asDeadlineMessage(String deadlineName, Object messageOrPayload) { Instant now = clock.instant(); return messageOrPayload instanceof Message ? new GenericDeadlineMessage<>(deadlineName, (Message) messageOrPayload, () -> now) : new GenericDeadlineMessage<>(deadlineName, new GenericMessage<>((T) messageOrPayload), () -> now); }
@Override public String schedule(Instant triggerDateTime, String deadlineName, Object payloadOrMessage, ScopeDescriptor deadlineScope) { DeadlineMessage<?> deadlineMessage = processDispatchInterceptors(asDeadlineMessage(deadlineName, payloadOrMessage)); DeadlineMessage<?> scheduledMessage = new GenericDeadlineMessage<>(deadlineName, deadlineMessage, () -> triggerDateTime); schedules.add(new ScheduledDeadlineInfo(triggerDateTime, deadlineName, scheduledMessage.getIdentifier(), counter.getAndIncrement(), scheduledMessage, deadlineScope)); return scheduledMessage.getIdentifier(); }
/** * Extracts a {@link DeadlineMessage} from provided {@code jobDataMap}. * * <b>Note</b> that this function is able to retrieve two different formats of DeadlineMessage. The first being * a now deprecated solution which used to serialized the entire {@link DeadlineMessage} into the * {@link JobDataMap}. This is only kept for backwards compatibility. The second is the new solution which * stores all the required deadline fields separately, only de-/serializing the payload and metadata of a * DeadlineMessage instead of the entire message. * * @param serializer the {@link Serializer} used to deserialize the contents of the given {@code} jobDataMap} * into a {@link DeadlineMessage} * @param jobDataMap the {@link JobDataMap} which should contain a {@link DeadlineMessage} * @return the {@link DeadlineMessage} pulled from the {@code jobDataMap} */ public static DeadlineMessage deadlineMessage(Serializer serializer, JobDataMap jobDataMap) { if (jobDataMap.containsKey(SERIALIZED_DEADLINE_MESSAGE)) { SimpleSerializedObject<byte[]> serializedDeadlineMessage = new SimpleSerializedObject<>( (byte[]) jobDataMap.get(SERIALIZED_DEADLINE_MESSAGE), byte[].class, (String) jobDataMap.get(SERIALIZED_DEADLINE_MESSAGE_CLASS_NAME), null ); return serializer.deserialize(serializedDeadlineMessage); } return new GenericDeadlineMessage<>((String) jobDataMap.get(DEADLINE_NAME), (String) jobDataMap.get(MESSAGE_ID), deserializeDeadlinePayload(serializer, jobDataMap), deserializeDeadlineMetaData(serializer, jobDataMap), retrieveDeadlineTimestamp(jobDataMap)); }
@Override public void run() { if (logger.isDebugEnabled()) { logger.debug("Triggered deadline"); } try { Instant triggerInstant = GenericEventMessage.clock.instant(); UnitOfWork<DeadlineMessage<?>> unitOfWork = new DefaultUnitOfWork<>( new GenericDeadlineMessage<>(deadlineName, deadlineMessage, () -> triggerInstant)); unitOfWork.attachTransaction(transactionManager); InterceptorChain chain = new DefaultInterceptorChain<>(unitOfWork, handlerInterceptors(), deadlineMessage -> { executeScheduledDeadline(deadlineMessage, deadlineScope); return null; }); ResultMessage<?> resultMessage = unitOfWork.executeWithResult(chain::proceed); if (resultMessage.isExceptional()) { Throwable e = resultMessage.exceptionResult(); throw new DeadlineException(format("An error occurred while triggering the deadline %s %s", deadlineName, deadlineId), e); } } finally { scheduledTasks.remove(new DeadlineId(deadlineName, deadlineId)); } }
@Override public GenericDeadlineMessage<T> andMetaData(Map<String, ?> additionalMetaData) { return new GenericDeadlineMessage<>( deadlineName, getDelegate().andMetaData(additionalMetaData), this::getTimestamp ); }
@Override public GenericDeadlineMessage<T> withMetaData(Map<String, ?> metaData) { return new GenericDeadlineMessage<>(deadlineName, getDelegate().withMetaData(metaData), this::getTimestamp); }
@Override public GenericDeadlineMessage<T> andMetaData(Map<String, ?> additionalMetaData) { return new GenericDeadlineMessage<>( deadlineName, getDelegate().andMetaData(additionalMetaData), this::getTimestamp ); }
/** * Returns the given {@code deadlineName} and {@code messageOrPayload} as a DeadlineMessage. If the * {@code messageOrPayload} parameter is of type {@link Message}, a new DeadlineMessage will be created using the * payload and meta data of the given deadline. * Otherwise, the given {@code messageOrPayload} is wrapped into a GenericDeadlineMessage as its payload. * * @param deadlineName A {@link String} denoting the deadline's name * @param messageOrPayload A {@link Message} or payload to wrap as a DeadlineMessage * @param <T> The generic type of the expected payload of the resulting object * @return a DeadlineMessage using the {@code deadlineName} as its deadline name and containing the given * {@code messageOrPayload} as the payload */ @SuppressWarnings("unchecked") public static <T> DeadlineMessage<T> asDeadlineMessage(String deadlineName, Object messageOrPayload) { Instant now = clock.instant(); return messageOrPayload instanceof Message ? new GenericDeadlineMessage<>(deadlineName, (Message) messageOrPayload, () -> now) : new GenericDeadlineMessage<>(deadlineName, new GenericMessage<>((T) messageOrPayload), () -> now); }
/** * Returns the given {@code deadlineName} and {@code messageOrPayload} as a DeadlineMessage. If the * {@code messageOrPayload} parameter is of type {@link Message}, a new DeadlineMessage will be created using the * payload and meta data of the given deadline. * Otherwise, the given {@code messageOrPayload} is wrapped into a GenericDeadlineMessage as its payload. * * @param deadlineName A {@link String} denoting the deadline's name * @param messageOrPayload A {@link Message} or payload to wrap as a DeadlineMessage * @param <T> The generic type of the expected payload of the resulting object * @return a DeadlineMessage using the {@code deadlineName} as its deadline name and containing the given * {@code messageOrPayload} as the payload */ @SuppressWarnings("unchecked") public static <T> DeadlineMessage<T> asDeadlineMessage(String deadlineName, Object messageOrPayload) { Instant now = clock.instant(); return messageOrPayload instanceof Message ? new GenericDeadlineMessage<>(deadlineName, (Message) messageOrPayload, () -> now) : new GenericDeadlineMessage<>(deadlineName, new GenericMessage<>((T) messageOrPayload), () -> now); }
@Override public GenericDeadlineMessage<T> withMetaData(Map<String, ?> metaData) { return new GenericDeadlineMessage<>(deadlineName, getDelegate().withMetaData(metaData), this::getTimestamp); }
/** * Extracts a {@link DeadlineMessage} from provided {@code jobDataMap}. * * <b>Note</b> that this function is able to retrieve two different formats of DeadlineMessage. The first being * a now deprecated solution which used to serialized the entire {@link DeadlineMessage} into the * {@link JobDataMap}. This is only kept for backwards compatibility. The second is the new solution which * stores all the required deadline fields separately, only de-/serializing the payload and metadata of a * DeadlineMessage instead of the entire message. * * @param serializer the {@link Serializer} used to deserialize the contents of the given {@code} jobDataMap} * into a {@link DeadlineMessage} * @param jobDataMap the {@link JobDataMap} which should contain a {@link DeadlineMessage} * @return the {@link DeadlineMessage} pulled from the {@code jobDataMap} */ public static DeadlineMessage deadlineMessage(Serializer serializer, JobDataMap jobDataMap) { if (jobDataMap.containsKey(SERIALIZED_DEADLINE_MESSAGE)) { SimpleSerializedObject<byte[]> serializedDeadlineMessage = new SimpleSerializedObject<>( (byte[]) jobDataMap.get(SERIALIZED_DEADLINE_MESSAGE), byte[].class, (String) jobDataMap.get(SERIALIZED_DEADLINE_MESSAGE_CLASS_NAME), null ); return serializer.deserialize(serializedDeadlineMessage); } return new GenericDeadlineMessage<>((String) jobDataMap.get(DEADLINE_NAME), (String) jobDataMap.get(MESSAGE_ID), deserializeDeadlinePayload(serializer, jobDataMap), deserializeDeadlineMetaData(serializer, jobDataMap), retrieveDeadlineTimestamp(jobDataMap)); }
/** * Extracts a {@link DeadlineMessage} from provided {@code jobDataMap}. * * <b>Note</b> that this function is able to retrieve two different formats of DeadlineMessage. The first being * a now deprecated solution which used to serialized the entire {@link DeadlineMessage} into the * {@link JobDataMap}. This is only kept for backwards compatibility. The second is the new solution which * stores all the required deadline fields separately, only de-/serializing the payload and metadata of a * DeadlineMessage instead of the entire message. * * @param serializer the {@link Serializer} used to deserialize the contents of the given {@code} jobDataMap} * into a {@link DeadlineMessage} * @param jobDataMap the {@link JobDataMap} which should contain a {@link DeadlineMessage} * @return the {@link DeadlineMessage} pulled from the {@code jobDataMap} */ public static DeadlineMessage deadlineMessage(Serializer serializer, JobDataMap jobDataMap) { if (jobDataMap.containsKey(SERIALIZED_DEADLINE_MESSAGE)) { SimpleSerializedObject<byte[]> serializedDeadlineMessage = new SimpleSerializedObject<>( (byte[]) jobDataMap.get(SERIALIZED_DEADLINE_MESSAGE), byte[].class, (String) jobDataMap.get(SERIALIZED_DEADLINE_MESSAGE_CLASS_NAME), null ); return serializer.deserialize(serializedDeadlineMessage); } return new GenericDeadlineMessage<>((String) jobDataMap.get(DEADLINE_NAME), (String) jobDataMap.get(MESSAGE_ID), deserializeDeadlinePayload(serializer, jobDataMap), deserializeDeadlineMetaData(serializer, jobDataMap), retrieveDeadlineTimestamp(jobDataMap)); }
@Override public void run() { Instant triggerInstant = GenericEventMessage.clock.instant(); DeadlineMessage<?> deadlineMessage = new GenericDeadlineMessage<>( deadlineName, new GenericMessage<>(messageOrPayload), () -> triggerInstant ); if (logger.isDebugEnabled()) { logger.debug("Triggered deadline"); } try { UnitOfWork<DeadlineMessage<?>> unitOfWork = new DefaultUnitOfWork<>(deadlineMessage); unitOfWork.attachTransaction(transactionManager); unitOfWork.execute(() -> executeScheduledDeadline(deadlineMessage, deadlineScope)); } finally { scheduledTasks.remove(new DeadlineId(deadlineName, deadlineId)); } }
@Override public void run() { if (logger.isDebugEnabled()) { logger.debug("Triggered deadline"); } try { Instant triggerInstant = GenericEventMessage.clock.instant(); UnitOfWork<DeadlineMessage<?>> unitOfWork = new DefaultUnitOfWork<>( new GenericDeadlineMessage<>(deadlineName, deadlineMessage, () -> triggerInstant)); unitOfWork.attachTransaction(transactionManager); InterceptorChain chain = new DefaultInterceptorChain<>(unitOfWork, handlerInterceptors(), deadlineMessage -> { executeScheduledDeadline(deadlineMessage, deadlineScope); return null; }); ResultMessage<?> resultMessage = unitOfWork.executeWithResult(chain::proceed); if (resultMessage.isExceptional()) { Throwable e = resultMessage.exceptionResult(); throw new DeadlineException(format("An error occurred while triggering the deadline %s %s", deadlineName, deadlineId), e); } } finally { scheduledTasks.remove(new DeadlineId(deadlineName, deadlineId)); } }