2016-11-17 120 views
2

我想在Tomcat中9 deply一些應用程序(4春天啓動Web應用程序),我注意到其中2使Tomcat log中顯示:Tomcat的死機部署SpringBoot應用

17-Nov-2016 00:15:07.110 INFO [localhost-startStop-2] org.apache.catalina.startup.HostConfig.deployWAR Deploying web application archive /Users/viruskimera/apache-tomcat-9.0.0.M11/webapps/ftpoutbound990-0.0.1-SNAPSHOT.war 

而且它保持這樣只是部署它不顯示部署完成。 的事情是,即使它不完全出現部署,該應用程序也能正常工作。 (I見我log4j的項和文件的文件夾監視處理)

這些應用程序監視使用Java watchService用下面的代碼2個不同的文件夾:

包com.ftpoutbound990.monitor;

import java.io.File; 
import java.io.FileInputStream; 
import java.io.IOException; 
import java.io.InputStream; 
import java.nio.file.ClosedWatchServiceException; 
import java.nio.file.FileSystems; 
import java.nio.file.Path; 
import java.nio.file.Paths; 
import java.nio.file.StandardWatchEventKinds; 
import java.nio.file.WatchEvent; 
import java.nio.file.WatchKey; 
import java.nio.file.WatchService; 

import org.apache.log4j.Logger; 
import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.beans.factory.annotation.Value; 
import org.springframework.stereotype.Component; 

import com.ftpoutbound990.client.FtpoutboundApp; 

@Component 
public class MonitorDirectory { 

    final static Logger logger = Logger.getLogger(MonitorDirectory.class); 

    @Autowired 
    private FtpoutboundApp ftpoutboundapp; 

    @Value("${folder990}") 
    private String folder990; 

    public void startMonitoring() throws IOException, InterruptedException { 
     logger.info("INICIO DE MONITOREO DE ARCHIVOS 990"); 
     try (WatchService watchService = FileSystems.getDefault().newWatchService()) { 

      Path faxFolder = Paths.get(folder990); 

      WatchService watchmyservice = FileSystems.getDefault().newWatchService(); 
      faxFolder.register(watchmyservice, StandardWatchEventKinds.ENTRY_CREATE); 

      boolean valid = true; 
      do { 
       try { 
        WatchKey watchKey = watchmyservice.take(); 

        for (WatchEvent event : watchKey.pollEvents()) { 
         WatchEvent.Kind kind = event.kind(); 
         if (StandardWatchEventKinds.ENTRY_CREATE.equals(event.kind())) { 
          String fileName = event.context().toString(); 
          logger.info("ARCHIVO NUEVO:" + fileName); 
          boolean isGrowing = false; 
          Long initialWeight = new Long(0); 
          Long finalWeight = new Long(0); 
          Path child = faxFolder.resolve(folder990 + fileName); 
          do { 
           initialWeight = child.toFile().length(); 
           Thread.sleep(1000); 
           finalWeight = child.toFile().length(); 
           isGrowing = initialWeight < finalWeight; 
           logger.info("AUN COPIANDO ARCHIVO:" + fileName); 
          } while (isGrowing); 
          logger.info("LISTO ARCHIVO:" + fileName); 
          getFile(fileName); 
         } 
        } 
        valid = watchKey.reset(); 
        // Thread.sleep(1000 * 10); 
       } catch (InterruptedException | ClosedWatchServiceException e) { 
        //watchmyservice.close(); 
        Thread.currentThread().interrupt(); 
       } 

      } while (valid); 
     } 
    } 

    public void getFile(String fileName) throws IOException { 

     File file = new File(folder990 + fileName); 
     ftpoutboundapp.createGateway(file); 
    } 

} 

如果我刪除了Do-While應用程序完成部署!但爲什麼?這是一個標準的Java watchservice代碼,任何人都可以幫助我認識到我錯過了什麼/有什麼問題? 事情是,Tomcat不部署我的第四個應用程序,因爲它不斷部署第三個。 在此先感謝您的幫助


