jjzjj

Java 定时器只触发一次(仅服务器问题)

coder 2024-06-21 原文

好吧,这是一个奇怪的问题,它似乎不是编码问题。我的家用电脑和服务器电脑(都运行 windows pro)上都安装了 Intellij。我远程进入并并排放置。两者都打开了 Intellij,并且都具有相同的复制和粘贴代码。在我的家用电脑上这段代码运行良好,它每 60 秒触发一次。但是在服务器计算机上它会触发一次并且不会再次触发。我把它打包到一个 jar 里,然后运行 jar 和同样的东西,它运行一次,再也不会运行了。这是代码。

public class BackupTask extends TimerTask {
    private static final ThreadLocal<DateFormat> df = new ThreadLocal<DateFormat>(){
        @Override
        protected DateFormat initialValue() {
            return new SimpleDateFormat("yyyy-MM-dd HH_mm_ss");
        }
    };

    public void run() {
        try {
            File src = new File("C:\\Users\\justi\\Desktop\\Server\\Saved");
            File dest = new File("\\\\READYSHARE\\USB_Storage\\Backups\\" + df.get().format(Calendar.getInstance().getTime()));

            if(!dest.exists()){
                dest.mkdir();
            }

            copyFolder(src, dest);
        }catch(Exception e){
            System.out.println("Error: " + e);
        }
    }

    public static void copyFolder(File src, File dest)
            throws IOException{

        if(src.isDirectory()){

            //if directory not exists, create it
            if(!dest.exists()){
                dest.mkdir();
            }

            //list all the directory contents
            String files[] = src.list();

            for (String file : files) {
                //construct the src and dest file structure
                File srcFile = new File(src, file);
                File destFile = new File(dest, file);
                //recursive copy
                copyFolder(srcFile,destFile);
            }

        }else{
            //if file, then copy it
            //Use bytes stream to support all file types
            InputStream in = new FileInputStream(src);
            OutputStream out = new FileOutputStream(dest);

            byte[] buffer = new byte[1024];

            int length;
            //copy the file content in bytes
            while ((length = in.read(buffer)) > 0){
                out.write(buffer, 0, length);
            }

            in.close();
            out.close();
        }
    }
}

自动备份.java

public class AutoBackup {
//    Timer timer;

    private final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);
    private final int TIME_BETWEEN_SAVES = 60;

    public AutoBackup(){
        final ScheduledFuture<?> beeperHandle = scheduler.scheduleAtFixedRate(new BackupTask(), 10, TIME_BETWEEN_SAVES, TimeUnit.SECONDS);
    }

    public static void main(String[] args) {
        new AutoBackup();
//        Timer timer = new Timer();
//        timer.schedule(new BackupTask(), 1000, 60 * 1000);
    }
}

这个程序只是一个非常简单的复制,并按预定的时间间隔从一个位置粘贴到另一个位置。我也尝试过以管理员身份运行 Intellij,但我只是不知道为什么会发生这种情况。服务器计算机配备核心 i5-4690k、Micro ITX 技嘉超耐用 GA-H97N-WIFI H97 主板和 16 GB 内存。让我知道是否有任何其他信息有帮助。

最佳答案

你描述的很奇怪,但我遇到了一个你的代码可能会失败的案例。让我详细描述一下。您首先创建线程池大小 = 1 的新调度执行程序:

Executors.newScheduledThreadPool(1);

这个单线程将用于执行您的可运行程序。然后你安排 runnable 以固定速率运行,先在 10 秒后运行,然后每 60 秒运行一次:

scheduler.scheduleAtFixedRate(new BackupTask(), 10, 60, TimeUnit.SECONDS);

现在,因为您在执行器中只有一个线程可以运行您的可运行对象,所以当您的 BackupTask 由于任何原因挂起,或者可能执行的时间更长时,只要第一个执行完成,下一个执行就会延迟。您正在进行网络备份,因此问题可能与网络有关。例如 - 执行 close() 可能会导致代码等待网络超时(取决于超时值的长度),或者在同一场景中执行 write(..)。

