List of usage examples for java.net SocketTimeoutException printStackTrace
public void printStackTrace()
From source file:br.com.anteros.android.synchronism.communication.HttpConnectionClient.java
public MobileResponse sendReceiveData(MobileRequest mobileRequest) { sessionId = HttpConnectionSession.getInstance().getSessionId(); add(mobileRequest.getFormattedHeader(), mobileRequest.getFormatedActions()); MobileResponse mobileResponse = new MobileResponse(); try {/* w w w . j a v a 2 s .c om*/ if (this.getSendData() != null) { for (MobileSendDataListener listener : this.getSendData().getListeners()) listener.onWaitServer(); } if (this.getSendData() != null) { for (MobileSendDataListener listener : this.getSendData().getListeners()) listener.onStatusConnectionServer("Conectando Servidor..."); } /* * Define url e estabelece conexo */ HttpPost httpPost = new HttpPost(url); HttpParams httpParameters = new BasicHttpParams(); // Set the timeout in milliseconds until a connection is // established. // The default value is zero, that means the timeout is not used. HttpConnectionParams.setConnectionTimeout(httpParameters, TIMEOUT_CONNECTION); // Set the default socket timeout (SO_TIMEOUT) // in milliseconds which is the timeout for waiting for data. HttpConnectionParams.setSoTimeout(httpParameters, TIMEOUT_SOCKET); if (httpClient == null) httpClient = new DefaultHttpClient(httpParameters); // /* * Setar o cookie da sesso */ if ((sessionId != null) && (!"".equals(sessionId))) { httpPost.setHeader("Cookie", "JSESSIONID=" + sessionId); } httpPost.setHeader("User-Agent", "Android"); httpPost.setHeader("Accept-Encoding", "gzip"); if (this.getSendData() != null) { for (MobileSendDataListener listener : this.getSendData().getListeners()) listener.onStatusConnectionServer("Enviando requisio..."); } // ByteArrayOutputStream baos = new ByteArrayOutputStream(); DataOutputStream out = new DataOutputStream(baos); // /* * Escrever no output */ out.writeInt(numOption); String aux[]; for (int i = 0; i < opPOST.size(); i++) { aux = (String[]) opPOST.get(i); out.writeUTF(aux[0]); byte[] b = aux[1].getBytes(); out.writeInt(b.length); out.write(b); aux = null; } out.flush(); ByteArrayEntity entity = new ByteArrayEntity(baos.toByteArray()); entity.setContentEncoding("UTF-8"); httpPost.setEntity(entity); httpPost.addHeader("Connection", "Keep-Alive"); httpPost.addHeader("Keep-Alive", "timeout=120000"); out.close(); // if (this.getSendData() != null) { for (MobileSendDataListener listener : this.getSendData().getListeners()) listener.onStatusConnectionServer("Recebendo dados..."); } if (this.getSendData() != null) { for (MobileSendDataListener listener : this.getSendData().getListeners()) listener.onDebugMessage("Recebendo dados conexo"); } /* * Aguardar resposta */ HttpResponse httpResponse = httpClient.execute(httpPost); List result = null; StatusLine statusLine = httpResponse.getStatusLine(); int code = statusLine.getStatusCode(); if (code != 200) { String msg = "Erro RECEBENDO resposta do Servidor " + url + " - Cdigo do Erro HTTP " + code + "-" + statusLine.getReasonPhrase(); mobileResponse.setStatus(msg); } else { if (this.getSendData() != null) { for (MobileSendDataListener listener : this.getSendData().getListeners()) listener.onStatusConnectionServer("Resposta OK !"); } /* * Ler cookie */ String tmpSessionId = null; for (Cookie c : httpClient.getCookieStore().getCookies()) { if ("JSESSIONID".equals(c.getName())) { tmpSessionId = c.getValue(); } } if (tmpSessionId != null) { sessionId = tmpSessionId; HttpConnectionSession.getInstance().setSessionId(sessionId); } // if (this.getSendData() != null) { for (MobileSendDataListener listener : this.getSendData().getListeners()) listener.onStatusConnectionServer("Lendo dados..."); } /* * Le os dados */ HttpEntity entityResponse = httpResponse.getEntity(); InputStream in = AndroidHttpClient.getUngzippedContent(entityResponse); BufferedReader reader = new BufferedReader(new InputStreamReader(in, "UTF-8")); String content = null; content = reader.readLine(); String line = null; while ((line = reader.readLine()) != null) { content += line; } line = ""; reader.close(); reader = null; in.close(); in = null; entityResponse.consumeContent(); entityResponse = null; // StringTokenizer messagePart = new StringTokenizer(content, "#"); content = null; if (this.getSendData() != null) { for (MobileSendDataListener listener : this.getSendData().getListeners()) listener.onDebugMessage("RECEBEU dados conexo"); } if (this.getSendData() != null) { for (MobileSendDataListener listener : this.getSendData().getListeners()) listener.onStatusConnectionServer("Processando resposta... "); } if (this.getSendData() != null) { for (MobileSendDataListener listener : this.getSendData().getListeners()) listener.onDebugMessage("Converteu string dados conexo"); } while (messagePart.hasMoreTokens()) { String resultData = messagePart.nextToken(); resultData = resultData.substring(resultData.indexOf("*") + 1, resultData.length()); if (result == null) result = formatData(resultData); else result.addAll(formatData(resultData)); } messagePart = null; } if (result != null) { mobileResponse.setFormattedParameters(result); result.clear(); result = null; } if (this.getSendData() != null) { for (MobileSendDataListener listener : this.getSendData().getListeners()) listener.onEndServer(); } } catch (SocketTimeoutException exTimeout) { exTimeout.printStackTrace(); wrapException(mobileResponse, "No foi possvel CONECTAR ao Servidor " + url + ". Verifique sua conexo e se o servidor est em funcionamento."); } catch (Exception e) { e.printStackTrace(); if ((e.getMessage() + "").contains("unreachable")) wrapException(mobileResponse, "Voc est sem acesso a internet. Verifique sua conexo. No foi possvel conectar ao servidor " + url); else wrapException(mobileResponse, "No foi possivel CONECTAR ao Servidor " + url + " " + e.getMessage()); } return mobileResponse; }
From source file:com.radicaldynamic.groupinform.tasks.DownloadFormsTask.java
@Override protected HashMap<String, String> doInBackground(ArrayList<FormDetails>... values) { ArrayList<FormDetails> toDownload = values[0]; int total = toDownload.size(); int count = 1; HashMap<String, String> result = new HashMap<String, String>(); for (int i = 0; i < total; i++) { FormDetails fd = toDownload.get(i); publishProgress(fd.formName, Integer.valueOf(count).toString(), Integer.valueOf(total).toString()); String message = ""; try {//w ww .j av a2 s.c o m // get the xml file // if we've downloaded a duplicate, this gives us the file File dl = downloadXform(fd.formName, fd.downloadUrl); // BEGIN custom // String[] projection = { // FormsColumns._ID, FormsColumns.FORM_FILE_PATH // }; // String[] selectionArgs = { // dl.getAbsolutePath() // }; // String selection = FormsColumns.FORM_FILE_PATH + "=?"; // Cursor alreadyExists = // Collect.getInstance() // .getContentResolver() // .query(FormsColumns.CONTENT_URI, projection, selection, selectionArgs, // null); // // Uri uri = null; // if (alreadyExists.getCount() <= 0) { // // doesn't exist, so insert it // ContentValues v = new ContentValues(); // v.put(FormsColumns.FORM_FILE_PATH, dl.getAbsolutePath()); // // HashMap<String, String> formInfo = FileUtils.parseXML(dl); // v.put(FormsColumns.DISPLAY_NAME, formInfo.get(FileUtils.TITLE)); // v.put(FormsColumns.MODEL_VERSION, formInfo.get(FileUtils.MODEL)); // v.put(FormsColumns.UI_VERSION, formInfo.get(FileUtils.UI)); // v.put(FormsColumns.JR_FORM_ID, formInfo.get(FileUtils.FORMID)); // v.put(FormsColumns.SUBMISSION_URI, formInfo.get(FileUtils.SUBMISSIONURI)); // uri = // Collect.getInstance().getContentResolver() // .insert(FormsColumns.CONTENT_URI, v); // } else { // alreadyExists.moveToFirst(); // uri = // Uri.withAppendedPath(FormsColumns.CONTENT_URI, // alreadyExists.getString(alreadyExists.getColumnIndex(FormsColumns._ID))); // } // END custom if (fd.manifestUrl != null) { // BEGIN custom // Cursor c = // Collect.getInstance().getContentResolver() // .query(uri, null, null, null, null); // if (c.getCount() > 0) { // // should be exactly 1 // c.moveToFirst(); // // String error = // downloadManifestAndMediaFiles( // c.getString(c.getColumnIndex(FormsColumns.FORM_MEDIA_PATH)), fd, // count, total); // if (error != null) { // message += error; // } // } String error = downloadManifestAndMediaFiles(dl.getParent(), fd, count, total); if (error != null) { message += error; } // END custom } else { // TODO: manifest was null? Log.e(t, "Manifest was null. PANIC"); } // BEGIN custom HashMap<String, String> formInfo = FileUtils.parseXML(dl); // Create form document and add attachments; commit to database FormDefinition fDoc = new FormDefinition(); fDoc.setName(formInfo.get(FileUtils.TITLE)); fDoc.setModelVersion(formInfo.get(FileUtils.MODEL)); fDoc.setUiVersion(formInfo.get(FileUtils.UI)); fDoc.setJavaRosaId(formInfo.get(FileUtils.FORMID)); fDoc.setSubmissionUri(formInfo.get(FileUtils.SUBMISSIONURI)); fDoc.setStatus(FormDefinition.Status.active); fDoc.setXmlHash(FileUtils.getMd5Hash(dl)); Collect.getInstance().getDbService().getDb().create(fDoc); String revision = fDoc.getRevision(); for (File f : dl.getParentFile().listFiles()) { String fileName = f.getName(); String attachmentName = fileName; if (Collect.Log.VERBOSE) Log.v(Collect.LOGTAG, t + ": attaching " + fileName + " to " + fDoc.getId()); if (fileName.equals(dl.getName())) attachmentName = "xml"; String fileExtension = fileName.substring(fileName.lastIndexOf(".") + 1); String contentType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(fileExtension); FileInputStream fis = new FileInputStream(f); AttachmentInputStream ais = new AttachmentInputStream(attachmentName, fis, contentType, f.length()); revision = Collect.getInstance().getDbService().getDb().createAttachment(fDoc.getId(), revision, ais); fis.close(); } // Remove temporary download directory FileUtilsExtended.deleteFolder(dl.getParent()); // END custom } catch (SocketTimeoutException se) { se.printStackTrace(); message += se.getMessage(); // BEGIN custom } catch (DuplicateXFormFile e) { message = " SKIPPED (duplicate)"; // END custom } catch (Exception e) { e.printStackTrace(); if (e.getCause() != null) { message += e.getCause().getMessage(); } else { message += e.getMessage(); } } count++; if (message.equalsIgnoreCase("")) { message = Collect.getInstance().getString(R.string.success); } result.put(fd.formName, message); } return result; }
From source file:org.opendatakit.briefcase.util.ServerFetcher.java
public boolean downloadFormAndSubmissionFiles(List<FormStatus> formsToTransfer) { boolean allSuccessful = true; // boolean error = false; int total = formsToTransfer.size(); for (int i = 0; i < total; i++) { FormStatus fs = formsToTransfer.get(i); if (isCancelled()) { fs.setStatusString("aborted. Skipping fetch of form and submissions...", true); EventBus.publish(new FormStatusEvent(fs)); return false; }//from w ww .ja v a 2 s. co m RemoteFormDefinition fd = (RemoteFormDefinition) fs.getFormDefinition(); fs.setStatusString("Fetching form definition", true); EventBus.publish(new FormStatusEvent(fs)); try { File tmpdl = FileSystemUtils.getTempFormDefinitionFile(); AggregateUtils.commonDownloadFile(serverInfo, tmpdl, fd.getDownloadUrl()); fs.setStatusString("resolving against briefcase form definitions", true); EventBus.publish(new FormStatusEvent(fs)); boolean successful = false; BriefcaseFormDefinition briefcaseLfd; DatabaseUtils formDatabase = null; try { try { briefcaseLfd = BriefcaseFormDefinition.resolveAgainstBriefcaseDefn(tmpdl); if (briefcaseLfd.needsMediaUpdate()) { if (fd.getManifestUrl() != null) { File mediaDir = FileSystemUtils.getMediaDirectory(briefcaseLfd.getFormDirectory()); String error = downloadManifestAndMediaFiles(mediaDir, fs); if (error != null) { allSuccessful = false; fs.setStatusString("Error fetching form definition: " + error, false); EventBus.publish(new FormStatusEvent(fs)); continue; } } } formDatabase = new DatabaseUtils( FileSystemUtils.getFormDatabase(briefcaseLfd.getFormDirectory())); } catch (BadFormDefinition e) { e.printStackTrace(); allSuccessful = false; fs.setStatusString("Error parsing form definition: " + e.getMessage(), false); EventBus.publish(new FormStatusEvent(fs)); continue; } fs.setStatusString("preparing to retrieve instance data", true); EventBus.publish(new FormStatusEvent(fs)); File formInstancesDir = FileSystemUtils .getFormInstancesDirectory(briefcaseLfd.getFormDirectory()); // this will publish events successful = downloadAllSubmissionsForForm(formInstancesDir, formDatabase, briefcaseLfd, fs); } catch (FileSystemException e) { e.printStackTrace(); allSuccessful = false; fs.setStatusString("unable to open form database: " + e.getMessage(), false); EventBus.publish(new FormStatusEvent(fs)); continue; } finally { if (formDatabase != null) { try { formDatabase.close(); } catch (SQLException e) { e.printStackTrace(); allSuccessful = false; fs.setStatusString("unable to close form database: " + e.getMessage(), false); EventBus.publish(new FormStatusEvent(fs)); continue; } } } allSuccessful = allSuccessful && successful; // on success, we haven't actually set a success event (because we don't know we're done) if (successful) { fs.setStatusString("SUCCESS!", true); EventBus.publish(new FormStatusEvent(fs)); } else { fs.setStatusString("FAILED.", true); EventBus.publish(new FormStatusEvent(fs)); } } catch (SocketTimeoutException se) { se.printStackTrace(); allSuccessful = false; fs.setStatusString( "Communications to the server timed out. Detailed message: " + se.getLocalizedMessage() + " while accessing: " + fd.getDownloadUrl() + " A network login screen may be interfering with the transmission to the server.", false); EventBus.publish(new FormStatusEvent(fs)); continue; } catch (IOException e) { e.printStackTrace(); allSuccessful = false; fs.setStatusString( "Unexpected error: " + e.getLocalizedMessage() + " while accessing: " + fd.getDownloadUrl() + " A network login screen may be interfering with the transmission to the server.", false); EventBus.publish(new FormStatusEvent(fs)); continue; } catch (FileSystemException e) { e.printStackTrace(); allSuccessful = false; fs.setStatusString( "Unexpected error: " + e.getLocalizedMessage() + " while accessing: " + fd.getDownloadUrl(), false); EventBus.publish(new FormStatusEvent(fs)); continue; } catch (URISyntaxException e) { e.printStackTrace(); allSuccessful = false; fs.setStatusString( "Unexpected error: " + e.getLocalizedMessage() + " while accessing: " + fd.getDownloadUrl(), false); EventBus.publish(new FormStatusEvent(fs)); continue; } catch (TransmissionException e) { e.printStackTrace(); allSuccessful = false; fs.setStatusString( "Unexpected error: " + e.getLocalizedMessage() + " while accessing: " + fd.getDownloadUrl(), false); EventBus.publish(new FormStatusEvent(fs)); continue; } } return allSuccessful; }
From source file:org.spinsuite.sync.SyncDataTask.java
/** * Call Web Service And get Response// w w w. ja va2 s . c o m * @author Carlos Parada, cparada@erpcya.com, ERPCyA http://www.erpcya.com * @param p_SO_Param * @throws IOException * @throws XmlPullParserException * @return void */ private void callWebService(SoapObject p_SO_Param, MSPSSyncMenu sm) { m_PublicTittle = sm.getName(); m_PublicMsg = sm.getDescription(); m_Progress = -1; publishOnRunning(); soapObject = new CommunicationSoap(m_Current_URL, m_NameSpace, m_MethodValue, SyncValues.IsNetService); soapObject.setM_SoapAction(m_NameSpace + "/" + m_MethodValue); if (p_SO_Param != null) soapObject.addSoapObject(p_SO_Param); soapObject.init_envelope(); if (m_TimeOut == 0) soapObject.initTransport(); else soapObject.initTransport(m_TimeOut); try { soapObject.call(); soapResponse = (SoapObject) soapObject.getM_Envelope().getResponse(); } catch (SocketTimeoutException e) { e.printStackTrace(); m_PublicMsg = e.getLocalizedMessage(); } catch (XmlPullParserException e) { e.printStackTrace(); m_PublicMsg = e.getLocalizedMessage(); } catch (IOException e) { e.printStackTrace(); m_PublicMsg = e.getLocalizedMessage(); } catch (Exception e) { e.printStackTrace(); m_PublicMsg = e.getLocalizedMessage(); } finally { publishOnRunning(); } }
From source file:org.andrewberman.sync.InheritMe.java
void downloadURLToFile(String url, File destFile) throws Exception { String origUrl = url;/* w w w.j av a2s . com*/ File origFile = destFile; Thread.sleep(200); try { destFile.getParentFile().mkdirs(); destFile.createNewFile(); } catch (Exception e) { errS.println("Error creating new file: " + destFile + " . Skipping this PDF..."); throw e; } out.print(" Downloading: "); url = StringEscapeUtils.escapeHtml(url); System.out.println(url); url = url.replaceAll(" ", "%20"); GetMethod get = new GetMethod(url); ByteArrayOutputStream outS = new ByteArrayOutputStream(); try { System.out.println(" Executing get..."); httpclient.executeMethod(get); System.out.println(" Done!"); BufferedInputStream in = new BufferedInputStream(get.getResponseBodyAsStream()); int i = 0; int ind = 0; long length = get.getResponseContentLength(); int starRatio = (int) length / 20; int numStars = 0; while ((i = in.read()) != -1) { if (length != -1 && ind % starRatio == 0) { status(" Downloading..." + repeat(".", ++numStars)); out.print("*"); } if (ind % 512 == 0) { waitOrExit(); } outS.write(i); ind++; } in.close(); outS.flush(); RandomAccessFile raf = new RandomAccessFile(destFile, "rw"); raf.write(outS.toByteArray()); // raf.write(get.getResponseBody()); raf.close(); } catch (java.net.SocketTimeoutException ste) { ste.printStackTrace(); if (this.retriesLeft > 0) { this.retriesLeft--; System.out.println("Retries left: " + this.retriesLeft); this.downloadURLToFile(origUrl, origFile); } else { throw ste; } } finally { outS.close(); get.releaseConnection(); outS = null; out.print("\n"); } }
From source file:com.zimbra.qa.unittest.TestZimbraHttpConnectionManager.java
public void testSoTimeoutViaHttpPostMethod() throws Exception { int serverPort = 7778; String resourceToPost = "/opt/zimbra/unittest/rights-unittest.xml"; long delayInServer = 100000; // delay 10 seconds in server int soTimeout = 60000; // 3000; // 3 seconds, 0 for infinite wait String qp = "?" + SimpleHttpServer.DelayWhen.BEFORE_WRITING_RESPONSE_HEADERS.name() + "=" + delayInServer; String uri = "http://localhost:" + serverPort + resourceToPost + qp; // start a http server for testing SimpleHttpServer.start(serverPort);/*from w w w .j a v a 2s . c om*/ // post the exported content to the target server HttpClient httpClient = ZimbraHttpConnectionManager.getInternalHttpConnMgr().newHttpClient(); PostMethod method = new PostMethod(uri); method.getParams().setSoTimeout(soTimeout); // infinite wait because it can take a long time to import a large mailbox File file = new File(resourceToPost); FileInputStream fis = null; long startTime = System.currentTimeMillis(); long endTime; try { fis = new FileInputStream(file); InputStreamRequestEntity isre = new InputStreamRequestEntity(fis, file.length(), MimeConstants.CT_APPLICATION_OCTET_STREAM); method.setRequestEntity(isre); int respCode = httpClient.executeMethod(method); dumpResponse(respCode, method, ""); Assert.fail(); // nope, it should have timed out } catch (java.net.SocketTimeoutException e) { e.printStackTrace(); // good, just what we want endTime = System.currentTimeMillis(); long elapsedTime = endTime - startTime; System.out.println("Client timed out after " + elapsedTime + " msecs"); } catch (Exception e) { e.printStackTrace(); Assert.fail(); } finally { method.releaseConnection(); } // shutdown the server SimpleHttpServer.shutdown(); }
From source file:org.apache.jxtadoop.hdfs.server.datanode.DataXceiver.java
/** * Read/write data from/to the DataXceiveServer. *///from w ww . jav a 2 s . c om public void run() { DataInputStream in = null; LOG.debug("DataXceiver starts processing new incoming data"); try { //in = new DataInputStream( // new BufferedInputStream(NetUtils.getInputStream(s), // SMALL_BUFFER_SIZE)); LOG.debug("Reading version from stream"); LOG.debug("DataXceiver socket connected : " + s.isConnected()); LOG.debug("DataXceiver socket closed : " + s.isClosed()); ReliableInputStream ris = (ReliableInputStream) s.getInputStream(); BufferedInputStream bis = new BufferedInputStream(ris); in = new DataInputStream(bis); short version = in.readShort(); LOG.debug("Version read : " + version); if (version != DataTransferProtocol.DATA_TRANSFER_VERSION) { throw new IOException("Version Mismatch"); } //boolean local = s.getInetAddress().equals(s.getLocalAddress()); boolean local = false; /** TODO A modifier proprement **/ LOG.debug("Reading op type from stream"); byte op = in.readByte(); LOG.debug("op type read : " + op); // Make sure the xciver count is not exceeded int curXceiverCount = datanode.getXceiverCount(); if (curXceiverCount > dataXceiverServer.maxXceiverCount) { throw new IOException("xceiverCount " + curXceiverCount + " exceeds the limit of concurrent xcievers " + dataXceiverServer.maxXceiverCount); } long startTime = DataNode.now(); switch (op) { case DataTransferProtocol.OP_READ_BLOCK: LOG.debug("Received a OP_READ_BLOCK op"); readBlock(in); datanode.myMetrics.readBlockOp.inc(DataNode.now() - startTime); if (local) datanode.myMetrics.readsFromLocalClient.inc(); else datanode.myMetrics.readsFromRemoteClient.inc(); break; case DataTransferProtocol.OP_WRITE_BLOCK: LOG.debug("Received a OP_WRITE_BLOCK op"); writeBlock(in); datanode.myMetrics.writeBlockOp.inc(DataNode.now() - startTime); if (local) datanode.myMetrics.writesFromLocalClient.inc(); else datanode.myMetrics.writesFromRemoteClient.inc(); break; case DataTransferProtocol.OP_READ_METADATA: LOG.debug("Received a OP_READ_METADATA op"); readMetadata(in); datanode.myMetrics.readMetadataOp.inc(DataNode.now() - startTime); break; case DataTransferProtocol.OP_REPLACE_BLOCK: // for balancing purpose; send to a destination LOG.debug("Received a OP_REPLACE_BLOCK op"); replaceBlock(in); datanode.myMetrics.replaceBlockOp.inc(DataNode.now() - startTime); break; case DataTransferProtocol.OP_COPY_BLOCK: // for balancing purpose; send to a proxy source LOG.debug("Received a OP_COPY_BLOCK op"); copyBlock(in); datanode.myMetrics.copyBlockOp.inc(DataNode.now() - startTime); break; case DataTransferProtocol.OP_BLOCK_CHECKSUM: //get the checksum of a block LOG.debug("Received a OP_BLOCK_CHECKSUM op"); getBlockChecksum(in); datanode.myMetrics.blockChecksumOp.inc(DataNode.now() - startTime); break; default: LOG.debug("Unknown op code"); throw new IOException("Unknown opcode " + op + " in data stream"); } IOUtils.closeStream(in); IOUtils.closeSocket(s); } catch (SocketTimeoutException ste) { LOG.debug("Time out while receiving data on DataXceiver"); LOG.debug(ste); ste.printStackTrace(); } catch (Exception t) { LOG.error(datanode.dnRegistration + ":DataXceiver FAILED", t); t.printStackTrace(); } finally { LOG.debug(datanode.dnRegistration + ":Number of active connections is: " + datanode.getXceiverCount()); // IOUtils.closeStream(in); // IOUtils.closeSocket(s); dataXceiverServer.childSockets.remove(s); s = null; } }
From source file:org.apache.jxtadoop.hdfs.server.datanode.DataXceiver.java
/** * Write a block to disk./*from www . ja v a 2 s . com*/ * * @param in The stream to read from * @throws IOException */ private void writeBlock(DataInputStream in) throws IOException { LOG.debug("Mathod called : writeBlock()"); DatanodeInfo srcDataNode = null; LOG.debug("writeBlock receive buf size " + s.getReceiveBufferSize() + " tcp no delay " + s.getTcpNoDelay()); // // Read in the header // Block block = new Block(in.readLong(), dataXceiverServer.estimateBlockSize, in.readLong()); LOG.info("Receiving block " + block + " src: " + remoteAddress + " dest: " + localAddress); int pipelineSize = in.readInt(); // num of datanodes in entire pipeline boolean isRecovery = in.readBoolean(); // is this part of recovery? String client = Text.readString(in); // working on behalf of this client boolean hasSrcDataNode = in.readBoolean(); // is src node info present if (hasSrcDataNode) { srcDataNode = new DatanodeInfo(); srcDataNode.readFields(in); } int numTargets = in.readInt(); if (numTargets < 0) { throw new IOException("Mislabelled incoming datastream."); } DatanodeInfo targets[] = new DatanodeInfo[numTargets]; for (int i = 0; i < targets.length; i++) { DatanodeInfo tmp = new DatanodeInfo(); tmp.readFields(in); targets[i] = tmp; } DataOutputStream mirrorOut = null; // stream to next target DataInputStream mirrorIn = null; // reply from next target DataOutputStream replyOut = null; // stream to prev target JxtaSocket mirrorSock = null; // socket to next target BlockReceiver blockReceiver = null; // responsible for data handling String mirrorNode = null; // the name:port of next target String firstBadLink = ""; // first datanode that failed in connection setup try { // open a block receiver and check if the block does not exist /*blockReceiver = new BlockReceiver(block, in, s.getRemoteSocketAddress().toString(), s.getLocalSocketAddress().toString(), isRecovery, client, srcDataNode, datanode);*/ blockReceiver = new BlockReceiver(block, in, ((JxtaSocketAddress) s.getRemoteSocketAddress()).getPeerId().toString(), ((JxtaSocketAddress) s.getLocalSocketAddress()).getPeerId().toString(), isRecovery, client, srcDataNode, datanode); // get a connection back to the previous target //replyOut = new DataOutputStream( // NetUtils.getOutputStream(s, datanode.socketWriteTimeout)); ReliableOutputStream replyOutRos = (ReliableOutputStream) s.getOutputStream(); replyOut = new DataOutputStream(replyOutRos); // // Open network conn to backup machine, if // appropriate // if (targets.length > 0) { // JxtaSocketAddress mirrorTarget = null; // Connect to backup machine mirrorNode = targets[0].getPeerId(); // mirrorTarget = NetUtils.createSocketAddr(mirrorNode); // mirrorSock = datanode.newSocket(); try { //int timeoutValue = numTargets * datanode.socketTimeout; //int writeTimeout = datanode.socketWriteTimeout + // (HdfsConstants.WRITE_TIMEOUT_EXTENSION * numTargets); // NetUtils.connect(mirrorSock, mirrorTarget, timeoutValue); mirrorSock = datanode.getDnPeer().getInfoSocket(mirrorNode.toString()); if (mirrorSock == null) throw new IOException("Failed to get a mirror socket"); //mirrorSock.setSoTimeout(timeoutValue); //mirrorSock.setTcpNoDelay(true); //mirrorSock.setSoTimeout(Integer.parseInt(datanode.getConf().get("hadoop.p2p.info.timeout"))); //mirrorSock.setSendBufferSize(DEFAULT_DATA_SOCKET_SIZE); /*mirrorOut = new DataOutputStream( new BufferedOutputStream( NetUtils.getOutputStream(mirrorSock, writeTimeout), SMALL_BUFFER_SIZE)); mirrorIn = new DataInputStream(NetUtils.getInputStream(mirrorSock)); */ mirrorOut = new DataOutputStream((ReliableOutputStream) mirrorSock.getOutputStream()); mirrorIn = new DataInputStream((ReliableInputStream) mirrorSock.getInputStream()); // Write header: Copied from DFSClient.java! mirrorOut.writeShort(DataTransferProtocol.DATA_TRANSFER_VERSION); mirrorOut.write(DataTransferProtocol.OP_WRITE_BLOCK); mirrorOut.writeLong(block.getBlockId()); mirrorOut.writeLong(block.getGenerationStamp()); mirrorOut.writeInt(pipelineSize); mirrorOut.writeBoolean(isRecovery); Text.writeString(mirrorOut, client); mirrorOut.writeBoolean(hasSrcDataNode); if (hasSrcDataNode) { // pass src node information srcDataNode.write(mirrorOut); } mirrorOut.writeInt(targets.length - 1); for (int i = 1; i < targets.length; i++) { targets[i].write(mirrorOut); } blockReceiver.writeChecksumHeader(mirrorOut); mirrorOut.flush(); // read connect ack (only for clients, not for replication req) if (client.length() != 0) { firstBadLink = Text.readString(mirrorIn); if (LOG.isDebugEnabled() || firstBadLink.length() > 0) { LOG.info("Datanode " + targets.length + " got response for connect ack " + " from downstream datanode with firstbadlink as " + firstBadLink); } } } catch (SocketTimeoutException ste) { LOG.debug("Time out while receiving data on DataXceiver"); LOG.debug(ste); ste.printStackTrace(); } catch (IOException e) { LOG.debug("IOException occurred : " + e.getMessage()); if (client.length() != 0) { Text.writeString(replyOut, mirrorNode); replyOut.flush(); } IOUtils.closeStream(mirrorOut); mirrorOut = null; IOUtils.closeStream(mirrorIn); mirrorIn = null; if (mirrorSock != null) { IOUtils.closeSocket(mirrorSock); mirrorSock = null; } if (client.length() > 0) { throw e; } else { LOG.info(datanode.dnRegistration + ":Exception transfering block " + block + " to mirror " + mirrorNode + ". continuing without the mirror.\n" + StringUtils.stringifyException(e)); } } } // send connect ack back to source (only for clients) if (client.length() != 0) { if (LOG.isDebugEnabled() || firstBadLink.length() > 0) { LOG.info("Datanode " + targets.length + " forwarding connect ack to upstream firstbadlink is " + firstBadLink); } Text.writeString(replyOut, firstBadLink); replyOut.flush(); } // receive the block and mirror to the next target String mirrorAddr = (mirrorSock == null) ? null : mirrorNode; blockReceiver.receiveBlock(mirrorOut, mirrorIn, replyOut, mirrorAddr, null, targets.length); // if this write is for a replication request (and not // from a client), then confirm block. For client-writes, // the block is finalized in the PacketResponder. if (client.length() == 0) { datanode.notifyNamenodeReceivedBlock(block, DataNode.EMPTY_DEL_HINT); LOG.info("Received block " + block + " src: " + remoteAddress + " dest: " + localAddress + " of size " + block.getNumBytes()); } if (datanode.blockScanner != null) { datanode.blockScanner.addBlock(block); } } catch (IOException ioe) { LOG.info("writeBlock " + block + " received exception " + ioe); throw ioe; } catch (Exception e) { LOG.warn("Exception occurred in writting block : " + e.getMessage()); } finally { // close all opened streams LOG.debug("Finalizing : writeBlock()"); IOUtils.closeStream(mirrorOut); IOUtils.closeStream(mirrorIn); IOUtils.closeStream(replyOut); IOUtils.closeSocket(mirrorSock); IOUtils.closeStream(blockReceiver); } }
From source file:com.zimbra.qa.unittest.TestZimbraHttpConnectionManager.java
public void testSoTimeoutViaConnMgrParam() throws Exception { int serverPort = 7778; String resourceToGet = "/opt/zimbra/unittest/rights-unittest.xml"; long delayInServer = 10000; // delay 10 seconds in server int soTimeout = 3000; // 3 seconds String qp = "?" + SimpleHttpServer.DelayWhen.BEFORE_WRITING_RESPONSE_HEADERS.name() + "=" + delayInServer; String uri = "http://localhost:" + serverPort + resourceToGet + qp; // start a http server for testing SimpleHttpServer.start(serverPort);//from ww w.j av a 2 s.co m HttpClient httpClient = ZimbraHttpConnectionManager.getExternalHttpConnMgr().newHttpClient(); GetMethod method = new GetMethod(uri); long startTime = System.currentTimeMillis(); long endTime; try { // int respCode = HttpClientUtil.executeMethod(httpClient, method); int respCode = httpClient.executeMethod(method); dumpResponse(respCode, method, ""); Assert.fail(); // nope, it should have timed out } catch (java.net.SocketTimeoutException e) { // good, just what we want endTime = System.currentTimeMillis(); long elapsedTime = endTime - startTime; System.out.println("Client timed out after " + elapsedTime + " msecs"); } catch (Exception e) { e.printStackTrace(); Assert.fail(); } finally { method.releaseConnection(); } // shutdown the server SimpleHttpServer.shutdown(); }
From source file:com.zimbra.qa.unittest.TestZimbraHttpConnectionManager.java
public void testSoTimeoutViaHttpMethod() throws Exception { int serverPort = 7778; String resourceToGet = "/opt/zimbra/unittest/rights-unittest.xml"; long delayInServer = 10000; // delay 10 seconds in server int soTimeout = 3000; // 3 seconds String qp = "?" + SimpleHttpServer.DelayWhen.BEFORE_WRITING_RESPONSE_HEADERS.name() + "=" + delayInServer; String uri = "http://localhost:" + serverPort + resourceToGet + qp; // start a http server for testing SimpleHttpServer.start(serverPort);//from w w w .j a v a 2s . c o m // HttpClient httpClient = ZimbraHttpConnectionManager.getExternalHttpConnMgr().newHttpClient(); HttpClient httpClient = ZimbraHttpConnectionManager.getInternalHttpConnMgr().newHttpClient(); GetMethod method = new GetMethod(uri); // method.getParams().setParameter(HttpConnectionParams.SO_TIMEOUT, Integer.valueOf(soTimeout)); method.getParams().setSoTimeout(soTimeout); long startTime = System.currentTimeMillis(); long endTime; try { // int respCode = HttpClientUtil.executeMethod(httpClient, method); int respCode = httpClient.executeMethod(method); dumpResponse(respCode, method, ""); Assert.fail(); // nope, it should have timed out } catch (java.net.SocketTimeoutException e) { // good, just what we want endTime = System.currentTimeMillis(); long elapsedTime = endTime - startTime; System.out.println("Client timed out after " + elapsedTime + " msecs"); } catch (Exception e) { e.printStackTrace(); Assert.fail(); } finally { method.releaseConnection(); } // shutdown the server SimpleHttpServer.shutdown(); }