Динамический Кафка Потребитель с АОП - PullRequest
0 голосов
/ 25 июня 2018

У меня есть несколько динамических потребителей Kafka (на основе идентификатора отдела и т. Д.), И вы можете найти код ниже.

По сути, я хотел записать время, затраченное на каждый метод onMessage()вызов, и поэтому я создал пользовательскую аннотацию уровня @LogExecutionTime и добавил его для onMessage() метода.Но мой logExecutionTime() из LogExecutionTimeAspect никогда не вызывается, несмотря на то, что мой onMessage() вызывается всякий раз, когда появляется сообщение по теме, а все остальное работает нормально.

Не могли бы вы помочь мне в этом?отсутствует LogExecutionTimeAspect класс, чтобы он начал работать?

LogExecutionTime:

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface LogExecutionTime {
}

LogExecutionTimeAspect класс:

@Aspect
@Component
public class LogExecutionTimeAspect {
    @Around("within(com.myproject..*) && @annotation(LogExecutionTime)")
    public Object logExecutionTime(ProceedingJoinPoint joinPoint) throws Throwable {
        long startTime = System.currentTimeMillis();
        Object object = joinPoint.proceed();
        long endTime = System.currentTimeMillis();
        System.out.println(" Time taken by Listener ::"+(endTime-startTime)+"ms");
        return object;
    }
}

DepartmentsMessageConsumer class:

@Component
public class DepartmentsMessageConsumer implements MessageListener  {

    @Value(value = "${spring.kafka.bootstrap-servers}" )
    private String bootstrapAddress;

    @PostConstruct
    public void init() {
        Map<String, Object> consumerProperties = new HashMap<>();
        consumerProperties.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, 
                                     bootstrapAddress);
        consumerProperties.put(ConsumerConfig.GROUP_ID_CONFIG, "DEPT_ID_HERE");
        ContainerProperties containerProperties = 
            new ContainerProperties("com.myproj.depts.topic");
        containerProperties.setMessageListener(this);
        DefaultKafkaConsumerFactory<String, Greeting> consumerFactory =
                new DefaultKafkaConsumerFactory<>(consumerProperties, 
                    new StringDeserializer(), 
                    new JsonDeserializer<>(Department.class));
        ConcurrentMessageListenerContainer container =
                new ConcurrentMessageListenerContainer<>(consumerFactory, 
                            containerProperties);
        container.start();
    }

    @Override
    @LogExecutionTime
    public void onMessage(Object message) {
        ConsumerRecord record = (ConsumerRecord) message;
        Department department = (Department)record.value();
        System.out.println(" department :: "+department);
    }
}

Класс ApplicationLauncher:

@SpringBootApplication
@EnableKafka
@EnableAspectJAutoProxy
@ComponentScan(basePackages = { "com.myproject" })
public class ApplicationLauncher extends SpringBootServletInitializer { 
    public static void main(String[] args) {
        SpringApplication.run(ApplicationLauncher.class, args);
    }
}

РЕДАКТИРОВАТЬ:

Я пытался @EnableAspectJAutoProxy(exposeProxy=true), но сделалне работает.

1 Ответ

0 голосов
/ 25 июня 2018

Вы должны включить эту опцию на @EnableAspectJAutoProxy:

/**
 * Indicate that the proxy should be exposed by the AOP framework as a {@code ThreadLocal}
 * for retrieval via the {@link org.springframework.aop.framework.AopContext} class.
 * Off by default, i.e. no guarantees that {@code AopContext} access will work.
 * @since 4.3.1
 */
boolean exposeProxy() default false;

С другой стороны, есть что-то вроде этого, которое будет лучше, чем AOP:

/**
 * A plugin interface that allows you to intercept (and possibly mutate) records received by the consumer. A primary use-case
 * is for third-party components to hook into the consumer applications for custom monitoring, logging, etc.
 *
 * <p>
 * This class will get consumer config properties via <code>configure()</code> method, including clientId assigned
 * by KafkaConsumer if not specified in the consumer config. The interceptor implementation needs to be aware that it will be
 * sharing consumer config namespace with other interceptors and serializers, and ensure that there are no conflicts.
 * <p>
 * Exceptions thrown by ConsumerInterceptor methods will be caught, logged, but not propagated further. As a result, if
 * the user configures the interceptor with the wrong key and value type parameters, the consumer will not throw an exception,
 * just log the errors.
 * <p>
 * ConsumerInterceptor callbacks are called from the same thread that invokes {@link org.apache.kafka.clients.consumer.KafkaConsumer#poll(long)}.
 * <p>
 * Implement {@link org.apache.kafka.common.ClusterResourceListener} to receive cluster metadata once it's available. Please see the class documentation for ClusterResourceListener for more information.
 */
public interface ConsumerInterceptor<K, V> extends Configurable {

ОБНОВЛЕНИЕ

@EnableAspectJAutoProxy(exposeProxy=true) не работало, и я знаю, что могу использовать перехватчик, но я хотел, чтобы он работал с AOP.

Тогда я предлагаю вам рассмотреть возможность разделения DepartmentsMessageConsumer и ConcurrentMessageListenerContainer.Я имею в виду переместить этот ConcurrentMessageListenerContainer в отдельный @Configuration класс.ApplicationLauncher - хороший кандидат.Сделайте это как @Bean и в зависимости от вашего DepartmentsMessageConsumer для инъекций.Суть в том, что вам нужно дать AOP шанс на инструмент DepartmentsMessageConsumer, но с @PostConstruct слишком рано создавать экземпляр и начинать потребление с Kafka.

...