編輯1:添加螺紋

28-Dec-2016 17:57:27.558 INFO [main] org.apache.catalina.core.StandardServer.await A valid shutdown command was received via the shutdown port. Stopping the Server instance. 
28-Dec-2016 17:57:27.559 INFO [main] org.apache.coyote.AbstractProtocol.pause Pausing ProtocolHandler ["http-nio-8080"] 
28-Dec-2016 17:57:27.613 INFO [main] org.apache.coyote.AbstractProtocol.pause Pausing ProtocolHandler ["ajp-nio-8009"] 
28-Dec-2016 17:57:27.664 INFO [main] org.apache.catalina.core.StandardService.stopInternal Stopping service Catalina 
28-Dec-2016 17:57:27.664 SEVERE [ContainerBackgroundProcessor[StandardEngine[Catalina]]] org.apache.catalina.startup.HostConfig.deployWARs Error waiting for multi-thread deployment of WAR files to complete 
java.lang.InterruptedException 
    at java.util.concurrent.FutureTask.awaitDone(FutureTask.java:404) 
    at java.util.concurrent.FutureTask.get(FutureTask.java:191) 
    at org.apache.catalina.startup.HostConfig.deployWARs(HostConfig.java:744) 
    at org.apache.catalina.startup.HostConfig.deployApps(HostConfig.java:407) 
    at org.apache.catalina.startup.HostConfig.check(HostConfig.java:1595) 
    at org.apache.catalina.startup.HostConfig.lifecycleEvent(HostConfig.java:280) 
    at org.apache.catalina.util.LifecycleBase.fireLifecycleEvent(LifecycleBase.java:92) 
    at org.apache.catalina.core.ContainerBase.backgroundProcess(ContainerBase.java:1136) 
    at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.processChildren(ContainerBase.java:1372) 
    at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.processChildren(ContainerBase.java:1376) 
    at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.run(ContainerBase.java:1344) 
    at java.lang.Thread.run(Thread.java:745) 

