Task Scheduling : Scheduling « Threads « Java

Home
Java
1.2D Graphics GUI
2.3D
3.Advanced Graphics
4.Ant
5.Apache Common
6.Chart
7.Class
8.Collections Data Structure
9.Data Type
10.Database SQL JDBC
11.Design Pattern
12.Development Class
13.EJB3
14.Email
15.Event
16.File Input Output
17.Game
18.Generics
19.GWT
20.Hibernate
21.I18N
22.J2EE
23.J2ME
24.JDK 6
25.JNDI LDAP
26.JPA
27.JSP
28.JSTL
29.Language Basics
30.Network Protocol
31.PDF RTF
32.Reflection
33.Regular Expressions
34.Scripting
35.Security
36.Servlets
37.Spring
38.Swing Components
39.Swing JFC
40.SWT JFace Eclipse
41.Threads
42.Tiny Application
43.Velocity
44.Web Services SOA
45.XML
Java » Threads » SchedulingScreenshots 
Task Scheduling
Task Scheduling
 
/*
Java Threads, 3rd Edition
By Scott Oaks, Henry Wong
3rd Edition September 2004 
ISBN: 0-596-00782-5

*/

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Timer;
import java.util.TimerTask;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class URLMonitorPanel extends JPanel implements URLPingTask.URLUpdate {

  Timer timer;

  URL url;

  URLPingTask task;

  JPanel status;

  JButton startButton, stopButton;

  public URLMonitorPanel(String url, Timer tthrows MalformedURLException {
    setLayout(new BorderLayout());
    timer = t;
    this.url = new URL(url);
    add(new JLabel(url), BorderLayout.CENTER);
    JPanel temp = new JPanel();
    status = new JPanel();
    status.setSize(2020);
    temp.add(status);
    startButton = new JButton("Start");
    startButton.setEnabled(false);
    startButton.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent ae) {
        makeTask();
        startButton.setEnabled(false);
        stopButton.setEnabled(true);
      }
    });
    stopButton = new JButton("Stop");
    stopButton.setEnabled(true);
    stopButton.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent ae) {
        task.cancel();
        startButton.setEnabled(true);
        stopButton.setEnabled(false);
      }
    });
    temp.add(startButton);
    temp.add(stopButton);
    add(temp, BorderLayout.EAST);
    makeTask();
  }

  private void makeTask() {
    task = new URLPingTask(url, this);
    timer.schedule(task, 0L5000L);
  }

  public void isAlive(final boolean b) {
    SwingUtilities.invokeLater(new Runnable() {
      public void run() {
        status.setBackground(b ? Color.GREEN : Color.RED);
        status.repaint();
      }
    });
  }

  public static void main(String[] argsthrows Exception {
    JFrame frame = new JFrame("URL Monitor");
    Container c = frame.getContentPane();
    c.setLayout(new BoxLayout(c, BoxLayout.Y_AXIS));
    Timer t = new Timer();
    String[] u = new String[]{"http://www.java2s.com","http://www.java2s.com"};
    
    for (int i = 0; i < u.length; i++) {
      c.add(new URLMonitorPanel(u[i], t));
    }
    frame.addWindowListener(new WindowAdapter() {
      public void windowClosing(WindowEvent evt) {
        System.exit(0);
      }
    });
    frame.pack();
    frame.show();
  }
}

class URLPingTask extends TimerTask {

  public interface URLUpdate {
    public void isAlive(boolean b);
  }

  URL url;

  URLUpdate updater;

  public URLPingTask(URL url) {
    this(url, null);
  }

  public URLPingTask(URL url, URLUpdate uu) {
    this.url = url;
    updater = uu;
  }

  public void run() {
    if (System.currentTimeMillis() - scheduledExecutionTime() 5000) {
      // Let the next task do it
      return;
    }
    try {
      HttpURLConnection huc = (HttpURLConnectionurl.openConnection();
      huc.setConnectTimeout(1000);
      huc.setReadTimeout(1000);
      int code = huc.getResponseCode();
      if (updater != null)
        updater.isAlive(true);
    catch (Exception e) {
      if (updater != null)
        updater.isAlive(false);
    }
  }
}


           
         
  
Related examples in the same category
1.Job SchedulerJob Scheduler
2.Executes a task with a specified timeout
3.Timer class used to implement login and query timeouts
4.Runs multiple jobs in parallel
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.