プロジェクトwebプロジェクトで実現します。常にバックグラウンドウィジェット+タイミング+マルチタスクを実行します。

7983 ワード

どのようにwebプロジェクトが起動したら、あるjavaプログラムセグメントを実行しますか?
xx種類をservletと書いてweb.xmlに配置します。このservletを設定してアプリケーションが起動したら実行します。プロジェクトのweb.xmlの構成コードは以下の通りです。

	
		mapleServlet
		
			com.csValue.Servlet.mapleServlet
		
		1
	
	
		mapleServlet
		/mapleServlet.view
	
servlet起動後、どのようにタイミングよく機能を起動しますか?
servletはプロジェクトの起動に伴って起動します。servletでどのようにタイミングを実現するかは、タイミング機能の実現を参照することができます。 の3つの方法で実現します。
本プロジェクトのmapleServletコードは以下の通りです。
package com.csValue.Servlet;

import java.io.IOException;
import java.io.PrintStream;
import java.util.Timer;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class mapleServlet extends HttpServlet
{
  private static final long serialVersionUID = 1L;

  public void init()
    throws ServletException
  {
    System.out.println("    ");

    Timer timer = new Timer();
    timer.schedule(new Task(), 6000L, 30000L);
  }

  protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException
  {
    doPost(request, response);
  }

  protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException
  {
  }
}
は、デフォルトではinit方法の実装Timerを実行し、timerのschedule方法を呼び出す。何分遅れましたか?何ミリ秒ごとにTaskを実行しますか?
Timerを利用してタイミング機能の全体ステップを実現し、コードで次のように表します。
package scheduleTest;

import java.util.Timer;
import java.util.TimerTask;

public class scheduleTimerTest {
  public static void main(String[] args) {
    TimerTask task = new TimerTask() {
      @Override
      public void run() {
        // task to run goes here
        System.out.println("Hello !!!");
      }
    };
    Timer timer = new Timer();
    long delay = 5000;
    long intevalPeriod = 1 * 1000;
    // schedules the task to be run in an interval
    timer.scheduleAtFixedRate(task, delay,
                                intevalPeriod);
  } // end of main
}
はTimerを実装し、scheduleAtFixedRate方法を呼び出す。ここで導入されたtaskパラメータは、前に実用化され、run方法では具体的に実現されている。出力ハロー
Timerのソースコードの研究についてはtimerを参照してください。その中でTimerソースの具体的な実現について、queueを通じてキューの中の各タスクのnotifyを実現しました。
taskは具体的な任務内容で、コードは以下の通りです。
public class Task extends TimerTask
{
  public void run()
  {
	Map changedLines=compareMapleBean(selectLocalMapleList(), selectMapleList());
    if (changedLines != null)
    {

    	  Object[] lines=changedLines.keySet().toArray();
    	  Map usageLine=new HashMap();
    	  usageLine.put("UVA 1", "");
    	  usageLine.put("UVA 2", "");
    	  usageLine.put("UVA 3", "");
    	  usageLine.put("UVA 4", "");
    	  usageLine.put("UVA 5", "");
    	  usageLine.put("UVA 6", "");
    	  usageLine.put("UVA 7", "");
    	  usageLine.put("UVA 8", "");
    	  usageLine.put("JINYI 9", "");
    	  usageLine.put("JINYI 10", "");
    	  usageLine.put("JINYI 11", "");
    	  usageLine.put("JINYI 12", "");
    	  usageLine.put("JINYI 13", "");
    	  usageLine.put("JINYI 14", "");
    	  usageLine.put("JINYI 15", "");
    	  usageLine.put("JINYI 16", "");
    	  usageLine.put("JINYI 17", "");
    	  usageLine.put("JINYI 18", "");
    	  usageLine.put("JINYI 19", "");
    	       
    	  for (Object object : lines) {
    		  if(usageLine.containsKey(object.toString())){
    			  updateMaple(object.toString(),changedLines.get(object.toString()).toString().split("::"));
    		  }
    	  }
        System.out.println("    ");
      }
  }
}
タイミング機能の具体的な実現?
戻ってきて、servletのコードを見ます。
timer.schedule(new Task(), 6000L, 30000L);
実現ソースコードは以下の通りです。
/**
     * Schedules the specified task for repeated fixed-delay execution,
     * beginning after the specified delay.  Subsequent executions take place
     * at approximately regular intervals separated by the specified period.
     *
     * 

In fixed-delay execution, each execution is scheduled relative to * the actual execution time of the previous execution. If an execution * is delayed for any reason (such as garbage collection or other * background activity), subsequent executions will be delayed as well. * In the long run, the frequency of execution will generally be slightly * lower than the reciprocal of the specified period (assuming the system * clock underlying Object.wait(long) is accurate). * *

Fixed-delay execution is appropriate for recurring activities * that require "smoothness." In other words, it is appropriate for * activities where it is more important to keep the frequency accurate * in the short run than in the long run. This includes most animation * tasks, such as blinking a cursor at regular intervals. It also includes * tasks wherein regular activity is performed in response to human * input, such as automatically repeating a character as long as a key * is held down. * * @param task task to be scheduled. * @param delay delay in milliseconds before task is to be executed. * @param period time in milliseconds between successive task executions. * @throws IllegalArgumentException if delay is negative, or * delay + System.currentTimeMillis() is negative. * @throws IllegalStateException if task was already scheduled or * cancelled, timer was cancelled, or timer thread terminated. */ public void schedule(TimerTask task, long delay, long period) { if (delay < 0) throw new IllegalArgumentException("Negative delay."); if (period <= 0) throw new IllegalArgumentException("Non-positive period."); sched(task, System.currentTimeMillis()+delay, -period); }

sched実現ソースコードは以下の通りである。
 /**
     * Schedule the specified timer task for execution at the specified
     * time with the specified period, in milliseconds.  If period is
     * positive, the task is scheduled for repeated execution; if period is
     * zero, the task is scheduled for one-time execution. Time is specified
     * in Date.getTime() format.  This method checks timer state, task state,
     * and initial execution time, but not period.
     *
     * @throws IllegalArgumentException if time() is negative.
     * @throws IllegalStateException if task was already scheduled or
     *         cancelled, timer was cancelled, or timer thread terminated.
     */
    private void sched(TimerTask task, long time, long period) {
        if (time < 0)
            throw new IllegalArgumentException("Illegal execution time.");

        synchronized(queue) {
            if (!thread.newTasksMayBeScheduled)
                throw new IllegalStateException("Timer already cancelled.");

            synchronized(task.lock) {
                if (task.state != TimerTask.VIRGIN)
                    throw new IllegalStateException(
                        "Task already scheduled or cancelled");
                task.nextExecutionTime = time;
                task.period = period;
                task.state = TimerTask.SCHEDULED;
            }

            queue.add(task);
            if (queue.getMin() == task)
                queue.notify();
        }
    }