我的建议是在您的代码中放置一些调试语句(请参阅下面的代码)。我知道这可能会在应用程序控制台中产生一些垃圾,但如果您不想远程调试,这可能是找出代码错误的唯一方法。

public static void copyFolder(File src, File dest) throws IOException{
    if (src.isDirectory()) {

        //if directory not exists, create it
        if(!dest.exists()){
            System.out.println("Creating directory " + dest);
            dest.mkdir();
            System.out.println("Created directory ");
        }

        for (String file : src.list()) {

            File srcFile = new File(src, file);
            File destFile = new File(dest, file);

            System.out.println("Copying " + srcFile + " to " + destFile);
            copyFolder(srcFile,destFile);
            System.out.println("Copied " + srcFile + " to " + destFile);
        }

    }else{

        InputStream in = new FileInputStream(src);
        OutputStream out = new FileOutputStream(dest);

        byte[] buffer = new byte[1024];

        System.out.println("Writing file " + src + " to " + dest);

        int length;
        //copy the file content in bytes
        while ((length = in.read(buffer)) > 0){
            out.write(buffer, 0, length);
        }

        System.out.println("Closing file " + src);
        in.close();

        System.out.println("Closing file " + dest);
        out.close();

        System.out.println("Writing file " + src + " to " + dest + " is done");
    }
}

此外,我对您的代码的一些评论:

您的 BackupTask 扩展了 TimerTask。这是不必要的。它足以实现 Runnable。

当你从流中写入/读取时,你应该始终确保在 finally 部分关闭你的资源,或者对资源使用 try(从 Java 7 开始)。否则,您可能会永远打开文件。

InputStream in = null;
OutputStream out = null;

byte[] buffer = new byte[1024];
int length;

try {
  in = new FileInputStream(src);
  out = new FileOutputStream(dest);
  while ((length = in.read(buffer)) > 0) {
    out.write(buffer, 0, length);
  }
} finally {
  if (in != null) {
    try {
      in.close();
    } catch (IOException e) {
      e.printStackTrace();
    }
  }
  if (out != null) {
    try {
      out.close();
    } catch (IOException e) {
      e.printStackTrace();
    }
  }
}

关于Java 定时器只触发一次(仅服务器问题),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34584985/

