各种例子,各种试,暂时无果,特来求帮助.
MyService有两个方法 ,start 和stop .
package test.syi;public class MyService   
{   
    private static Thread thread=null;   
    private static Service service = null;   
  
    /**  
     * 启动服务方法(该方法必须有参数 String [] args)  
     * @param args  
     */  
     public static void StartService(String[] args)   
     {   
         System.out.println("启动服务");   
         // 产生服务线程   
         service = new Service();   
         thread=new Thread(service);   
         try  
         {   
             // 将服务线程设定为用户线程,以避免StartService方法结束后线程退出   
             thread.setDaemon(false);   
             if(!thread.isDaemon())   
             {   
                 System.out.println("成功设定线程为用户线程!");   
             }   
   
             //启动服务线程   
             thread.start();   
         }   
         catch(SecurityException se)   
         {   
             System.out.println("线程类型设定失败!");   
         }   
     }   
    
    /**  
    * 退出服务方法(该方法必须有参数 String [] args)  
    * @param args  
    */  
    public static void StopService(String[] args)   
    {   
        System.out.println("停止服务");   
        service.setRunFlag(false);   
    }   
   
}    package test.syi;public class Service implements Runnable {
private boolean runFlag = true; /**
 * 设定服务线程运行标志值
 * 
 * @param runFlag
 */
public synchronized void setRunFlag(boolean runFlag) {
this.runFlag = runFlag;
} /**
 * 取得服务线程运行标志值
 * 
 * @param void
 */
private synchronized boolean getRunFlag() {
return runFlag;
} public void run() {
System.out.println("服务线程开始运行");
while (getRunFlag()) {
// 模拟用户登录,监控服务是否可用.
// Monitoring.mnt();
System.out.println(1); try {
Thread.sleep(SystemConfig.SLEEP_TIME);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
// 关闭输出流.
Monitoring.close();
}
}package test.syi;import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.Calendar;public class Monitoring { private static final File filePath = new File(SystemConfig.FILE_PATH);
private static final File F = new File(filePath + SystemConfig.FILE_NAME);
private static String urlPath = SystemConfig.URL_PATH;
private static BufferedWriter bw = null;

public static void mnt() {
try {
// 创建路径.
filePath.mkdir();
// 当文件不存在时,创建文件.
F.createNewFile(); // 
if (bw == null)
bw = new BufferedWriter(new FileWriter(F,true));
} catch (IOException e1) {
e1.printStackTrace();
} try { URL url = new URL(urlPath);
HttpURLConnection uc = (HttpURLConnection) url.openConnection();
uc.setRequestMethod("POST");
uc.setDoOutput(true);
uc.setDoInput(true);
OutputStream os = uc.getOutputStream(); os.write("username=test&password=test".getBytes("GBK"));
os.flush();
os.close();
uc.connect();
BufferedReader br = new BufferedReader(new InputStreamReader(uc
.getInputStream()));
writer(bw, getNoErrorMsg(br));
} catch (Exception e) { writer(bw, getErrorMsg(e));
}
} private static void writer(BufferedWriter bw, String msg) {
try {
StringBuffer sb = new StringBuffer();
sb.append((new SimpleDateFormat("yyyy年MM月dd日    HH:mm:ss")).format(Calendar.getInstance().getTime()) + "\r\n");
sb.append(msg);
sb.append("\r\n" + "\r\n" + "\r\n");
// System.out.print(sb.toString());
bw.write(sb.toString());
bw.flush();
} catch (IOException e1) {
e1.printStackTrace();
}
} private static String getNoErrorMsg( BufferedReader br) {
String str = null;
StringBuffer sb = new StringBuffer();
sb.append("no error . :-)");
sb.append("---------------------------" + "\r\n");
try { while ((str = br.readLine()) != null) {
sb.append(str + "\r\n");
}
} catch (IOException e) {
e.printStackTrace();
}
return sb.toString();
}

private static String getErrorMsg( Exception e) {
StringBuffer sb = new StringBuffer();
sb.append("error!!!" + "\r\n");
sb.append("---------------------------" + "\r\n");
StackTraceElement[] ss = e.getStackTrace(); for (StackTraceElement s : ss) {
sb.append(s.toString() + "\r\n");
}
return sb.toString();
}

public static void close()
{
try {
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}package test.syi;import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;public class SystemConfig {
public static String FILE_PATH ;
public static String FILE_NAME;
public static String URL_PATH ;
public static Integer SLEEP_TIME ;

static 
{
try {
Properties pro = new Properties();
pro.load(new FileInputStream("src/test/syi/system_config.properties"));
FILE_PATH = pro.get("filePath").toString();
FILE_NAME = pro.get("fileName").toString();
URL_PATH = pro.get("urlPath").toString();
SLEEP_TIME = Integer.valueOf(pro.get("sleepTime").toString()); } catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
filePath=d:/logs
fileName=/log.txt
urlPath=http://localhost:8080/eposp/account.do?method=logon
sleepTime=10000真没多少分了...帮帮忙....

解决方案 »

  1.   

    LZ想问什么?
    想自己做服务,要么socket要么rmi
    因为两个batch(启动和停止)执行时,是启动两个jvm,不同的jvm,加载的类是没有关系的,所以,即使你写成类方法,也没有意义,所以一般做端口监听,启动时,服务会监听某个端口,停止时,通过停止batch启动某个程序给端口发送个信息,服务接到该信息后,停止服务。
    这么说,能理解吗?
      

  2.   

    能理解.
    一开始比较2,想的也简单了. 
    用两个类,两个main方法.分别调用start 和 stop , 然后发现根本不行..
    所以特来请教...
    那我试着写一个 ServerSocket,其实也是间接解决了如何调用 start 和 stop 两个方法的问题.
      

  3.   

    没事写着玩,楼主看这个能用么
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;public class Main {
    public static void main(String[] args) {
    class Xxx extends Thread {
    private volatile boolean hold;
    private volatile boolean stop; public void setHold(boolean hold) {
    this.hold = hold;
    System.out.println("input hold:" + hold);
    System.out.println("this hold:" + this.hold);
    } public void setStop(boolean stop) {
    this.stop = stop;
    } public void run() {
    while (!stop) {
    if (hold) {
    continue;
    }
    System.out.println("Xxx running");
    try {
    this.sleep(1000);
    } catch (InterruptedException e) {
    e.printStackTrace();
    }
    } }
    }
    Xxx x = new Xxx();
    x.start();
    BufferedReader reader = new BufferedReader(new InputStreamReader(
    System.in));
    boolean run = true;
    while (run) {
    try {
    String command = reader.readLine();
    System.out.println(command); if ("exit".equals(command)) {
    System.out.println(1);
    x.setStop(true);
    run = false;
    }
    if ("goon".equals(command)) {
    System.out.println(2);
    x.setHold(false);
    }
    if ("stop".equals(command)) {
    System.out.println(3);
    x.setHold(true);
    }
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    } }}
      

  4.   

    4L 前辈,有点跑题哩...最后觉得写个小的Windows窗口, 两个按钮,起,停,OK....
    暂时这样,今天看能否交工,交不了再来请教各位.