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; } }
private static void printHelp(OptionParser parser) { try { parser.printHelpOn(System.out); } catch (Exception exception) { exception.printStackTrace(); } }
@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; } }
@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); }
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); }
/** * 延时方法 * * @param ms 毫秒 */ private static final void delay(int ms) { try { Thread.sleep(ms); } catch (Exception e) { e.printStackTrace(); } }
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() 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"); } }
@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; }
@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(); } }
/** * 以阻塞的方式立即下载一个文件,该方法会覆盖已经存在的文件 * * @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 void process(WatchedEvent event) { if (event.getType() == Event.EventType.NodeChildrenChanged) { LOG.debug("Running children changed [" + event.getPath() + "]"); try { getZkRunning(); } catch (Exception e) { e.printStackTrace(); LOG.error(e); } } else if (event.getType() == Event.EventType.NodeDeleted) { String znodePath = event.getPath(); LOG.debug("Running znode deleted [" + znodePath + "]"); try { restartServer(znodePath); } catch (Exception e) { e.printStackTrace(); LOG.error(e); } } }
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); }
/** 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; } }
@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(); } }
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; }
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); }
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 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); } } }
@Override public void run() { while (true) { if (persistQueue.peek() != null) { try { StreamsDatum entry = persistQueue.remove(); write(entry); } catch (Exception e) { e.printStackTrace(); } } try { Thread.sleep(new Random().nextInt(1)); } catch (InterruptedException e) { } } }
@Override public Void call() throws Exception { String name = Integer.toString(threadId); for (int i = 0; i < count; i++) { final ODatabaseDocumentTx database = poolFactory.get(databaseUrl, "admin", "admin").acquire(); try { if ((i + 1) % 100 == 0) System.out.println( "\nWriter " + threadId + "(" + database.getURL() + ") managed " + (i + 1) + "/" + count + " records so far"); final ODocument person = createRecord(database, i); updateRecord(database, i); checkRecord(database, i); checkIndex(database, (String) person.field("name"), person.getIdentity()); if (delayWriter > 0) Thread.sleep(delayWriter); } catch (InterruptedException e) { System.out.println("Writer received interrupt (db=" + database.getURL()); Thread.currentThread().interrupt(); break; } catch (Exception e) { System.out.println("Writer received exception (db=" + database.getURL()); e.printStackTrace(); break; } finally { database.close(); runningWriters.countDown(); } } System.out.println("\nWriter " + name + " END"); return null; }
/* * public synchronized ArrayList<Request> getWorkloadsList() { * ArrayList<Request> workloads = new ArrayList<Request>(); * * LOG.debug("Reading " + parentZnode + * Constants.DEFAULT_ZOOKEEPER_ZNODE_WORKLOADS); * * try { List<String> children = getChildren(parentZnode + * Constants.DEFAULT_ZOOKEEPER_ZNODE_WORKLOADS, null); * * if (!children.isEmpty()) { for (String child : children) { Request * request = new Request(); String workloadZnode = parentZnode + * Constants.DEFAULT_ZOOKEEPER_ZNODE_WORKLOADS + "/" + child; Stat stat = * zkc.exists(workloadZnode, false); if (stat != null) { byte[] bytes = * zkc.getData(workloadZnode, false, stat); try { * deserializer.deserialize(request, bytes); workloads.add(request); } catch * (TException e) { e.printStackTrace(); } } } } } catch (Exception e) { * e.printStackTrace(); } * * return workloads; } */ public synchronized ArrayList<String> getClientsList() { ArrayList<String> clients = new ArrayList<String>(); LOG.debug("Reading " + parentZnode + Constants.DEFAULT_ZOOKEEPER_ZNODE_CLIENTS); try { List<String> children = getChildren(parentZnode + Constants.DEFAULT_ZOOKEEPER_ZNODE_CLIENTS, null); if (!children.isEmpty()) { for (String child : children) { clients.add(child); } } } catch (Exception e) { e.printStackTrace(); } return clients; }
@Override public void run() { while (provider.isRunning()) { ResponseList<Post> postResponseList; try { postResponseList = client.getFeed(id); Set<Post> update = Sets.newHashSet(postResponseList); Set<Post> repeats = Sets.intersection(priorPollResult, Sets.newHashSet(update)); Set<Post> entrySet = Sets.difference(update, repeats); LOGGER.debug( this.id + " response: " + update.size() + " previous: " + repeats.size() + " new: " + entrySet.size()); for (Post item : entrySet) { String json = DataObjectFactory.getRawJSON(item); org.apache.streams.facebook.Post post = mapper.readValue(json, org.apache.streams.facebook.Post.class); try { lock.readLock().lock(); ComponentUtils.offerUntilSuccess(new StreamsDatum(post), providerQueue); countersCurrent.incrementAttempt(); } finally { lock.readLock().unlock(); } } priorPollResult = update; } catch (Exception e) { e.printStackTrace(); } finally { try { Thread.sleep(configuration.getPollIntervalMillis()); } catch (InterruptedException e) { Thread.currentThread().interrupt(); } } } }
public void run() { try { URL url = new URL(protocol + "://localhost:" + port + "/test1/" + f); HttpURLConnection urlc = (HttpURLConnection) url.openConnection(); if (urlc instanceof HttpsURLConnection) { HttpsURLConnection urlcs = (HttpsURLConnection) urlc; urlcs.setHostnameVerifier( new HostnameVerifier() { public boolean verify(String s, SSLSession s1) { return true; } }); urlcs.setSSLSocketFactory(ctx.getSocketFactory()); } byte[] buf = new byte[4096]; if (fixedLen) { urlc.setRequestProperty("XFixed", "yes"); } InputStream is = urlc.getInputStream(); File temp = File.createTempFile("Test1", null); temp.deleteOnExit(); OutputStream fout = new BufferedOutputStream(new FileOutputStream(temp)); int c, count = 0; while ((c = is.read(buf)) != -1) { count += c; fout.write(buf, 0, c); } is.close(); fout.close(); if (count != size) { throw new RuntimeException("wrong amount of data returned"); } String orig = root + "/" + f; compare(new File(orig), temp); temp.delete(); } catch (Exception e) { e.printStackTrace(); fail = true; } }
@Override public void run() { try { barrier1.await(); barrier2.await(); for (int i = 0; i < searcherIterations; i++) { Engine.Searcher searcher = engine.searcher(); TopDocs topDocs = searcher.searcher().search(new TermQuery(new Term("content", content(i))), 10); // read one searcher.searcher().doc(topDocs.scoreDocs[0].doc, new LoadFirstFieldSelector()); searcher.release(); } } catch (Exception e) { System.out.println("Searcher thread failed"); e.printStackTrace(); } finally { latch.countDown(); } }
@EventHandler public void onTick(TickEvent event) { if (!bot.hasSpawned() || !bot.isConnected()) return; if (tickDelay > 0) { tickDelay--; return; } MainPlayerEntity player = bot.getPlayer(); if (player == null || !bot.hasSpawned() || spamMessage == null) return; if (nextMessage > 0) { nextMessage--; return; } try { String message = spamMessage; MessageFormatter formatter = new MessageFormatter(); synchronized (bots) { if (bots.size() > 0) { DarkBotMCSpambot bot = bots.get(++nextBot >= bots.size() ? nextBot = 0 : nextBot); if (bot != null && bot.bot != null && bot.bot.getSession() != null) formatter.setVariable("bot", bot.bot.getSession().getUsername()); } } if (spamList.length > 0) { formatter.setVariable( "spamlist", spamList[++nextSpamList >= spamList.length ? nextSpamList = 0 : nextSpamList]); } formatter.setVariable("rnd", Util.generateRandomString(15 + random.nextInt(6))); formatter.setVariable( "msg", Character.toString( msgChars[++nextMsgChar >= msgChars.length ? nextMsgChar = 0 : nextMsgChar])); message = formatter.format(message); bot.say(message); } catch (Exception e) { e.printStackTrace(); } nextMessage = messageDelay; }
// query HBase table for threshold for the stock symbol that was tweeted about private int findThresholdForStock(String hashtag) { int threshold = DEFAULT_ALERT_THRESHOLD; try { conn = phoenixDriver.connect( "jdbc:phoenix:sandbox.hortonworks.com:2181:/hbase-unsecure", new Properties()); ResultSet rst = conn.createStatement() .executeQuery( "select tweet_threshold from securities where lower(symbol)='" + hashtag + "'"); while (rst.next()) { threshold = rst.getInt(1); System.out.println("Found threshold in Hbase: " + threshold + " for: " + hashtag); } if (threshold == 0) { System.out.println( "Unable to find threshold in HBase for: " + hashtag + ". Setting to default: " + DEFAULT_ALERT_THRESHOLD); threshold = DEFAULT_ALERT_THRESHOLD; } } catch (Exception e) { e.printStackTrace(); throw new RuntimeException(e); } finally { try { if (conn != null) conn.close(); } catch (Exception e) { throw new RuntimeException(e); } } return threshold; }
public void run() { try { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); PrintWriter pw = new PrintWriter(socket.getOutputStream(), true); String msg; while (true) { msg = br.readLine(); pw.println(msg); if (msg.trim().equals("-quit")) { pw.close(); br.close(); exec.shutdownNow(); break; } } } catch (Exception e) { e.printStackTrace(); } }
public final void run() { while (true) { TestParameters params = cipherTest.getTest(); if (params == null) { // no more tests break; } if (params.isEnabled() == false) { System.out.println("Skipping disabled test " + params); continue; } try { runTest(params); System.out.println("Passed " + params); } catch (Exception e) { cipherTest.setFailed(); System.out.println("** Failed " + params + "**"); e.printStackTrace(); } } }
public void run() { System.out.println("starting " + threadname + " run method on port " + SudokuServer.PORT); System.out.flush(); try { Socket sock = new Socket(hostname, SudokuServer.PORT); PrintWriter dos = new PrintWriter(sock.getOutputStream()); BufferedReader dis = new BufferedReader(new InputStreamReader(sock.getInputStream())); dos.println(testcase); dos.flush(); String response = dis.readLine(); System.out.println( "Client " + threadname + " sent: " + testcase + " received response:" + response); dos.close(); dis.close(); synchronized (sc) { sc.result = response; } } catch (Exception e) { e.printStackTrace(); } System.out.println("finishing " + threadname + " run method"); }