使用exec-maven-plugin运行守护进程避免“IllegalThreadStateException” [英] Running daemon with exec-maven-plugin avoiding `IllegalThreadStateException`

查看:708
本文介绍了使用exec-maven-plugin运行守护进程避免“IllegalThreadStateException”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想运行一个应该在maven包阶段启动的守护程序线程。这就是我在pom.xml中所拥有的:

I would like to run a daemon thread which should start on maven package phase. This is what I have in pom.xml:

<build>
    <plugins>
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>exec-maven-plugin</artifactId>
            <version>1.2.1</version>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>java</goal>
                    </goals>
                </execution>                                
            </executions>
            <configuration>
                 <mainClass>com.test.Startup</mainClass>
                 <cleanupDaemonThreads>true</cleanupDaemonThreads>
            </configuration>
       </plugin>
  </plugins>
</build>

以下是Startup类的内容:

And here is the content of the class Startup:

public class Startup {

    public static class Testing extends Thread {
        @Override
        public void run() {
            while(true) {
                System.out.println("testing..");
            }
        }
    }

    public static void main(String[] list) throws Exception {
        Testing t = new Testing();
        t.setDaemon(true);
        t.start();
    }
}

线程开始运行但编译停止线程正在运行。经过一段时间(超时或其他)后,线程停止并继续编译。无论如何,我可以让这个线程在后台开始并让编译继续自己进行吗?

The thread starts to run but the compile stops while the thread is running. After some time (timeout or something) the thread stops and the compilation continues. Is there anyway I can get this thread to start on the background and make the compilation continue on its own?

maven的一些输出:

Some output from the maven:

[WARNING] thread Thread[Thread-1,5,com.test.Startup] was interrupted but is still alive after waiting at least 15000msecs
[WARNING] thread Thread[Thread-1,5,com.test.Startup] will linger despite being asked to die via interruption
[WARNING] NOTE: 1 thread(s) did not finish despite being asked to  via interruption. This is not a problem with exec:java, it is a problem with the running code. Although not serious, it should be remedied.
[WARNING] Couldn't destroy threadgroup org.codehaus.mojo.exec.ExecJavaMojo$IsolatedThreadGroup[name=com.test.Startup,maxpri=10]
java.lang.IllegalThreadStateException
    at java.lang.ThreadGroup.destroy(ThreadGroup.java:754)
    at org.codehaus.mojo.exec.ExecJavaMojo.execute(ExecJavaMojo.java:334)
    at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo(DefaultBuildPluginManager.java:101)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:209)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:153)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:145)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:84)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:59)
    at org.apache.maven.lifecycle.internal.LifecycleStarter.singleThreadedBuild(LifecycleStarter.java:183)
    at org.apache.maven.lifecycle.internal.LifecycleStarter.execute(LifecycleStarter.java:161)
    at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:320)
    at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:156)
    at org.apache.maven.cli.MavenCli.execute(MavenCli.java:537)
    at org.apache.maven.cli.MavenCli.doMain(MavenCli.java:196)
    at org.apache.maven.cli.MavenCli.main(MavenCli.java:141)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced(Launcher.java:290)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launch(Launcher.java:230)
    at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode(Launcher.java:409)
    at org.codehaus.plexus.classworlds.launcher.Launcher.main(Launcher.java:352)

我打算为这个线程创建一个套接字监听器,让它存在于只要maven关闭JVM,但目前似乎套接字在编译期间只会打开一段时间。

I am planning to create a socket listener to this thread and let it live in the background as long as the maven shuts the JVM down but currently it seems the socket will be on only for some time during the compilation.

推荐答案

发布问题评论部分讨论的答案。
这个解决方案对我有用!感谢 Andrew Logvinov

Posting the answer that is discussed in the comments section of the question. This solution worked for me! Thanks Andrew Logvinov


cleanupDaemonThreads = false

cleanupDaemonThreads = false

配置标签中的内容

<结构>
< mainClass> com.test.Startup< / mainClass>
< cleanupDaemonThreads> false< / cleanupDaemonThreads>
< / configuration>

这篇关于使用exec-maven-plugin运行守护进程避免“IllegalThreadStateException”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