@Override public Class<T> getPayloadType() { return delegate.getPayloadType(); }
@Override public boolean matches(Message message) { return message.getPayloadType() != null && payloadType.isAssignableFrom(message.getPayloadType()); }
@Override public MonitorCallback onMessageIngested(Message<?> message) { String monitorName = monitorNameBuilder.apply(message.getPayloadType()); MessageMonitor<Message<?>> messageMonitorForPayloadType = payloadTypeMonitors.computeIfAbsent(monitorName, payloadType -> monitorSupplier.get()); return messageMonitorForPayloadType.onMessageIngested(message); }
@Override public Object handle(UnitOfWork<? extends T> unitOfWork, InterceptorChain interceptorChain) throws Exception { T message = unitOfWork.getMessage(); logger.info("Incoming message: [{}]", message.getPayloadType().getSimpleName()); try { Object returnValue = interceptorChain.proceed(); logger.info("[{}] executed successfully with a [{}] return value", message.getPayloadType().getSimpleName(), returnValue == null ? "null" : returnValue.getClass().getSimpleName()); return returnValue; } catch (Exception t) { logger.warn(format("[%s] execution failed:", message.getPayloadType().getSimpleName()), t); throw t; } } }
@Override public boolean canHandle(Message<?> message) { return typeMatches(message) && payloadType.isAssignableFrom(message.getPayloadType()) && parametersMatch(message); }
@Override @SuppressWarnings("unchecked") public <E> Stream<E> filterCandidates(T message, Stream<E> candidates) { Property routingProperty = getProperty(message.getPayloadType(), routingKey); if (routingProperty == null) { return Stream.empty(); } Object routingValue = routingProperty.getValue(message.getPayload()); return candidates.filter(candidate -> matchesInstance(candidate, routingValue)); }
@Override public Object handle(Message<?> message, T target) throws Exception { try { if (executable instanceof Method) { return ((Method) executable).invoke(target, resolveParameterValues(message)); } else if (executable instanceof Constructor) { return ((Constructor) executable).newInstance(resolveParameterValues(message)); } else { throw new IllegalStateException("What kind of handler is this?"); } } catch (IllegalAccessException | InvocationTargetException | InstantiationException e) { checkAndRethrowForExceptionOrError(e); throw new MessageHandlerInvocationException( String.format("Error handling an object of type [%s]", message.getPayloadType()), e); } }
@Override public BiFunction<Integer, EventMessage<?>, EventMessage<?>> handle(List<? extends EventMessage<?>> messages) { StringBuilder sb = new StringBuilder(String.format("Events published: [%s]", messages.stream().map(m -> m.getPayloadType().getSimpleName()).collect(Collectors.joining(", ")))); CurrentUnitOfWork.ifStarted(unitOfWork -> { Message<?> message = unitOfWork.getMessage(); if (message == null) { sb.append(" while processing an operation not tied to an incoming message"); } else { sb.append(String.format(" while processing a [%s]", message.getPayloadType().getSimpleName())); } ExecutionResult executionResult = unitOfWork.getExecutionResult(); if (executionResult != null) { if (executionResult.isExceptionResult()) { @SuppressWarnings("ThrowableResultOfMethodCallIgnored") Throwable exception = executionResult.getExceptionResult(); exception = exception instanceof ExecutionException ? exception.getCause() : exception; sb.append(String.format(" which failed with a [%s]", exception.getClass().getSimpleName())); } else if (executionResult.getResult() != null) { sb.append(String.format(" which yielded a [%s] return value", executionResult.getResult().getClass().getSimpleName())); } } }); logger.info(sb.toString()); return (i, m) -> m; } }
private static Object invokeAnnotated(Message<?> command, Class<? extends Annotation> annotation) throws InvocationTargetException, IllegalAccessException { for (Method m : methodsOf(command.getPayloadType())) { if (AnnotationUtils.isAnnotationPresent(m, annotation)) { ensureAccessible(m); return m.invoke(command.getPayload()); } } for (Field f : fieldsOf(command.getPayloadType())) { if (AnnotationUtils.isAnnotationPresent(f, annotation)) { return getFieldValue(f, command.getPayload()); } } return null; }
@SuppressWarnings("unchecked") public <T> SerializedObject<T> serializePayload(Serializer serializer, Class<T> expectedRepresentation) { synchronized (payloadGuard) { SerializedObject existingForm = serializedPayload.get(serializer); if (existingForm == null) { SerializedObject<T> serialized = serializer.serialize(message.getPayload(), expectedRepresentation); if (message.getPayload() == null) { // make sure the payload type is maintained serialized = new SimpleSerializedObject<>(serialized.getData(), serialized.getContentType(), serializer.typeForClass(message.getPayloadType())); } serializedPayload.put(serializer, serialized); return serialized; } else { return serializer.getConverter().convert(existingForm, expectedRepresentation); } } }
@Override public Class<T> getPayloadType() { return delegate.getPayloadType(); }
@Override public Class<T> getPayloadType() { return delegate.getPayloadType(); }
@Override public boolean matches(Message message) { return message.getPayloadType() != null && payloadType.isAssignableFrom(message.getPayloadType()); }
@Override public boolean matches(Message message) { return message.getPayloadType() != null && payloadType.isAssignableFrom(message.getPayloadType()); }
@Override public MonitorCallback onMessageIngested(Message<?> message) { String monitorName = monitorNameBuilder.apply(message.getPayloadType()); MessageMonitor<Message<?>> messageMonitorForPayloadType = payloadTypeMonitors.computeIfAbsent(monitorName, payloadType -> monitorSupplier.get()); return messageMonitorForPayloadType.onMessageIngested(message); }
@Override public Object handle(UnitOfWork<? extends T> unitOfWork, InterceptorChain interceptorChain) throws Exception { T message = unitOfWork.getMessage(); logger.info("Incoming message: [{}]", message.getPayloadType().getSimpleName()); try { Object returnValue = interceptorChain.proceed(); logger.info("[{}] executed successfully with a [{}] return value", message.getPayloadType().getSimpleName(), returnValue == null ? "null" : returnValue.getClass().getSimpleName()); return returnValue; } catch (Exception t) { logger.warn(format("[%s] execution failed:", message.getPayloadType().getSimpleName()), t); throw t; } } }
@Override public boolean canHandle(Message<?> message) { return typeMatches(message) && payloadType.isAssignableFrom(message.getPayloadType()) && parametersMatch(message); }
@Override public boolean canHandle(Message<?> message) { return typeMatches(message) && payloadType.isAssignableFrom(message.getPayloadType()) && parametersMatch(message); }
@Override @SuppressWarnings("unchecked") public <E> Stream<E> filterCandidates(T message, Stream<E> candidates) { Property routingProperty = getProperty(message.getPayloadType(), routingKey); if (routingProperty == null) { return Stream.empty(); } Object routingValue = routingProperty.getValue(message.getPayload()); return candidates.filter(candidate -> matchesInstance(candidate, routingValue)); }
@Override @SuppressWarnings("unchecked") public <E> Stream<E> filterCandidates(T message, Stream<E> candidates) { Property routingProperty = getProperty(message.getPayloadType(), routingKey); if (routingProperty == null) { return Stream.empty(); } Object routingValue = routingProperty.getValue(message.getPayload()); return candidates.filter(candidate -> matchesInstance(candidate, routingValue)); }