1、绕过ssl认证的工具类:
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.X509Certificate;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
public final class SSLUtil {
private static final TrustManager\[\] UNQUESTIONING\_TRUST\_MANAGER = new TrustManager\[\] { new X509TrustManager() {
public java.security.cert.X509Certificate\[\] getAcceptedIssuers()
{
return null;
}
public void checkClientTrusted(X509Certificate\[\] certs, String authType)
{
}
public void checkServerTrusted(X509Certificate\[\] certs, String authType)
{
}
} };
public static void turnOffSslChecking() throws NoSuchAlgorithmException, KeyManagementException
{
final SSLContext sc = SSLContext.getInstance("SSL");
sc.init(null, UNQUESTIONING\_TRUST\_MANAGER, null);
HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
}
public static void turnOnSslChecking() throws KeyManagementException, NoSuchAlgorithmException
{
SSLContext.getInstance("SSL").init(null, null, null);
}
private SSLUtil()
{
throw new UnsupportedOperationException("Do not instantiate libraries.");
}
}
2、直接测试类:
import java.io.File;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.util.HashMap;
import java.util.Map;
import org.springframework.core.io.FileSystemResource;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;
public class AzkabanTest {
private static final String API = "http://172.16.4.117:9091";
private static final String SESSION\_ID = "6102b053-8720-4940-8baf-0bed38748821";
private static final String PROJECT = "test";
private static final String PROJECT\_ID = "12";
private static final String SCHEDULE\_ID = "13";
private static RestTemplate restTemplate = null;
static
{
SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory();
requestFactory.setConnectTimeout(2000);
requestFactory.setReadTimeout(2000);
restTemplate = new RestTemplate(requestFactory);
}
public static void main(String\[\] args) throws Exception
{
loginTest(); // 登录
// createProTest(); // 创建Project
// deleteProTest(); // 删除Project
// uploadZip(); // 上传zip
// scheduleEXEaFlowTest(); // 创建定时任务
// scheduleByCronEXEaFlowTest(); // 创建定时任务cron
// unscheduleFlowTest(); // 取消定时任务
}
/\*\*
\* 登录测试 登录调度系统
\*/
public static void loginTest() throws Exception
{
SSLUtil.turnOffSslChecking();
HttpHeaders hs = new HttpHeaders();
hs.add("Content-Type", "application/x-www-form-urlencoded; charset=utf-8");
hs.add("X-Requested-With", "XMLHttpRequest");
LinkedMultiValueMap<String, String> linkedMultiValueMap = new LinkedMultiValueMap<String, String>();
linkedMultiValueMap.add("action", "login");
linkedMultiValueMap.add("username", "azkaban");
linkedMultiValueMap.add("password", "azkaban");
HttpEntity<MultiValueMap<String, String>> httpEntity = new HttpEntity<>(linkedMultiValueMap, hs);
String postForObject = restTemplate.postForObject(API, httpEntity, String.class);
System.out.println(postForObject);
}
/\*\*
\* 创建任务测试 创建一个project
\*/
public static void createProTest() throws Exception
{
SSLUtil.turnOffSslChecking();
HttpHeaders hs = new HttpHeaders();
hs.add("Content-Type", "application/x-www-form-urlencoded; charset=utf-8");
hs.add("X-Requested-With", "XMLHttpRequest");
LinkedMultiValueMap<String, String> linkedMultiValueMap = new LinkedMultiValueMap<String, String>();
linkedMultiValueMap.add("session.id", SESSION\_ID);
linkedMultiValueMap.add("action", "create");
linkedMultiValueMap.add("name", PROJECT);
linkedMultiValueMap.add("description", "testproject");
HttpEntity<MultiValueMap<String, String>> httpEntity = new HttpEntity<>(linkedMultiValueMap, hs);
String postForObject = restTemplate.postForObject(API + "/manager", httpEntity, String.class);
System.out.println(postForObject);
}
/\*\*
\* 删除任务测试 删除一个project
\*/
public static void deleteProTest() throws Exception
{
SSLUtil.turnOffSslChecking();
HttpHeaders hs = new HttpHeaders();
hs.add("Content-Type", "application/x-www-form-urlencoded; charset=utf-8");
hs.add("X-Requested-With", "XMLHttpRequest");
hs.add("Accept", "text/plain;charset=utf-8");
Map<String, String> map = new HashMap<>();
map.put("id", SESSION\_ID);
map.put("project", PROJECT);
ResponseEntity<String> exchange = restTemplate.exchange(API + "/manager?session.id={id}&delete=true&project={project}", HttpMethod.GET, new HttpEntity<String>(hs), String.class, map);
System.out.println(exchange.getBody());
System.out.println(exchange.getStatusCode());
System.out.println(exchange.getStatusCodeValue());
}
/\*\*
\* 上传zip 上传依赖文件 zip包
\*/
public static void uploadZip() throws Exception
{
SSLUtil.turnOffSslChecking();
FileSystemResource resource = new FileSystemResource(new File("C:/Users/wuzy/Desktop/1.zip"));
LinkedMultiValueMap<String, Object> linkedMultiValueMap = new LinkedMultiValueMap<String, Object>();
linkedMultiValueMap.add("session.id", SESSION\_ID);
linkedMultiValueMap.add("ajax", "upload");
linkedMultiValueMap.add("project", PROJECT);
linkedMultiValueMap.add("file", resource);
String postForObject = restTemplate.postForObject(API + "/manager", linkedMultiValueMap, String.class);
System.out.println(postForObject);
}
/\*\*
\* Schedule a period-based Flow 根据时间 创建调度任务
\*/
public static void scheduleEXEaFlowTest() throws KeyManagementException, NoSuchAlgorithmException
{
SSLUtil.turnOffSslChecking();
HttpHeaders hs = new HttpHeaders();
hs.add("Content-Type", "application/x-www-form-urlencoded; charset=utf-8");
hs.add("X-Requested-With", "XMLHttpRequest");
LinkedMultiValueMap<String, String> linkedMultiValueMap = new LinkedMultiValueMap<String, String>();
linkedMultiValueMap.add("session.id", SESSION\_ID);
linkedMultiValueMap.add("ajax", "scheduleFlow");
linkedMultiValueMap.add("projectName", PROJECT);
linkedMultiValueMap.add("projectId", PROJECT\_ID);
linkedMultiValueMap.add("flow", "2");
// linkedMultiValueMap.add("scheduleTime", "10,28,am,EDT");
linkedMultiValueMap.add("scheduleTime", "15,08,pm,PDT");
linkedMultiValueMap.add("scheduleDate", "12/1/2017");
linkedMultiValueMap.add("flowName", "test01 description");
// 是否循环
linkedMultiValueMap.add("is\_recurring", "on");
// 循环周期 天 年 月等
// M Months
// w Weeks
// d Days
// h Hours
// m Minutes
// s Seconds
linkedMultiValueMap.add("period", "30s"); // 经测试,定时任务支持至少是60秒或其整数倍
HttpEntity<MultiValueMap<String, String>> httpEntity = new HttpEntity<>(linkedMultiValueMap, hs);
String postForObject = restTemplate.postForObject(API + "/schedule", httpEntity, String.class);
System.out.println(postForObject);
}
/\*\*
\* Flexible scheduling using Cron 通过cron表达式调度执行 创建调度任务
\*/
public static void scheduleByCronEXEaFlowTest() throws KeyManagementException, NoSuchAlgorithmException
{
SSLUtil.turnOffSslChecking();
HttpHeaders hs = new HttpHeaders();
hs.add("Content-Type", "application/x-www-form-urlencoded; charset=utf-8");
hs.add("X-Requested-With", "XMLHttpRequest");
LinkedMultiValueMap<String, String> linkedMultiValueMap = new LinkedMultiValueMap<String, String>();
linkedMultiValueMap.add("session.id", SESSION\_ID);
linkedMultiValueMap.add("ajax", "scheduleCronFlow");
linkedMultiValueMap.add("projectName", PROJECT);
linkedMultiValueMap.add("cronExpression", "\* \*/1 \* \* \* ?");
linkedMultiValueMap.add("flow", "中文");
linkedMultiValueMap.add("flowName", "dsaf");
HttpEntity<MultiValueMap<String, String>> httpEntity = new HttpEntity<>(linkedMultiValueMap, hs);
String postForObject = restTemplate.postForObject(API + "/schedule", httpEntity, String.class);
System.out.println(postForObject);
}
/\*\*
\* Unschedule a Flow 取消一个流的调度
\*/
public static void unscheduleFlowTest() throws KeyManagementException, NoSuchAlgorithmException
{
SSLUtil.turnOffSslChecking();
HttpHeaders hs = new HttpHeaders();
hs.add("Content-Type", "application/x-www-form-urlencoded; charset=utf-8");
hs.add("X-Requested-With", "XMLHttpRequest");
LinkedMultiValueMap<String, String> linkedMultiValueMap = new LinkedMultiValueMap<String, String>();
linkedMultiValueMap.add("session.id", SESSION\_ID);
linkedMultiValueMap.add("action", "removeSched");
linkedMultiValueMap.add("scheduleId", SCHEDULE\_ID);
HttpEntity<MultiValueMap<String, String>> httpEntity = new HttpEntity<>(linkedMultiValueMap, hs);
String postForObject = restTemplate.postForObject(API + "/schedule", httpEntity, String.class);
System.out.println(postForObject);
}
}
3、Spring Boot接口测试:
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.web.client.RestTemplate;
@Configuration
public class AzkabanConfig {
@Bean
public RestTemplate getRestTemplate()
{
SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory();
requestFactory.setConnectTimeout(2000);
requestFactory.setReadTimeout(2000);
RestTemplate restTemplate = new RestTemplate(requestFactory);
return restTemplate;
}
}
import java.io.File;
import java.util.Date;
public interface IAzkabanService {
/\*\*
\* Azkaban登录接口,返回sessionId
\* @author wuzy
\* @date 2017年12月21日
\* @return
\* @throws Exception
\*/
public String login() throws Exception;
/\*\*
\* Azkaban创建project
\* @author wuzy
\* @date 2017年12月21日
\* @param projectName project名称
\* @param description project描述
\* @throws Exception
\*/
public void createProject(String projectName, String description) throws Exception;
/\*\*
\* Azkaban删除project
\* @author wuzy
\* @date 2017年12月21日
\* @param projectName project名称
\* @throws Exception
\*/
public void deleteProject(String projectName) throws Exception;
/\*\*
\* Azkaban上传zip文件
\* @author wuzy
\* @date 2017年12月21日
\* @param projectName
\* @param file
\* @return projectId
\* @throws Exception
\*/
public String uploadZip(String projectName, File file) throws Exception;
/\*\*
\* 根据时间 创建调度任务
\* @author wuzy
\* @date 2017年12月21日
\* @param projectId
\* @param projectName
\* @param flow
\* @param flowName
\* @param recurring 是否循环,on循环
\* @param period 循环频率: M Months,w Weeks,d Days,h Hours,m Minutes,s Seconds;如60s,支持分钟的倍数
\* @param date 开始时间
\* @return 返回scheduleId
\* @throws Exception
\*/
public String scheduleEXEaFlow(String projectId, String projectName, String flow, String flowName, String recurring, String period, Date date) throws Exception;
/\*\*
\* 根据cron表达式 创建调度任务
\* @author wuzy
\* @date 2017年12月21日
\* @param projectName
\* @param cron
\* @param flow
\* @param flowName
\* @return 返回scheduleId
\* @throws Exception
\*/
public String scheduleByCronEXEaFlow(String projectName, String cron, String flow, String flowName) throws Exception;
/\*\*
\* 根据scheduleId取消一个流的调度
\* @author wuzy
\* @date 2017年12月21日
\* @param scheduleId
\* @throws Exception
\*/
public void unscheduleFlow(String scheduleId) throws Exception;
/\*\*
\* 下载Azkaban压缩文件
\* @author wuzy
\* @date 2017年12月22日
\* @param projectName
\* @param zipPath
\* @throws Exception
\*/
public void downLoadZip(String projectName, String zipPath) throws Exception;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpStatus;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.FileSystemResource;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.ueb.baseplatform.scheduling.common.Constant;
import com.ueb.baseplatform.scheduling.disconf.GlobalConfig;
import com.ueb.baseplatform.scheduling.service.IAzkabanService;
import com.ueb.baseplatform.scheduling.util.SSLUtil;
@Service
public class AzkabanServiceImpl implements IAzkabanService {
private static final Logger logger = LoggerFactory.getLogger(AzkabanServiceImpl.class);
private static final String CONTENT\_TYPE = "application/x-www-form-urlencoded; charset=utf-8";
private static final String X\_REQUESTED\_WITH = "XMLHttpRequest";
@Autowired
private RestTemplate restTemplate;
@Autowired
private GlobalConfig globalConfig;
@Override
public String login() throws Exception
{
SSLUtil.turnOffSslChecking();
HttpHeaders hs = new HttpHeaders();
hs.add("Content-Type", CONTENT\_TYPE);
hs.add("X-Requested-With", X\_REQUESTED\_WITH);
LinkedMultiValueMap<String, String> linkedMultiValueMap = new LinkedMultiValueMap<String, String>();
linkedMultiValueMap.add("action", "login");
linkedMultiValueMap.add("username", globalConfig.getAzkUsername());
linkedMultiValueMap.add("password", globalConfig.getAzkPassword());
HttpEntity<MultiValueMap<String, String>> httpEntity = new HttpEntity<>(linkedMultiValueMap, hs);
String result = restTemplate.postForObject(globalConfig.getAzkUrl(), httpEntity, String.class);
logger.info("--------Azkaban返回登录信息:" + result);
return new Gson().fromJson(result, JsonObject.class).get("session.id").getAsString();
}
@Override
public void createProject(String projectName, String description) throws Exception
{
SSLUtil.turnOffSslChecking();
HttpHeaders hs = new HttpHeaders();
hs.add("Content-Type", CONTENT\_TYPE);
hs.add("X-Requested-With", X\_REQUESTED\_WITH);
LinkedMultiValueMap<String, String> linkedMultiValueMap = new LinkedMultiValueMap<String, String>();
linkedMultiValueMap.add("session.id", login());
linkedMultiValueMap.add("action", "create");
linkedMultiValueMap.add("name", projectName);
linkedMultiValueMap.add("description", description);
HttpEntity<MultiValueMap<String, String>> httpEntity = new HttpEntity<>(linkedMultiValueMap, hs);
String result = restTemplate.postForObject(globalConfig.getAzkUrl() + "/manager", httpEntity, String.class);
logger.info("--------Azkaban返回创建Project信息:" + result);
// 创建成功和已存在,都表示创建成功
if (!Constant.AZK\_SUCCESS.equals(new Gson().fromJson(result, JsonObject.class).get("status").getAsString()))
{
if (!"Project already exists.".equals(new Gson().fromJson(result, JsonObject.class).get("message").getAsString()))
{
throw new Exception("创建Azkaban Project失败");
}
}
}
@Override
public void deleteProject(String projectName) throws Exception
{
SSLUtil.turnOffSslChecking();
HttpHeaders hs = new HttpHeaders();
hs.add("Content-Type", CONTENT\_TYPE);
hs.add("X-Requested-With", X\_REQUESTED\_WITH);
hs.add("Accept", "text/plain;charset=utf-8");
Map<String, String> map = new HashMap<>();
map.put("id", login());
map.put("project", projectName);
ResponseEntity<String> exchange = restTemplate.exchange(globalConfig.getAzkUrl() + "/manager?session.id={id}&delete=true&project={project}", HttpMethod.GET, new HttpEntity<String>(hs),
String.class, map);
logger.info("--------Azkaban返回删除Azkaban Project信息:" + exchange);
if (HttpStatus.SC\_OK != exchange.getStatusCodeValue())
{
throw new Exception("删除Azkaban Project失败");
}
}
@Override
public String uploadZip(String projectName, File file) throws Exception
{
SSLUtil.turnOffSslChecking();
FileSystemResource resource = new FileSystemResource(file);
LinkedMultiValueMap<String, Object> linkedMultiValueMap = new LinkedMultiValueMap<String, Object>();
linkedMultiValueMap.add("session.id", login());
linkedMultiValueMap.add("ajax", "upload");
linkedMultiValueMap.add("project", projectName);
linkedMultiValueMap.add("file", resource);
String result = restTemplate.postForObject(globalConfig.getAzkUrl() + "/manager", linkedMultiValueMap, String.class);
logger.info("--------Azkaban返回上传文件信息:" + result);
if (StringUtils.isEmpty(new Gson().fromJson(result, JsonObject.class).get("projectId").getAsString()))
{
throw new Exception("上传文件至Azkaban失败");
}
return new Gson().fromJson(result, JsonObject.class).get("projectId").getAsString();
}
@Override
public String scheduleEXEaFlow(String projectId, String projectName, String flow, String flowName, String recurring, String period, Date date) throws Exception
{
SSLUtil.turnOffSslChecking();
HttpHeaders hs = new HttpHeaders();
hs.add("Content-Type", CONTENT\_TYPE);
hs.add("X-Requested-With", X\_REQUESTED\_WITH);
LinkedMultiValueMap<String, String> linkedMultiValueMap = new LinkedMultiValueMap<String, String>();
linkedMultiValueMap.add("session.id", login());
linkedMultiValueMap.add("ajax", "scheduleFlow");
linkedMultiValueMap.add("projectName", projectName);
linkedMultiValueMap.add("projectId", projectId);
linkedMultiValueMap.add("flow", flow);
linkedMultiValueMap.add("flowName", flowName);
linkedMultiValueMap.add("is\_recurring", recurring);
linkedMultiValueMap.add("period", period);
scheduleTimeInit(linkedMultiValueMap, date);
HttpEntity<MultiValueMap<String, String>> httpEntity = new HttpEntity<>(linkedMultiValueMap, hs);
String result = restTemplate.postForObject(globalConfig.getAzkUrl() + "/schedule", httpEntity, String.class);
logger.info("--------Azkaban返回根据时间创建定时任务信息:" + result);
if (!Constant.AZK\_SUCCESS.equals(new Gson().fromJson(result, JsonObject.class).get("status").getAsString()) || new Gson().fromJson(result, JsonObject.class).get("scheduleId").getAsInt() < 0)
{
throw new Exception("根据时间创建定时任务失败");
}
return new Gson().fromJson(result, JsonObject.class).get("scheduleId").getAsString();
}
private void scheduleTimeInit(LinkedMultiValueMap<String, String> linkedMultiValueMap, Date date)
{
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
Integer year = calendar.get(Calendar.YEAR);
Integer month = calendar.get(Calendar.MONTH) + 1;
Integer day = calendar.get(Calendar.DATE);
Integer hour = calendar.get(Calendar.HOUR\_OF\_DAY);
Integer minute = calendar.get(Calendar.MINUTE);
linkedMultiValueMap.add("scheduleTime", hour + "," + minute + (hour > 11 ? ",pm,PDT" : ",am,EDT"));
linkedMultiValueMap.add("scheduleDate", month + "/" + day + "/" + year);
}
@Override
public String scheduleByCronEXEaFlow(String projectName, String cron, String flow, String flowName) throws Exception
{
SSLUtil.turnOffSslChecking();
HttpHeaders hs = new HttpHeaders();
hs.add("Content-Type", CONTENT\_TYPE);
hs.add("X-Requested-With", X\_REQUESTED\_WITH);
LinkedMultiValueMap<String, String> linkedMultiValueMap = new LinkedMultiValueMap<String, String>();
linkedMultiValueMap.add("session.id", login());
linkedMultiValueMap.add("ajax", "scheduleCronFlow");
linkedMultiValueMap.add("projectName", projectName);
linkedMultiValueMap.add("cronExpression", cron);
linkedMultiValueMap.add("flow", flow);
linkedMultiValueMap.add("flowName", flowName);
HttpEntity<MultiValueMap<String, String>> httpEntity = new HttpEntity<>(linkedMultiValueMap, hs);
String result = restTemplate.postForObject(globalConfig.getAzkUrl() + "/schedule", httpEntity, String.class);
logger.info("--------Azkaban返回根据cron表达式创建定时任务信息:" + result);
if (!Constant.AZK\_SUCCESS.equals(new Gson().fromJson(result, JsonObject.class).get("status").getAsString()))
{
throw new Exception("根据cron表达式创建定时任务失败");
}
return new Gson().fromJson(result, JsonObject.class).get("scheduleId").getAsString();
}
@Override
public void unscheduleFlow(String scheduleId) throws Exception
{
SSLUtil.turnOffSslChecking();
HttpHeaders hs = new HttpHeaders();
hs.add("Content-Type", CONTENT\_TYPE);
hs.add("X-Requested-With", X\_REQUESTED\_WITH);
LinkedMultiValueMap<String, String> linkedMultiValueMap = new LinkedMultiValueMap<String, String>();
linkedMultiValueMap.add("session.id", login());
linkedMultiValueMap.add("action", "removeSched");
linkedMultiValueMap.add("scheduleId", scheduleId);
HttpEntity<MultiValueMap<String, String>> httpEntity = new HttpEntity<>(linkedMultiValueMap, hs);
String result = restTemplate.postForObject(globalConfig.getAzkUrl() + "/schedule", httpEntity, String.class);
logger.info("--------Azkaban返回取消流调度信息:" + result);
if (!Constant.AZK\_SUCCESS.equals(new Gson().fromJson(result, JsonObject.class).get("status").getAsString()))
{
throw new Exception("根据cron表达式创建定时任务失败");
}
}
@Override
public void downLoadZip(String projectName, String zipPath)
{
OutputStream output = null;
BufferedOutputStream bufferedOutput = null;
try
{
URL url = new URL(globalConfig.getAzkUrl() + "/manager?session.id=" + login() + "&download=true&project=" + projectName);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setConnectTimeout(3 \* 1000);
InputStream inputStream = conn.getInputStream();
File file = new File(zipPath);
output = new FileOutputStream(file);
bufferedOutput = new BufferedOutputStream(output);
bufferedOutput.write(IOUtils.toByteArray(inputStream));
}
catch (Exception e)
{
logger.info("--------下载Azkaban压缩文件异常:" + e.getMessage(), e);
}
finally
{
if (bufferedOutput != null)
{
try
{
bufferedOutput.flush();
bufferedOutput.close();
}
catch (IOException e)
{
logger.info("关闭流异常:" + e.getMessage(), e);
}
}
if (output != null)
{
try
{
output.close();
}
catch (IOException e)
{
logger.info("关闭流异常:" + e.getMessage(), e);
}
}
}
}
}
PS:Azkaban Github API:http://azkaban.github.io/azkaban/docs/latest/#ajax-api
手机扫一扫
移动阅读更方便
你可能感兴趣的文章