use of io.micronaut.inject.ExecutableMethod in project micronaut-jms by micronaut-projects.
the class AbstractJMSListenerMethodProcessor method generateAndBindListener.
@SuppressWarnings("unchecked")
private MessageListener generateAndBindListener(Object bean, ExecutableMethod<?, ?> method, ExecutorService executor, boolean acknowledge) {
return message -> executor.submit(() -> {
try {
DefaultExecutableBinder<Message> binder = new DefaultExecutableBinder<>();
BoundExecutable boundExecutable = binder.bind(method, jmsArgumentBinderRegistry, message);
boundExecutable.invoke(bean);
if (acknowledge) {
try {
message.acknowledge();
} catch (JMSException e) {
logger.error("Failed to acknowledge receipt of message with the broker. " + "This message may be falsely retried.", e);
throw new MessageAcknowledgementException(e.getMessage(), e);
}
}
} catch (Exception e) {
logger.error("Failed to process a message: " + message + " " + e.getMessage(), e);
}
});
}
use of io.micronaut.inject.ExecutableMethod in project micronaut-jms by micronaut-projects.
the class JMSProducerMethodInterceptor method intercept.
@Override
public Object intercept(MethodInvocationContext<Object, Object> context) {
if (!context.hasAnnotation(JMSProducer.class)) {
return context.proceed();
}
ExecutableMethod<?, ?> method = context.getExecutableMethod();
Class<? extends Annotation> annotationType;
JMSDestinationType destinationType;
if (method.hasAnnotation(Queue.class)) {
annotationType = Queue.class;
destinationType = QUEUE;
} else if (method.hasAnnotation(Topic.class)) {
annotationType = Topic.class;
destinationType = TOPIC;
} else {
return context.proceed();
}
String connectionFactory = method.stringValue(JMSProducer.class).orElseThrow(() -> new ConfigurationException("@JMSProducer must specify a connection factory."));
String destinationName = method.stringValue(annotationType).orElseThrow(() -> new ConfigurationException("@" + annotationType.getSimpleName() + " must specify a destination."));
Map<String, Object> parameterValueMap = context.getParameterValueMap();
Object body = Arrays.stream(method.getArguments()).filter(arg -> arg.isDeclaredAnnotationPresent(MessageBody.class)).map(arg -> parameterValueMap.get(arg.getName())).findFirst().orElseThrow(() -> new ConfigurationException("One method argument must be annotated with @Body"));
String serializerName = method.stringValue(annotationType, "serializer").orElse(null);
Serializer serializer = serializerName == null ? beanContext.getBean(DefaultSerializerDeserializer.class) : beanContext.getBean(Serializer.class, Qualifiers.byName(serializerName));
MessageHeader[] headers = Arrays.stream(method.getArguments()).filter(arg -> arg.isDeclaredAnnotationPresent(io.micronaut.messaging.annotation.MessageHeader.class)).map(arg -> {
String argName = arg.getName();
String headerName = arg.getAnnotationMetadata().stringValue(io.micronaut.messaging.annotation.MessageHeader.class).orElseThrow(() -> new IllegalArgumentException("@Header annotation on argument '" + argName + "' must have a name"));
return new MessageHeader(headerName, parameterValueMap.get(argName));
}).toArray(MessageHeader[]::new);
JMSConnectionPool pool = beanContext.getBean(JMSConnectionPool.class, Qualifiers.byName(connectionFactory));
JmsProducer producer = new JmsProducer(destinationType, pool, serializer);
producer.send(destinationName, body, headers);
return null;
}
Aggregations