@Test public void assertProcessWithoutSuccessCode() { when(jobConfig.getProps().getProperty(HttpJobProperties.URI_KEY)).thenReturn(getRequestUri("/unknownMethod")); when(jobConfig.getProps().getProperty(HttpJobProperties.METHOD_KEY)).thenReturn("GET"); when(jobConfig.getProps().getProperty(HttpJobProperties.DATA_KEY)).thenReturn(""); when(jobConfig.getProps().getProperty(HttpJobProperties.CONNECT_TIMEOUT_KEY, "3000")).thenReturn("4000"); when(jobConfig.getProps().getProperty(HttpJobProperties.READ_TIMEOUT_KEY, "5000")).thenReturn("5000"); jobExecutor.process(elasticJob, jobConfig, jobFacade, shardingContext); }
@Override public void process(final ElasticJob elasticJob, final JobConfiguration jobConfig, final JobFacade jobFacade, final ShardingContext shardingContext) { HttpParam httpParam = getHttpParam(jobConfig.getProps()); HttpURLConnection connection = null; try { URL url = new URL(httpParam.getUrl()); connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod(httpParam.getMethod()); connection.setDoOutput(true); connection.setConnectTimeout(httpParam.getConnectTimeout()); connection.setReadTimeout(httpParam.getReadTimeout()); if (!Strings.isNullOrEmpty(httpParam.getContentType())) { connection.setRequestProperty("Content-Type", httpParam.getContentType()); } connection.setRequestProperty(HttpJobProperties.SHARDING_CONTEXT_KEY, GsonFactory.getGson().toJson(shardingContext)); connection.connect(); String data = httpParam.getData(); if (isWriteMethod(httpParam.getMethod()) && !Strings.isNullOrEmpty(data)) { try (OutputStream outputStream = connection.getOutputStream()) { outputStream.write(data.getBytes(StandardCharsets.UTF_8)); } } int code = connection.getResponseCode(); InputStream resultInputStream; if (isRequestSucceed(code)) { resultInputStream = connection.getInputStream(); } else { log.warn("HTTP job {} executed with response code {}", jobConfig.getJobName(), code); resultInputStream = connection.getErrorStream(); } StringBuilder result = new StringBuilder(); try (BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(resultInputStream, StandardCharsets.UTF_8))) { String line; while (null != (line = bufferedReader.readLine())) { result.append(line); } } if (isRequestSucceed(code)) { log.debug("HTTP job execute result : {}", result.toString()); } else { log.warn("HTTP job {} executed with response body {}", jobConfig.getJobName(), result.toString()); } } catch (final IOException ex) { throw new JobExecutionException(ex); } finally { if (null != connection) { connection.disconnect(); } } }
HttpJobExecutor implements TypedJobItemExecutor { @Override public void process(final ElasticJob elasticJob, final JobConfiguration jobConfig, final JobFacade jobFacade, final ShardingContext shardingContext) { HttpParam httpParam = getHttpParam(jobConfig.getProps()); HttpURLConnection connection = null; try { URL url = new URL(httpParam.getUrl()); connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod(httpParam.getMethod()); connection.setDoOutput(true); connection.setConnectTimeout(httpParam.getConnectTimeout()); connection.setReadTimeout(httpParam.getReadTimeout()); if (!Strings.isNullOrEmpty(httpParam.getContentType())) { connection.setRequestProperty("Content-Type", httpParam.getContentType()); } connection.setRequestProperty(HttpJobProperties.SHARDING_CONTEXT_KEY, GsonFactory.getGson().toJson(shardingContext)); connection.connect(); String data = httpParam.getData(); if (isWriteMethod(httpParam.getMethod()) && !Strings.isNullOrEmpty(data)) { try (OutputStream outputStream = connection.getOutputStream()) { outputStream.write(data.getBytes(StandardCharsets.UTF_8)); } } int code = connection.getResponseCode(); InputStream resultInputStream; if (isRequestSucceed(code)) { resultInputStream = connection.getInputStream(); } else { log.warn("HTTP job {} executed with response code {}", jobConfig.getJobName(), code); resultInputStream = connection.getErrorStream(); } StringBuilder result = new StringBuilder(); try (BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(resultInputStream, StandardCharsets.UTF_8))) { String line; while (null != (line = bufferedReader.readLine())) { result.append(line); } } if (isRequestSucceed(code)) { log.debug("HTTP job execute result : {}", result.toString()); } else { log.warn("HTTP job {} executed with response body {}", jobConfig.getJobName(), result.toString()); } } catch (final IOException ex) { throw new JobExecutionException(ex); } finally { if (null != connection) { connection.disconnect(); } } } }
HttpJobExecutor implements TypedJobItemExecutor { @Override public void process(final ElasticJob elasticJob, final JobConfiguration jobConfig, final JobFacade jobFacade, final ShardingContext shardingContext) { HttpParam httpParam = getHttpParam(jobConfig.getProps()); HttpURLConnection connection = null; try { URL url = new URL(httpParam.getUrl()); connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod(httpParam.getMethod()); connection.setDoOutput(true); connection.setConnectTimeout(httpParam.getConnectTimeout()); connection.setReadTimeout(httpParam.getReadTimeout()); if (!Strings.isNullOrEmpty(httpParam.getContentType())) { connection.setRequestProperty("Content-Type", httpParam.getContentType()); } connection.setRequestProperty(HttpJobProperties.SHARDING_CONTEXT_KEY, GsonFactory.getGson().toJson(shardingContext)); connection.connect(); String data = httpParam.getData(); if (isWriteMethod(httpParam.getMethod()) && !Strings.isNullOrEmpty(data)) { try (OutputStream outputStream = connection.getOutputStream()) { outputStream.write(data.getBytes(StandardCharsets.UTF_8)); } } int code = connection.getResponseCode(); InputStream resultInputStream; if (isRequestSucceed(code)) { resultInputStream = connection.getInputStream(); } else { log.warn("HTTP job {} executed with response code {}", jobConfig.getJobName(), code); resultInputStream = connection.getErrorStream(); } StringBuilder result = new StringBuilder(); try (BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(resultInputStream, StandardCharsets.UTF_8))) { String line; while (null != (line = bufferedReader.readLine())) { result.append(line); } } if (isRequestSucceed(code)) { log.debug("HTTP job execute result : {}", result.toString()); } else { log.warn("HTTP job {} executed with response body {}", jobConfig.getJobName(), result.toString()); } } catch (final IOException ex) { throw new JobExecutionException(ex); } finally { if (null != connection) { connection.disconnect(); } } }  }
HttpJobExecutor implements TypedJobItemExecutor { @Override public void process(final ElasticJob elasticJob, final JobConfiguration jobConfig, final JobFacade jobFacade, final ShardingContext shardingContext) { HttpParam httpParam = getHttpParam(jobConfig.getProps()); HttpURLConnection connection = null; try { URL url = new URL(httpParam.getUrl()); connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod(httpParam.getMethod()); connection.setDoOutput(true); connection.setConnectTimeout(httpParam.getConnectTimeout()); connection.setReadTimeout(httpParam.getReadTimeout()); if (!Strings.isNullOrEmpty(httpParam.getContentType())) { connection.setRequestProperty("Content-Type", httpParam.getContentType()); } connection.setRequestProperty(HttpJobProperties.SHARDING_CONTEXT_KEY, GsonFactory.getGson().toJson(shardingContext)); connection.connect(); String data = httpParam.getData(); if (isWriteMethod(httpParam.getMethod()) && !Strings.isNullOrEmpty(data)) { try (OutputStream outputStream = connection.getOutputStream()) { outputStream.write(data.getBytes(StandardCharsets.UTF_8)); } } int code = connection.getResponseCode(); InputStream resultInputStream; if (isRequestSucceed(code)) { resultInputStream = connection.getInputStream(); } else { log.warn("HTTP job {} executed with response code {}", jobConfig.getJobName(), code); resultInputStream = connection.getErrorStream(); } StringBuilder result = new StringBuilder(); try (BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(resultInputStream, StandardCharsets.UTF_8))) { String line; while (null != (line = bufferedReader.readLine())) { result.append(line); } } if (isRequestSucceed(code)) { log.debug("HTTP job execute result : {}", result.toString()); } else { log.warn("HTTP job {} executed with response body {}", jobConfig.getJobName(), result.toString()); } } catch (final IOException ex) { throw new JobExecutionException(ex); } finally { if (null != connection) { connection.disconnect(); } } }  @Override void process(final ElasticJob elasticJob, final JobConfiguration jobConfig, final JobFacade jobFacade, final ShardingContext shardingContext); @Override String getType(); }
HttpJobExecutor implements TypedJobItemExecutor { @Override public void process(final ElasticJob elasticJob, final JobConfiguration jobConfig, final JobFacade jobFacade, final ShardingContext shardingContext) { HttpParam httpParam = getHttpParam(jobConfig.getProps()); HttpURLConnection connection = null; try { URL url = new URL(httpParam.getUrl()); connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod(httpParam.getMethod()); connection.setDoOutput(true); connection.setConnectTimeout(httpParam.getConnectTimeout()); connection.setReadTimeout(httpParam.getReadTimeout()); if (!Strings.isNullOrEmpty(httpParam.getContentType())) { connection.setRequestProperty("Content-Type", httpParam.getContentType()); } connection.setRequestProperty(HttpJobProperties.SHARDING_CONTEXT_KEY, GsonFactory.getGson().toJson(shardingContext)); connection.connect(); String data = httpParam.getData(); if (isWriteMethod(httpParam.getMethod()) && !Strings.isNullOrEmpty(data)) { try (OutputStream outputStream = connection.getOutputStream()) { outputStream.write(data.getBytes(StandardCharsets.UTF_8)); } } int code = connection.getResponseCode(); InputStream resultInputStream; if (isRequestSucceed(code)) { resultInputStream = connection.getInputStream(); } else { log.warn("HTTP job {} executed with response code {}", jobConfig.getJobName(), code); resultInputStream = connection.getErrorStream(); } StringBuilder result = new StringBuilder(); try (BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(resultInputStream, StandardCharsets.UTF_8))) { String line; while (null != (line = bufferedReader.readLine())) { result.append(line); } } if (isRequestSucceed(code)) { log.debug("HTTP job execute result : {}", result.toString()); } else { log.warn("HTTP job {} executed with response body {}", jobConfig.getJobName(), result.toString()); } } catch (final IOException ex) { throw new JobExecutionException(ex); } finally { if (null != connection) { connection.disconnect(); } } }  @Override void process(final ElasticJob elasticJob, final JobConfiguration jobConfig, final JobFacade jobFacade, final ShardingContext shardingContext); @Override String getType();  }