Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/239.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
每当mysql数据库发生更改时,获取对java应用程序的http请求_Java_Php_Mysql_Database Connection_Netbeans 7 - Fatal编程技术网

每当mysql数据库发生更改时,获取对java应用程序的http请求

每当mysql数据库发生更改时,获取对java应用程序的http请求,java,php,mysql,database-connection,netbeans-7,Java,Php,Mysql,Database Connection,Netbeans 7,使用下面显示的代码,我成功地从使用php的服务器中的sql数据库检索数据。 我需要定期检查数据库以查看是否添加了任何新数据,如果有,我需要将它们检索到我的java应用程序中。 我正在使用netbeans IDE 我该怎么做? try { URL url = new URL("http://taxi.com/login.php?param=10"); HttpURLConnection conn = (HttpURLConnection) url.openConnection();

使用下面显示的代码,我成功地从使用php的服务器中的sql数据库检索数据。 我需要定期检查数据库以查看是否添加了任何新数据,如果有,我需要将它们检索到我的java应用程序中。 我正在使用netbeans IDE 我该怎么做?

try {
    URL url = new URL("http://taxi.com/login.php?param=10");
    HttpURLConnection conn = (HttpURLConnection) url.openConnection();
    conn.setRequestMethod("GET");
    conn.setRequestProperty("Accept", "application/json");

    if (conn.getResponseCode() != 200) {
        throw new RuntimeException("Failed : HTTP error code : "
                + conn.getResponseCode());
    }

    BufferedReader br = new BufferedReader(new InputStreamReader(
        (conn.getInputStream())));

    String output;
    System.out.println("Output from Server .... \n");
    while ((output = br.readLine()) != null) {
        System.out.println(output);
    }

    conn.disconnect();

  } catch (MalformedURLException e) {

    e.printStackTrace();

  } catch (IOException e) {

    e.printStackTrace();

  }

}

在这个解决方案中,我假设您至少使用Java5
假设您希望每5分钟检查一次新数据,并在25分钟后终止。
您可以这样做:

PHPDataChecker.java

public class PHPDataChecker implements Runnable {
    public void run() {
       // Paste here all the code in your question
    }
}
public class Main {
    private static boolean canStop=false;

    private static void stopPHPDataChecker() {
        canStop=true;
    }

    public static void main(String[] args) {
        // Setup a task for checking data and then schedule it
        PHPDataChecker pdc = new PHPDataChecker();
        ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);
        final ScheduledFuture<?> pdcHandle = scheduler.scheduleAtFixedRate(pdc, 0L, 5L, TimeUnit.MINUTES);// Start pooling

        // Setup a new task to kill the polling after 25 minutes
        scheduler.schedule(new Runnable() {

            public void run() {
                System.out.println(">> TRY TO STOP!!!");
                pdcHandle.cancel(true);
                Main.stopPHPDataChecker();
                System.out.println("DONE");
            }

        }, 25L, TimeUnit.MINUTES);

        // Actively wait stop condition (canStop)
        do {
            if (canStop) {
                scheduler.shutdown();
            }
        } while (!canStop);

        System.out.println("END");
    }
}
Main.java

public class PHPDataChecker implements Runnable {
    public void run() {
       // Paste here all the code in your question
    }
}
public class Main {
    private static boolean canStop=false;

    private static void stopPHPDataChecker() {
        canStop=true;
    }

    public static void main(String[] args) {
        // Setup a task for checking data and then schedule it
        PHPDataChecker pdc = new PHPDataChecker();
        ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);
        final ScheduledFuture<?> pdcHandle = scheduler.scheduleAtFixedRate(pdc, 0L, 5L, TimeUnit.MINUTES);// Start pooling

        // Setup a new task to kill the polling after 25 minutes
        scheduler.schedule(new Runnable() {

            public void run() {
                System.out.println(">> TRY TO STOP!!!");
                pdcHandle.cancel(true);
                Main.stopPHPDataChecker();
                System.out.println("DONE");
            }

        }, 25L, TimeUnit.MINUTES);

        // Actively wait stop condition (canStop)
        do {
            if (canStop) {
                scheduler.shutdown();
            }
        } while (!canStop);

        System.out.println("END");
    }
}
公共类主{
私有静态布尔canStop=false;
私有静态无效数据检查器(){
canStop=true;
}
公共静态void main(字符串[]args){
//设置检查数据的任务,然后安排它
PHPDataChecker pdc=新的PHPDataChecker();
ScheduledExecutorService scheduler=执行者。newScheduledThreadPool(1);
final ScheduledFuture pdcHandle=scheduler.scheduleAtFixedRate(pdc,0L,5L,TimeUnit.MINUTES);//开始池
//设置新任务以在25分钟后终止轮询
scheduler.schedule(新的Runnable(){
公开募捐{
System.out.println(“>>尝试停止!!!”;
pdcdhandle.cancel(true);
Main.stopPHPDataChecker();
系统输出打印项次(“完成”);
}
},25L,时间单位为分钟);
//主动等待停止条件(canStop)
做{
如果(可以停止){
scheduler.shutdown();
}
}而(!canStop);
系统输出打印项次(“结束”);
}
}

看起来MS提供了一个,但总体思路是数据库向[JMS]()队列或其他内容发出任何更改(您感兴趣的)信号,并让您的应用程序订阅该JMS主题并接收通知。谢谢您提供的信息!你有什么具体的例子或代码,我可以参考?因为我对JMS和它的工作方式还不熟悉!:)谢谢你的回答!我试过了,但我在第4行和第5行遇到了一个错误,说“表达的非法开始”,当我删除“私人最终”错误时,错误消失了!移除它们可以吗?看起来效果不错!:)谢谢你的回答!当我收到所需信息时,如何终止计划程序?@senrulz我已经更新了anwser中的代码,显示了如何终止计划程序。