Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a customisation annotation for splitting request param and collect return result?

I have a method params is a list which is lager than 50000 items; Limited to the business logic, the list must less than 30000, so that I have a method to split this array to 2d array before the logic

public static final <T> Collection<List<T>> partitionBasedOnSize(List<T> inputList, int size) {
        AtomicInteger counter = new AtomicInteger(0);
        return inputList.stream().collect(Collectors.groupingBy(s -> counter.getAndIncrement() / size)).values();
}

This is my current solution:

public List<Account> getChildrenList(List<Long> ids) {
        List<Account> childrenList = new ArrayList<>();
        Collection<List<Long>> childrenId2dList = PartitionArray.partitionBasedOnSize(childrenIdsList, 30000);
        for (List<Long> list : childrenId2dList) {
            //this is my business logic: start
            childrenList.addAll(accountRepository.getAccounts(list));
            //this is my business logic: end
        }
        return childrenAccountsList;
}

I would like to create an annotation on top of the method instead of many duplicate code(check and spite each time...)

Sorry for my bad english, I have draw a diagram: method called>spite array>business logic>collect all result>return enter image description here

like image 706
Ben Luk Avatar asked Oct 15 '18 04:10

Ben Luk


1 Answers

Here is a solution using annotations, using AspectJ :

First, define the desired annotation:

package partition;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

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

    /**
     * The size of the partition, for instance 30000
     */
    int size();
}

Thus your service will look like:

package partition;

import java.util.List;

public class AccountService {

    private AccountRepository accountRepository;

    public AccountService(AccountRepository accountRepository) {
        this.accountRepository = accountRepository;
    }

    @Partitioned(size = 30000)
    public List<Account> getAccounts(List<Long> ids) {
        return accountRepository.getAccounts(ids);
    }
}

So far it's easy. Then comes the processing of the annotation, where AspectJ enters the game. Define an aspect linked to the annotation:

package partition;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

@Aspect
public class PartitionedAspect {

    @Pointcut("@annotation(partitioned)")
    public void callAt(Partitioned partitioned) {
    }

    @Around("callAt(partitioned)")
    public <T, D> Object around(ProceedingJoinPoint pjp, Partitioned partitioned) throws Throwable {
        List<T> inputIds = (List) pjp.getArgs()[0];
        if (inputIds.size() > partitioned.size()) {
            List<D> dataList = new ArrayList<>();
            Collection<List<T>> partitionedIds = PartitionArray.partitionBasedOnSize(inputIds, partitioned.size());
            for (List<T> idList : partitionedIds) {
                List<D> data = (List) pjp.proceed(new Object[]{idList});
                dataList.addAll(data);
            }
            return dataList;
        }
        return pjp.proceed();
    }
}

Of course you have to import AspectJ, and also do some additional stuff at compile-time. Assuming you are using maven, add those lines to your pom.xml (plugin and dependencies):

<build>
    <plugins>
        ...
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>aspectj-maven-plugin</artifactId>
            <version>1.7</version>
            <configuration>
                <complianceLevel>1.8</complianceLevel>
                <source>1.8</source>
                <target>1.8</target>
                <showWeaveInfo>true</showWeaveInfo>
                <verbose>true</verbose>
                <Xlint>ignore</Xlint>
                <encoding>UTF-8</encoding>
            </configuration>
            <executions>
                <execution>
                    <goals>
                        <goal>compile</goal>
                        <goal>test-compile</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

<dependencies>
    ...
    <dependency>
        <groupId>org.aspectj</groupId>
        <artifactId>aspectjrt</artifactId>
        <version>${aspectj.version}</version>
    </dependency>
    <dependency>
        <groupId>org.aspectj</groupId>
        <artifactId>aspectjweaver</artifactId>
        <version>${aspectj.version}</version>
    </dependency>
...
</dependencies>
like image 106
Benoit Avatar answered Nov 15 '22 09:11

Benoit