2011-11-23 15 views
6

मैं वेब सेवा को लागू करने के लिए रीस्टलेट का उपयोग कर रहा हूं। क्लाइंट (रीसेट का भी उपयोग करता है) सर्वर पर लगातार कई कॉल करता है, लेकिन थोड़ी सी कॉल सफलतापूर्वक पूर्ण होने के बाद, आगे कॉल सर्वर को लटकाता है, जो संदेश दिखाता है:रीस्टलेट क्लाइंट द्वारा रीस्टलेट क्लाइंट द्वारा दोहराए गए कॉल

जानकारी: नए कनेक्शन और लेनदेन को स्वीकार करना बंद करें । धागे की अधिकतम संख्या में वृद्धि पर विचार करें।

मैंने कोशिश की:

getContext().getParameters().add("maxThreads", "200"); 

लेकिन यह मदद नहीं करता है। किसी भी मामले में, ऐसा लगता है कि क्लाइंट असीमित कॉल करने में सक्षम होना चाहिए, और अधिकतम थ्रेड को बढ़ाकर सीमा को बढ़ाएं। ऐसा लगता है कि मैं प्रत्येक क्लाइंट कॉल के बाद कुछ संसाधन मुक्त नहीं कर रहा हूं या डिस्कनेक्ट नहीं कर रहा हूं, लेकिन मुझे नहीं पता कि ऐसा कैसे करें।

निम्नलिखित (छोटे जैसा मैं इसे बना सकता हूं) अकेले खड़े कार्यक्रम में समस्या का प्रदर्शन होता है। यह एक सरल सर्वर शुरू होता है और उसके बाद एक ग्राहक यह समय की एक गुच्छा कहता है:

/** You may copy, modify, and re-use this code as you see fit - Jim Irrer */ 
import java.io.ByteArrayInputStream; 
import java.io.IOException; 
import java.io.InputStream; 

import org.restlet.Application; 
import org.restlet.Component; 
import org.restlet.Request; 
import org.restlet.Response; 
import org.restlet.Restlet; 
import org.restlet.Server; 
import org.restlet.data.MediaType; 
import org.restlet.data.Method; 
import org.restlet.data.Protocol; 
import org.restlet.data.Status; 
import org.restlet.representation.InputRepresentation; 
import org.restlet.representation.Representation; 
import org.restlet.resource.ClientResource; 
import org.restlet.resource.Directory; 

public class SimpleServerPut extends Component implements Runnable { 
    private static final int PORT = 8080; 

    private static int readToByteArray(InputStream inputStream, byte[] buf) throws IOException { 
     int length = 0; 
     int b; 
     while ((b = inputStream.read()) != -1) { 
      buf[length++] = (byte)b; 
     } 
     return length; 
    } 

    @Override 
    public void run() { 
     getContext().getParameters().add("maxThreads", "200"); 

     // Create the HTTP server and listen on port PORT 
     SimpleServerPut simpleServer = new SimpleServerPut(); 
     Server server = new Server(Protocol.HTTP, PORT, simpleServer); 
     simpleServer.getClients().add(Protocol.FILE); 

     // Create an application 
     Application application = new Application(simpleServer.getContext()) { 
      @Override 
      public Restlet createRoot() { 
       return new Directory(getContext(), "C:"); 
      } 
     }; 

     // Attach the application to the component and start it 
     simpleServer.getDefaultHost().attach("/stuff/", application); 
     try { 
      server.start(); 
     } 
     catch (Exception ex) { 
      ex.printStackTrace(); 
     } 
    } 

    @Override 
    public void handle(Request request, Response response) { 
     // assume the worst 
     response.setStatus(Status.CLIENT_ERROR_METHOD_NOT_ALLOWED); 
     response.setEntity("No no - Bad client! Only do PUTs.", MediaType.TEXT_PLAIN); 

     try { 
      if (request.getMethod() == Method.PUT) { 
       InputStream inputStream = request.getEntity().getStream(); 
       byte[] buf = new byte[64*1024]; 
       int totalLength = readToByteArray(inputStream, buf); 
       response.setStatus(Status.SUCCESS_OK); 
       String msg = "Number of bytes received: " + totalLength; 
       response.setEntity(msg, MediaType.TEXT_PLAIN); 
       System.out.println("server: " + msg); 
       return; 
      } 
     } 
     catch (Exception ex) { 
      ex.printStackTrace(); 
     } 
    } 

