首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >尽管被要求通过中断而死,计时器线程仍将继续存在。

尽管被要求通过中断而死,计时器线程仍将继续存在。
EN

Stack Overflow用户
提问于 2016-02-17 14:29:29
回答 1查看 1.6K关注 0票数 1

我正在尝试使用JDBC轮询一个数据库,但是无论我采用什么方法,我都会遇到这个错误。我尝试过解决方案HERE,但是似乎什么都没有用

我的java文件'App.java‘

代码语言:javascript
复制
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文件

代码语言:javascript
复制
<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>

当我跑的时候我所犯的错误

代码语言:javascript
复制
mvn install && mvn exec:java -Dexec.mainClass="mhealth.App"

它轮询几秒钟,然后在我的控制台上得到这个错误。

代码语言:javascript
复制
[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>,程序只是成功地构建并停止,我需要该程序继续运行和轮询。

代码语言:javascript
复制
[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] ------------------------------------------------------------------------

很抱歉发了这么长的邮件,我想我最好不要漏掉任何细节

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-02-18 07:46:44

start方法在CamelContext上是而不是阻塞,读取它的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

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/35459469

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档