Вопрос

I started to read about for in join in Java so I goal is to verify messages in JavaMail which has attatchement.

I´m doubt if it´s rigth. Because I saw some tutorial spliting the array in the 2 parts for each lopp. I put my all class code here below to demonstrate what I did.

package service.forkinjoin;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.RecursiveTask;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.Part;
import javax.mail.internet.MimeBodyPart;

import service.FileUtil;

public class SepareteMessagesProcess extends RecursiveTask<List<Message>> {

    private static final long serialVersionUID = 7126215365819834781L;
    private List<Message> listMessages;
    private static List<Message> listMessagesToDelete = new ArrayList<>();
    private final int threadCount = Runtime.getRuntime().availableProcessors();

    public SepareteMessagesProcess(List<Message> listMessages) {
        this.listMessages = listMessages;
    }

    @Override
    protected List<Message> compute() {

        if (this.listMessages.size() <= threadCount) {
            try {
                this.separateMessages(this.listMessages);
            } catch (MessagingException | IOException e) {
                e.printStackTrace();
            }
        } else {
            int[] arrayMaxIndex = this.generateIndexs(this.listMessages);
            List<SepareteMessagesProcess> list = this.splitList(this.listMessages, arrayMaxIndex);
            invokeAll(list);
        }

        return listMessagesToDelete;
    }

    private void separateMessages(List<Message> listMessages) throws MessagingException, IOException {

        for (Iterator<Message> iterator = listMessages.iterator(); iterator.hasNext();) {

            Message message = (Message) iterator.next();

            if ((this.hasNoAttachment(message.getContentType()) || (this.hasNoXmlAttachment(message)))) {
                listMessagesToDelete.add(message);
            } 
        }
    }

    private List<SepareteMessagesProcess> splitList(List<Message> listMessages, int[] arrayMaxIndex) {

        List<SepareteMessagesProcess> list = new ArrayList<>(this.threadCount);
        int end = 0;
        int start = 0;

        for (int i = 0; i < this.threadCount; i++) {
            end += (arrayMaxIndex[i]);
            list.add(new SepareteMessagesProcess(listMessages.subList(start, end)));
            start += arrayMaxIndex[i];
        }

        return list;
    }

    private int[] generateIndexs(List<Message> listMessages) {

        int value = listMessages.size() / this.threadCount;
        int[] arrayMaxIndex = new int[this.threadCount];

        for (int i = 0; i < this.threadCount; i++) {
            arrayMaxIndex[i] = value;
        }
        arrayMaxIndex[this.threadCount - 1] += listMessages.size() % threadCount;

        return arrayMaxIndex;
    }

    private boolean hasNoAttachment(String content) {
        return !content.contains("multipart/MIXED");
    }

    private boolean hasNoXmlAttachment(Message message) throws IOException, MessagingException {

        Multipart multipart = (Multipart) message.getContent();         

        for (int i = 0; i < multipart.getCount(); i++) {

            MimeBodyPart mimeBodyPart = (MimeBodyPart) multipart.getBodyPart(i);

            if (Part.ATTACHMENT.equalsIgnoreCase(mimeBodyPart.getDisposition())) {

                if  (FileUtil.isXmlFile(mimeBodyPart.getFileName())) {
                    return false;
                }
            }
        }

        return true;
    }
}
Это было полезно?

Решение

No. Rather than rewrite the tutorial here, just follow the example it gives, such as:

 if (problem is small)
     directly solve problem
   else {
    split problem into independent parts
    fork new subtasks to solve each part
    join all subtasks
    compose result from subresults
}
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top