private static void createSolrAlert(String userId, String created, String hashtag) { Date d = new Date(created); // SimpleDateFormat formatter=new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ss'Z+9HOUR'"); // SimpleDateFormat formatter=new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ss'Z-4HOUR'"); SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'"); created = formatter.format(d); SolrServer server = new HttpSolrServer("http://sandbox.hortonworks.com:8983/solr/tweets"); // create a tweet doc SolrInputDocument doc = new SolrInputDocument(); doc.addField("id", userId + ":" + hashtag + "-" + created); doc.addField("doctype_s", "alert"); doc.addField("createdAt_dt", created); doc.addField("tag_s", hashtag); try { server.add(doc); System.out.println("SolrBolt: successfully added alert to Solr server" + hashtag); } catch (Exception e) { e.printStackTrace(); // collector.fail(input); } }
public void run() { long nextStart = System.currentTimeMillis(); try { while (!dead) { long timeToSleep = nextStart - System.currentTimeMillis(); if (timeToSleep > 10) Thread.sleep(timeToSleep); nextStart += Tickable.TIME_TICK; if ((CMProps.Bools.MUDSTARTED.property()) && (!CMLib.threads().isAllSuspended())) { globalTickCount++; for (Iterator<Exit> iter = exits.iterator(); iter.hasNext(); ) { Exit exit = iter.next(); try { if (!exit.tick(globalTickCount)) exits.remove(exit); } catch (Exception e) { Log.errOut("ServiceEngine", e.toString()); } } for (Iterator<TimeClock> iter = clocks.iterator(); iter.hasNext(); ) { TimeClock clock = iter.next(); try { if (!clock.tick(globalTickCount)) clocks.remove(clock); } catch (Exception e) { Log.errOut("ServiceEngine", e.toString()); } } } } } catch (InterruptedException e) { } }
private static void printHelp(OptionParser parser) { try { parser.printHelpOn(System.out); } catch (Exception exception) { exception.printStackTrace(); } }
public void handleMessage(Message msg, int flags) { try { lock.lock(); if (activated == false) return; // return true; if (msg.getMsgType() == Behavior.MSG_TYPE_COMMAND) { Behavior.CommandMessage cmdMsg = (Behavior.CommandMessage) msg; String command = cmdMsg.getCmd(); // Remove the executor, because anything we do will // end the current execution. Engine.getExecutor().remove(this); if (Log.loggingDebug) Log.debug( "BaseBehavior.onMessage: command = " + command + "; oid = " + obj.getOid() + "; name " + obj.getName()); if (command.equals(MSG_CMD_TYPE_GOTO)) { GotoCommandMessage gotoMsg = (GotoCommandMessage) msg; Point destination = gotoMsg.getDestination(); mode = MSG_CMD_TYPE_GOTO; roamingBehavior = true; gotoSetup(destination, gotoMsg.getSpeed()); } else if (command.equals(MSG_CMD_TYPE_STOP)) { followTarget = null; pathState.clear(); obj.getWorldNode().setDir(new MVVector(0, 0, 0)); obj.updateWorldNode(); mode = MSG_CMD_TYPE_STOP; // If roamingBehavior is set, that means that we // used formerly had a roaming behavior, so send // an ArrivedEventMessage so that the other // behavior starts up again. if (roamingBehavior) { try { Engine.getAgent().sendBroadcast(new ArrivedEventMessage(obj)); } catch (Exception e) { Log.error( "BaseBehavior.onMessage: Error sending ArrivedEventMessage, error was '" + e.getMessage() + "'"); throw new RuntimeException(e); } } } else if (command.equals(BaseBehavior.MSG_CMD_TYPE_FOLLOW)) { FollowCommandMessage followMsg = (FollowCommandMessage) msg; mode = MSG_CMD_TYPE_FOLLOW; followSetup(followMsg.getTarget(), followMsg.getSpeed()); } } else if (msg.getMsgType() == WorldManagerClient.MSG_TYPE_MOB_PATH_CORRECTION) { Engine.getExecutor().remove(this); interpolatePath(); interpolatingPath = false; } // return true; } finally { lock.unlock(); } }
@Override public void run() { try { TBase<?, ?> tBase = SerializationUtils.deserialize(bytes, deserializerFactory); dispatchHandler.dispatchSendMessage(tBase); } catch (TException e) { if (logger.isWarnEnabled()) { logger.warn( "packet serialize error. SendSocketAddress:{} Cause:{}", remoteAddress, e.getMessage(), e); } if (logger.isDebugEnabled()) { logger.debug("packet dump hex:{}", PacketUtils.dumpByteArray(bytes)); } } catch (Exception e) { // there are cases where invalid headers are received if (logger.isWarnEnabled()) { logger.warn( "Unexpected error. SendSocketAddress:{} Cause:{}", remoteAddress, e.getMessage(), e); } if (logger.isDebugEnabled()) { logger.debug("packet dump hex:{}", PacketUtils.dumpByteArray(bytes)); } } }
@Override public Boolean call() throws Exception { long timeoutMillis = 5000; try { getServersFile(); getZkRunning(); while (true) { while (!restartQueue.isEmpty()) { LOG.debug("Restart queue size [" + restartQueue.size() + "]"); RestartHandler handler = restartQueue.poll(); Future<ScriptContext> runner = pool.submit(handler); ScriptContext scriptContext = runner.get(); // blocking call if (scriptContext.getExitCode() != 0) restartQueue.add(handler); } try { Thread.sleep(timeoutMillis); } catch (InterruptedException e) { } } } catch (Exception e) { e.printStackTrace(); LOG.error(e); pool.shutdown(); throw e; } }
public void run() { for (int i = 0; i < mMaxIterations; i++) try { if (Thread.interrupted()) { throw new InterruptedException(); } Integer result = (Integer) mQueue.take(); System.out.println("iteration = " + result); } catch (InterruptedException e) { System.out.println( "Thread properly interrupted by " + e.toString() + " in consumerRunnable"); // This isn't an error - it just means that // we've been interrupted by the main Thread. return; } catch (TimeoutException e) { System.out.println("Exception " + e.toString() + " occurred in consumerRunnable"); // Indicate a timeout. mConsumerCounter = TIMEOUT_OCCURRED; return; } catch (Exception e) { System.out.println("Exception " + e.toString() + " occurred in consumerRunnable"); // Indicate a failure. mConsumerCounter = FAILURE_OCCURRED; return; } }
@Override public Boolean call() throws Exception { List<Future<Boolean>> futureList = new ArrayList<>(); List<Get> getList = new ArrayList<>(Constant.BATCH_GROUP_SIZE); int count = 0; for (Get get : gets) { getList.add(get); if (count % Constant.BATCH_GROUP_SIZE == 0) { Future<Boolean> f = AsyncThreadPoolFactory.ASYNC_HBASE_THREAD_POOL.submit(new GetTask(getList, result)); futureList.add(f); getList = new ArrayList<>(Constant.BATCH_GROUP_SIZE); } count++; } Future<Boolean> f = AsyncThreadPoolFactory.ASYNC_HBASE_THREAD_POOL.submit(new GetTask(getList, result)); futureList.add(f); boolean isOk = false; for (Future<Boolean> future : futureList) { try { isOk = future.get(500, TimeUnit.MILLISECONDS); } catch (TimeoutException ignored) { } catch (Exception e) { e.printStackTrace(); } finally { if (!isOk) { future.cancel(Boolean.FALSE); } } } return isOk; }
public static void main(String[] args) { Callable<Integer> task = () -> { try { TimeUnit.SECONDS.sleep(1); return 123; } catch (InterruptedException e) { throw new IllegalStateException("task interrupted", e); } }; ExecutorService executor = Executors.newFixedThreadPool(1); Future<Integer> future = executor.submit(task); System.out.println("future done? " + future.isDone()); Integer result = -1; try { result = future.get(); } catch (Exception e) { e.printStackTrace(); } System.out.println("future done? " + future.isDone()); System.out.println("result: " + result); stop(executor); }
@Override public void run() { try { barrier1.await(); barrier2.await(); for (int i = 0; i < writerIterations; i++) { int id = idGenerator.incrementAndGet(); String sId = Integer.toString(id); Document doc = doc().add(field("_id", sId)).add(field("content", content(id))).build(); if (create) { engine.create( new Engine.Create(doc, Lucene.STANDARD_ANALYZER, "type", sId, TRANSLOG_PAYLOAD)); } else { engine.index( new Engine.Index( new Term("_id", sId), doc, Lucene.STANDARD_ANALYZER, "type", sId, TRANSLOG_PAYLOAD)); } } } catch (Exception e) { System.out.println("Writer thread failed"); e.printStackTrace(); } finally { latch.countDown(); } }
@Test(timeout = 2000) public void testFirehoseFailsAsExpected() { AtomicInteger c = new AtomicInteger(); TestSubscriber<Integer> ts = new TestSubscriber<>(); firehose(c) .observeOn(Schedulers.computation()) .map( v -> { try { Thread.sleep(10); } catch (Exception e) { e.printStackTrace(); } return v; }) .subscribe(ts); ts.awaitTerminalEvent(); System.out.println( "testFirehoseFailsAsExpected => Received: " + ts.valueCount() + " Emitted: " + c.get()); // FIXME it is possible slow is not slow enough or the main gets delayed and thus more than one // source value is emitted. int vc = ts.valueCount(); assertTrue("10 < " + vc, vc <= 10); ts.assertError(MissingBackpressureException.class); }
/** * 延时方法 * * @param ms 毫秒 */ private static final void delay(int ms) { try { Thread.sleep(ms); } catch (Exception e) { e.printStackTrace(); } }
public ServerManager(WmsMaster wmsMaster) throws Exception { try { this.wmsMaster = wmsMaster; this.conf = wmsMaster.getConfiguration(); this.zkc = wmsMaster.getZkClient(); this.ia = wmsMaster.getInetAddress(); this.startupTimestamp = wmsMaster.getStartTime(); this.metrics = wmsMaster.getMetrics(); maxRestartAttempts = conf.getInt( Constants.WMS_MASTER_SERVER_RESTART_HANDLER_ATTEMPTS, Constants.DEFAULT_WMS_MASTER_SERVER_RESTART_HANDLER_ATTEMPTS); retryIntervalMillis = conf.getInt( Constants.WMS_MASTER_SERVER_RESTART_HANDLER_RETRY_INTERVAL_MILLIS, Constants.DEFAULT_WMS_MASTER_SERVER_RESTART_HANDLER_RETRY_INTERVAL_MILLIS); retryCounterFactory = new RetryCounterFactory(maxRestartAttempts, retryIntervalMillis); parentZnode = conf.get(Constants.ZOOKEEPER_ZNODE_PARENT, Constants.DEFAULT_ZOOKEEPER_ZNODE_PARENT); pool = Executors.newSingleThreadExecutor(); } catch (Exception e) { e.printStackTrace(); LOG.error(e); throw e; } }
public void run() throws Exception { Thread[] threads = new Thread[THREADS]; for (int i = 0; i < THREADS; i++) { try { threads[i] = new Thread(peerFactory.newClient(this), "Client " + i); } catch (Exception e) { e.printStackTrace(); return; } threads[i].start(); } try { for (int i = 0; i < THREADS; i++) { threads[i].join(); } } catch (InterruptedException e) { setFailed(); e.printStackTrace(); } if (failed) { throw new Exception("*** Test '" + peerFactory.getName() + "' failed ***"); } else { System.out.println("Test '" + peerFactory.getName() + "' completed successfully"); } }
public void add_new_process(String[] command) { try { // System.err.println("Adding command to queue"); this.queue.put(command); } catch (Exception e) { e.getMessage(); } }
/** * 以阻塞的方式立即下载一个文件,该方法会覆盖已经存在的文件 * * @param task * @return "ok" if download success (else return errmessage); */ static String download(DownloadTask task) { if (!openedStatus && show) openStatus(); URL url; HttpURLConnection conn; try { url = new URL(task.getOrigin()); conn = (HttpURLConnection) url.openConnection(); conn.setConnectTimeout(30000); conn.setReadTimeout(30000); conn.setRequestProperty( "User-Agent", "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/43.0.2357.134 Safari/" + Math.random()); if ("www.imgjav.com".equals(url.getHost())) { // 该网站需要带cookie请求 conn.setRequestProperty( "User-Agent", "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/43.0.2357.134 Safari/537.36"); // conn.setRequestProperty("Cookie", "__cfduid=d219ea333c7a9b5743b572697b631925a1446093229; // cf_clearance=6ae62d843f5d09acf393f9e4eb130d9366840c82-1446093303-28800"); conn.setRequestProperty( "Cookie", "__cfduid=d6ee846b378bb7d5d173a05541f8a2b6a1446090548; cf_clearance=ea10e8db31f8b6ee51570b118dd89b7e616d7b62-1446099714-28800"); conn.setRequestProperty("Host", "www.imgjav.com"); } Path directory = Paths.get(task.getDest()).getParent(); if (!Files.exists(directory)) Files.createDirectories(directory); } catch (Exception e) { e.printStackTrace(); return e.getMessage(); } try (InputStream is = conn.getInputStream(); BufferedInputStream in = new BufferedInputStream(is); FileOutputStream fos = new FileOutputStream(task.getDest()); OutputStream out = new BufferedOutputStream(fos); ) { int length = conn.getContentLength(); if (length < 1) throw new IOException("length<1"); byte[] binary = new byte[length]; byte[] buff = new byte[65536]; int len; int index = 0; while ((len = in.read(buff)) != -1) { System.arraycopy(buff, 0, binary, index, len); index += len; allLen += len; // allLen有线程安全的问题 ,可能会不正确。无需精确数据,所以不同步了 task.setReceivePercent(String.format("%.2f", ((float) index / length) * 100) + "%"); } out.write(binary); } catch (IOException e) { e.printStackTrace(); return e.getMessage(); } return "ok"; }
public static void main(String[] args) { Date start = new Date(); if (args.length < 3) { System.out.println("Wrong number of arguments:\n" + USAGE); return; } // get # threads int tc = Integer.parseInt(args[0]); String outfile = args[1]; // make a threadsafe queue of all files to process ConcurrentLinkedQueue<String> files = new ConcurrentLinkedQueue<String>(); for (int i = 2; i < args.length; i++) { files.add(args[i]); } // hastable for results Hashtable<String, Integer> results = new Hashtable<String, Integer>(HASH_SIZE, LF); // spin up the threads Thread[] workers = new Thread[tc]; for (int i = 0; i < tc; i++) { workers[i] = new Worker(files, results); workers[i].start(); } // wait for them to finish try { for (int i = 0; i < tc; i++) { workers[i].join(); } } catch (Exception e) { System.out.println("Caught Exception: " + e.getMessage()); } // terminal output Date end = new Date(); System.out.println(end.getTime() - start.getTime() + " total milliseconds"); System.out.println(results.size() + " unique words"); // sort results for easy comparison/verification List<Map.Entry<String, Integer>> sorted_results = new ArrayList<Map.Entry<String, Integer>>(results.entrySet()); Collections.sort(sorted_results, new KeyComp()); // file output try { PrintStream out = new PrintStream(outfile); for (int i = 0; i < sorted_results.size(); i++) { out.println(sorted_results.get(i).getKey() + "\t" + sorted_results.get(i).getValue()); } } catch (Exception e) { System.out.println("Caught Exception: " + e.getMessage()); } }
public boolean connect() { try { channel = nettyClient.connect(ip, new Integer(port)); refreshHeartBeat(); reconnectTimes = 0; return true; } catch (Exception e) { logger.error("连接异常:" + e.getMessage()); return false; } }
public static final void main(String[] args) throws Exception { String uri = args[0]; int reqNum = Integer.parseInt(args[1]); int reqTerm = Integer.parseInt(args[2]); try { // new ClientSimple().request1(uri, reqNum, reqTerm); new ClientSimple().request2(uri, reqNum, reqTerm); } catch (Exception e) { System.out.println("ERROR:" + e.getMessage()); } }
void solve() { Thread tcThread = new Thread(tc); // System.out.println("starting client thread"); tcThread.start(); try { tcThread.join(); } catch (Exception e) { e.printStackTrace(); } // System.out.println("joined client thread"); result = SudokuSolver.solve(tc.testcase); success = SudokuSolver.isLegalSolution(result, tc.testcase); }
@Override public void run() { try { LongValue value = new LongValueNative(); barrier.await(); for (int i = 0; i < iterations; i++) { map.acquireUsing(key, value); value.addAtomicValue(1); } } catch (Exception e) { e.printStackTrace(); } }
/** Registers a SelectableQueue */ public boolean register(SelectableQueue sq, Communicator communicator) { try { // Register the new pipe with the queue. It will write a byte to this // pipe when the queue is hot, and it will offer its communicator to our // queue. sq.register(this, communicator); return true; } catch (Exception e) { e.printStackTrace(); return false; } }
public void run() { // each file is processed into a local hash table and then merged with the global results // this will cause much less contention on the global table, but still avoids a sequential // update Hashtable<String, Integer> local_results = new Hashtable<String, Integer>(WordCountJ.HASH_SIZE, WordCountJ.LF); // grab a file to work on String cf; while ((cf = files.poll()) != null) { try { BufferedReader input = new BufferedReader(new FileReader(cf)); String text; // well go line-by-line... maybe this is not the fastest while ((text = input.readLine()) != null) { // parse words Matcher matcher = pattern.matcher(text); while (matcher.find()) { String word = matcher.group(1); if (local_results.containsKey(word)) { local_results.put(word, 1 + local_results.get(word)); } else { local_results.put(word, 1); } } } input.close(); } catch (Exception e) { System.out.println(" caught a " + e.getClass() + "\n with message: " + e.getMessage()); return; } // merge local hashmap with shared one,could have a // seperate thread do this but that might be cheating Iterator<Map.Entry<String, Integer>> updates = local_results.entrySet().iterator(); while (updates.hasNext()) { Map.Entry<String, Integer> kv = updates.next(); String k = kv.getKey(); Integer v = kv.getValue(); synchronized (results) { if (results.containsKey(k)) { results.put(k, v + results.get(k)); } else { results.put(k, v); } } } local_results.clear(); } }
protected DBObject prepareObject(StreamsDatum streamsDatum) { DBObject dbObject = null; if (streamsDatum.getDocument() instanceof String) { dbObject = (DBObject) JSON.parse((String) streamsDatum.getDocument()); } else { try { ObjectNode node = mapper.valueToTree(streamsDatum.getDocument()); dbObject = (DBObject) JSON.parse(node.toString()); } catch (Exception e) { e.printStackTrace(); LOGGER.error("Unsupported type: " + streamsDatum.getDocument().getClass(), e); } } return dbObject; }
public static void main(String[] args) throws Exception { int counter = 0; while (true) { Thread outThread = null; Thread errThread = null; try { // org.pitest.mutationtest.instrument.MutationTestUnit#runTestInSeperateProcessForMutationRange // *** start slave ServerSocket commSocket = new ServerSocket(0); int commPort = commSocket.getLocalPort(); System.out.println("commPort = " + commPort); // org.pitest.mutationtest.execute.MutationTestProcess#start // - org.pitest.util.CommunicationThread#start FutureTask<Integer> commFuture = createFuture(commSocket); // - org.pitest.util.WrappingProcess#start // - org.pitest.util.JavaProcess#launch Process slaveProcess = startSlaveProcess(commPort); outThread = new Thread(new ReadFromInputStream(slaveProcess.getInputStream()), "stdout"); errThread = new Thread(new ReadFromInputStream(slaveProcess.getErrorStream()), "stderr"); outThread.start(); errThread.start(); // *** wait for slave to die // org.pitest.mutationtest.execute.MutationTestProcess#waitToDie // - org.pitest.util.CommunicationThread#waitToFinish System.out.println("waitToFinish"); Integer controlReturned = commFuture.get(); System.out.println("controlReturned = " + controlReturned); // NOTE: the following won't get called if commFuture.get() fails! // - org.pitest.util.JavaProcess#destroy outThread.interrupt(); // org.pitest.util.AbstractMonitor#requestStop errThread.interrupt(); // org.pitest.util.AbstractMonitor#requestStop slaveProcess.destroy(); } catch (Exception e) { e.printStackTrace(System.out); } // test: the threads should exit eventually outThread.join(); errThread.join(); counter++; System.out.println("try " + counter + ": stdout and stderr threads exited normally"); } }
private static void build(Iterable<String> in) throws IOException, InterruptedException { final List<String> args = newArrayList(in); Collections.sort(args); final int threads = Runtime.getRuntime().availableProcessors(); final ExecutorService ex = Executors.newFixedThreadPool(threads); final String base = "base"; new WithVm("base").createIfNotPresent(); for (String pkg : args) ex.submit( () -> { final WithVm newVm = new WithVm("fbuild-" + pkg, TimeUnit.MINUTES.toMillis(30)); final File rbuild = new File("wip-" + pkg + ".rbuild"); try { newVm.cloneFrom(base); newVm.start(); newVm.inTee(rbuild, "apt-get", "-oAPT::Get::Only-Source=true", "source", pkg); newVm.inTee(rbuild, "apt-get", "build-dep", "-y", "--force-yes", pkg); newVm.inTee(rbuild, "ifdown", "eth0"); final boolean success = 0 == newVm.inTee( rbuild, "sh", "-c", "cd " + pkg + "-* && dpkg-buildpackage -us -uc"); newVm.stopNow(); if (success) { rbuild.renameTo(new File("success-" + pkg + ".rbuild")); newVm.destroy(); System.out.println("success: " + pkg); } else { rbuild.renameTo(new File("failure-" + pkg + ".rbuild")); System.out.println("failure: " + pkg); } } catch (Exception e) { rbuild.renameTo(new File("error-" + pkg + ".rbuild")); System.err.println("build error: " + pkg); e.printStackTrace(); newVm.stopNow(); } return null; }); ex.shutdown(); ex.awaitTermination(Long.MAX_VALUE, TimeUnit.MILLISECONDS); }
private void runHbaseUpsert(String upsert) { try { conn.createStatement().executeUpdate(upsert); conn.commit(); } catch (Exception e) { e.printStackTrace(); throw new RuntimeException(e); } finally { try { if (conn != null) conn.close(); } catch (Exception e) { throw new RuntimeException(e); } } }
private void handleException(Exception e) { if (INTERRUPTED_EXCEPTION_TYPES.contains(e.getClass())) { LOG.info("Changes feed was interrupted"); } else { LOG.error("Caught exception while listening to changes feed:", e); } }
public void run() { try { WebSocketClient client = new WebSocketClient(cli, WebSocketServer.this.listenerClass.newInstance()); client.setExecutor(socketServer.executor); client.setScheduler(socketServer.scheduler); client.service(); } catch (Exception e) { logger.error(e.getMessage(), e); } finally { try { cli.close(); } catch (IOException io) { logger.error(io.getMessage(), io); } } }
protected void doException(final Exception e) throws Exception { if (parent.launchLocation != null) { final ArrayList<StackTraceElement> stack = new ArrayList<StackTraceElement>(Arrays.asList(e.getStackTrace())); stack.addAll(Arrays.asList(parent.launchLocation)); e.setStackTrace(stack.toArray(new StackTraceElement[stack.size()])); } postToUiThreadAndWait( new Callable<Object>() { public Object call() throws Exception { if (e instanceof InterruptedException || e instanceof InterruptedIOException) parent.onInterrupted(e); else parent.onException(e); return null; } }); }