Search in sources :

Example 6 with QueueName

use of com.google.cloud.tasks.v2.QueueName in project java-docs-samples by GoogleCloudPlatform.

the class CreateTaskWithName method createTaskWithName.

public static void createTaskWithName(String projectId, String locationId, String queueId, String taskId) throws Exception {
    try (CloudTasksClient client = CloudTasksClient.create()) {
        // TODO(developer): Uncomment these lines and replace with your values.
        // String projectId = "your-project-id";
        // String locationId = "us-central1";
        // String queueId = "default";
        // String taskId = "first-try"
        String queueName = QueueName.of(projectId, locationId, queueId).toString();
        Task.Builder taskBuilder = Task.newBuilder().setName(TaskName.of(projectId, locationId, queueId, taskId).toString()).setAppEngineHttpRequest(AppEngineHttpRequest.newBuilder().setRelativeUri("/worker").setHttpMethod(HttpMethod.GET).build());
        // Send create task request.
        Task response = client.createTask(queueName, taskBuilder.build());
        System.out.println(response);
    }
}
Also used : Task(com.google.cloud.tasks.v2.Task) CloudTasksClient(com.google.cloud.tasks.v2.CloudTasksClient)

Example 7 with QueueName

use of com.google.cloud.tasks.v2.QueueName in project java-docs-samples by GoogleCloudPlatform.

the class PurgeQueue method purgeQueue.

public static void purgeQueue(String projectId, String locationId, String queueId) throws Exception {
    try (CloudTasksClient client = CloudTasksClient.create()) {
        // TODO(developer): Uncomment these lines and replace with your values.
        // String projectId = "your-project-id";
        // String locationId = "us-central1";
        // String queueId = "foo";
        // Construct the fully qualified queue name.
        String queueName = QueueName.of(projectId, locationId, queueId).toString();
        client.purgeQueue(queueName);
        System.out.println("Queue Purged.");
    }
}
Also used : CloudTasksClient(com.google.cloud.tasks.v2.CloudTasksClient)

Example 8 with QueueName

use of com.google.cloud.tasks.v2.QueueName 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]
}
Also used : Options(org.apache.commons.cli.Options) CommandLine(org.apache.commons.cli.CommandLine) Task(com.google.cloud.tasks.v2.Task) CloudTasksClient(com.google.cloud.tasks.v2.CloudTasksClient) CommandLineParser(org.apache.commons.cli.CommandLineParser) ParseException(org.apache.commons.cli.ParseException) ByteString(com.google.protobuf.ByteString) DefaultParser(org.apache.commons.cli.DefaultParser)

Example 9 with QueueName

use of com.google.cloud.tasks.v2.QueueName in project java-tasks by googleapis.

the class CloudTasksClientTest method listTasksTest.

@Test
public void listTasksTest() throws Exception {
    Task responsesElement = Task.newBuilder().build();
    ListTasksResponse expectedResponse = ListTasksResponse.newBuilder().setNextPageToken("").addAllTasks(Arrays.asList(responsesElement)).build();
    mockCloudTasks.addResponse(expectedResponse);
    QueueName parent = QueueName.of("[PROJECT]", "[LOCATION]", "[QUEUE]");
    ListTasksPagedResponse pagedListResponse = client.listTasks(parent);
    List<Task> resources = Lists.newArrayList(pagedListResponse.iterateAll());
    Assert.assertEquals(1, resources.size());
    Assert.assertEquals(expectedResponse.getTasksList().get(0), resources.get(0));
    List<AbstractMessage> actualRequests = mockCloudTasks.getRequests();
    Assert.assertEquals(1, actualRequests.size());
    ListTasksRequest actualRequest = ((ListTasksRequest) actualRequests.get(0));
    Assert.assertEquals(parent.toString(), actualRequest.getParent());
    Assert.assertTrue(channelProvider.isHeaderSent(ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), GaxGrpcProperties.getDefaultApiClientHeaderPattern()));
}
Also used : AbstractMessage(com.google.protobuf.AbstractMessage) ListTasksPagedResponse(com.google.cloud.tasks.v2.CloudTasksClient.ListTasksPagedResponse) Test(org.junit.Test)

Example 10 with QueueName

use of com.google.cloud.tasks.v2.QueueName in project java-tasks by googleapis.

the class TasksSampleApplication method main.

/**
 * Runs the Cloud Tasks sample application.
 */
public static void main(String[] args) throws IOException {
    String projectId = ServiceOptions.getDefaultProjectId();
    LocationName parent = LocationName.of(projectId, LOCATION_ID);
    QueueName queueName = QueueName.of(parent.getProject(), parent.getLocation(), GRAALVM_TEST_QUEUE_NAME + UUID.randomUUID().toString());
    try (CloudTasksClient client = CloudTasksClient.create()) {
        // Create queue
        Queue queue = Queue.newBuilder().setName(queueName.toString()).setRateLimits(RateLimits.newBuilder().setMaxConcurrentDispatches(1).build()).build();
        CreateQueueRequest createQueueRequest = CreateQueueRequest.newBuilder().setParent(parent.toString()).setQueue(queue).build();
        Queue createdQueue = client.createQueue(createQueueRequest);
        System.out.println("Test queue ready: " + createdQueue);
        // Create task
        HttpRequest taskTarget = HttpRequest.newBuilder().setUrl("https://google.com").setHttpMethod(HttpMethod.GET).build();
        Task taskRequest = Task.newBuilder().setHttpRequest(taskTarget).build();
        Task task = client.createTask(queueName, taskRequest);
        System.out.println("Created task: " + task);
        // Cleanup
        client.purgeQueue(queueName);
        System.out.println("Queue purged");
        client.deleteQueue(queueName);
        System.out.println("Queue deleted");
    }
}
Also used : HttpRequest(com.google.cloud.tasks.v2.HttpRequest) Task(com.google.cloud.tasks.v2.Task) CreateQueueRequest(com.google.cloud.tasks.v2.CreateQueueRequest) CloudTasksClient(com.google.cloud.tasks.v2.CloudTasksClient) QueueName(com.google.cloud.tasks.v2.QueueName) Queue(com.google.cloud.tasks.v2.Queue) LocationName(com.google.cloud.tasks.v2.LocationName)

Aggregations

CloudTasksClient (com.google.cloud.tasks.v2.CloudTasksClient)9 Task (com.google.cloud.tasks.v2.Task)7 QueueName (com.google.cloud.tasks.v2.QueueName)3 CreateQueueRequest (com.google.cloud.tasks.v2.CreateQueueRequest)2 HttpRequest (com.google.cloud.tasks.v2.HttpRequest)2 LocationName (com.google.cloud.tasks.v2.LocationName)2 Queue (com.google.cloud.tasks.v2.Queue)2 ByteString (com.google.protobuf.ByteString)2 ApiCallContext (com.google.api.gax.rpc.ApiCallContext)1 UnaryCallable (com.google.api.gax.rpc.UnaryCallable)1 AppEngineHttpRequest (com.google.cloud.tasks.v2.AppEngineHttpRequest)1 ListTasksPagedResponse (com.google.cloud.tasks.v2.CloudTasksClient.ListTasksPagedResponse)1 CreateTaskRequest (com.google.cloud.tasks.v2.CreateTaskRequest)1 AbstractMessage (com.google.protobuf.AbstractMessage)1 Callback (com.squareup.okhttp.Callback)1 OkHttpClient (com.squareup.okhttp.OkHttpClient)1 Request (com.squareup.okhttp.Request)1 Response (com.squareup.okhttp.Response)1 IOException (java.io.IOException)1 CommandLine (org.apache.commons.cli.CommandLine)1