Код: Выделить всё
import com.google.cloud.tasks.v2.CloudTasksClient;
import com.google.cloud.tasks.v2.HttpMethod;
import com.google.cloud.tasks.v2.HttpRequest;
import com.google.cloud.tasks.v2.OidcToken;
import com.google.cloud.tasks.v2.QueueName;
import com.google.cloud.tasks.v2.Task;
import com.google.protobuf.ByteString;
import java.io.IOException;
import java.nio.charset.Charset;
public class CreateHttpTaskWithToken {
public static void main(String[] args) throws IOException {
// TODO(developer): Replace these variables before running the sample.
String projectId = "my-project-id";
String locationId = "us-central1";
String queueId = "my-queue";
String serviceAccountEmail =
"java-docs-samples-testing@java-docs-samples-testing.iam.gserviceaccount.com";
createTask(projectId, locationId, queueId, serviceAccountEmail);
}
// Create a task with a HTTP target and authorization token using the Cloud Tasks client.
public static void createTask(
String projectId, String locationId, String queueId, String serviceAccountEmail)
throws IOException {
// Instantiates a client.
try (CloudTasksClient client = CloudTasksClient.create()) {
String url =
"https://example.com/taskhandler"; // The full url path that the request will be sent to
String payload = "Hello, World!"; // The task HTTP request body
// Construct the fully qualified queue name.
String queuePath = QueueName.of(projectId, locationId, queueId).toString();
// Add your service account email to construct the OIDC token.
// in order to add an authentication header to the request.
OidcToken.Builder oidcTokenBuilder =
OidcToken.newBuilder().setServiceAccountEmail(serviceAccountEmail);
// Construct the task body.
Task.Builder taskBuilder =
Task.newBuilder()
.setHttpRequest(
HttpRequest.newBuilder()
.setBody(ByteString.copyFrom(payload, Charset.defaultCharset()))
.setHttpMethod(HttpMethod.POST)
.setUrl(url)
.setOidcToken(oidcTokenBuilder)
.build());
// Send create task request.
Task task = client.createTask(queuePath, taskBuilder.build());
System.out.println("Task created: " + task.getName());
}
}
}
Код: Выделить всё
@WebServlet(
name = "Tasks",
description = "Create Cloud Task",
urlPatterns = "/tasks/create"
)
public class TaskServlet extends HttpServlet {
@Override
public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
// Do some long running operation
}
}
Есть ли код, как это сделать сделать это, или для этого мне нужно установить какую-то другую конфигурацию в моем проекте Google Cloud?
Подробнее здесь: https://stackoverflow.com/questions/793 ... sk-handler
Мобильная версия