org.apache.rocketmq.spring.core.RocketMQListener Java Examples

The following examples show how to use org.apache.rocketmq.spring.core.RocketMQListener. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: DefaultRocketMQListenerContainer.java    From rocketmq-spring with Apache License 2.0 5 votes vote down vote up
private Type getMessageType() {
    Class<?> targetClass;
    if (rocketMQListener != null) {
        targetClass = AopProxyUtils.ultimateTargetClass(rocketMQListener);
    } else {
        targetClass = AopProxyUtils.ultimateTargetClass(rocketMQReplyListener);
    }
    Type matchedGenericInterface = null;
    while (Objects.nonNull(targetClass)) {
        Type[] interfaces = targetClass.getGenericInterfaces();
        if (Objects.nonNull(interfaces)) {
            for (Type type : interfaces) {
                if (type instanceof ParameterizedType &&
                    (Objects.equals(((ParameterizedType) type).getRawType(), RocketMQListener.class) || Objects.equals(((ParameterizedType) type).getRawType(), RocketMQReplyListener.class))) {
                    matchedGenericInterface = type;
                    break;
                }
            }
        }
        targetClass = targetClass.getSuperclass();
    }
    if (Objects.isNull(matchedGenericInterface)) {
        return Object.class;
    }

    Type[] actualTypeArguments = ((ParameterizedType) matchedGenericInterface).getActualTypeArguments();
    if (Objects.nonNull(actualTypeArguments) && actualTypeArguments.length > 0) {
        return actualTypeArguments[0];
    }
    return Object.class;
}
 
Example #2
Source File: ListenerContainerConfiguration.java    From rocketmq-spring with Apache License 2.0 5 votes vote down vote up
private DefaultRocketMQListenerContainer createRocketMQListenerContainer(String name, Object bean,
    RocketMQMessageListener annotation) {
    DefaultRocketMQListenerContainer container = new DefaultRocketMQListenerContainer();

    container.setRocketMQMessageListener(annotation);

    String nameServer = environment.resolvePlaceholders(annotation.nameServer());
    nameServer = StringUtils.isEmpty(nameServer) ? rocketMQProperties.getNameServer() : nameServer;
    String accessChannel = environment.resolvePlaceholders(annotation.accessChannel());
    container.setNameServer(nameServer);
    if (!StringUtils.isEmpty(accessChannel)) {
        container.setAccessChannel(AccessChannel.valueOf(accessChannel));
    }
    container.setTopic(environment.resolvePlaceholders(annotation.topic()));
    String tags = environment.resolvePlaceholders(annotation.selectorExpression());
    if (!StringUtils.isEmpty(tags)) {
        container.setSelectorExpression(tags);
    }
    container.setConsumerGroup(environment.resolvePlaceholders(annotation.consumerGroup()));
    if (RocketMQListener.class.isAssignableFrom(bean.getClass())) {
        container.setRocketMQListener((RocketMQListener) bean);
    } else if (RocketMQReplyListener.class.isAssignableFrom(bean.getClass())) {
        container.setRocketMQReplyListener((RocketMQReplyListener) bean);
    }
    container.setMessageConverter(rocketMQMessageConverter.getMessageConverter());
    container.setName(name);

    return container;
}
 
Example #3
Source File: DefaultRocketMQListenerContainer.java    From rocketmq-spring with Apache License 2.0 4 votes vote down vote up
public RocketMQListener getRocketMQListener() {
    return rocketMQListener;
}
 
Example #4
Source File: DefaultRocketMQListenerContainer.java    From rocketmq-spring with Apache License 2.0 4 votes vote down vote up
public void setRocketMQListener(RocketMQListener rocketMQListener) {
    this.rocketMQListener = rocketMQListener;
}
 
Example #5
Source File: ListenerContainerConfiguration.java    From rocketmq-spring with Apache License 2.0 4 votes vote down vote up
private void registerContainer(String beanName, Object bean) {
    Class<?> clazz = AopProxyUtils.ultimateTargetClass(bean);

    if (RocketMQListener.class.isAssignableFrom(bean.getClass()) && RocketMQReplyListener.class.isAssignableFrom(bean.getClass())) {
        throw new IllegalStateException(clazz + " cannot be both instance of " + RocketMQListener.class.getName() + " and " + RocketMQReplyListener.class.getName());
    }

    if (!RocketMQListener.class.isAssignableFrom(bean.getClass()) && !RocketMQReplyListener.class.isAssignableFrom(bean.getClass())) {
        throw new IllegalStateException(clazz + " is not instance of " + RocketMQListener.class.getName() + " or " + RocketMQReplyListener.class.getName());
    }

    RocketMQMessageListener annotation = clazz.getAnnotation(RocketMQMessageListener.class);

    String consumerGroup = this.environment.resolvePlaceholders(annotation.consumerGroup());
    String topic = this.environment.resolvePlaceholders(annotation.topic());

    boolean listenerEnabled =
        (boolean) rocketMQProperties.getConsumer().getListeners().getOrDefault(consumerGroup, Collections.EMPTY_MAP)
            .getOrDefault(topic, true);

    if (!listenerEnabled) {
        log.debug(
            "Consumer Listener (group:{},topic:{}) is not enabled by configuration, will ignore initialization.",
            consumerGroup, topic);
        return;
    }
    validate(annotation);

    String containerBeanName = String.format("%s_%s", DefaultRocketMQListenerContainer.class.getName(),
        counter.incrementAndGet());
    GenericApplicationContext genericApplicationContext = (GenericApplicationContext) applicationContext;

    genericApplicationContext.registerBean(containerBeanName, DefaultRocketMQListenerContainer.class,
        () -> createRocketMQListenerContainer(containerBeanName, bean, annotation));
    DefaultRocketMQListenerContainer container = genericApplicationContext.getBean(containerBeanName,
        DefaultRocketMQListenerContainer.class);
    if (!container.isRunning()) {
        try {
            container.start();
        } catch (Exception e) {
            log.error("Started container failed. {}", container, e);
            throw new RuntimeException(e);
        }
    }

    log.info("Register the listener to container, listenerBeanName:{}, containerBeanName:{}", beanName, containerBeanName);
}
 
Example #6
Source File: RocketMQListenerBindingContainer.java    From spring-cloud-alibaba with Apache License 2.0 4 votes vote down vote up
@Override
public void setupMessageListener(RocketMQListener<?> rocketMQListener) {
	this.rocketMQListener = rocketMQListener;
}
 
Example #7
Source File: RocketMQListenerBindingContainer.java    From spring-cloud-alibaba with Apache License 2.0 4 votes vote down vote up
public RocketMQListener getRocketMQListener() {
	return rocketMQListener;
}
 
Example #8
Source File: RocketMQListenerBindingContainer.java    From spring-cloud-alibaba with Apache License 2.0 4 votes vote down vote up
public void setRocketMQListener(RocketMQListener rocketMQListener) {
	this.rocketMQListener = rocketMQListener;
}