1

i'm trying to poll a database using JDBC but i keep running into this error no matter what approach i take .i've tried the solution HERE but nothing seems to be working

my java file '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;

   }
}

my POM file

<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>

the error i get when i run

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

it polls for a couple of seconds and then i get this error on my console

[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

Of course if i try <cleanupDaemonThreads>true</cleanupDaemonThreads> the program just builds successfully and stops ,i need the program to keep running and polling

[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] ------------------------------------------------------------------------

Sorry for the long post,thought it'd be best if i didn't skip out any details

Community
  • 1
  • 1
Ashish Padakannaya
  • 463
  • 2
  • 7
  • 17

1 Answers1

1

The start method on CamelContext is not blocking, read its javadoc. You need to keep the JVM running somehow. You can also use Camel's Main class that can do this. See for example: https://github.com/apache/camel/blob/master/tooling/archetypes/camel-archetype-java/src/main/resources/archetype-resources/src/main/java/MainApp.java

Claus Ibsen
  • 56,060
  • 7
  • 50
  • 65