线程并发大小超出异常

时间:2014-09-27 16:57:44

标签: java multithreading spring

我收到了以下错误:

 SEVERE: Servlet.service() for servlet [appServlet] in context with path [/ssh] threw exception  [Request processing failed; nested exception is org.springframework.core.task.TaskRejectedException:  Executor [java.util.concurrent.ThreadPoolExecutor@6d7ebea0[Terminated, pool size = 0, active threads =  0, queued tasks = 0, completed tasks = 3]] did not accept task: com.jcraft.jsch.Session@4afc1ede] with  root cause
 java.util.concurrent.RejectedExecutionException: Task com.jcraft.jsch.Session@4afc1ede rejected from java.util.concurrent.ThreadPoolExecutor@6d7ebea0[Terminated, pool size = 0, active threads = 0, queued tasks = 0, completed tasks = 3]

我的任务执行程序的当前bean配置是:

 <beans:bean id="taskExecutor" class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor">
    <beans:property name="corePoolSize" value="1" />
    <beans:property name="maxPoolSize" value="1" />
    <beans:property name="waitForTasksToCompleteOnShutdown" value="false" />
</beans:bean>

执行的代码如下(参见“在这里发生错误的部分”)。该部分尝试将可运行的Session作为异步线程运行:

 @Service
 public class SSHService implements SSHServiceInterface {

   @Value("${connect.host}")
   String host;

   @Value("${connect.port}")
   int port;

   @Value("${connect.user}")
   String user = "groupby";

   @Value("${connect.password}")
   String password = "gbi123gbi";

   @Value("${ssh.key.path}")
   String filePath;

   @Value("${remote.host}")
    String remoteHost;

  @Value("${remote.port}")
   int remotePort;

  @Value("${remote.forward.port}")
  int remoteForwardPort;



  private ThreadPoolTaskExecutor exector;

  @Autowired
  public void setExector(ThreadPoolTaskExecutor exector) {
        this.exector = exector;
  }

  public void connect()
  {
       if(this.exector.getActiveCount()<=0)
        try {
            this.exector.execute(this.initiate()); //THE ERROR HAPPENS HERE
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (JSchException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }       
  }
  public void disconnect()
  {
    if(this.exector.getActiveCount()>0)
        this.exector.shutdown();    
  }

  protected Session initiate() throws FileNotFoundException, JSchException
  {
    JSch jsch=new JSch();
    java.util.Properties config = new java.util.Properties(); 
    config.put("StrictHostKeyChecking", "no");
    Session session=jsch.getSession(this.user, this.host, this.port);
    session.setConfig(config);
    session.setPassword(password);
    session.connect();
    //session.setPortForwardingR(this.remoteForwardPort,this.remoteHost,this.remotePort);
    session.setPortForwardingR(this.remoteForwardPort, this.remoteHost, this.remotePort);
    return session;
 }

}

0 个答案:

没有答案