为用于从任务队列迁移的队列设置重试参数示例。
深入探索
如需查看包含此代码示例的详细文档,请参阅以下内容:
代码示例
Java
如需了解如何安装和使用 Cloud Tasks 客户端库,请参阅 Cloud Tasks 客户端库。
如需向 Cloud Tasks 进行身份验证,请设置应用默认凭据。如需了解详情,请参阅为本地开发环境设置身份验证。
import com.google.cloud.tasks.v2.CloudTasksClient;
import com.google.cloud.tasks.v2.LocationName;
import com.google.cloud.tasks.v2.Queue;
import com.google.cloud.tasks.v2.QueueName;
import com.google.cloud.tasks.v2.RateLimits;
import com.google.cloud.tasks.v2.RetryConfig;
import com.google.protobuf.Duration;
public class RetryTask {
public static void retryTask(
String projectId, String locationId, String fooqueue, String barqueue, String bazqueue)
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 fooqueue = "fooqueue";
// String barqueue = "barqueue";
// String bazqueue = "bazqueue";
LocationName parent = LocationName.of(projectId, locationId);
Duration retryDuration = Duration.newBuilder().setSeconds(2 * 60 * 60 * 24).build();
Duration min = Duration.newBuilder().setSeconds(10).build();
Duration max1 = Duration.newBuilder().setSeconds(200).build();
Duration max2 = Duration.newBuilder().setSeconds(300).build();
Queue foo =
Queue.newBuilder()
.setName(QueueName.of(projectId, locationId, fooqueue).toString())
.setRateLimits(RateLimits.newBuilder().setMaxDispatchesPerSecond(1.0))
.setRetryConfig(
RetryConfig.newBuilder().setMaxAttempts(7).setMaxRetryDuration(retryDuration))
.build();
Queue bar =
Queue.newBuilder()
.setName(QueueName.of(projectId, locationId, barqueue).toString())
.setRateLimits(RateLimits.newBuilder().setMaxDispatchesPerSecond(1.0))
.setRetryConfig(
RetryConfig.newBuilder()
.setMinBackoff(min)
.setMaxBackoff(max1)
.setMaxDoublings(0))
.build();
Queue baz =
Queue.newBuilder()
.setName(QueueName.of(projectId, locationId, bazqueue).toString())
.setRateLimits(RateLimits.newBuilder().setMaxDispatchesPerSecond(1.0))
.setRetryConfig(
RetryConfig.newBuilder()
.setMinBackoff(min)
.setMaxBackoff(max2)
.setMaxDoublings(3))
.build();
Queue[] queues = new Queue[] {foo, bar, baz};
for (Queue queue : queues) {
Queue response = client.createQueue(parent, queue);
System.out.println(response);
}
}
}
}
Python
如需了解如何安装和使用 Cloud Tasks 客户端库,请参阅 Cloud Tasks 客户端库。
如需向 Cloud Tasks 进行身份验证,请设置应用默认凭据。如需了解详情,请参阅为本地开发环境设置身份验证。
from google.protobuf import duration_pb2
client = tasks.CloudTasksClient()
# TODO(developer): Uncomment these lines and replace with your values.
# project = 'my-project-id'
# location = 'us- central1'
# fooqueue = 'fooqueue'
# barqueue = 'barqueue'
# bazqueue = 'bazqueue'
parent = f"projects/{project}/locations/{location}"
max_retry = duration_pb2.Duration()
max_retry.seconds = 2*60*60*24
foo = {
'name': client.queue_path(project, location, fooqueue),
'rate_limits': {
'max_dispatches_per_second': 1
},
'retry_config': {
'max_attempts': 7,
'max_retry_duration': max_retry
}
}
min = duration_pb2.Duration()
min.seconds = 10
max = duration_pb2.Duration()
max.seconds = 200
bar = {
'name': client.queue_path(project, location, barqueue),
'rate_limits': {
'max_dispatches_per_second': 1
},
'retry_config': {
'min_backoff': min,
'max_backoff': max,
'max_doublings': 0
}
}
max.seconds = 300
baz = {
'name': client.queue_path(project, location, bazqueue),
'rate_limits': {
'max_dispatches_per_second': 1
},
'retry_config': {
'min_backoff': min,
'max_backoff': max,
'max_doublings': 3
}
}
queues = [foo, bar, baz]
for queue in queues:
response = client.create_queue(parent=parent, queue=queue)
print(response)
后续步骤
如需搜索并过滤其他 Google Cloud 产品的代码示例,请参阅 Google Cloud 示例浏览器。