use of com.google.cloud.tasks.v2.Task in project java-docs-samples by GoogleCloudPlatform.
the class CreateTask method main.
public static void main(String... args) throws Exception {
Options options = new Options();
options.addOption(PROJECT_ID_OPTION);
options.addOption(QUEUE_OPTION);
options.addOption(LOCATION_OPTION);
options.addOption(PAYLOAD_OPTION);
options.addOption(IN_SECONDS_OPTION);
if (args.length == 0) {
printUsage(options);
return;
}
CommandLineParser parser = new DefaultParser();
CommandLine params = null;
try {
params = parser.parse(options, args);
} catch (ParseException e) {
System.err.println("Invalid command line: " + e.getMessage());
printUsage(options);
return;
}
String projectId;
if (params.hasOption("project-id")) {
projectId = params.getOptionValue("project-id");
} else {
projectId = System.getenv(GOOGLE_CLOUD_PROJECT_KEY);
}
if (Strings.isNullOrEmpty(projectId)) {
printUsage(options);
return;
}
String queueName = params.getOptionValue(QUEUE_OPTION.getOpt());
String location = params.getOptionValue(LOCATION_OPTION.getOpt());
String payload = params.getOptionValue(PAYLOAD_OPTION.getOpt(), "default payload");
// Instantiates a client.
try (CloudTasksClient client = CloudTasksClient.create()) {
// Variables provided by the CLI.
// projectId = "my-project-id";
// queueName = "my-appengine-queue";
// location = "us-central1";
// payload = "hello";
// Construct the fully qualified queue name.
String queuePath = QueueName.of(projectId, location, queueName).toString();
// Construct the task body.
Task.Builder taskBuilder = Task.newBuilder().setAppEngineHttpRequest(AppEngineHttpRequest.newBuilder().setBody(ByteString.copyFrom(payload, Charset.defaultCharset())).setRelativeUri("/tasks/create").setHttpMethod(HttpMethod.POST).build());
if (params.hasOption(IN_SECONDS_OPTION.getOpt())) {
// Add the scheduled time to the request.
int seconds = Integer.parseInt(params.getOptionValue(IN_SECONDS_OPTION.getOpt()));
taskBuilder.setScheduleTime(Timestamp.newBuilder().setSeconds(Instant.now(Clock.systemUTC()).plusSeconds(seconds).getEpochSecond()));
}
// Send create task request.
Task task = client.createTask(queuePath, taskBuilder.build());
System.out.println("Task created: " + task.getName());
}
// [END cloud_tasks_appengine_create_task]
}
use of com.google.cloud.tasks.v2.Task in project teammates by TEAMMATES.
the class GoogleCloudTasksService method addDeferredTask.
@Override
public void addDeferredTask(TaskWrapper task, long countdownTime) {
try (CloudTasksClient client = CloudTasksClient.create()) {
String queuePath = QueueName.of(Config.APP_ID, Config.APP_REGION, task.getQueueName()).toString();
AppEngineHttpRequest.Builder requestBuilder = AppEngineHttpRequest.newBuilder().setAppEngineRouting(AppEngineRouting.newBuilder().setVersion(Config.APP_VERSION).build()).setHttpMethod(HttpMethod.POST);
if (task.getRequestBody() == null) {
// the value is not important
String relativeUrl = "http://place.holder";
AppUrl url = new AppUrl(relativeUrl + task.getWorkerUrl());
task.getParamMap().forEach((key, value) -> url.withParam(key, value));
requestBuilder.setRelativeUri(url.toString());
} else {
String requestBody = JsonUtils.toCompactJson(task.getRequestBody());
requestBuilder.putHeaders("Content-Type", "application/json; charset=UTF-8").setRelativeUri(task.getWorkerUrl()).setBody(ByteString.copyFrom(requestBody, Const.ENCODING));
}
Task.Builder taskBuilder = Task.newBuilder().setAppEngineHttpRequest(requestBuilder.build());
if (countdownTime > 0) {
taskBuilder.setScheduleTime(Timestamp.newBuilder().setSeconds(Instant.now().plusMillis(countdownTime).getEpochSecond()));
}
client.createTask(queuePath, taskBuilder.build());
} catch (IOException e) {
log.severe("Cannot create Cloud Tasks client", e);
}
}
use of com.google.cloud.tasks.v2.Task in project nomulus by google.
the class CloudTasksUtilsTest method testSuccess_createGetTasks_withEmptyParams.
@Test
void testSuccess_createGetTasks_withEmptyParams() {
Task task = cloudTasksUtils.createGetTask("/the/path", "myservice", ImmutableMultimap.of());
assertThat(task.getAppEngineHttpRequest().getHttpMethod()).isEqualTo(HttpMethod.GET);
assertThat(task.getAppEngineHttpRequest().getRelativeUri()).isEqualTo("/the/path");
assertThat(task.getAppEngineHttpRequest().getAppEngineRouting().getService()).isEqualTo("myservice");
assertThat(task.getScheduleTime().getSeconds()).isEqualTo(0);
}
use of com.google.cloud.tasks.v2.Task in project nomulus by google.
the class CloudTasksUtilsTest method testSuccess_createPostTasks_withZeroJitterSeconds.
@Test
void testSuccess_createPostTasks_withZeroJitterSeconds() {
Task task = cloudTasksUtils.createPostTaskWithJitter("/the/path", "myservice", params, Optional.of(0));
assertThat(task.getAppEngineHttpRequest().getHttpMethod()).isEqualTo(HttpMethod.POST);
assertThat(task.getAppEngineHttpRequest().getRelativeUri()).isEqualTo("/the/path");
assertThat(task.getAppEngineHttpRequest().getAppEngineRouting().getService()).isEqualTo("myservice");
assertThat(task.getAppEngineHttpRequest().getHeadersMap().get("Content-Type")).isEqualTo("application/x-www-form-urlencoded");
assertThat(task.getAppEngineHttpRequest().getBody().toString(StandardCharsets.UTF_8)).isEqualTo("key1=val1&key2=val2&key1=val3");
assertThat(task.getScheduleTime().getSeconds()).isEqualTo(0);
}
use of com.google.cloud.tasks.v2.Task in project nomulus by google.
the class CloudTasksUtilsTest method testSuccess_createPostTasks_withNullParams.
@Test
void testSuccess_createPostTasks_withNullParams() {
Task task = cloudTasksUtils.createPostTask("/the/path", "myservice", null);
assertThat(task.getAppEngineHttpRequest().getHttpMethod()).isEqualTo(HttpMethod.POST);
assertThat(task.getAppEngineHttpRequest().getRelativeUri()).isEqualTo("/the/path");
assertThat(task.getAppEngineHttpRequest().getAppEngineRouting().getService()).isEqualTo("myservice");
assertThat(task.getAppEngineHttpRequest().getBody().toString(StandardCharsets.UTF_8)).isEmpty();
assertThat(task.getScheduleTime().getSeconds()).isEqualTo(0);
}
Aggregations