监视服务java.lang.IllegalStateException

时间:2017-11-15 19:12:25

标签: java exception-handling illegalstateexception

我创建了一个监视文件夹的监视服务。一旦我从该文件夹创建或更新文件,我就会遇到错误。我在下面分享了我的代码和错误日志。请让我知道如何处理这个问题。

//代码。

@Override
public void runTask() {

    log.info("Task Running in Lithium **********************************************************");
    log.info("Path *** "+path);    
        try {
           WatchService watchService = path.getFileSystem().newWatchService();
           path.register(watchService, StandardWatchEventKinds.ENTRY_MODIFY, StandardWatchEventKinds.ENTRY_CREATE);
           while (true) {
               final WatchKey wk = watchService.take();
                for (WatchEvent<?> event : wk.pollEvents()) {
                    final Path changed = (Path) event.context();
                    log.info("before File changed");
                    log.info("Path Changed "+path);

                    if (changed.endsWith("LithiumRole.xlsx") || changed.endsWith("LithiumRole.xls")) {
                        log.info("Inside File changed");
                        log.info(filePath+fileName);

                        lithiumRoleService.doProcess(ExcelUtil.getWorkbook(new FileInputStream(new File(filePath+fileName))));
             }
            else
                log.info("changed event path does not ends with LithiumRole.xlsx");

                }
        // reset the key
        boolean valid = wk.reset();
        if (!valid) {
            log.info("watch Key has been unregistered");
          }
     }

  } catch (final ApplicationException applicationEx) {
      log.error("Exception occured!", applicationEx);
  } catch (IOException e) {
    // TODO Auto-generated catch block
      e.printStackTrace();
  } catch (InterruptedException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

    }

//错误日志。

  

信息:非法访问:此Web应用程序实例已停止   已经。不能加载   com.sun.org.apache.xerces.internal.parsers.SAXParser。最终   跟踪堆栈跟踪是由调试引发的错误引起的   目的以及尝试终止引起的线程   非法访问,并没有功能影响。   java.lang.IllegalStateException at   org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1612)     在   org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1571)     在   org.apache.tomee.catalina.LazyStopWebappClassLoader.loadClass(LazyStopWebappClassLoader.java:129)     在org.xml.sax.helpers.NewInstance.newInstance(NewInstance.java:82)     在   org.xml.sax.helpers.XMLReaderFactory.loadClass(XMLReaderFactory.java:228)     在   org.xml.sax.helpers.XMLReaderFactory.createXMLReader(XMLReaderFactory.java:191)     在org.dom4j.io.SAXHelper.createXMLReader(SAXHelper.java:83)at   org.dom4j.io.SAXReader.createXMLReader(SAXReader.java:894)at at   org.dom4j.io.SAXReader.getXMLReader(SAXReader.java:715)at at   org.dom4j.io.SAXReader.read(SAXReader.java:435)at   org.dom4j.io.SAXReader.read(SAXReader.java:343)at   org.apache.poi.openxml4j.opc.PackageRelationshipCollection.parseRelationshipsPart(PackageRelationshipCollection.java:305)     在   org.apache.poi.openxml4j.opc.PackageRelationshipCollection。(PackageRelationshipCollection.java:156)     在   org.apache.poi.openxml4j.opc.PackageRelationshipCollection。(PackageRelationshipCollection.java:124)     在   org.apache.poi.openxml4j.opc.PackagePart.loadRelationships(PackagePart.java:527)     在   org.apache.poi.openxml4j.opc.PackagePart。(PackagePart.java:112)     在   org.apache.poi.openxml4j.opc.PackagePart。(PackagePart.java:83)     在   org.apache.poi.openxml4j.opc.PackagePart。(PackagePart.java:128)     在   org.apache.poi.openxml4j.opc.ZipPackagePart。(ZipPackagePart.java:78)     在   org.apache.poi.openxml4j.opc.ZipPackage.getPartsImpl(ZipPackage.java:187)     在   org.apache.poi.openxml4j.opc.OPCPackage.getParts(OPCPackage.java:585)     在org.apache.poi.openxml4j.opc.OPCPackage.open(OPCPackage.java:222)     在   org.apache.poi.ss.usermodel.WorkbookFactory.create(WorkbookFactory.java:63)     在com.test.am.utils.excel.ExcelUtil.getWorkbook(ExcelUtil.java:311)     在   com.test.maxup.lithium.task.LithiumRoleTask.runTask(LithiumRoleTask.java:56)     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)at   sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)     在   sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)     在java.lang.reflect.Method.invoke(Method.java:498)at   org.springframework.util.MethodInvoker.invoke(MethodInvoker.java:273)     在   org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean $ MethodInvokingJob.executeInternal(MethodInvokingJobDetailFactoryBean.java:264)     在   org.springframework.scheduling.quartz.QuartzJobBean.execute(QuartzJobBean.java:86)     在org.quartz.core.JobRunShell.run(JobRunShell.java:199)at   org.quartz.simpl.SimpleThreadPool $ WorkerThread.run(SimpleThreadPool.java:546)

1 个答案:

答案 0 :(得分:0)

抛出java.lang.IllegalStateException的其中一个软件包是java.nio.file,这是您在案例中使用的软件包。

注意你的例外:

  

com.test.am.utils.excel.ExcelUtil.getWorkbook(ExcelUtil.java:311)at at   com.test.maxup.lithium.task.LithiumRoleTask.runTask(LithiumRoleTask.java:56)

我无法说明行号,但我很确定它是这个地方:

lithiumRoleService.doProcess(ExcelUtil.getWorkbook(new FileInputStream(new File(filePath+fileName))));

确保先创建文件(下面未经测试的代码):

    Path path = Paths.get(filePath+fileName);
    if (!Files.exists(path)) {
        try {
            Files.createDirectories(path);
        } catch (FileAlreadyExistsException x) {
            // log exceptiopn
        } catch (IOException x) {
            // log exception
        }
    }
lithiumRoleService.doProcess(ExcelUtil.getWorkbook(new FileInputStream(path.toString())));

while(true)也是想要的行为吗?除非抛出异常,否则此循环永远不会停止。