28-Dec-2016 18:00:21.904 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Server version:  Apache Tomcat/9.0.0.M11 
28-Dec-2016 18:00:21.907 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Server built:   Oct 6 2016 18:54:10 UTC 
28-Dec-2016 18:00:21.908 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Server number:   9.0.0.0 
28-Dec-2016 18:00:21.908 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log OS Name:    Mac OS X 
28-Dec-2016 18:00:21.908 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log OS Version:   10.9.5 
28-Dec-2016 18:00:21.908 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Architecture:   x86_64 
28-Dec-2016 18:00:21.908 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Java Home:    /Library/Java/JavaVirtualMachines/jdk1.8.0_73.jdk/Contents/Home/jre 
28-Dec-2016 18:00:21.908 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log JVM Version:   1.8.0_73-b02 
28-Dec-2016 18:00:21.908 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log JVM Vendor:   Oracle Corporation 
28-Dec-2016 18:00:21.909 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log CATALINA_BASE:   /Users/viruskimera/apache-tomcat-9.0.0.M11 
28-Dec-2016 18:00:21.909 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log CATALINA_HOME:   /Users/viruskimera/apache-tomcat-9.0.0.M11 
28-Dec-2016 18:00:21.909 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.util.logging.config.file=/Users/viruskimera/apache-tomcat-9.0.0.M11/conf/logging.properties 
28-Dec-2016 18:00:21.909 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager 
28-Dec-2016 18:00:21.909 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djdk.tls.ephemeralDHKeySize=2048 
28-Dec-2016 18:00:21.910 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.protocol.handler.pkgs=org.apache.catalina.webresources 
28-Dec-2016 18:00:21.910 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dcatalina.base=/Users/viruskimera/apache-tomcat-9.0.0.M11 
28-Dec-2016 18:00:21.910 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dcatalina.home=/Users/viruskimera/apache-tomcat-9.0.0.M11 
28-Dec-2016 18:00:21.910 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.io.tmpdir=/Users/viruskimera/apache-tomcat-9.0.0.M11/temp 
28-Dec-2016 18:00:21.910 INFO [main] org.apache.catalina.core.AprLifecycleListener.lifecycleEvent The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: /Users/viruskimera/Library/Java/Extensions:/Library/Java/Extensions:/Network/Library/Java/Extensions:/System/Library/Java/Extensions:/usr/lib/java:. 
28-Dec-2016 18:00:22.063 INFO [main] org.apache.coyote.AbstractProtocol.init Initializing ProtocolHandler ["http-nio-8080"] 
28-Dec-2016 18:00:22.093 INFO [main] org.apache.tomcat.util.net.NioSelectorPool.getSharedSelector Using a shared selector for servlet write/read 
28-Dec-2016 18:00:22.096 INFO [main] org.apache.coyote.AbstractProtocol.init Initializing ProtocolHandler ["ajp-nio-8009"] 
28-Dec-2016 18:00:22.098 INFO [main] org.apache.tomcat.util.net.NioSelectorPool.getSharedSelector Using a shared selector for servlet write/read 
28-Dec-2016 18:00:22.100 INFO [main] org.apache.catalina.startup.Catalina.load Initialization processed in 604 ms 
28-Dec-2016 18:00:22.145 INFO [main] org.apache.catalina.core.StandardService.startInternal Starting service Catalina 
28-Dec-2016 18:00:22.145 INFO [main] org.apache.catalina.core.StandardEngine.startInternal Starting Servlet Engine: Apache Tomcat/9.0.0.M11 
28-Dec-2016 18:00:22.224 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployWAR Deploying web application archive /Users/viruskimera/apache-tomcat-9.0.0.M11/webapps/ftpoutbound990-0.0.1-SNAPSHOT.war 
28-Dec-2016 18:00:26.346 INFO [localhost-startStop-1] org.apache.jasper.servlet.TldScanner.scanJars At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time. 

    . ____   _   __ _ _ 
/\\/___'_ __ _ _(_)_ __ __ _ \ \ \ \ 
(()\___ | '_ | '_| | '_ \/ _` | \ \ \ \ 
\\/ ___)| |_)| | | | | || (_| | )))) 
    ' |____| .__|_| |_|_| |_\__, |//// 
=========|_|==============|___/=/_/_/_/ 
:: Spring Boot ::  (v1.3.6.RELEASE) 

回答

1

以在盲目出手..

代碼的簡單審查表明,如果有異常拋出試圖捕獲,布爾標誌valid仍然true導致無限循環。

因此,在catch塊的主體中​​將標誌設置爲false!

+0

我在檢查此問題。 – viruskimera

2

tomcat部署者線程似乎調用startMonitoring方法。按照預期,這種方法永遠不會返回,因此部署沒有完成。

要解決此問題,需要在單獨的(或後臺)線程中運行監視方法。

下面的重構示例可能會有所幫助。

需要注意的是,WatcherService實例的單個實例可以用於觀察多個目錄。

import java.io.File; 
import java.io.IOException; 
import java.nio.file.FileSystems; 
import java.nio.file.Path; 
import java.nio.file.Paths; 
import java.nio.file.StandardWatchEventKinds; 
import java.nio.file.WatchEvent; 
import java.nio.file.WatchKey; 
import java.nio.file.WatchService; 
import java.util.concurrent.ExecutorService; 
import java.util.concurrent.Executors; 

import javax.annotation.PostConstruct; 
import javax.annotation.PreDestroy; 

import org.apache.log4j.Logger; 
import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.beans.factory.annotation.Value; 
import org.springframework.stereotype.Component; 

@Component 
public class MonitorDirectory { 

    final static Logger logger = Logger.getLogger(MonitorDirectory.class); 

    @Autowired 
    private FtpoutboundApp ftpoutboundapp; 

