尽管被要求通过中断死亡,但计时器线程仍然存在

时间:2016-02-17 14:29:29

标签: java multithreading maven timer apache-camel

我正在尝试使用JDBC轮询数据库,但无论采取何种方法,我都会遇到此错误。我已经尝试了解决方案HERE但似乎没有任何工作

我的java文件'App.java'

package mhealth;


import org.apache.camel.CamelContext;
import org.apache.camel.Exchange;
import org.apache.camel.Processor;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.component.jms.JmsComponent;
import org.apache.camel.impl.DefaultCamelContext;


import javax.sql.DataSource;
import org.apache.camel.impl.SimpleRegistry;
import org.apache.commons.dbcp.BasicDataSource;




public class App {


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

        String url = "jdbc:postgresql://x.x.x.x:5432/mhealth";

        DataSource dataSource = setupDataSource(url);


        SimpleRegistry reg = new SimpleRegistry() ;
        reg.put("myDataSource",dataSource);


        CamelContext context = new DefaultCamelContext(reg);
        context.addRoutes(new App().new MyRouteBuilder());
        context.start();


    }

    class MyRouteBuilder extends RouteBuilder {
        public void configure() {


        from("timer://Timer?period=3s")
                    .setBody(constant("SELECT count(*)>0 as count FROM forms_data " + 
                        "where created_time > (now() - INTERVAL '4 days' + interval '5 hours 30 minutes')" +
                         "OR last_updated_time > (now() - INTERVAL '4 days' + interval '5 hours 30 minutes') "))
                    .to("jdbc:myDataSource")
                    .split(body())
                    .choice()
                        .when(body().convertToString().contains("count=t"))
                            .setBody(constant("select * from beneficiary_journey"))
                            .to("jdbc:myDataSource")
                            .split(body())
                            .to("stream:out");
        }



    }


    private static DataSource setupDataSource(String connectURI) {

        BasicDataSource ds = new BasicDataSource();
        ds.setDriverClassName("org.postgresql.Driver");

        ds.setUsername("postgres");
        ds.setPassword("postgres");

        ds.setUrl(connectURI);
        System.out.println("works!!");
        return ds;

   }
}

我的POM文件

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>test</groupId>
  <artifactId>healthTest</artifactId>
  <version>1.0-SNAPSHOT</version>
  <!-- <packaging>jar</packaging> -->

  <name>healthTest</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

  <dependencies>

    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
    </dependency>

    <dependency>
        <groupId>org.postgresql</groupId>
        <artifactId>postgresql</artifactId>
        <version>9.3-1100-jdbc41</version>
    </dependency>

    <dependency>
      <groupId>org.apache.camel</groupId>
      <artifactId>camel-core</artifactId>
      <version>2.16.1</version>
    </dependency>

    <dependency>
      <groupId>org.apache.camel</groupId>
      <artifactId>camel-jms</artifactId>
      <version>2.16.1</version>
    </dependency>

    <dependency>
      <groupId>commons-dbcp</groupId>
      <artifactId>commons-dbcp</artifactId>
      <version>1.2.2</version>
    </dependency>

    <dependency>
      <groupId>org.apache.camel</groupId>
      <artifactId>camel-jdbc</artifactId>
      <version>2.16.1</version>
    </dependency>

    <dependency>
      <groupId>org.apache.camel</groupId>
      <artifactId>camel-stream</artifactId>
      <version>2.16.1</version>
    </dependency>

    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <version>5.1.38</version>
    </dependency>

    <dependency>
      <groupId>log4j</groupId>
      <artifactId>log4j</artifactId>
      <version>1.2.17</version>
    </dependency>

    <dependency>
      <groupId>org.slf4j</groupId>
      <artifactId>slf4j-log4j12</artifactId>
      <version>1.7.14</version>
    </dependency>


</dependencies>

