在单独的线程中移动项目时,不完整的丛容器注入

时间:2017-08-31 18:50:16

标签: java multithreading maven plexus

我有一个涉及丛集容器的简化示例:

package de.vogel612.depanalyzer;

import de.vogel612.depanalyzer.dependency.MavenResolutionTaskReal;
import org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout;
import org.apache.maven.artifact.repository.layout.DefaultRepositoryLayout;
import org.apache.maven.artifact.repository.layout.FlatRepositoryLayout;
import org.apache.maven.repository.internal.MavenRepositorySystemUtils;
import org.codehaus.plexus.DefaultPlexusContainer;
import org.codehaus.plexus.PlexusContainer;
import org.eclipse.aether.RepositorySystem;
import org.eclipse.aether.connector.basic.BasicRepositoryConnectorFactory;
import org.eclipse.aether.impl.RemoteRepositoryManager;
import org.eclipse.aether.spi.connector.RepositoryConnectorFactory;
import org.eclipse.aether.spi.connector.transport.TransporterFactory;
import org.eclipse.aether.spi.locator.ServiceLocator;
import org.eclipse.aether.transport.file.FileTransporterFactory;
import org.eclipse.aether.transport.http.HttpTransporterFactory;

public class MCVE {

    private static final ServiceLocator serviceLocator = MavenRepositorySystemUtils.newServiceLocator()
            .addService(RepositoryConnectorFactory.class, BasicRepositoryConnectorFactory.class)
            .addService(TransporterFactory.class, FileTransporterFactory.class)
            .addService(TransporterFactory.class, HttpTransporterFactory.class);


    private static final PriorityBlockingQueue<ResolutionTask> taskQueue = new PriorityBlockingQueue<>();
    private static final ExecutorService consumer = Executors.newSingleThreadExector();
    // actually initialized in main, since number of threads is configurable
    private static ExecutorService workers = Executors.newFixedThreadPool(threads, runnable -> {
            Thread result = new Thread(runnable, "WorkerQueue Thread");
            result.setDaemon(true);
            return result;
        });

    public static void main(String[] args) throws Exception {

        PlexusContainer container = new DefaultPlexusContainer();
        // bind eclipse aether RepositorySystem
        container.addComponent(serviceLocator.getService(RepositorySystem.class), RepositorySystem.class, "default");
        container.addComponent(serviceLocator.getService(RemoteRepositoryManager.class), RemoteRepositoryManager.class, "default");

        container.addComponent(new DefaultRepositoryLayout(), ArtifactRepositoryLayout.class, "default");
        container.addComponent(new FlatRepositoryLayout(), ArtifactRepositoryLayout.class, "flat");

        taskQueue.put(container.lookup(MavenResolutionTask.class));
        // ... 
    }
}

请注意最后两行container.addComponent行。现在,当我在下面的设置中使用容器时,我发现了一些非常奇怪的行为:

public class ResolutionTask extends CompletableFuture<List<SomeType>> implements Runnable { 
    // actually contains an override for run, which does error handling
}

public class MavenResolutionTask extends ResolutionTask {
    @Requires
    Maven mavenInstance;

    @Override
    public void run() {
        // use the maven instance to compute some stuff:
        complete(Collections.singletonList());
    }
}

此代码工作正常并按预期执行:

List<DependencyResult> results = new ArrayList<>();
while (!taskQueue.isEmpty()) {
    ResolutionTask currentTask = taskQueue.poll();
    workers.execute(currentTask);
    results.addAll(currentTask.join());
}
results.forEach(System.out::println);

一旦这个简单的工作队列处理程序从主线程移出,如下所示,我收到一个异常:

consumer.execute(() -> {
    List<DependencyResult> results = new ArrayList<>();
    while (!taskQueue.isEmpty()) {
        ResolutionTask currentTask = taskQueue.poll();
        workers.execute(currentTask);
        results.addAll(currentTask.join());
    }
    results.forEach(System.out::println);
});
Exception in thread "pool-1-thread-1" java.util.concurrent.CompletionException: java.lang.RuntimeException: Exception occurred during maven invocation
    at java.util.concurrent.CompletableFuture.reportJoin(CompletableFuture.java:375)
    at java.util.concurrent.CompletableFuture.join(CompletableFuture.java:1934)
    at de.vogel612.depanalyzer.Main.lambda$main$1(Main.java:109)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
    at java.lang.Thread.run(Thread.java:748)
Caused by: java.lang.RuntimeException: Exception occurred during maven invocation
    at de.vogel612.depanalyzer.dependency.MavenResolutionTask.runImpl(MavenResolutionTask.java:35)
    at de.vogel612.depanalyzer.dependency.ResolutionTask.run(ResolutionTask.java:27)
    ... 3 more
    Suppressed: org.apache.maven.InternalErrorException: Internal error: java.lang.NullPointerException
        at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:164)
        at de.vogel612.depanalyzer.dependency.MavenResolutionTask.runImpl(MavenResolutionTask.java:32)
        ... 4 more
    Caused by: java.lang.NullPointerException
        at org.apache.maven.RepositoryUtils.getLayout(RepositoryUtils.java:217)
        at org.apache.maven.RepositoryUtils.toRepo(RepositoryUtils.java:201)
        at org.apache.maven.RepositoryUtils.toRepos(RepositoryUtils.java:191)
        at org.apache.maven.project.DefaultProjectBuilder$InternalConfig.<init>(DefaultProjectBuilder.java:684)
        at org.apache.maven.project.DefaultProjectBuilder.build(DefaultProjectBuilder.java:340)
        at org.apache.maven.DefaultMaven.collectProjects(DefaultMaven.java:637)
        at org.apache.maven.DefaultMaven.getProjectsForMavenReactor(DefaultMaven.java:586)
        at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:229)
        at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:152)
        ... 5 more

此异常的“根本原因”指向一个实际上没有帮助的地方。当maven尝试初始化远程存储库时,它会访问Map<String, ArtifactRepositoryLayout>包含条目{"default", new DefaultRepositoryLayout()},{"flat", new FlatRepositoryLayout()},但

此地图可在maven-compat's LegacyRepositorySystem中找到,但在consumer中访问taskQueue时未正确初始化。 当从consumer中取出等式时,代码确实产生了正确的输出......

为什么会这样?

0 个答案:

没有答案