2

我在 java 中使用 URLConnection 对象,将数据发布到远程数据库(通过 REST)。我需要发布很多小数据包,所以我想让我的连接保持打开状态,直到我确定没有更多新的数据包要发送。但是,有可能在很长一段时间内没有要发送的数据包。

我想知道当连接断开时(例如由于超时),URLConnection 对象是否会被重置为 null。如果是这种情况,我可以检查对象是否为空并重新连接。但是,如果不是这种情况,我想就如何廉价地解决这个问题提出一些建议(是否有必要为每个数据包打开和关闭连接?)。

这是我当前的代码(尚未完全完成!:)):

public class PersistenceServiceImpl implements PersistenceService, Runnable{

public void activate(){
    new Thread(this).start();
}

@Override
public void run() {
    LinkedBlockingQueue<JSONObject> queue = JSONQueue.getUniqueInstance();
    JSONObject json;
    try {
        final URL url = new URL("");
        URLConnection urlConn = null;
        DataOutputStream printout;
        while(true){
            makeConnection(url, urlConn);
            try {
                json = queue.take();
                printout = new DataOutputStream (urlConn.getOutputStream());
                String content = json.toString();
                printout.writeBytes(content);
                printout.flush();
                printout.close();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }catch(Exception e){

    }
}

public void makeConnection(URL url, URLConnection urlConn) {
    if(urlConn == null){
        try {
            urlConn = url.openConnection();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }else{
        try {
            urlConn.connect();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    urlConn.setDoInput(false);
    urlConn.setDoOutput(true);
    urlConn.setUseCaches(false);
    urlConn.setRequestProperty("Content-Type", "application/json");
}

}

编辑:

我的新的和改进的(或者我希望如此)代码:p:

public class PersistenceServiceImpl implements PersistenceService, Runnable{

public void activate(){
    new Thread(this).start();
}

LinkedBlockingQueue<JSONObject> queue = JSONQueue.getUniqueInstance();
JSONObject json;
final String urlString = new String("http://localhost/");
@Override
public void run() {
    while(true){
        try {
            URL url = new URL("http://localhost:8080/RESTful");
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setDoOutput(true);
            conn.setRequestMethod("POST");
            conn.setRequestProperty("Content-Type", "application/json");
            json = queue.take();
            DataOutputStream output = new DataOutputStream(conn.getOutputStream());
            output.write(json.toString().getBytes("utf-8"));
            output.flush();
            output.close();
        } catch (MalformedURLException e) {
            System.out.println("MALFORMEDURLEXCEPTION: fout url!!");
            e.printStackTrace();
        } catch (ProtocolException e){
            System.out.println("PROTCOL EXCEPTION: post fout");
        } catch (IOException e) {
            System.out.println("IOEXCEPTION: connection failure");
            e.printStackTrace();
        } catch (InterruptedException e) {
            System.out.println("INTERRUPTEDEXCEPTION: queue.take() deed iets fout");
            e.printStackTrace();
        } 
4

3 回答 3

0

会抛出一些异常。在你的 catch(Exception e) 部分处理它。请注意,您的 URLConnection 实例可能会损坏但不能为空。

于 2013-07-22T10:59:10.657 回答
0

您的代码无效,因为它假定通过引用传递,这在 Java 中不会发生。但你不必担心。HttpURLConnection已经在幕后进行连接池,直到您调用disconnect().

于 2013-07-22T11:18:01.710 回答
-1

. .

于 2013-07-23T08:17:44.490 回答