LynAs
LynAs

Reputation: 6537

Spring run independent thread

I am running a v4 Spring mvc app with spring security 3.2 I am trying to run a independent thread from the controller

@Controller
    public class C1{
         @AutoWired
         C2 c2;

         @RequestMappting("/")
         public String home(){
            c2.run();
            System.out.println("something");
            return "home";
         }
    }
}

@Component
@Scope
public class C2 extends Thread{
  @Override
  public void run(){
    while(true){
       System.out.println("random stuff");
    }
  }
}

when I run this code it gets stuck in c2.run(); the next line does not execute; What i am trying to achieve here is print that random stuff continuously and render the home.jsp page
What am i doing doing wrong how to fix this???

Upvotes: 1

Views: 6549

Answers (3)

Babl
Babl

Reputation: 7646

There is no need to use Threads directly when using Spring Framework. It has a great Task Execution and Scheduling abstraction which should be used. So what you want can be achieved.

@Controller
public class C1{
     @AutoWired
     C2 c2;

     @RequestMappting("/")
     public String home(){
        c2.run();
        System.out.println("something");
        return "home";
     }        
}

@Component
@Scope
public class C2{

  @Async
  public void run(){
    while(true){
       System.out.println("random stuff");
    }
  }
}

@Configuration
@EnableAsync
public class AppConfig implements AsyncConfigurer {

 @Override
 public Executor getAsyncExecutor() {
     ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
     executor.setCorePoolSize(7);
     executor.setMaxPoolSize(42);
     executor.setQueueCapacity(11);
     executor.setThreadNamePrefix("MyExecutor-");
     executor.initialize();
     return executor;
 }

 @Override
 public AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler() {
     return MyAsyncUncaughtExceptionHandler();
 }

}

Upvotes: 2

Darshan Lila
Darshan Lila

Reputation: 5868

To start a Thread you need to invoke start() method on a Thread. Make Following chages to your code:

@Controller
    public class C1{
         @AutoWired
         C2 c2;

         @RequestMapping("/")
         public String home(){
            c2.start();
            System.out.println("something");
            return "home";
         }
    }
}

@Component
@Scope
public class C2 extends Thread{
  @Override
  public void run(){
    while(true){
       System.out.println("random stuff");
    }
  }
}

Also note that the thread would run infinitely.

Upvotes: 1

Andy Brown
Andy Brown

Reputation: 12999

c2.run() just calls the method, it doesn't start the thread. So it will run just once. Please refer to the java documentation for starting a thread.

There are other issues to consider here, such as whether you really want to start a new background thread every time your page is requested and whether you want it to go into the tight resource-intensive loop that you have illustrated.

You've also got a typo: RequestMappting should be RequestMapping

Upvotes: 1

Related Questions