List of usage examples for java.util TimerTask TimerTask
protected TimerTask()
From source file:de.dal33t.powerfolder.Controller.java
/** * Sets up the task, which should be executes periodically. *///from ww w. j a v a 2s . c o m private void setupPeriodicalTasks() { // ============ // Test the connectivity after a while. // ============ LimitedConnectivityChecker.install(this); // ============ // Schedule a task to do housekeeping every day, just after midnight. // ============ Calendar cal = new GregorianCalendar(); long now = cal.getTime().getTime(); // Midnight tomorrow morning. cal.set(Calendar.MILLISECOND, 0); cal.set(Calendar.SECOND, 0); cal.set(Calendar.MINUTE, 0); cal.set(Calendar.HOUR_OF_DAY, 0); cal.add(Calendar.DATE, 1); // Add a few seconds to be sure the file name definately is for // tomorrow. cal.add(Calendar.SECOND, 2); long midnight = cal.getTime().getTime(); // How long to wait initially? long secondsToMidnight = (midnight - now) / 1000; logFine("Initial log reconfigure in " + secondsToMidnight + " seconds"); threadPool.scheduleAtFixedRate(new TimerTask() { @Override public void run() { performHousekeeping(true); } }, secondsToMidnight, 60 * 60 * 24, TimeUnit.SECONDS); // Also run housekeeping one minute after start up. threadPool.schedule(new TimerTask() { @Override public void run() { performHousekeeping(false); } }, 1, TimeUnit.MINUTES); // ============ // Do profiling // ============ if (Profiling.ENABLED) { threadPool.scheduleWithFixedDelay(new TimerTask() { @Override public void run() { logFine(Profiling.dumpStats()); } }, 0, 1, TimeUnit.MINUTES); } // ============ // Monitor the default directory for possible new folders. // ============ threadPool.scheduleAtFixedRate(new TimerTask() { @Override public void run() { if (ConfigurationEntry.LOOK_FOR_FOLDER_CANDIDATES.getValueBoolean(Controller.this)) { folderRepository.lookForNewFolders(); } if (ConfigurationEntry.LOOK_FOR_FOLDERS_TO_BE_REMOVED.getValueBoolean(Controller.this)) { folderRepository.lookForFoldersToBeRemoved(); } } }, 10L, 10L, TimeUnit.SECONDS); // ============ // Hourly tasks // ============ // @todo what's this for? comment? boolean alreadyDetected = ConfigurationEntry.TRANSFER_LIMIT_AUTODETECT.getValueBoolean(this) && ConfigurationEntry.UPLOAD_LIMIT_WAN.getValueInt(this) > 0; // If already detected wait 10 mins before next test. Otherwise start // instantly. long initialDelay = alreadyDetected ? 600 : 5; threadPool.scheduleAtFixedRate(new TimerTask() { @Override public void run() { performHourly(); } }, initialDelay, 3600, TimeUnit.SECONDS); // ========= // Profiling // ========= // final Collector cpu = CollectorFactory.getFactory().createCollector( // CollectorID.CPU_USAGE.id); threadPool.scheduleAtFixedRate(new Runnable() { @Override public void run() { if (!verbose) { return; } // if (cpu == null || cpu.getMaxValue() == 0) { // return; // } // double cpuUsage = cpu.getValue() * 100 / cpu.getMaxValue(); // logWarning("" + cpuUsage + "% " + cpu.getValue() + " / " + // cpu.getMaxValue()); // if (cpuUsage > 1) { if (isFine()) { logFine("Dataitems: " + Debug.countDataitems(Controller.this)); } String dump = Debug.dumpCurrentStacktraces(false); if (StringUtils.isNotBlank(dump) && isFine() && ConfigurationEntry.LOG_ACTIVE_THREADS.getValueBoolean(getController())) { logFine("Active threads:\n\n" + dump); } else { logFine("No active threads"); } // } } }, 1, 5, TimeUnit.MINUTES); }
From source file:com.thebuzzmedia.exiftool.ExifToolNew3.java
/** * Will attempt 3 times to use the running exif process, and if unable to complete successfully will throw * IOException/*from w w w . j a v a 2 s .co m*/ */ private Map<String, String> processStayOpen(List<String> args) throws IOException { int attempts = 0; while (attempts < 3 && !shuttingDown.get()) { attempts++; // make sure process is started ensureProcessRunning(); TimerTask attemptTimer = null; try { if (timeoutWhenKeepAlive > 0) { attemptTimer = new TimerTask() { @Override public void run() { LOG.warn("Process ran too long closing, max " + timeoutWhenKeepAlive + " mills"); process.close(); } }; cleanupTimer.schedule(attemptTimer, timeoutWhenKeepAlive); } LOG.debug("Streaming arguments to ExifToolNew3 process..."); return ExifToolService.toMap(process.sendArgs(args)); } catch (IOException ex) { if (STREAM_CLOSED_MESSAGE.equals(ex.getMessage()) && !shuttingDown.get()) { // only catch "Stream Closed" error (happens when process // has died) LOG.warn(String.format("Caught IOException(\"%s\"), will restart daemon", STREAM_CLOSED_MESSAGE)); process.close(); } else { throw ex; } } finally { if (attemptTimer != null) attemptTimer.cancel(); } } if (shuttingDown.get()) { throw new IOException("Shutting Down"); } throw new IOException("Ran out of attempts"); }
From source file:com.easemob.chatui.adapter.MessageAdapter.java
/** * ?/* w w w. j av a 2s.c o m*/ * * @param message * @param holder * @param position * @param convertView */ private void handleImageMessage(final EMMessage message, final ViewHolder holder, final int position, View convertView) { holder.pb.setTag(position); holder.iv.setOnLongClickListener(new OnLongClickListener() { @Override public boolean onLongClick(View v) { activity.startActivityForResult((new Intent(activity, ContextMenu.class)) .putExtra("position", position).putExtra("type", EMMessage.Type.IMAGE.ordinal()), ChatActivity.REQUEST_CODE_CONTEXT_MENU); return true; } }); // ?? if (message.direct == Direct.RECEIVE) { // "it is receive msg"; if (message.status == EMMessage.Status.INPROGRESS) { // "!!!! back receive"; holder.iv.setImageResource(R.drawable.default_image); showDownloadImageProgress(message, holder); // downloadImage(message, holder); } else { // "!!!! not back receive, show image directly"); holder.pb.setVisibility(View.GONE); holder.tv.setVisibility(View.GONE); holder.iv.setImageResource(R.drawable.default_image); ImageMessageBody imgBody = (ImageMessageBody) message.getBody(); if (imgBody.getLocalUrl() != null) { // String filePath = imgBody.getLocalUrl(); String remotePath = imgBody.getRemoteUrl(); String filePath = ImageUtils.getImagePath(remotePath); String thumbRemoteUrl = imgBody.getThumbnailUrl(); if (TextUtils.isEmpty(thumbRemoteUrl) && !TextUtils.isEmpty(remotePath)) { thumbRemoteUrl = remotePath; } String thumbnailPath = ImageUtils.getThumbnailImagePath(thumbRemoteUrl); showImageView(thumbnailPath, holder.iv, filePath, imgBody.getRemoteUrl(), message); } } return; } // ??? // process send message // send pic, show the pic directly ImageMessageBody imgBody = (ImageMessageBody) message.getBody(); String filePath = imgBody.getLocalUrl(); if (filePath != null && new File(filePath).exists()) { showImageView(ImageUtils.getThumbnailImagePath(filePath), holder.iv, filePath, null, message); } else { showImageView(ImageUtils.getThumbnailImagePath(filePath), holder.iv, filePath, IMAGE_DIR, message); } switch (message.status) { case SUCCESS: holder.pb.setVisibility(View.GONE); holder.tv.setVisibility(View.GONE); holder.staus_iv.setVisibility(View.GONE); break; case FAIL: holder.pb.setVisibility(View.GONE); holder.tv.setVisibility(View.GONE); holder.staus_iv.setVisibility(View.VISIBLE); break; case INPROGRESS: holder.staus_iv.setVisibility(View.GONE); holder.pb.setVisibility(View.VISIBLE); holder.tv.setVisibility(View.VISIBLE); if (timers.containsKey(message.getMsgId())) return; // set a timer final Timer timer = new Timer(); timers.put(message.getMsgId(), timer); timer.schedule(new TimerTask() { @Override public void run() { activity.runOnUiThread(new Runnable() { public void run() { holder.pb.setVisibility(View.VISIBLE); holder.tv.setVisibility(View.VISIBLE); holder.tv.setText(message.progress + "%"); if (message.status == EMMessage.Status.SUCCESS) { holder.pb.setVisibility(View.GONE); holder.tv.setVisibility(View.GONE); // message.setSendingStatus(Message.SENDING_STATUS_SUCCESS); timer.cancel(); } else if (message.status == EMMessage.Status.FAIL) { holder.pb.setVisibility(View.GONE); holder.tv.setVisibility(View.GONE); // message.setSendingStatus(Message.SENDING_STATUS_FAIL); // message.setProgress(0); holder.staus_iv.setVisibility(View.VISIBLE); Toast.makeText(activity, activity.getString(R.string.send_fail) + activity.getString(R.string.connect_failuer_toast), 0).show(); timer.cancel(); } } }); } }, 0, 500); break; default: sendPictureMessage(message, holder); } }
From source file:cn.ucai.superwechat.adapter.MessageAdapter.java
/** * ?/*from w ww. j av a 2 s .c o m*/ * * @param message * @param holder * @param position * @param convertView */ private void handleImageMessage(final EMMessage message, final ViewHolder holder, final int position, View convertView) { holder.pb.setTag(position); holder.iv.setOnLongClickListener(new OnLongClickListener() { @Override public boolean onLongClick(View v) { activity.startActivityForResult((new Intent(activity, ContextMenu.class)) .putExtra("position", position).putExtra("type", EMMessage.Type.IMAGE.ordinal()), ChatActivity.REQUEST_CODE_CONTEXT_MENU); return true; } }); // ?? if (message.direct == EMMessage.Direct.RECEIVE) { // "it is receive msg"; if (message.status == EMMessage.Status.INPROGRESS) { // "!!!! back receive"; holder.iv.setImageResource(cn.ucai.superwechat.R.drawable.default_image); showDownloadImageProgress(message, holder); // downloadImage(message, holder); } else { // "!!!! not back receive, show image directly"); holder.pb.setVisibility(View.GONE); holder.tv.setVisibility(View.GONE); holder.iv.setImageResource(cn.ucai.superwechat.R.drawable.default_image); ImageMessageBody imgBody = (ImageMessageBody) message.getBody(); if (imgBody.getLocalUrl() != null) { // String filePath = imgBody.getLocalUrl(); String remotePath = imgBody.getRemoteUrl(); String filePath = ImageUtils.getImagePath(remotePath); String thumbRemoteUrl = imgBody.getThumbnailUrl(); if (TextUtils.isEmpty(thumbRemoteUrl) && !TextUtils.isEmpty(remotePath)) { thumbRemoteUrl = remotePath; } String thumbnailPath = ImageUtils.getThumbnailImagePath(thumbRemoteUrl); showImageView(thumbnailPath, holder.iv, filePath, imgBody.getRemoteUrl(), message); } } return; } // ??? // process send message // send pic, show the pic directly ImageMessageBody imgBody = (ImageMessageBody) message.getBody(); String filePath = imgBody.getLocalUrl(); if (filePath != null && new File(filePath).exists()) { showImageView(ImageUtils.getThumbnailImagePath(filePath), holder.iv, filePath, null, message); } else { showImageView(ImageUtils.getThumbnailImagePath(filePath), holder.iv, filePath, IMAGE_DIR, message); } switch (message.status) { case SUCCESS: holder.pb.setVisibility(View.GONE); holder.tv.setVisibility(View.GONE); holder.staus_iv.setVisibility(View.GONE); break; case FAIL: holder.pb.setVisibility(View.GONE); holder.tv.setVisibility(View.GONE); holder.staus_iv.setVisibility(View.VISIBLE); break; case INPROGRESS: holder.staus_iv.setVisibility(View.GONE); holder.pb.setVisibility(View.VISIBLE); holder.tv.setVisibility(View.VISIBLE); if (timers.containsKey(message.getMsgId())) return; // set a timer final Timer timer = new Timer(); timers.put(message.getMsgId(), timer); timer.schedule(new TimerTask() { @Override public void run() { activity.runOnUiThread(new Runnable() { public void run() { holder.pb.setVisibility(View.VISIBLE); holder.tv.setVisibility(View.VISIBLE); holder.tv.setText(message.progress + "%"); if (message.status == EMMessage.Status.SUCCESS) { holder.pb.setVisibility(View.GONE); holder.tv.setVisibility(View.GONE); // message.setSendingStatus(Message.SENDING_STATUS_SUCCESS); timer.cancel(); } else if (message.status == EMMessage.Status.FAIL) { holder.pb.setVisibility(View.GONE); holder.tv.setVisibility(View.GONE); // message.setSendingStatus(Message.SENDING_STATUS_FAIL); // message.setProgress(0); holder.staus_iv.setVisibility(View.VISIBLE); Toast.makeText(activity, activity.getString(cn.ucai.superwechat.R.string.send_fail) + activity .getString(cn.ucai.superwechat.R.string.connect_failuer_toast), Toast.LENGTH_SHORT).show(); timer.cancel(); } } }); } }, 0, 500); break; default: sendPictureMessage(message, holder); } }
From source file:eu.stratosphere.nephele.taskmanager.TaskManager.java
@Override public void killTaskManager() throws IOException { // Kill the entire JVM after a delay of 10ms, so this RPC will finish properly before final Timer timer = new Timer(); final TimerTask timerTask = new TimerTask() { @Override/*from w w w.ja v a 2s. c o m*/ public void run() { System.exit(0); } }; timer.schedule(timerTask, 10L); }
From source file:com.delphix.session.test.ServiceTest.java
@Test public void testSessionReset() { int numThreads = 4; // Configure the server Server server = serverManager.locate(helloService.getServiceName()); final Timer timer = new Timer(); server.addListener(new SessionEventListener(new SessionEventRunnable() { @Override//from ww w .jav a 2 s . c om public void work() { timer.schedule(new TimerTask() { @Override public void run() { nexus.close(); } }, 500); } })); // Create the session ClientConfig spec = initServiceSpec(new HelloService(helloService)); ClientNexus client = clientManager.create(spec); login(client); // Issue commands over the fore channel issueCommands(client, numThreads, 32, 0); // Wait for the login to complete awaitDone(numThreads + 1); timer.cancel(); }
From source file:com.com.easemob.chatuidemo.adapter.MessageAdapter.java
/** * ?/*ww w .j a va 2 s . c om*/ * * @param message * @param holder * @param position * @param convertView */ private void handleImageMessage(final EMMessage message, final ViewHolder holder, final int position, View convertView) { holder.pb.setTag(position); holder.iv.setOnLongClickListener(new OnLongClickListener() { @Override public boolean onLongClick(View v) { activity.startActivityForResult((new Intent(activity, ContextMenu.class)) .putExtra("position", position).putExtra("type", EMMessage.Type.IMAGE.ordinal()), ChatMessage.REQUEST_CODE_CONTEXT_MENU); return true; } }); // ?? if (message.direct == EMMessage.Direct.RECEIVE) { // "it is receive msg"; if (message.status == EMMessage.Status.INPROGRESS) { // "!!!! back receive"; holder.iv.setImageResource(R.drawable.default_image); showDownloadImageProgress(message, holder); // downloadImage(message, holder); } else { // "!!!! not back receive, show image directly"); holder.pb.setVisibility(View.GONE); holder.tv.setVisibility(View.GONE); holder.iv.setImageResource(R.drawable.default_image); ImageMessageBody imgBody = (ImageMessageBody) message.getBody(); if (imgBody.getLocalUrl() != null) { // String filePath = imgBody.getLocalUrl(); String remotePath = imgBody.getRemoteUrl(); String filePath = ImageUtils.getImagePath(remotePath); String thumbRemoteUrl = imgBody.getThumbnailUrl(); if (TextUtils.isEmpty(thumbRemoteUrl) && !TextUtils.isEmpty(remotePath)) { thumbRemoteUrl = remotePath; } String thumbnailPath = ImageUtils.getThumbnailImagePath(thumbRemoteUrl); showImageView(thumbnailPath, holder.iv, filePath, imgBody.getRemoteUrl(), message); } } return; } // ??? // process send message // send pic, show the pic directly ImageMessageBody imgBody = (ImageMessageBody) message.getBody(); String filePath = imgBody.getLocalUrl(); if (filePath != null && new File(filePath).exists()) { showImageView(ImageUtils.getThumbnailImagePath(filePath), holder.iv, filePath, null, message); } else { showImageView(ImageUtils.getThumbnailImagePath(filePath), holder.iv, filePath, IMAGE_DIR, message); } switch (message.status) { case SUCCESS: holder.pb.setVisibility(View.GONE); holder.tv.setVisibility(View.GONE); holder.staus_iv.setVisibility(View.GONE); break; case FAIL: holder.pb.setVisibility(View.GONE); holder.tv.setVisibility(View.GONE); holder.staus_iv.setVisibility(View.VISIBLE); break; case INPROGRESS: holder.staus_iv.setVisibility(View.GONE); holder.pb.setVisibility(View.VISIBLE); holder.tv.setVisibility(View.VISIBLE); if (timers.containsKey(message.getMsgId())) return; // set a timer final Timer timer = new Timer(); timers.put(message.getMsgId(), timer); timer.schedule(new TimerTask() { @Override public void run() { activity.runOnUiThread(new Runnable() { public void run() { holder.pb.setVisibility(View.VISIBLE); holder.tv.setVisibility(View.VISIBLE); holder.tv.setText(message.progress + "%"); if (message.status == EMMessage.Status.SUCCESS) { holder.pb.setVisibility(View.GONE); holder.tv.setVisibility(View.GONE); // message.setSendingStatus(Message.SENDING_STATUS_SUCCESS); timer.cancel(); } else if (message.status == EMMessage.Status.FAIL) { holder.pb.setVisibility(View.GONE); holder.tv.setVisibility(View.GONE); // message.setSendingStatus(Message.SENDING_STATUS_FAIL); // message.setProgress(0); holder.staus_iv.setVisibility(View.VISIBLE); Toast.makeText(activity, activity.getString(R.string.send_fail) + activity.getString(R.string.connect_failuer_toast), 0).show(); timer.cancel(); } } }); } }, 0, 500); break; default: sendPictureMessage(message, holder); } }
From source file:de.madvertise.android.sdk.MadvertiseView.java
/** * Handles the refresh timer, initiates the stopping of the request thread * and caching of ads.//from www . j av a 2 s. com * * @param starting */ private void onViewCallback(final boolean starting) { synchronized (this) { if (starting) { if (mAdTimer == null) { mAdTimer = new Timer(); mAdTimer.schedule(new TimerTask() { public void run() { MadvertiseUtil.logMessage(null, Log.DEBUG, "Refreshing ad ..."); requestNewAd(true); } }, (long) mSecondsToRefreshAd * 1000, (long) mSecondsToRefreshAd * 1000); } } else { if (mAdTimer != null) { MadvertiseUtil.logMessage(null, Log.DEBUG, "Stopping refresh timer ..."); mAdTimer.cancel(); mAdTimer = null; // When there is no timer needed, the current request can be // stopped stopRequestThread(); } } } }
From source file:com.xpple.jahoqy.adapter.MessageAdapter.java
/** * ?/* ww w . j a v a 2 s .c om*/ * * @param message * @param holder * @param position * @param convertView */ private void handleVideoMessage(final EMMessage message, final ViewHolder holder, final int position, View convertView) { VideoMessageBody videoBody = (VideoMessageBody) message.getBody(); // final File image=new File(PathUtil.getInstance().getVideoPath(), // videoBody.getFileName()); String localThumb = videoBody.getLocalThumb(); holder.iv.setOnLongClickListener(new OnLongClickListener() { @Override public boolean onLongClick(View v) { activity.startActivityForResult(new Intent(activity, ContextMenu.class) .putExtra("position", position).putExtra("type", Type.VIDEO.ordinal()), ChatActivity.REQUEST_CODE_CONTEXT_MENU); return true; } }); if (localThumb != null) { showVideoThumbView(localThumb, holder.iv, videoBody.getThumbnailUrl(), message); } if (videoBody.getLength() > 0) { String time = DateUtils.toTimeBySecond(videoBody.getLength()); holder.timeLength.setText(time); } holder.playBtn.setImageResource(R.drawable.video_download_btn_nor); if (message.direct == Direct.RECEIVE) { if (videoBody.getVideoFileLength() > 0) { String size = TextFormater.getDataSize(videoBody.getVideoFileLength()); holder.size.setText(size); } } else { if (videoBody.getLocalUrl() != null && new File(videoBody.getLocalUrl()).exists()) { String size = TextFormater.getDataSize(new File(videoBody.getLocalUrl()).length()); holder.size.setText(size); } } if (message.direct == Direct.RECEIVE) { // System.err.println("it is receive msg"); if (message.status == EMMessage.Status.INPROGRESS) { // System.err.println("!!!! back receive"); holder.iv.setImageResource(R.drawable.default_image); showDownloadImageProgress(message, holder); } else { // System.err.println("!!!! not back receive, show image directly"); holder.iv.setImageResource(R.drawable.default_image); if (localThumb != null) { showVideoThumbView(localThumb, holder.iv, videoBody.getThumbnailUrl(), message); } } return; } holder.pb.setTag(position); // until here ,deal with send video msg switch (message.status) { case SUCCESS: holder.pb.setVisibility(View.GONE); holder.staus_iv.setVisibility(View.GONE); holder.tv.setVisibility(View.GONE); break; case FAIL: holder.pb.setVisibility(View.GONE); holder.tv.setVisibility(View.GONE); holder.staus_iv.setVisibility(View.VISIBLE); break; case INPROGRESS: if (timers.containsKey(message.getMsgId())) return; // set a timer final Timer timer = new Timer(); timers.put(message.getMsgId(), timer); timer.schedule(new TimerTask() { @Override public void run() { activity.runOnUiThread(new Runnable() { @Override public void run() { holder.pb.setVisibility(View.VISIBLE); holder.tv.setVisibility(View.VISIBLE); holder.tv.setText(message.progress + "%"); if (message.status == EMMessage.Status.SUCCESS) { holder.pb.setVisibility(View.GONE); holder.tv.setVisibility(View.GONE); // message.setSendingStatus(Message.SENDING_STATUS_SUCCESS); timer.cancel(); } else if (message.status == EMMessage.Status.FAIL) { holder.pb.setVisibility(View.GONE); holder.tv.setVisibility(View.GONE); // message.setSendingStatus(Message.SENDING_STATUS_FAIL); // message.setProgress(0); holder.staus_iv.setVisibility(View.VISIBLE); Toast.makeText(activity, activity.getString(R.string.send_fail) + activity.getString(R.string.connect_failuer_toast), 0).show(); timer.cancel(); } } }); } }, 0, 500); break; default: // sendMsgInBackground(message, holder); sendPictureMessage(message, holder); } }