Search in sources :

Example 1 with JMSDestinationType

use of io.micronaut.jms.model.JMSDestinationType in project micronaut-jms by micronaut-projects.

the class AbstractJMSListenerMethodProcessor method registerListener.

private void registerListener(ExecutableMethod<?, ?> method, String connectionFactoryName, BeanDefinition<?> beanDefinition, AnnotationValue<T> destinationAnnotation, JMSDestinationType type) {
    validateArguments(method);
    final Class<?> targetClass = Stream.of(method.getArguments()).filter(arg -> arg.isDeclaredAnnotationPresent(MessageBody.class) || arg.isDeclaredAnnotationPresent(io.micronaut.jms.annotations.Message.class)).findAny().map(Argument::getClass).get();
    final String destination = destinationAnnotation.getRequiredValue(String.class);
    final int acknowledgeMode = destinationAnnotation.getRequiredValue("acknowledgeMode", Integer.class);
    final boolean transacted = destinationAnnotation.getRequiredValue("transacted", Boolean.class);
    final JMSListenerContainerFactory listenerFactory = beanContext.findBean(JMSListenerContainerFactory.class).orElseThrow(() -> new IllegalStateException("No JMSListenerFactory configured"));
    final JMSConnectionPool connectionPool = beanContext.getBean(JMSConnectionPool.class, Qualifiers.byName(connectionFactoryName));
    final Object bean = beanContext.findBean(beanDefinition.getBeanType()).get();
    final ExecutorService executor = getExecutorService(destinationAnnotation);
    MessageListener listener = generateAndBindListener(bean, method, executor, CLIENT_ACKNOWLEDGE == acknowledgeMode);
    listenerFactory.registerListener(connectionPool, destination, listener, targetClass, transacted, acknowledgeMode, type);
}
Also used : JMSListener(io.micronaut.jms.annotations.JMSListener) BeanContext(io.micronaut.context.BeanContext) JMSConnectionPool(io.micronaut.jms.pool.JMSConnectionPool) LoggerFactory(org.slf4j.LoggerFactory) MessageListener(javax.jms.MessageListener) ExecutableMethod(io.micronaut.inject.ExecutableMethod) JMSDestinationType(io.micronaut.jms.model.JMSDestinationType) JMSListenerContainerFactory(io.micronaut.jms.listener.JMSListenerContainerFactory) Argument(io.micronaut.core.type.Argument) BoundExecutable(io.micronaut.core.bind.BoundExecutable) Message(javax.jms.Message) DefaultExecutableBinder(io.micronaut.core.bind.DefaultExecutableBinder) ExecutorService(java.util.concurrent.ExecutorService) MessageAcknowledgementException(io.micronaut.messaging.exceptions.MessageAcknowledgementException) Logger(org.slf4j.Logger) Qualifiers(io.micronaut.inject.qualifiers.Qualifiers) ExecutableMethodProcessor(io.micronaut.context.processor.ExecutableMethodProcessor) JMSException(javax.jms.JMSException) Assert(io.micronaut.jms.util.Assert) Stream(java.util.stream.Stream) AnnotationValue(io.micronaut.core.annotation.AnnotationValue) Annotation(java.lang.annotation.Annotation) BeanDefinition(io.micronaut.inject.BeanDefinition) JMSArgumentBinderRegistry(io.micronaut.jms.bind.JMSArgumentBinderRegistry) MessageBody(io.micronaut.messaging.annotation.MessageBody) CLIENT_ACKNOWLEDGE(javax.jms.Session.CLIENT_ACKNOWLEDGE) Message(javax.jms.Message) MessageListener(javax.jms.MessageListener) JMSConnectionPool(io.micronaut.jms.pool.JMSConnectionPool) JMSListenerContainerFactory(io.micronaut.jms.listener.JMSListenerContainerFactory) ExecutorService(java.util.concurrent.ExecutorService) MessageBody(io.micronaut.messaging.annotation.MessageBody)

Example 2 with JMSDestinationType

use of io.micronaut.jms.model.JMSDestinationType 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;
}
Also used : ConfigurationException(io.micronaut.context.exceptions.ConfigurationException) Arrays(java.util.Arrays) TOPIC(io.micronaut.jms.model.JMSDestinationType.TOPIC) BeanContext(io.micronaut.context.BeanContext) JMSConnectionPool(io.micronaut.jms.pool.JMSConnectionPool) QUEUE(io.micronaut.jms.model.JMSDestinationType.QUEUE) Qualifiers(io.micronaut.inject.qualifiers.Qualifiers) Singleton(jakarta.inject.Singleton) Queue(io.micronaut.jms.annotations.Queue) DefaultSerializerDeserializer(io.micronaut.jms.serdes.DefaultSerializerDeserializer) Topic(io.micronaut.jms.annotations.Topic) ExecutableMethod(io.micronaut.inject.ExecutableMethod) JmsProducer(io.micronaut.jms.templates.JmsProducer) JMSDestinationType(io.micronaut.jms.model.JMSDestinationType) JMSProducer(io.micronaut.jms.annotations.JMSProducer) MethodInterceptor(io.micronaut.aop.MethodInterceptor) MethodInvocationContext(io.micronaut.aop.MethodInvocationContext) Map(java.util.Map) Annotation(java.lang.annotation.Annotation) MessageHeader(io.micronaut.jms.model.MessageHeader) Serializer(io.micronaut.jms.serdes.Serializer) MessageBody(io.micronaut.messaging.annotation.MessageBody) DefaultSerializerDeserializer(io.micronaut.jms.serdes.DefaultSerializerDeserializer) JmsProducer(io.micronaut.jms.templates.JmsProducer) JMSProducer(io.micronaut.jms.annotations.JMSProducer) JMSDestinationType(io.micronaut.jms.model.JMSDestinationType) JMSConnectionPool(io.micronaut.jms.pool.JMSConnectionPool) ConfigurationException(io.micronaut.context.exceptions.ConfigurationException) MessageHeader(io.micronaut.jms.model.MessageHeader) Topic(io.micronaut.jms.annotations.Topic) Serializer(io.micronaut.jms.serdes.Serializer)

Aggregations

BeanContext (io.micronaut.context.BeanContext)2 ExecutableMethod (io.micronaut.inject.ExecutableMethod)2 Qualifiers (io.micronaut.inject.qualifiers.Qualifiers)2 JMSDestinationType (io.micronaut.jms.model.JMSDestinationType)2 JMSConnectionPool (io.micronaut.jms.pool.JMSConnectionPool)2 MessageBody (io.micronaut.messaging.annotation.MessageBody)2 Annotation (java.lang.annotation.Annotation)2 MethodInterceptor (io.micronaut.aop.MethodInterceptor)1 MethodInvocationContext (io.micronaut.aop.MethodInvocationContext)1 ConfigurationException (io.micronaut.context.exceptions.ConfigurationException)1 ExecutableMethodProcessor (io.micronaut.context.processor.ExecutableMethodProcessor)1 AnnotationValue (io.micronaut.core.annotation.AnnotationValue)1 BoundExecutable (io.micronaut.core.bind.BoundExecutable)1 DefaultExecutableBinder (io.micronaut.core.bind.DefaultExecutableBinder)1 Argument (io.micronaut.core.type.Argument)1 BeanDefinition (io.micronaut.inject.BeanDefinition)1 JMSListener (io.micronaut.jms.annotations.JMSListener)1 JMSProducer (io.micronaut.jms.annotations.JMSProducer)1 Queue (io.micronaut.jms.annotations.Queue)1 Topic (io.micronaut.jms.annotations.Topic)1