Java 在正常功能上实现调度程序错误

标签 java scheduled-tasks

目前,我有我的java应用程序,我从主函数中调用了该函数,然后所有的工作都完成了。下面是我的代码片段。所以我需要及时运行函数的调用并阅读有关调度程序的信息。所以我尝试在我的主函数中实现它并收到错误:此处不允许“void”类型final ScheduledFuture<?> timeHandle = scheduler.scheduleAtFixedRate(ws1TI(), 0, 10);这个问题有什么解决办法吗“?

public class ws1TI
{
public static void ws1TI()
      {
            try
            {
             SOAPConnectionFactory soapConnectionFactory = SOAPConnectionFactory.newInstance();
             SOAPConnection soapConnection = soapConnectionFactory.createConnection();
             String url = "http://*********.asmx?WSDL";
             SOAPMessage soapResponse = soapConnection.call(createSOAPRequest(), url);
             printSOAPResponse(soapResponse);
             updateAcknoledgement();
            }
            catch (Exception e)
            {
                  e.printStackTrace();
                  System.err.println(e.toString());
            }
      }
      public static     void main(String[] args)
      {
        ScheduledExecutorService scheduler =Executors.newSingleThreadScheduledExecutor();   
        final ScheduledFuture<?> timeHandle = scheduler.scheduleAtFixedRate(ws1TI(), 0, 10); 

      }    
}

最佳答案

scheduleAtFixedRate 应收到 Runnable ,不是空值。例如,您可以将 ws1TI 函数转换为 Runnable,如下所示:

public class ws1TI {
    public static class WS1TI implements Runnable {
        @Override
        public void run() {
            try {
                 SOAPConnectionFactory soapConnectionFactory = SOAPConnectionFactory.newInstance();
                 SOAPConnection soapConnection = soapConnectionFactory.createConnection();
                 String url = "http://*********.asmx?WSDL";
                 SOAPMessage soapResponse = soapConnection.call(createSOAPRequest(), url);
                 printSOAPResponse(soapResponse);
                 updateAcknoledgement();
            }
            catch (Exception e) {
                 e.printStackTrace();
                 System.err.println(e.toString());
            }
        }
    }

    public static void main(String[] args) {
        ScheduledExecutorService scheduler =
            Executors.newSingleThreadScheduledExecutor();   
        final ScheduledFuture<?> timeHandle = 
            scheduler.scheduleAtFixedRate(new WS1TI(), 0L, 10L, TimeUnits.MINUTE); 
    }
}

关于Java 在正常功能上实现调度程序错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27753085/

相关文章:

java - Java中非线性方程的最大化

java - 自动执行计划任务 - 运行 main.java 文件

java - 替代计划的任务,将来可能会取消

java - Hibernate 与不同表的一对一映射可以单独存在

c# - 从任务访问 Enterprise Architect 存储库

java - Spring 启动: Getting @Scheduled cron value from database

python - request.urlretrieve 在 PythonAnywhere 中作为计划任务运行时失败

java - 回放 Rational Function Tester

java - 不支持 Google 电子钱包付款方式

java - 如何以编程方式确定在 Java 中运行 x11 窗口的进程