<build>
    <plugins>

      <!-- Allows the example to be run via 'mvn compile exec:java' -->
      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>exec-maven-plugin</artifactId>
        <version>1.2.1</version>
        <configuration>
          <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>java</goal>
                    </goals>
                </execution>                                
            </executions>
          <!-- <includePluginDependencies>false</includePluginDependencies> -->
          <mainClass>mhealth.App</mainClass>
          <cleanupDaemonThreads>false</cleanupDaemonThreads>
        </configuration>
      </plugin>
    </plugins>
</build>

</project>

我跑的时候得到的错误

mvn install && mvn exec:java -Dexec.mainClass="mhealth.App"

它轮询了几秒钟,然后我在我的控制台上出现了这个错误

[WARNING] thread Thread[Camel (camel-1) thread #0 - timer://Timer,5,mhealth.App] was interrupted but is still alive after waiting at least 15000msecs
[WARNING] thread Thread[Camel (camel-1) thread #0 - timer://Timer,5,mhealth.App] will linger despite being asked to die via interruption
[WARNING] thread Thread[Abandoned connection cleanup thread,5,mhealth.App] will linger despite being asked to die via interruption
[WARNING] thread Thread[Timer-0,5,mhealth.App] will linger despite being asked to die via interruption
[WARNING] NOTE: 3 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=mhealth.App,maxpri=10]
java.lang.IllegalThreadStateException
    at java.lang.ThreadGroup.destroy(ThreadGroup.java:775)
    at org.codehaus.mojo.exec.ExecJavaMojo.execute(ExecJavaMojo.java:334)
    at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo(DefaultBuildPluginManager.java:132)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:208)
    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:116)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:80)
    at org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder.build(SingleThreadedBuilder.java:51)
    at org.apache.maven.lifecycle.internal.LifecycleStarter.execute(LifecycleStarter.java:120)
    at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:355)
    at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:155)
    at org.apache.maven.cli.MavenCli.execute(MavenCli.java:584)
    at org.apache.maven.cli.MavenCli.doMain(MavenCli.java:216)
    at org.apache.maven.cli.MavenCli.main(MavenCli.java:160)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced(Launcher.java:289)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launch(Launcher.java:229)
    at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode(Launcher.java:415)
    at org.codehaus.plexus.classworlds.launcher.Launcher.main(Launcher.java:356)
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 17.996 s
[INFO] Finished at: 2016-02-17T19:45:04+05:30
[INFO] Final Memory: 13M/172M

当然,如果我尝试<cleanupDaemonThreads>true</cleanupDaemonThreads> 程序刚刚成功构建并停止,我需要程序继续运行和轮询

[INFO] Scanning for projects...
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building healthTest 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ healthTest ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory /home/ashish/Desktop/healthTest/src/main/resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ healthTest ---
[INFO] Nothing to compile - all classes are up to date
[INFO] 
[INFO] >>> exec-maven-plugin:1.2.1:java (default-cli) > validate @ healthTest >>>
[INFO] 
[INFO] <<< exec-maven-plugin:1.2.1:java (default-cli) < validate @ healthTest <<<
[INFO] 
[INFO] --- exec-maven-plugin:1.2.1:java (default-cli) @ healthTest ---
works!!
log4j:WARN No appenders could be found for logger (org.apache.camel.impl.DefaultCamelContext).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2.952 s
[INFO] Finished at: 2016-02-17T19:55:57+05:30
[INFO] Final Memory: 19M/169M
[INFO] ------------------------------------------------------------------------

对于这篇长篇文章感到抱歉,我认为如果我没有删除任何细节是最好的

1 个答案:

答案 0 :(得分:1)

CamelContext上的start方法阻止,读取其javadoc。您需要让JVM以某种方式运行 。您也可以使用可以执行此操作的Camel Main类。例如,请参阅:https://github.com/apache/camel/blob/master/tooling/archetypes/camel-archetype-java/src/main/resources/archetype-resources/src/main/java/MainApp.java