java启动redis服务

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

import org.apache.commons.lang.StringUtils;

import sms.JedisPoolDemo;

public class T1{
	
	public static String getResult(Process exec) throws IOException{
		boolean boo = true;
		StringBuilder sb = new StringBuilder();
		InputStreamReader r = new InputStreamReader(exec.getInputStream());
		BufferedReader reader = new BufferedReader(r);
		
		String line = "";
		while((line = reader.readLine()) != null){
			if(line.contains("=")){ // 表示已经读取到了服务
				boo = false; // 关闭开关,下次读取空行表示已经读取结束
			}
			sb.append(line);
			if(!boo && StringUtils.isBlank(line)){ // 读到空行,并且开关关闭状态 跳出循环
				break;
			}
		}
		
		return sb.toString();
	}
	
	public static void getProcess() throws IOException{
		Process exec = null;
		String result = null;
		
		// 获取所有服务的列表
		exec = Runtime.getRuntime().exec("cmd /c tasklist");
		result = getResult(exec);
		// redis 服务没有开启
		if(!result.contains("reids")){
			// 开启redis服务
			exec = Runtime.getRuntime().exec("cmd /c start net start redis");
		}
	}
	
	public static void main(String[] args) throws IOException {
		getProcess();
		// 测试redis是否正常
		JedisPoolDemo.set("name", "小二");
		System.out.println(JedisPoolDemo.get("name"));
	}
}


版权声明:本文为weixin_43177361原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。