    @Value("${folder990}") 
    private String folder990; 

    private WatchService watchService; 

    private ExecutorService eventLoop = Executors.newFixedThreadPool(1); 

    @PostConstruct 
    public void init() throws IOException, InterruptedException { 
     watchService = FileSystems.getDefault().newWatchService(); 
     eventLoop.submit((Runnable)() -> { 
      try { 
       startMonitoring(); 
      } catch (Exception e) { 
       logger.error("ERROR...", e); 
      } 
     }); 
    } 

    @PreDestroy 
    public void destroy() throws IOException { 
     eventLoop.shutdownNow(); 
     if (watchService != null) { 
      watchService.close(); 
     } 
    } 

    public void startMonitoring() throws IOException, InterruptedException { 
     logger.info("INICIO DE MONITOREO DE ARCHIVOS 990"); 
     Path faxFolder = Paths.get(folder990); 
     WatchKey watchKey = faxFolder.register(watchService, StandardWatchEventKinds.ENTRY_CREATE); 
     boolean valid = true; 
     do { 
      for (WatchEvent<?> event : watchKey.pollEvents()) { 
       WatchEvent.Kind<?> kind = event.kind(); 
       if (StandardWatchEventKinds.ENTRY_CREATE.equals(kind)) { 
        String fileName = event.context().toString(); 
        logger.info("ARCHIVO NUEVO:" + fileName); 
        boolean isGrowing = false; 
        Long initialWeight = new Long(0); 
        Long finalWeight = new Long(0); 
        Path child = faxFolder.resolve(folder990 + fileName); 
        do { 
         initialWeight = child.toFile().length(); 
         Thread.sleep(1000); 
         finalWeight = child.toFile().length(); 
         isGrowing = initialWeight < finalWeight; 
         logger.info("AUN COPIANDO ARCHIVO:" + fileName); 
        } while (isGrowing); 
        logger.info("LISTO ARCHIVO:" + fileName); 

        getFile(fileName); 
       } 
       valid = watchKey.reset(); 
      } 
     } while (valid); 
    } 

    public void getFile(String fileName) throws IOException { 
     File file = new File(folder990 + fileName); 
     ftpoutboundapp.createGateway(file); 
    } 

} 
+0

我正在嘗試這個 – viruskimera

+0

看看我的編輯1,這是一個本地運行,但我擔心它會在服務器中相同。我應用了你的代碼並凍結了它,然後關閉了tomcat並顯示:看看SEVERE ERROR:28-Dec-2016 17:57:27.664,它表示等待部署完成的錯誤。我甚至嘗試將v1.4.1.RELEASE中的spring版本更改爲v1.3.6.RELEASE,因爲1個應用程序使用v1.4.1.RELEASE正確部署,但發生同樣的情況。 – viruskimera

+0

順便說一句我注意到java進程的內存消耗增加。 – viruskimera

1

你可以檢查下面的幾件事。

是否在您的應用程序中僅調用Component MonitorDirectory一次。如果不是,您可能需要聲明'eventLoop'爲靜態,並檢查PostConstruct中的初始化以避免多次初始化和調用。

此外,ExecutorService的被初始化爲1的線程池,如果多MonitorDirectory對象在應用程序初始化,這可能會導致部署凍結

是否有任何機會,文件大小可在服務器環境有所萎縮。這可能會導致'isGrowing'檢查無限循環..

+0

部分代碼是由上面的用戶Skadya提出的,但我會說MOnitor只被調用1次,怎麼可能不止一次調用監視器?注意這個運行1個電腦,但是我們已經從該電腦拷貝了所有東西(jdk ,tomcat等)我嘗試使用@scheduled註釋,似乎工作,但仍處理一些問題。 – viruskimera

+0

在Spring中,每當一個對象被初始化時,'postConstruct'方法將被調用。如果應用程序可以爲同一對象(具有併發性的典型Web應用程序)生成多個對象,則可能發生這種情況。 –