Solo  当前访客:6 开始使用

JAVA日知录

一个关注| Java | Spring Boot | Spring Cloud | 干货分享的博客网站

AOP 更新 ElasticSearch 索引---更新索引

2020-04-23 14:30:04 shuikan95
0  评论    180  浏览

更新索引 相关文件

  • UpdateItemDTO
package com.lab.es.demo.dto;

import lombok.Data;

import java.io.Serializable;
import java.math.BigDecimal;

@Data
public class UpdateItemDTO implements Serializable {
    private String id;
    private String itemName;
    private BigDecimal itemPrice;
    private Long monthlySales;
}
  • IndexHandle
package com.lab.es.demo.annotation;

import java.lang.annotation.*;

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface IndexHandle {
    String desc();
}
  • IndexAspect
package com.lab.es.demo.aspect;

import com.lab.es.demo.annotation.IndexHandle;
import com.lab.es.demo.dto.UpdateItemDTO;
import com.lab.es.demo.message.SendMessage;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;

@Slf4j
@Aspect
@Component
@Order(99)
public class IndexAspect {
    @Autowired
    private SendMessage sendMessage;

    @Pointcut("@annotation(com.lab.es.demo.annotation.IndexHandle)")
    public void annotationPoinCut() {
    }

    @AfterReturning(value = "annotationPoinCut()", returning = "returnValue")
    public void afterReturning(JoinPoint joinPoint, Object returnValue) {
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        Method method = signature.getMethod();
        IndexHandle indexHandle = method.getAnnotation(IndexHandle.class);

        Object[] args = joinPoint.getArgs();
        sendMessage.sendItem((UpdateItemDTO) args[0]);
        log.info(indexHandle.desc() + " afterReturning: 返回值: " + returnValue);
    }
}
  • SendMessage
package com.lab.es.demo.message;

import com.lab.es.demo.dto.UpdateItemDTO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.stereotype.Component;

@Slf4j
@Component
public class SendMessage {
    @Value("${rocketmq.topic.testDelayTopic}")
    private String testDelayTopic;

    @Autowired
    private RocketMQTemplate rocketMQTemplate;

    public void sendItem(UpdateItemDTO updateItemDTO) {
        log.info(StringUtils.center("testDelayTopic", 52, "="));

        SendResult sendResult = rocketMQTemplate.syncSend(testDelayTopic, MessageBuilder.withPayload(updateItemDTO).build(),3000,3);
        log.info("syncSend to topic {} sendResult={} %n", testDelayTopic, sendResult);
    }
}
  • ReceiveMessage
package com.lab.es.demo.message;

import com.alibaba.fastjson.JSON;
import com.lab.es.demo.dto.UpdateItemDTO;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.action.update.UpdateResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

@Slf4j
@Component
@RocketMQMessageListener(topic = "${rocketmq.topic.testDelayTopic}", consumerGroup = "delayConsumerGroup")
public class ReceiveMessage implements RocketMQListener<UpdateItemDTO> {
    @Autowired
    @Qualifier("restHighLevelClient")
    private RestHighLevelClient client;

    @SneakyThrows
    @Override
    public void onMessage(UpdateItemDTO updateItemDTO) {
        UpdateRequest updateRequest = new UpdateRequest("tmall_items", updateItemDTO.getId());
        updateRequest.timeout("5s");

        updateRequest.doc(JSON.toJSONString(updateItemDTO), XContentType.JSON);
        UpdateResponse updateResponse = client.update(updateRequest, RequestOptions.DEFAULT);
        System.out.println(updateResponse.status());
    }
}
  • RocketMQ 异步更新 搜索到 美妆 的第一个商品

image.png

  • AopTest 商品名 追加 666,价格改为 209.99,月销改为 666
package com.lab.es.demo;

import com.lab.es.demo.dto.UpdateItemDTO;
import com.lab.es.demo.service.ItemService;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.math.BigDecimal;
import java.util.Scanner;

@Slf4j
@RunWith(SpringRunner.class)
@SpringBootTest(classes = BookESApplication.class)
public class AopTest {
    @Autowired
    private ItemService itemService;

    @Test
    public void updateItemIndex() throws Exception {
        UpdateItemDTO itemDTO = new UpdateItemDTO();
        itemDTO.setId("612037593613");
        itemDTO.setItemName("爆款抖音火口红6支套装多色一盒持久不脱色国货彩妆美妆666");
        itemDTO.setItemPrice(BigDecimal.valueOf(209.99));
        itemDTO.setMonthlySales(666L);
        itemService.updateItem(itemDTO);

        Scanner input = new Scanner(System.in);
        String str = input.next();
    }
}

AOP 更新 ElasticSearch 索引

  • 执行测试用例
mvn test -Dtest=com.lab.es.demo.AopTest#updateItemIndex

image.png

  • 重新搜索 美妆 看 更新索引后的商品展示

image.png


标题:AOP 更新 ElasticSearch 索引---更新索引
作者:shuikan95
地址:http://www.javadaily.cn/articles/2020/04/22/1587556096367.html

, ,

发表评论


TOP