有关Java 定时器只触发一次(仅服务器问题)的更多相关文章

  1. ruby - 使用 ruby​​ 和 savon 的 SOAP 服务 - 2

    我正在尝试使用ruby​​和Savon来使用网络服务。测试服务为http://www.webservicex.net/WS/WSDetails.aspx?WSID=9&CATID=2require'rubygems'require'savon'client=Savon::Client.new"http://www.webservicex.net/stockquote.asmx?WSDL"client.get_quotedo|soap|soap.body={:symbol=>"AAPL"}end返回SOAP异常。检查soap信封,在我看来soap请求没有正确的命名空间。任何人都可以建议我

  2. ruby - 具有身份验证的私有(private) Ruby Gem 服务器 - 2

    我想安装一个带有一些身份验证的私有(private)Rubygem服务器。我希望能够使用公共(public)Ubuntu服务器托管内部gem。我读到了http://docs.rubygems.org/read/chapter/18.但是那个没有身份验证-如我所见。然后我读到了https://github.com/cwninja/geminabox.但是当我使用基本身份验证(他们在他们的Wiki中有)时,它会提示从我的服务器获取源。所以。如何制作带有身份验证的私有(private)Rubygem服务器?这是不可能的吗?谢谢。编辑:Geminabox问题。我尝试“捆绑”以安装新的gem..

  3. ruby - 在 64 位 Snow Leopard 上使用 rvm、postgres 9.0、ruby 1.9.2-p136 安装 pg gem 时出现问题 - 2

    我想为Heroku构建一个Rails3应用程序。他们使用Postgres作为他们的数据库,所以我通过MacPorts安装了postgres9.0。现在我需要一个postgresgem并且共识是出于性能原因你想要pggem。但是我对我得到的错误感到非常困惑当我尝试在rvm下通过geminstall安装pg时。我已经非常明确地指定了所有postgres目录的位置可以找到但仍然无法完成安装:$envARCHFLAGS='-archx86_64'geminstallpg--\--with-pg-config=/opt/local/var/db/postgresql90/defaultdb/po

  4. ruby - 通过 rvm 升级 ruby​​gems 的问题 - 2

    尝试通过RVM将RubyGems升级到版本1.8.10并出现此错误:$rvmrubygemslatestRemovingoldRubygemsfiles...Installingrubygems-1.8.10forruby-1.9.2-p180...ERROR:Errorrunning'GEM_PATH="/Users/foo/.rvm/gems/ruby-1.9.2-p180:/Users/foo/.rvm/gems/ruby-1.9.2-p180@global:/Users/foo/.rvm/gems/ruby-1.9.2-p180:/Users/foo/.rvm/gems/rub

  5. ruby - 使用 Vim Rails,您可以创建一个新的迁移文件并一次性打开它吗? - 2

    使用带有Rails插件的vim,您可以创建一个迁移文件,然后一次性打开该文件吗?textmate也可以这样吗? 最佳答案 你可以使用rails.vim然后做类似的事情::Rgeneratemigratonadd_foo_to_bar插件将打开迁移生成的文件,这正是您想要的。我不能代表textmate。 关于ruby-使用VimRails,您可以创建一个新的迁移文件并一次性打开它吗?,我们在StackOverflow上找到一个类似的问题: https://sta

  6. ruby - 如何每月在 Heroku 运行一次 Scheduler 插件? - 2

    在选择我想要运行操作的频率时,唯一的选项是“每天”、“每小时”和“每10分钟”。谢谢!我想为我的Rails3.1应用程序运行调度程序。 最佳答案 这不是一个优雅的解决方案,但您可以安排它每天运行,并在实际开始工作之前检查日期是否为当月的第一天。 关于ruby-如何每月在Heroku运行一次Scheduler插件?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/8692687/

  7. ruby - 通过 RVM (OSX Mountain Lion) 安装 Ruby 2.0.0-p247 时遇到问题 - 2

    我的最终目标是安装当前版本的RubyonRails。我在OSXMountainLion上运行。到目前为止,这是我的过程:已安装的RVM$\curl-Lhttps://get.rvm.io|bash-sstable检查已知(我假设已批准)安装$rvmlistknown我看到当前的稳定版本可用[ruby-]2.0.0[-p247]输入命令安装$rvminstall2.0.0-p247注意:我也试过这些安装命令$rvminstallruby-2.0.0-p247$rvminstallruby=2.0.0-p247我很快就无处可去了。结果:$rvminstall2.0.0-p247Search

  8. ruby - Fast-stemmer 安装问题 - 2

    由于fast-stemmer的问题,我很难安装我想要的任何ruby​​gem。我把我得到的错误放在下面。Buildingnativeextensions.Thiscouldtakeawhile...ERROR:Errorinstallingfast-stemmer:ERROR:Failedtobuildgemnativeextension./System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/bin/rubyextconf.rbcreatingMakefilemake"DESTDIR="cleanmake"DESTDIR=

  9. java - 等价于 Java 中的 Ruby Hash - 2

    我真的很习惯使用Ruby编写以下代码:my_hash={}my_hash['test']=1Java中对应的数据结构是什么? 最佳答案 HashMapmap=newHashMap();map.put("test",1);我假设? 关于java-等价于Java中的RubyHash,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/22737685/

  10. ruby-on-rails - 启动 Rails 服务器时 ImageMagick 的警告 - 2

    最近,当我启动我的Rails服务器时,我收到了一长串警告。虽然它不影响我的应用程序,但我想知道如何解决这些警告。我的估计是imagemagick以某种方式被调用了两次?当我在警告前后检查我的git日志时。我想知道如何解决这个问题。-bcrypt-ruby(3.1.2)-better_errors(1.0.1)+bcrypt(3.1.7)+bcrypt-ruby(3.1.5)-bcrypt(>=3.1.3)+better_errors(1.1.0)bcrypt和imagemagick有关系吗?/Users/rbchris/.rbenv/versions/2.0.0-p247/lib/ru

随机推荐