List of usage examples for java.util TimerTask TimerTask
protected TimerTask()
From source file:edu.mayo.cts2.framework.plugin.service.lexevs.naming.DefaultCodingSchemeNameTranslator.java
protected void scheduleflushCache() { TimerTask flushTask = new TimerTask() { @Override// ww w .j a v a2 s .c om public void run() { buildCaches(); } }; Timer timer = new Timer(); int minutesInMillis = FLUSH_PERIOD_MINUTES * 60 * 1000; timer.schedule(flushTask, 0, minutesInMillis); }
From source file:idgs.client.TcpClient.java
/** * /*from www . java 2 s . c o m*/ * @param timeout * @param retryTimes * @throws IOException */ public boolean connect(final boolean enableTimeoutCheck, final int timeout, final int retryTimes) { if (enableTimeoutCheck) { if (retryTimes <= 0) { // up to re-try times return isConnected(); } Timer timer = new Timer(); // after timeout seconds, run timer task, if not connected, re-try again timer.schedule(new TimerTask() { public void run() { if (!isConnected()) { int newRetryTimes = retryTimes - 1; connect(enableTimeoutCheck, timeout, newRetryTimes); } } }, timeout); } try { SocketChannel channel = SocketChannel.open(); channel.configureBlocking(false); channel.register(selector, SelectionKey.OP_CONNECT); channel.connect(servAddr); select(); } catch (IOException e) { log.warn("try to connecte to server[" + servAddr.toString() + "] error, " + e.getMessage()); } return isConnected(); }
From source file:org.apache.james.ESReporterTest.java
@Test public void timeMetricsShouldBeReportedWhenImapCommandsReceived() throws Exception { IMAPClient client = new IMAPClient(); client.connect(InetAddress.getLocalHost(), IMAP_PORT); client.login(USERNAME, PASSWORD);/* w w w .java2 s . c o m*/ TimerTask timerTask = new TimerTask() { @Override public void run() { try { client.list("", "*"); } catch (Exception e) { LOGGER.error("Error while sending LIST command", e); } } }; timer.schedule(timerTask, DELAY_IN_MS, PERIOD_IN_MS); await().atMost(Duration.TEN_MINUTES).until(() -> checkMetricRecordedInElasticSearch()); }
From source file:de.sub.goobi.forms.ModuleServerForm.java
/** * Start module server.//from www . jav a2s .com */ public void readAllModulesFromConfiguration() { if (modulmanager == null) { synchronized (ModuleServerForm.class) { if (modulmanager == null) { int port = ConfigCore.getIntParameter("kitodoModuleServerPort"); final GoobiModuleManager manager = new GoobiModuleManager(port, new ExtendedProzessImpl(), new ExtendedDataImpl()); // Alle Modulbeschreibungen aus der Konfigurationsdatei // modules.xml einlesen manager.addAll(getModulesFromConfigurationFile()); modulmanager = manager; } // Nachrichtensystem initialisieren int delay = 5000; int period = 1000; messageTimer = new Timer(); messageTimer.scheduleAtFixedRate(new TimerTask() { @Override public void run() { ModuleServerForm.check_new_messages(modulmanager); } }, delay, period); running = true; } } }
From source file:com.easibeacon.examples.shop.MainActivity.java
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_listview); _barSearchBeacons = (ProgressBar) findViewById(R.id.barSearchBeacons); _txtState = (TextView) findViewById(R.id.txtState); ibp = IBeaconProtocol.getInstance(this); ibp.setListener(this); TimerTask searchIbeaconTask = new TimerTask() { @Override//from w w w .j av a2 s . c om public void run() { runOnUiThread(new Runnable() { @Override public void run() { scanBeacons(); } }); } }; Timer timer = new Timer(); timer.scheduleAtFixedRate(searchIbeaconTask, 1000, 2000); }
From source file:de.blizzy.backup.BackupApplication.java
static void scheduleBackupRun(boolean forceRunNow) { if (timer != null) { if (backupTimerTask != null) { backupTimerTask.cancel();/*w ww .ja v a 2 s. co m*/ backupTimerTask = null; } backupTimerTask = new TimerTask() { @Override public void run() { runBackup(); } }; if (forceRunNow) { timer.schedule(backupTimerTask, 0); } else { IDialogSettings backupSection = Utils.getSection("backup"); //$NON-NLS-1$ long lastRun = backupSection.getLong("lastRun"); //$NON-NLS-1$ Settings settings = settingsManager.getSettings(); if (settings.isRunHourly()) { nextBackupRunTime = lastRun; } else { Calendar c = Calendar.getInstance(); c.setTimeInMillis(lastRun); c.set(Calendar.HOUR_OF_DAY, settings.getDailyHours()); c.set(Calendar.MINUTE, settings.getDailyMinutes()); nextBackupRunTime = c.getTimeInMillis(); } long now = System.currentTimeMillis(); for (;;) { nextBackupRunTime = clearSeconds(nextBackupRunTime); if (nextBackupRunTime > now) { break; } nextBackupRunTime += settings.isRunHourly() ? 60L * 60L * 1000L : 24L * 60L * 60L * 1000L; } timer.schedule(backupTimerTask, Math.max(nextBackupRunTime - now, 0)); } } }
From source file:net.frygo.findmybuddy.GCMIntentService.java
private static void generateNotification(Context context, String message) { Random rand = new Random(); int x = rand.nextInt(); NotificationManager notificationManager = (NotificationManager) context .getSystemService(Context.NOTIFICATION_SERVICE); String title = context.getString(R.string.app_name); Intent notificationIntent = new Intent(context, customlistview.class); notificationIntent.putExtra("alert", message); message = message + " would like to add you as friend"; PendingIntent intent = PendingIntent.getActivity(context, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_CANCEL_CURRENT); Notification notification = new Notification(R.drawable.logo, message, System.currentTimeMillis()); notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP); notification.setLatestEventInfo(context, title, message, intent); notification.flags |= Notification.FLAG_AUTO_CANCEL; notificationManager.notify(x, notification); PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE); final PowerManager.WakeLock mWakelock = pm .newWakeLock(PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP, title); mWakelock.acquire();//ww w .j ava 2s .c o m // Timer before putting Android Device to sleep mode. Timer timer = new Timer(); TimerTask task = new TimerTask() { public void run() { mWakelock.release(); } }; timer.schedule(task, 5000); }
From source file:com.alliander.osgp.adapter.protocol.iec61850.application.services.DeviceRegistrationService.java
/** * After the device has registered with the platform successfully, the * device has to be informed that the registration worked. Disable an * attribute so the device will stop attempting to register once a minute. * * @param deviceIdentification/*from w w w. j a v a 2 s . c o m*/ * The device identification. * @param ipAddress * The IP address of the device. * @param ied * The type of IED. * * @throws ProtocolAdapterException * In case the connection to the device can not be established. */ public void disableRegistration(final String deviceIdentification, final InetAddress ipAddress, final IED ied) throws ProtocolAdapterException { this.iec61850DeviceConnectionService.connect(ipAddress.getHostAddress(), deviceIdentification, ied, LogicalDevice.LIGHTING); final Iec61850ClientAssociation iec61850ClientAssociation = this.iec61850DeviceConnectionService .getIec61850ClientAssociation(deviceIdentification); final ServerModel serverModel = this.iec61850DeviceConnectionService.getServerModel(deviceIdentification); final DeviceConnection deviceConnection = new DeviceConnection( new Iec61850Connection(iec61850ClientAssociation, serverModel), deviceIdentification, ied); final Function<Void> function = new Function<Void>() { @Override public Void apply() throws Exception { DeviceRegistrationService.this.setLocationInformation(deviceConnection); DeviceRegistrationService.this.disableRegistration(deviceConnection); if (DeviceRegistrationService.this.isReportingAfterDeviceRegistrationEnabled) { LOGGER.info("Reporting enabled for device: {}", deviceConnection.getDeviceIdentification()); new Iec61850EnableReportingCommand().enableReportingOnDeviceWithoutUsingSequenceNumber( DeviceRegistrationService.this.iec61850DeviceConnectionService.getIec61850Client(), deviceConnection); // Don't disconnect now! The device should be able to send // reports. new Timer().schedule(new TimerTask() { @Override public void run() { try { new Iec61850ClearReportCommand().clearReportOnDevice(deviceConnection); } catch (final ProtocolAdapterException e) { LOGGER.error("Unable to clear report for device: " + deviceConnection.getDeviceIdentification(), e); } DeviceRegistrationService.this.iec61850DeviceConnectionService .disconnect(deviceConnection.getDeviceIdentification()); } }, DeviceRegistrationService.this.delayAfterDeviceRegistration); } else { LOGGER.info("Reporting disabled for device: {}", deviceIdentification); DeviceRegistrationService.this.iec61850DeviceConnectionService .disconnect(deviceConnection.getDeviceIdentification()); } return null; } }; this.iec61850DeviceConnectionService.sendCommandWithRetry(function); }
From source file:gov.nih.nci.caxchange.messaging.AdverseEventLoadTest.java
private TimerTask getTimerTask() { return new TimerTask() { @Override/*w ww . j a va 2 s .c o m*/ public void run() { es.execute(getRunnable()); } }; }
From source file:com.lfv.lanzius.application.SoundClip.java
public synchronized void play() { if (stream != null && clip != null) { // Stop if already running stop();// w w w.ja v a 2 s .c o m period = true; // Create a new sound task and schedule it periodicPlayTask = new TimerTask() { public void run() { playPeriodic(); } }; soundTimer.schedule(periodicPlayTask, 100, periodMillis); } else log.warn("Trying to play sound but no sound has been loaded"); }