Announcement Announcement Module
Collapse
No announcement yet.
JVM termination, daemon threads Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • JVM termination, daemon threads

    Day 1 of Spring Integration here...I've got a simple directory monitor working but the JVM doesn't exit when I interrupt the main thread with Control-C. I tried specifying daemon=false as described here but that had no effect.

    My configuration (with failed attempt at daemon=false) is as follows. What configuration do I need to make the JVM exit?

    Code:
    	
    	<int:channel id="fileChannel" datatype="java.io.File">
    		<int:dispatcher task-executor="fileChannelTaskExecutor" />
    	</int:channel>
    
    	<bean id="fileChannelTaskExecutor"
    		class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor">
    		<property name="corePoolSize" value="2"/>
    		<property name="daemon" value="false"/>
    	</bean>	
    
    	<int:poller default="true" max-messages-per-poll="1"
    		fixed-rate="1000" />
    
    	<int-file:inbound-channel-adapter 
    		directory="file:c:/watch" channel="fileChannel" prevent-duplicates="true" />
    
    	<int:outbound-channel-adapter channel="fileChannel" 
    		ref="target" method="handleMessage" >
    	</int:outbound-channel-adapter>
    
    	<bean id="target" class="...

  • #2
    Can you attach a thread dump? (I think it's Ctrl-Break on Windows, but you can use jstack or VisualVM too).

    Comment


    • #3
      I have no problems with CTRL-C ; but I am an using a JVM Shutdown and a Spring Context Listener Shutdown hook. The
      JVM Shutdown will tell Spring to close... The Spring Context listener will tell the Sched/Executor to passivate. Seems to work for me!


      Code:
      class Main {
        private static ClassPathXmlApplicationContext springCtx;
        public static void main(String [] args) {
      
          // Create a global shutdown handler to take down the Spring Container
          Runtime.getRuntime().addShutdownHook(new Thread() {
                  public void run() {
                      if (springCtx != null) {
                          springCtx.close();
                      }
                      log.info("*** Started Spring shutdown...");
                  }
              });
      
              // Bootstrap Spring Container
              springCtx = new ClassPathXmlApplicationContext(new String[]{
                          "nbpump-spring-context.xml",
                          "activemq-jms-config.xml"
                      });
              // When the Spring Container is told to shutdown, add a Listener to terminate the Task Scheduler and its thread pool
              springCtx.addApplicationListener(new TaskSchedulerShutdownHandler());
             //
             //  The rest of your main method goes here
        }
      
        // Task Shutdown handler - uses static Spring Context to locate Task Scheduler and Executor and take it down easy.
      static class TaskSchedulerShutdownHandler implements ApplicationListener<ContextClosedEvent> {
              public void onApplicationEvent(ContextClosedEvent event) {
                  ThreadPoolTaskExecutor executor = springCtx.getBean(ThreadPoolTaskExecutor.class);
                  ThreadPoolTaskScheduler scheduler = springCtx.getBean(ThreadPoolTaskScheduler.class);
                  if (executor != null) {
                      executor.shutdown();
                  }
                  if (scheduler != null) {
                      scheduler.shutdown();
                  }
                  log.info("*** Shutdown Spring scheduler and executor...");
              }
          }

      Comment


      • #4
        Originally posted by Gary Russell View Post
        Can you attach a thread dump? (I think it's Ctrl-Break on Windows, but you can use jstack or VisualVM too).
        Thanks for the push in the right direction; it turns out this had nothing to do with threads.
        I had been starting the windows cmd shell with Cygwin shell as parent process. Turns out
        I can't interrupt any Java process with a cmd shell started that way. If I start cmd shell
        the ordinary way, everything works fine.

        Comment

        Working...
        X