List of usage examples for java.lang Thread interrupted
public static boolean interrupted()
From source file:rocks.inspectit.ui.rcp.storage.util.DataRetriever.java
/** * Retrieves the wanted data described in the {@link StorageDescriptor} from the desired * {@link CmrRepositoryDefinition}. This method will try to invoke as less as possible HTTP * requests for all descriptors./* w w w . j a v a 2 s . c o m*/ * <p> * The method will execute the HTTP requests sequentially. * <p> * It is not guaranteed that amount of returned objects in the list is same as the amount of * provided descriptors. If some of the descriptors are pointing to the wrong files or files * positions, it can happen that this influences the rest of the descriptor that point to the * same file. Thus, a special care needs to be taken that the data in descriptors is correct. * * @param <E> * Type of the objects are wanted. * @param cmrRepositoryDefinition * {@link CmrRepositoryDefinition}. * @param storageData * {@link StorageData} that points to the wanted storage. * @param descriptors * Descriptors. * @return List of objects in the supplied generic type. Note that if the data described in the * descriptor is not of a supplied generic type, there will be a casting exception * thrown. * @throws SerializationException * If {@link SerializationException} occurs. * @throws IOException * If {@link IOException} occurs. */ @SuppressWarnings("unchecked") public <E extends DefaultData> List<E> getDataViaHttp(CmrRepositoryDefinition cmrRepositoryDefinition, IStorageData storageData, List<IStorageDescriptor> descriptors) throws IOException, SerializationException { Map<Integer, List<IStorageDescriptor>> separateFilesGroup = createFilesGroup(descriptors); List<E> receivedData = new ArrayList<>(); String serverUri = getServerUri(cmrRepositoryDefinition); HttpClient httpClient = new DefaultHttpClient(); for (Map.Entry<Integer, List<IStorageDescriptor>> entry : separateFilesGroup.entrySet()) { HttpGet httpGet = new HttpGet( serverUri + storageManager.getHttpFileLocation(storageData, entry.getKey())); StringBuilder rangeHeader = new StringBuilder("bytes="); RangeDescriptor rangeDescriptor = null; for (IStorageDescriptor descriptor : entry.getValue()) { if (null == rangeDescriptor) { rangeDescriptor = new RangeDescriptor(descriptor); } else { if ((rangeDescriptor.getEnd() + 1) == descriptor.getPosition()) { rangeDescriptor.setEnd((descriptor.getPosition() + descriptor.getSize()) - 1); } else { rangeHeader.append(rangeDescriptor.toString()); rangeHeader.append(','); rangeDescriptor = new RangeDescriptor(descriptor); } } } rangeHeader.append(rangeDescriptor); httpGet.addHeader("Range", rangeHeader.toString()); ISerializer serializer = null; try { serializer = serializerQueue.take(); } catch (InterruptedException e) { Thread.interrupted(); } InputStream inputStream = null; Input input = null; try { HttpResponse response = httpClient.execute(httpGet); HttpEntity entity = response.getEntity(); if (MultipartEntityUtil.isMultipart(entity)) { inputStream = entity.getContent(); @SuppressWarnings("deprecation") // all non-deprecated constructors have default modifier MultipartStream multipartStream = new MultipartStream(inputStream, MultipartEntityUtil.getBoundary(entity).getBytes()); ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); boolean nextPart = multipartStream.skipPreamble(); while (nextPart) { multipartStream.readHeaders(); multipartStream.readBodyData(byteArrayOutputStream); input = new Input(byteArrayOutputStream.toByteArray()); while (KryoUtil.hasMoreBytes(input)) { Object object = serializer.deserialize(input); E element = (E) object; receivedData.add(element); } nextPart = multipartStream.readBoundary(); } } else { // when kryo changes the visibility of optional() method, we can really stream input = new Input(EntityUtils.toByteArray(entity)); while (KryoUtil.hasMoreBytes(input)) { Object object = serializer.deserialize(input); E element = (E) object; receivedData.add(element); } } } finally { if (null != inputStream) { inputStream.close(); } if (null != input) { input.close(); } serializerQueue.add(serializer); } } return receivedData; }
From source file:org.cyclop.service.common.FileStorage.java
private FileChannel lock(Path histPath, FileChannel channel) throws IOException { LOG.debug("Trying to log file: {}", histPath); long start = System.currentTimeMillis(); String lastExMessage = null;// www . jav a 2 s.c o m FileChannel lockChannel = null; while (lockChannel == null && System.currentTimeMillis() - start < config.fileStore.lockWaitTimeoutMillis) { try { FileLock lock = channel.lock(); lockChannel = lock.channel(); } catch (FileLockInterruptionException | OverlappingFileLockException e) { lockRetryCount.incrementAndGet(); lastExMessage = e.getMessage(); LOG.debug("File lock on '{}' cannot be obtained (retrying operation): {}", histPath, lastExMessage); try { Thread.sleep(100); } catch (InterruptedException e1) { Thread.interrupted(); } } } if (lockChannel == null) { throw new ServiceException("File lock on '" + histPath + "' cannot be obtained: " + lastExMessage); } return lockChannel; }
From source file:org.apache.hadoop.hdfs.PeerCache.java
/** * Periodically check in the cache and expire the entries * older than expiryPeriod minutes//from w ww. java 2 s. c o m */ private void run() throws InterruptedException { for (long lastExpiryTime = Time.monotonicNow(); !Thread.interrupted(); Thread.sleep(expiryPeriod)) { final long elapsed = Time.monotonicNow() - lastExpiryTime; if (elapsed >= expiryPeriod) { evictExpired(expiryPeriod); lastExpiryTime = Time.monotonicNow(); } } clear(); throw new InterruptedException("Daemon Interrupted"); }
From source file:org.apache.sqoop.mapreduce.db.netezza.NetezzaExternalTableImportMapper.java
public void map(Integer dataSliceId, NullWritable val, Context context) throws IOException, InterruptedException { conf = context.getConfiguration();/*from w ww.j a va 2 s. c o m*/ dbc = new DBConfiguration(conf); numMappers = ConfigurationHelper.getConfNumMaps(conf); char rd = (char) conf.getInt(DelimiterSet.OUTPUT_RECORD_DELIM_KEY, '\n'); initNetezzaExternalTableImport(dataSliceId); counter = new PerfCounters(); counter.startClock(); Text outputRecord = new Text(); try { if (extTableThread.isAlive()) { String inputRecord = recordReader.readLine(); while (inputRecord != null) { if (Thread.interrupted()) { if (!extTableThread.isAlive()) { break; } } outputRecord.set(inputRecord + rd); // May be we should set the output to be String for faster performance // There is no real benefit in changing it to Text and then // converting it back in our case writeRecord(outputRecord, context); counter.addBytes(1 + inputRecord.length()); inputRecord = recordReader.readLine(); } } } finally { recordReader.close(); extTableThread.join(); counter.stopClock(); LOG.info("Transferred " + counter.toString()); if (extTableThread.hasExceptions()) { extTableThread.printException(); throw new IOException(extTableThread.getException()); } } }
From source file:org.dataconservancy.dcs.ingest.services.Finisher.java
private void checkIndex(Dcp dcp) { List<String> entityIds = new ArrayList<String>(); for (DcsEntity e : dcp) { entityIds.add(e.getId());/*w w w.j a v a2 s .c om*/ } if (entityIds.isEmpty()) { return; } long elapsed = 0; do { Iterator<String> entityItr = entityIds.iterator(); try { while (entityItr.hasNext()) { final String id = entityItr.next(); if (lookupQueryService.lookup(id) != null) { entityItr.remove(); } } Thread.sleep(pollIntervalMillis); elapsed = elapsed + pollIntervalMillis; } catch (InterruptedException e) { Thread.interrupted(); } catch (QueryServiceException e) { log.debug("Error communicating with the query service: " + e.getMessage(), e); } } while (elapsed < maxPollTimeMillis && entityIds.size() > 0); if (!entityIds.isEmpty()) { StringBuilder msg = new StringBuilder("Unable to confirm the presence of the following entities in the " + "index after polling " + elapsed + "ms:"); for (String id : entityIds) { msg.append(" [").append(id).append("]"); } throw new RuntimeException(msg.toString()); } }
From source file:org.apache.tinkerpop.gremlin.tinkergraph.process.computer.TinkerGraphComputer.java
@Override public Future<ComputerResult> submit() { // a graph computer can only be executed once if (this.executed) throw Exceptions.computerHasAlreadyBeenSubmittedAVertexProgram(); else/*ww w.ja v a2s .c om*/ this.executed = true; // it is not possible execute a computer if it has no vertex program nor mapreducers if (null == this.vertexProgram && this.mapReducers.isEmpty()) throw GraphComputer.Exceptions.computerHasNoVertexProgramNorMapReducers(); // it is possible to run mapreducers without a vertex program if (null != this.vertexProgram) { GraphComputerHelper.validateProgramOnComputer(this, this.vertexProgram); this.mapReducers.addAll(this.vertexProgram.getMapReducers()); } // get the result graph and persist state to use for the computation this.resultGraph = GraphComputerHelper.getResultGraphState(Optional.ofNullable(this.vertexProgram), Optional.ofNullable(this.resultGraph)); this.persist = GraphComputerHelper.getPersistState(Optional.ofNullable(this.vertexProgram), Optional.ofNullable(this.persist)); if (!this.features().supportsResultGraphPersistCombination(this.resultGraph, this.persist)) throw GraphComputer.Exceptions.resultGraphPersistCombinationNotSupported(this.resultGraph, this.persist); // ensure requested workers are not larger than supported workers if (this.workers > this.features().getMaxWorkers()) throw GraphComputer.Exceptions.computerRequiresMoreWorkersThanSupported(this.workers, this.features().getMaxWorkers()); // initialize the memory this.memory = new TinkerMemory(this.vertexProgram, this.mapReducers); final Future<ComputerResult> result = computerService.submit(() -> { final long time = System.currentTimeMillis(); final TinkerGraphComputerView view = TinkerHelper.createGraphComputerView(this.graph, this.graphFilter, null != this.vertexProgram ? this.vertexProgram.getVertexComputeKeys() : Collections.emptySet()); final TinkerWorkerPool workers = new TinkerWorkerPool(this.graph, this.memory, this.workers); try { if (null != this.vertexProgram) { // execute the vertex program this.vertexProgram.setup(this.memory); while (true) { if (Thread.interrupted()) throw new TraversalInterruptedException(); this.memory.completeSubRound(); workers.setVertexProgram(this.vertexProgram); workers.executeVertexProgram((vertices, vertexProgram, workerMemory) -> { vertexProgram.workerIterationStart(workerMemory.asImmutable()); while (vertices.hasNext()) { final Vertex vertex = vertices.next(); if (Thread.interrupted()) throw new TraversalInterruptedException(); vertexProgram.execute(ComputerGraph.vertexProgram(vertex, vertexProgram), new TinkerMessenger<>(vertex, this.messageBoard, vertexProgram.getMessageCombiner()), workerMemory); } vertexProgram.workerIterationEnd(workerMemory.asImmutable()); workerMemory.complete(); }); this.messageBoard.completeIteration(); this.memory.completeSubRound(); if (this.vertexProgram.terminate(this.memory)) { this.memory.incrIteration(); break; } else { this.memory.incrIteration(); } } view.complete(); // drop all transient vertex compute keys } // execute mapreduce jobs for (final MapReduce mapReduce : mapReducers) { final TinkerMapEmitter<?, ?> mapEmitter = new TinkerMapEmitter<>( mapReduce.doStage(MapReduce.Stage.REDUCE)); final SynchronizedIterator<Vertex> vertices = new SynchronizedIterator<>(this.graph.vertices()); workers.setMapReduce(mapReduce); workers.executeMapReduce(workerMapReduce -> { workerMapReduce.workerStart(MapReduce.Stage.MAP); while (true) { if (Thread.interrupted()) throw new TraversalInterruptedException(); final Vertex vertex = vertices.next(); if (null == vertex) break; workerMapReduce.map(ComputerGraph.mapReduce(vertex), mapEmitter); } workerMapReduce.workerEnd(MapReduce.Stage.MAP); }); // sort results if a map output sort is defined mapEmitter.complete(mapReduce); // no need to run combiners as this is single machine if (mapReduce.doStage(MapReduce.Stage.REDUCE)) { final TinkerReduceEmitter<?, ?> reduceEmitter = new TinkerReduceEmitter<>(); final SynchronizedIterator<Map.Entry<?, Queue<?>>> keyValues = new SynchronizedIterator( (Iterator) mapEmitter.reduceMap.entrySet().iterator()); workers.executeMapReduce(workerMapReduce -> { workerMapReduce.workerStart(MapReduce.Stage.REDUCE); while (true) { if (Thread.interrupted()) throw new TraversalInterruptedException(); final Map.Entry<?, Queue<?>> entry = keyValues.next(); if (null == entry) break; workerMapReduce.reduce(entry.getKey(), entry.getValue().iterator(), reduceEmitter); } workerMapReduce.workerEnd(MapReduce.Stage.REDUCE); }); reduceEmitter.complete(mapReduce); // sort results if a reduce output sort is defined mapReduce.addResultToMemory(this.memory, reduceEmitter.reduceQueue.iterator()); } else { mapReduce.addResultToMemory(this.memory, mapEmitter.mapQueue.iterator()); } } // update runtime and return the newly computed graph this.memory.setRuntime(System.currentTimeMillis() - time); this.memory.complete(); // drop all transient properties and set iteration // determine the resultant graph based on the result graph/persist state final Graph resultGraph = view.processResultGraphPersist(this.resultGraph, this.persist); TinkerHelper.dropGraphComputerView(this.graph); // drop the view from the original source graph return new DefaultComputerResult(resultGraph, this.memory.asImmutable()); } catch (InterruptedException ie) { workers.closeNow(); throw new TraversalInterruptedException(); } catch (Exception ex) { workers.closeNow(); throw new RuntimeException(ex); } finally { workers.close(); } }); this.computerService.shutdown(); return result; }
From source file:com.btoddb.fastpersitentqueue.flume.FpqChannelTest.java
@Test public void testThreading() throws Exception { final int numEntries = 1000; final int numPushers = 4; final int numPoppers = 4; final int entrySize = 1000; channel.setMaxTransactionSize(2000); final int popBatchSize = 100; channel.setMaxMemorySegmentSizeInBytes(10000000); channel.setMaxJournalFileSize(10000000); channel.setMaxJournalDurationInMs(30000); channel.setFlushPeriodInMs(1000);//w w w .j a va 2 s .c om channel.setNumberOfFlushWorkers(4); final Random pushRand = new Random(1000L); final Random popRand = new Random(1000000L); final AtomicInteger pusherFinishCount = new AtomicInteger(); final AtomicInteger numPops = new AtomicInteger(); final AtomicLong counter = new AtomicLong(); final AtomicLong pushSum = new AtomicLong(); final AtomicLong popSum = new AtomicLong(); channel.start(); ExecutorService execSrvc = Executors.newFixedThreadPool(numPushers + numPoppers); Set<Future> futures = new HashSet<Future>(); // start pushing for (int i = 0; i < numPushers; i++) { Future future = execSrvc.submit(new Runnable() { @Override public void run() { for (int i = 0; i < numEntries; i++) { try { long x = counter.getAndIncrement(); pushSum.addAndGet(x); ByteBuffer bb = ByteBuffer.wrap(new byte[entrySize]); bb.putLong(x); Transaction tx = channel.getTransaction(); tx.begin(); MyEvent event1 = new MyEvent(); event1.addHeader("x", String.valueOf(x)).setBody(new byte[numEntries - 8]); // take out size of long channel.put(event1); tx.commit(); tx.close(); Thread.sleep(pushRand.nextInt(5)); } catch (Exception e) { e.printStackTrace(); } } pusherFinishCount.incrementAndGet(); } }); futures.add(future); } // start popping for (int i = 0; i < numPoppers; i++) { Future future = execSrvc.submit(new Runnable() { @Override public void run() { while (pusherFinishCount.get() < numPushers || !channel.isEmpty()) { try { Transaction tx = channel.getTransaction(); tx.begin(); Event event; int count = popBatchSize; while (null != (event = channel.take()) && count-- > 0) { popSum.addAndGet(Long.valueOf(event.getHeaders().get("x"))); numPops.incrementAndGet(); } tx.commit(); tx.close(); Thread.sleep(popRand.nextInt(10)); } catch (Exception e) { e.printStackTrace(); } } } }); futures.add(future); } boolean finished = false; while (!finished) { try { for (Future f : futures) { f.get(); } finished = true; } catch (InterruptedException e) { // ignore Thread.interrupted(); } } assertThat(numPops.get(), is(numEntries * numPushers)); assertThat(channel.isEmpty(), is(true)); assertThat(pushSum.get(), is(popSum.get())); }
From source file:com.offbynull.voip.audio.gateways.io.AudioRunnable.java
@Override public void run() { try {//from w ww. jav a 2s.c o m while (true) { // Poll for new messages List<Object> incomingObjects = bus.pull(); Validate.notNull(incomingObjects); Validate.noNullElements(incomingObjects); for (Object incomingObj : incomingObjects) { if (incomingObj instanceof Message) { Message msg = (Message) incomingObj; Address src = msg.getSourceAddress(); Address dst = msg.getDestinationAddress(); Object payload = msg.getMessage(); LOG.debug("Processing incoming message from {} to {}: {}", src, dst, payload); if (payload instanceof LoadDevicesRequest) { Object response = loadDevices(); sendMessage(src, dst, response); } else if (payload instanceof OpenDevicesRequest) { Object response = openDevices(src, dst, (OpenDevicesRequest) payload); sendMessage(src, dst, response); } else if (payload instanceof CloseDevicesRequest) { Object response = closeDevices(); sendMessage(src, dst, response); } else if (payload instanceof OutputPcmBlock) { if (openedToAddress == null || openedFromAddress == null) { LOG.warn("Output PCM block received but devices closed"); continue; } OutputPcmBlock outputPCMBlock = (OutputPcmBlock) payload; byte[] data = outputPCMBlock.getData(); OutputData outputData = new OutputData(data); outputQueue.put(outputData); } else { LOG.error("Unrecognized message: {}", payload); } } else if (incomingObj instanceof InputData) { // message from input read thread (microphone reader thread) if (openedToAddress == null || openedFromAddress == null) { LOG.warn("Input PCM block received but devices not opened"); continue; } InputData inputData = (InputData) incomingObj; byte[] data = inputData.getData(); InputPcmBlock inputPCMBlock = new InputPcmBlock(data); sendMessage(openedFromAddress, openedToAddress, inputPCMBlock); } else if (incomingObj instanceof AddShuttle) { AddShuttle addShuttle = (AddShuttle) incomingObj; Shuttle shuttle = addShuttle.getShuttle(); Shuttle existingShuttle = outgoingShuttles.putIfAbsent(shuttle.getPrefix(), shuttle); Validate.validState(existingShuttle == null); } else if (incomingObj instanceof RemoveShuttle) { RemoveShuttle removeShuttle = (RemoveShuttle) incomingObj; String prefix = removeShuttle.getPrefix(); Shuttle oldShuttle = outgoingShuttles.remove(prefix); Validate.validState(oldShuttle != null); } else { throw new IllegalStateException("Unexpected message type: " + incomingObj); } } } } catch (InterruptedException ie) { LOG.debug("Audio gateway interrupted"); Thread.interrupted(); } catch (Exception e) { LOG.error("Internal error encountered", e); } finally { closeDevices(); bus.close(); } }
From source file:org.jboss.tools.ws.ui.utils.JAXRSTester.java
/** * Call a JAX-RS service//w w w.j a v a2 s . c o m * @param address * @param parameters * @param headers * @param methodType * @param requestBody * @param proxy * @param port * @throws Exception */ public void doTest(String address, Map<String, String> parameters, Map<String, String> headers, String methodType, String requestBody, String proxy, int port, String uid, String pwd) throws Exception { // handle the proxy Proxy proxyObject = null; if (proxy != null && proxy.length() > 0 && port > 0) { InetSocketAddress proxyAddress = new InetSocketAddress(proxy, port); proxyObject = new Proxy(Proxy.Type.HTTP, proxyAddress); } // clear the returned results resultBody = EMPTY_STRING; // get the parms string String query = buildWebQuery(parameters); // Clear the address of any leading/trailing spaces address = address.trim(); // build the complete URL URL url = null; if (query != null && query.trim().length() > 0) { // add the ? if there are parameters if (!address.endsWith("?") && !address.contains("?")) {//$NON-NLS-1$ //$NON-NLS-2$ // if we're a "GET" - add the ? by default if (methodType.equalsIgnoreCase("GET")) { //$NON-NLS-1$ address = address + "?"; //$NON-NLS-1$ // if we're a PUT or POST, check if we have parms // and add the ? if we do } else if (methodType.equalsIgnoreCase("POST")//$NON-NLS-1$ || methodType.equalsIgnoreCase("PUT") //$NON-NLS-1$ || methodType.equalsIgnoreCase("DELETE")) { //$NON-NLS-1$ if (query.trim().length() > 0) { address = address + "?"; //$NON-NLS-1$ } } } else if (address.contains("?")) { //$NON-NLS-1$ address = address + "&"; //$NON-NLS-1$ } // add parms to the url if we have some url = new URL(address + query); } else { url = new URL(address); } // make connection HttpURLConnection httpurlc = null; if (proxyObject == null) { httpurlc = (HttpURLConnection) url.openConnection(); } else { // if have proxy, pass it along httpurlc = (HttpURLConnection) url.openConnection(proxyObject); } // since we are expecting output back, set to true httpurlc.setDoOutput(true); // not sure what this does - may be used for authentication? httpurlc.setAllowUserInteraction(false); // set whether this is a GET or POST httpurlc.setRequestMethod(methodType); // if we have headers to add if (headers != null && !headers.isEmpty()) { Iterator<?> iter = headers.entrySet().iterator(); while (iter.hasNext()) { Entry<?, ?> entry = (Entry<?, ?>) iter.next(); if (entry.getKey() != null && entry.getKey() instanceof String) httpurlc.addRequestProperty((String) entry.getKey(), (String) entry.getValue()); } } // if we have basic authentication to add, add it! if (uid != null && pwd != null) { String authStr = uid + ':' + pwd; byte[] authEncByte = Base64.encodeBase64(authStr.getBytes()); String authStringEnc = new String(authEncByte); httpurlc.addRequestProperty("Authorization", "Basic " + authStringEnc); //$NON-NLS-1$//$NON-NLS-2$ } requestHeaders = httpurlc.getRequestProperties(); // Check if task has been interrupted if (Thread.interrupted()) { throw new InterruptedException(); } // CONNECT! httpurlc.connect(); // Check if task has been interrupted if (Thread.interrupted()) { throw new InterruptedException(); } // If we are doing a POST and we have some request body to pass along, do it if (requestBody != null && (methodType.equalsIgnoreCase("POST") //$NON-NLS-1$ || methodType.equalsIgnoreCase("PUT"))) { //$NON-NLS-1$ requestBody = WSTestUtils.stripNLsFromXML(requestBody); OutputStreamWriter out = new OutputStreamWriter(httpurlc.getOutputStream()); String stripped = stripCRLF(requestBody); out.write(stripped); out.close(); } // Check if task has been interrupted if (Thread.interrupted()) { throw new InterruptedException(); } // if we have headers to pass to user, copy them if (httpurlc.getHeaderFields() != null) { resultHeaders = httpurlc.getHeaderFields(); } // retrieve result and put string results into the response InputStream is = null; try { is = httpurlc.getInputStream(); // Check if task has been interrupted if (Thread.interrupted()) { throw new InterruptedException(); } BufferedReader br = new BufferedReader(new InputStreamReader(is, "UTF-8"));//$NON-NLS-1$ StringBuilder sb = new StringBuilder(); String line; while ((line = br.readLine()) != null) { sb.append(line); sb.append("\n");//$NON-NLS-1$ } br.close(); resultBody = sb.toString(); // Check if task has been interrupted if (Thread.interrupted()) { throw new InterruptedException(); } } catch (IOException ie) { try { is = httpurlc.getErrorStream(); // is possible that we're getting nothing back in the error stream if (is != null) { BufferedReader br = new BufferedReader(new InputStreamReader(is, "UTF-8"));//$NON-NLS-1$ StringBuilder sb = new StringBuilder(); String line; while ((line = br.readLine()) != null) { sb.append(line); sb.append("\n");//$NON-NLS-1$ } br.close(); resultBody = sb.toString(); } } catch (IOException ie2) { resultBody = ie2.getLocalizedMessage(); } } // as a last resort, if we still have nothing to report, // show an error message to the user if (resultBody == null || resultBody.trim().isEmpty()) { resultBody = JBossWSUIMessages.JAXRSRSTestView_Message_Unsuccessful_Test; } // disconnect explicitly (may not be necessary) httpurlc.disconnect(); }