2016-09-24 2 views
0

私は、APIから読み込んでいくつかのフィルタリングされた出力を返すプロキシマイクロサービスを持っています。私はHttpsURLConnectionURLConnectionのメソッドを使用しています)を使用しています。URLConnectionを使用して毎時キャッシュをリフレッシュするにはどうすればよいですか?

String httpsURL = "https://myrestserver/path"+ id ; 
Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("myproxy", 8080)); 
URL myurl = new URL(httpsURL); 
HttpsURLConnection con = (HttpsURLConnection)myurl.openConnection(proxy); 
con.setRequestMethod("GET"); 
con.setRequestProperty("Content-Type","application/json"); 
con.setRequestProperty("Authorization", authString); 
con.setDoInput(true); 
DataInputStream input = new DataInputStream(con.getInputStream()); 
result = new Scanner(input).useDelimiter("\\Z").next(); 

私は私のトラフィックや待ち時間を短縮するためにキャッシュを使用するようにしたいのですが、私は彼らに時間ごとに更新したいと思います。

私の質問はURLConnectionを毎時キャッシュにリフレッシュするにはどうすればいいですか?

昇天 - Javaの7

+0

(データの入力ストリームは、サーバから返されたデータに基づいています) ()を再度押すと、データがリフレッシュされます。 java.util.Timerに関連して、これはおそらくあなたが必要とするものです。私は何かが明らかに欠けていますか? –

+0

クライアントがマイクロサービスを呼び出すときに、タイマーの実行を組み合わせる必要はありませんか? – hawkeye

答えて

0

私が理解から、あなたは一定の間隔で繰り返しクライアントを呼び出す必要があります。ここでそれを行う方法です(Java7)。これがあなたの必要とインラインであるかどうかは分かりません。ダミーURLで2秒ごとにサービスコールが行われます。ドキュメントから読み取ると同様に再度接続を開いてのgetInputStreamを実行した場合、何度も何度も呼び出したタスクので、データが更新されます、

import java.io.DataInputStream; 
import java.net.HttpURLConnection; 
import java.net.URL; 
import java.util.Timer; 
import java.util.TimerTask; 

public class CallAPIProxy { 
    Timer timer; 

    public CallAPIProxy(int seconds) { 
     timer = new Timer(); 
     // timer.schedule(new APICallerTask(), seconds * 1000); 
     timer.scheduleAtFixedRate(new APICallerTask(), 2000, 2000); 

    } 

    class APICallerTask extends TimerTask { 
     public void run() { 
      String httpsURL = "http://jsonplaceholder.typicode.com/posts/1"; 
      // String httpsURL = "https://myrestserver/path" + id; 
      // Proxy proxy = new Proxy(Proxy.Type.HTTP, new 
      // InetSocketAddress("myproxy", 8080)); 
      URL myurl; 
      try { 
       myurl = new URL(httpsURL); 
       System.setProperty("http.agent", "Chrome"); 

       // HttpsURLConnection con = (HttpsURLConnection) 
       // myurl.openConnection(proxy); 
       HttpURLConnection con = (HttpURLConnection) myurl.openConnection(); 
       con.setRequestMethod("GET"); 
       // con.setRequestProperty("Content-Type", "application/json"); 
       // con.setRequestProperty("Authorization", authString); 
       con.setDoInput(true); 
       DataInputStream input = new DataInputStream(con.getInputStream()); 
       // String result = new 
       // Scanner(input).useDelimiter("\\Z").next(); 
       // Scanner result = new Scanner(input); 
       StringBuffer inputLine = new StringBuffer(); 
       String tmp; 
       while ((tmp = input.readLine()) != null) { 
        inputLine.append(tmp); 
        System.out.println(tmp); 
       } 
       input.close(); 
       System.out.println("Result " + inputLine); 
      } catch (Exception e) { 
       e.printStackTrace(); 
      } 
     } 
    } 

    public static void main(String args[]) { 
     System.out.println("About to call the API task."); 
     new CallAPIProxy(2); 
     System.out.println("Task scheduled."); 
    } 
関連する問題