    private static String callServer() throws IOException { 
     String urlText = "http://localhost:" + PORT + "/"; 
     ClientResource clientResource = new ClientResource(urlText); 
     clientResource.setReferrerRef(urlText); 

     byte[] buf = new byte[1000]; 
     for (int i = 0; i < buf.length; i++) { 
      buf[i] = (byte)((int)'a' + (i%26)); 
     } 
     ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(buf); 
     Representation representation = new InputRepresentation(byteArrayInputStream, MediaType.APPLICATION_OCTET_STREAM); 
     Representation representation2 = clientResource.put(representation); 
     byte[] responseBuf = new byte[16*1024]; 
     int length = readToByteArray(representation2.getStream(), responseBuf); 
     Response response = clientResource.getResponse(); 
     Status status = response.getStatus(); 
     return "status: " + status + " message: " + new String(responseBuf, 0, length); 
    } 

    // Start server and call it a bunch of times 
    public static void main(String[] args) throws Exception { 
     SimpleServerPut simpleServer = new SimpleServerPut(); 
     new Thread(simpleServer).start(); 
     Thread.sleep(200); // cheap trick to make sure that server is running 
     // make a bunch of client calls 
     for (int t = 0; t < 100; t++) { 
      System.out.println("client count: " + (t+1) + " " + callServer()); 
     } 
     System.exit(0); 
    } 
} 
+1

maxThreads कुंजी मान युग्म परम: ClientResource के जुड़े ग्राहक को सीधे (Restlet संस्करण 2.0.15 का उपयोग) ± को org.restlet में सेट करने की आवश्यकता है। सर्वर, org.restlet.Component में नहीं। इस तरह: 'सर्वर सर्वर = mycomponent.getServers()। जोड़ें (प्रोटोकॉल.एचटीटीपी, "लोकलहोस्ट", 9 0 9 0); server.getContext()। GetParameters()। ("MaxThreads", "20"); ' –

उत्तर

4

ताकि ग्राहक संसाधन विज्ञप्ति एक पंक्ति जोड़ दें: ग्राहक के लिए

Response response = clientResource.getResponse(); 
    Status status = response.getStatus(); 
    clientResource.release(); // add this line 

और सब कुछ काम करता है। आखिर में जब ग्राहक मर जाता है तो सर्वर का समय समाप्त हो जाता है, लेकिन इसमें कुछ समय लगता है।

0

क्लाइंटResource.release() को कॉल करने के अलावा, आप प्रतिनिधित्व पर निकास() को कॉल करना चाह सकते हैं।

Representation responseRepresentation = response.getEntity(); 
if (responseRepresentation != null) { 
    try { 
     responseRepresentation.exhaust(); 
    } catch (IOException e) { 
     // handle exception 
    } 
    responseRepresentation.release(); 
} 

this thread में संबंधित चर्चा।

1

मैं Restlet API

जाहिर .jar मैं का उपयोग कर के पिछले स्थिर रिलीज को डाउनलोड मेरी समस्या हल कर दिया है एक पुराने संस्करण जहां release() आदेश कोई असर नहीं पड़ता है थे।

अद्यतन करने से पहले ग्राहक लॉग केवल ग्राहक की शुरुआत आउटपुट:

Sep 05, 2012 9:50:19 AM org.restlet.engine.http.connector.HttpClientHelper start 
INFO: Starting the default HTTP client 

अब यह भी बंद outputing रहे हैं:

Sep 05, 2012 9:50:19 AM org.restlet.engine.http.connector.HttpClientHelper stop 
INFO: Stopping the default HTTP client 
+0

क्या आप स्पष्ट हैं कि आप किस संस्करण का उपयोग कर रहे थे और किस संस्करण ने समस्या का समाधान किया था? यह मुद्दों को डीबग करने के लिए दूसरों (मुझे सहित!) की मदद करेगा। –

5

हम केवल रोक कर समस्या का समाधान करने में सक्षम थे

Client c = (Client)clientResource.getNext(); 
try { 
    c.stop(); 
} catch (Exception e) { 
    //handle exception 
} 
+0

ऐसा लगता है कि धागे को तुरंत मरने के लिए मुझे यह भी करना पड़ा, लेकिन शायद यह रेस्टलेट के विशिष्ट संस्करण के साथ एक समस्या है? (ऊपर @ कैसियस-वर्गास-प्रतिष्ठा का उत्तर देखें) - आप किस संस्करण का उपयोग कर रहे हैं? –

+1

हमारे द्वारा उपयोग किया गया संस्करण 2.0 था।15, इस समय नवीनतम स्थिर संस्करण (और अभी भी यह है) – mahnkong

+0

धन्यवाद, हम एक ही संस्करण का उपयोग कर रहे हैं, और इसलिए मैं अब हमारे कोडबेस में यह स्पष्ट .stop() छोड़ दूंगा। –

संबंधित मुद्दे